@@ -31,6 +31,9 @@ pub fn init(context: &PyContext) {
31
31
str_type. set_attr ( "rstrip" , context. new_rustfunc ( str_rstrip) ) ;
32
32
str_type. set_attr ( "endswith" , context. new_rustfunc ( str_endswith) ) ;
33
33
str_type. set_attr ( "startswith" , context. new_rustfunc ( str_startswith) ) ;
34
+ str_type. set_attr ( "title" , context. new_rustfunc ( str_title) ) ;
35
+
36
+ // str_type.set_attr("center", context.new_rustfunc(str_center));
34
37
}
35
38
36
39
pub fn get_value ( obj : & PyObjectRef ) -> String {
@@ -226,6 +229,39 @@ fn str_endswith(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
226
229
Ok ( vm. ctx . new_bool ( value. ends_with ( pat. as_str ( ) ) ) )
227
230
}
228
231
232
+ fn str_title ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
233
+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
234
+ let value = get_value ( & s) ;
235
+ let titled_str = value
236
+ . split ( ' ' )
237
+ . map ( |w| {
238
+ let mut c = w. chars ( ) ;
239
+ match c. next ( ) {
240
+ None => String :: new ( ) ,
241
+ Some ( f) => f
242
+ . to_uppercase ( )
243
+ . chain ( c. flat_map ( |t| t. to_lowercase ( ) ) )
244
+ . collect ( ) ,
245
+ }
246
+ } )
247
+ . collect :: < Vec < _ > > ( )
248
+ . join ( " " ) ;
249
+ Ok ( vm. ctx . new_str ( titled_str) )
250
+ }
251
+
252
+ // fn str_center(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
253
+ // arg_check!(
254
+ // vm,
255
+ // args,
256
+ // required = [(s, Some(vm.ctx.str_type())), (len, Some(vm.ctx.int_type()))],
257
+ // optional = [(chars, None)]
258
+ // );
259
+ // let value = get_value(&s);
260
+ // let len = get_value(&len).parse::<usize>();
261
+ // let chars = args.get_kwargs
262
+ // Ok(vm.ctx.new_str(value))
263
+ // }
264
+
229
265
fn str_startswith ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
230
266
arg_check ! (
231
267
vm,
0 commit comments