@@ -15,6 +15,14 @@ pub fn init(context: &PyContext) {
1515 str_type. set_attr ( "__new__" , context. new_rustfunc ( str_new) ) ;
1616 str_type. set_attr ( "__str__" , context. new_rustfunc ( str_str) ) ;
1717 str_type. set_attr ( "__repr__" , context. new_rustfunc ( str_repr) ) ;
18+ str_type. set_attr ( "lower" , context. new_rustfunc ( str_lower) ) ;
19+ str_type. set_attr ( "upper" , context. new_rustfunc ( str_upper) ) ;
20+ str_type. set_attr ( "split" , context. new_rustfunc ( str_split) ) ;
21+ str_type. set_attr ( "strip" , context. new_rustfunc ( str_strip) ) ;
22+ str_type. set_attr ( "lstrip" , context. new_rustfunc ( str_lstrip) ) ;
23+ str_type. set_attr ( "rstrip" , context. new_rustfunc ( str_rstrip) ) ;
24+ str_type. set_attr ( "endswith" , context. new_rustfunc ( str_endswith) ) ;
25+ str_type. set_attr ( "startswith" , context. new_rustfunc ( str_startswith) ) ;
1826}
1927
2028pub fn get_value ( obj : & PyObjectRef ) -> String {
@@ -120,6 +128,75 @@ fn str_mul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
120128 }
121129}
122130
131+ fn str_upper ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
132+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
133+ let value = get_value ( & s) . to_uppercase ( ) ;
134+ Ok ( vm. ctx . new_str ( value) )
135+ }
136+
137+ fn str_lower ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
138+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
139+ let value = get_value ( & s) . to_lowercase ( ) ;
140+ Ok ( vm. ctx . new_str ( value) )
141+ }
142+
143+ fn str_split ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
144+ arg_check ! (
145+ vm,
146+ args,
147+ required = [ ( s, Some ( vm. ctx. str_type( ) ) ) , ( pat, Some ( vm. ctx. str_type( ) ) ) ]
148+ ) ;
149+ let value = get_value ( & s) ;
150+ // if some
151+ let pat = get_value ( & pat) ;
152+ let str_pat = pat. as_str ( ) ;
153+ let elements = value
154+ . split ( str_pat)
155+ . map ( |o| vm. ctx . new_str ( o. to_string ( ) ) )
156+ . collect ( ) ;
157+ Ok ( vm. ctx . new_list ( elements) )
158+ }
159+
160+ fn str_strip ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
161+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
162+ let value = get_value ( & s) . trim ( ) . to_string ( ) ;
163+ Ok ( vm. ctx . new_str ( value) )
164+ }
165+
166+ fn str_lstrip ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
167+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
168+ let value = get_value ( & s) . trim_left ( ) . to_string ( ) ;
169+ Ok ( vm. ctx . new_str ( value) )
170+ }
171+
172+ fn str_rstrip ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
173+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
174+ let value = get_value ( & s) . trim_right ( ) . to_string ( ) ;
175+ Ok ( vm. ctx . new_str ( value) )
176+ }
177+
178+ fn str_endswith ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
179+ arg_check ! (
180+ vm,
181+ args,
182+ required = [ ( s, Some ( vm. ctx. str_type( ) ) ) , ( pat, Some ( vm. ctx. str_type( ) ) ) ]
183+ ) ;
184+ let value = get_value ( & s) ;
185+ let pat = get_value ( & pat) ;
186+ Ok ( vm. ctx . new_bool ( value. ends_with ( pat. as_str ( ) ) ) )
187+ }
188+
189+ fn str_startswith ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
190+ arg_check ! (
191+ vm,
192+ args,
193+ required = [ ( s, Some ( vm. ctx. str_type( ) ) ) , ( pat, Some ( vm. ctx. str_type( ) ) ) ]
194+ ) ;
195+ let value = get_value ( & s) ;
196+ let pat = get_value ( & pat) ;
197+ Ok ( vm. ctx . new_bool ( value. starts_with ( pat. as_str ( ) ) ) )
198+ }
199+
123200// TODO: should with following format
124201// class str(object='')
125202// class str(object=b'', encoding='utf-8', errors='strict')
0 commit comments