@@ -9,6 +9,12 @@ use num_bigint::ToBigInt;
9
9
use num_traits:: ToPrimitive ;
10
10
use std:: hash:: { Hash , Hasher } ;
11
11
12
+ // functions like isdigit also checks if exponents are digits
13
+ // exponents from 0-9
14
+ const VALID_UNICODES : & ' static [ & ' static u16 ; 10 ] = & [
15
+ & 0x2070 , & 0x00B9 , & 0x00B2 , & 0x00B3 , & 0x2074 , & 0x2075 , & 0x2076 , & 0x2077 , & 0x2078 , & 0x2079 ,
16
+ ] ;
17
+
12
18
pub fn init ( context : & PyContext ) {
13
19
let ref str_type = context. str_type ;
14
20
str_type. set_attr ( "__add__" , context. new_rustfunc ( str_add) ) ;
@@ -32,6 +38,10 @@ pub fn init(context: &PyContext) {
32
38
str_type. set_attr ( "endswith" , context. new_rustfunc ( str_endswith) ) ;
33
39
str_type. set_attr ( "startswith" , context. new_rustfunc ( str_startswith) ) ;
34
40
str_type. set_attr ( "title" , context. new_rustfunc ( str_title) ) ;
41
+ str_type. set_attr ( "swapcase" , context. new_rustfunc ( str_swapcase) ) ;
42
+ str_type. set_attr ( "isalnum" , context. new_rustfunc ( str_isalnum) ) ;
43
+ str_type. set_attr ( "isalpha" , context. new_rustfunc ( str_isalpha) ) ;
44
+ str_type. set_attr ( "isdigit" , context. new_rustfunc ( str_isdigit) ) ;
35
45
36
46
// str_type.set_attr("center", context.new_rustfunc(str_center));
37
47
}
@@ -229,6 +239,22 @@ fn str_endswith(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
229
239
Ok ( vm. ctx . new_bool ( value. ends_with ( pat. as_str ( ) ) ) )
230
240
}
231
241
242
+ fn str_swapcase ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
243
+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
244
+ let value = get_value ( & s) ;
245
+ let mut swapped_str = String :: with_capacity ( value. len ( ) ) ;
246
+ for c in value. chars ( ) {
247
+ if c. is_lowercase ( ) {
248
+ swapped_str. push ( c. to_ascii_uppercase ( ) ) ;
249
+ } else if c. is_uppercase ( ) {
250
+ swapped_str. push ( c. to_ascii_lowercase ( ) ) ;
251
+ } else {
252
+ swapped_str. push ( c) ;
253
+ }
254
+ }
255
+ Ok ( vm. ctx . new_str ( swapped_str) )
256
+ }
257
+
232
258
fn str_title ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
233
259
arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
234
260
let value = get_value ( & s) ;
@@ -287,6 +313,51 @@ fn str_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
287
313
Ok ( vm. ctx . new_bool ( value. contains ( needle. as_str ( ) ) ) )
288
314
}
289
315
316
+ fn str_isalnum ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
317
+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
318
+ let value = get_value ( & s) ;
319
+ let mut is_alnum: bool = true ;
320
+ for c in value. chars ( ) {
321
+ if !c. is_alphanumeric ( ) {
322
+ is_alnum = false ;
323
+ break ;
324
+ }
325
+ }
326
+ Ok ( vm. ctx . new_bool ( is_alnum) )
327
+ }
328
+
329
+ fn str_isalpha ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
330
+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
331
+ let value = get_value ( & s) ;
332
+ let mut is_alpha: bool = true ;
333
+ for c in value. chars ( ) {
334
+ if !c. is_alphabetic ( ) {
335
+ is_alpha = false ;
336
+ break ;
337
+ }
338
+ }
339
+ Ok ( vm. ctx . new_bool ( is_alpha) )
340
+ }
341
+
342
+ fn str_isdigit ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
343
+ arg_check ! ( vm, args, required = [ ( s, Some ( vm. ctx. str_type( ) ) ) ] ) ;
344
+ let value = get_value ( & s) ;
345
+ let mut is_digit: bool = true ;
346
+ for c in value. chars ( ) {
347
+ if !c. is_digit ( 10 ) {
348
+ // checking if char is exponent
349
+ let char_as_uni: u16 = c as u16 ;
350
+ if VALID_UNICODES . contains ( & & char_as_uni) {
351
+ continue ;
352
+ } else {
353
+ is_digit = false ;
354
+ break ;
355
+ }
356
+ }
357
+ }
358
+ Ok ( vm. ctx . new_bool ( is_digit) )
359
+ }
360
+
290
361
fn str_getitem ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
291
362
arg_check ! (
292
363
vm,
0 commit comments