@@ -1476,73 +1476,47 @@ STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
1476
1476
return str_partitioner (self_in , arg , -1 );
1477
1477
}
1478
1478
1479
- enum { CASE_UPPER , CASE_LOWER };
1480
-
1481
1479
// Supposedly not too critical operations, so optimize for code size
1482
- STATIC mp_obj_t str_caseconv (int op , mp_obj_t self_in ) {
1480
+ STATIC mp_obj_t str_caseconv (unichar ( * op )( unichar ) , mp_obj_t self_in ) {
1483
1481
GET_STR_DATA_LEN (self_in , self_data , self_len );
1484
1482
byte * data ;
1485
1483
mp_obj_t s = mp_obj_str_builder_start (mp_obj_get_type (self_in ), self_len , & data );
1486
1484
for (int i = 0 ; i < self_len ; i ++ ) {
1487
- if (op == CASE_UPPER ) {
1488
- * data ++ = unichar_toupper (* self_data ++ );
1489
- } else {
1490
- * data ++ = unichar_tolower (* self_data ++ );
1491
- }
1485
+ * data ++ = op (* self_data ++ );
1492
1486
}
1493
1487
* data = 0 ;
1494
1488
return mp_obj_str_builder_end (s );
1495
1489
}
1496
1490
1497
1491
STATIC mp_obj_t str_lower (mp_obj_t self_in ) {
1498
- return str_caseconv (CASE_LOWER , self_in );
1492
+ return str_caseconv (unichar_tolower , self_in );
1499
1493
}
1500
1494
1501
1495
STATIC mp_obj_t str_upper (mp_obj_t self_in ) {
1502
- return str_caseconv (CASE_UPPER , self_in );
1496
+ return str_caseconv (unichar_toupper , self_in );
1503
1497
}
1504
1498
1505
- enum { IS_SPACE , IS_ALPHA , IS_DIGIT , IS_UPPER , IS_LOWER };
1506
-
1507
- STATIC mp_obj_t str_uni_istype (int type , mp_obj_t self_in ) {
1499
+ STATIC mp_obj_t str_uni_istype (bool (* f )(unichar ), mp_obj_t self_in ) {
1508
1500
GET_STR_DATA_LEN (self_in , self_data , self_len );
1509
1501
1510
1502
if (self_len == 0 ) {
1511
1503
return mp_const_false ; // default to False for empty str
1512
1504
}
1513
1505
1514
- typedef bool (* check_function )(unichar );
1515
- check_function f ;
1516
-
1517
- if (type != IS_UPPER && type != IS_LOWER ) {
1518
- switch (type ) {
1519
- case IS_SPACE : f = & unichar_isspace ; break ;
1520
- case IS_ALPHA : f = & unichar_isalpha ; break ;
1521
- case IS_DIGIT : f = & unichar_isdigit ; break ;
1522
- default :
1523
- nlr_raise (mp_obj_new_exception_msg (& mp_type_TypeError , "unknown type provided for str_uni_istype" ));
1524
- }
1525
-
1506
+ if (f != unichar_isupper && f != unichar_islower ) {
1526
1507
for (int i = 0 ; i < self_len ; i ++ ) {
1527
1508
if (!f (* self_data ++ )) {
1528
1509
return mp_const_false ;
1529
1510
}
1530
1511
}
1531
1512
} else {
1532
- switch (type ) {
1533
- case IS_UPPER : f = & unichar_isupper ; break ;
1534
- case IS_LOWER : f = & unichar_islower ; break ;
1535
- default :
1536
- nlr_raise (mp_obj_new_exception_msg (& mp_type_TypeError , "unknown type provided for str_uni_istype" ));
1537
- }
1538
-
1539
1513
bool contains_alpha = false;
1540
1514
1541
1515
for (int i = 0 ; i < self_len ; i ++ ) { // only check alphanumeric characters
1542
1516
if (unichar_isalpha (* self_data ++ )) {
1543
1517
contains_alpha = true;
1544
- if (!f (* (self_data - 1 ))) {
1545
- return mp_const_false ; // we already incremented
1518
+ if (!f (* (self_data - 1 ))) { // -1 because we already incremented above
1519
+ return mp_const_false ;
1546
1520
}
1547
1521
}
1548
1522
}
@@ -1556,23 +1530,23 @@ STATIC mp_obj_t str_uni_istype(int type, mp_obj_t self_in) {
1556
1530
}
1557
1531
1558
1532
STATIC mp_obj_t str_isspace (mp_obj_t self_in ) {
1559
- return str_uni_istype (IS_SPACE , self_in );
1533
+ return str_uni_istype (unichar_isspace , self_in );
1560
1534
}
1561
1535
1562
1536
STATIC mp_obj_t str_isalpha (mp_obj_t self_in ) {
1563
- return str_uni_istype (IS_ALPHA , self_in );
1537
+ return str_uni_istype (unichar_isalpha , self_in );
1564
1538
}
1565
1539
1566
1540
STATIC mp_obj_t str_isdigit (mp_obj_t self_in ) {
1567
- return str_uni_istype (IS_DIGIT , self_in );
1541
+ return str_uni_istype (unichar_isdigit , self_in );
1568
1542
}
1569
1543
1570
1544
STATIC mp_obj_t str_isupper (mp_obj_t self_in ) {
1571
- return str_uni_istype (IS_UPPER , self_in );
1545
+ return str_uni_istype (unichar_isupper , self_in );
1572
1546
}
1573
1547
1574
1548
STATIC mp_obj_t str_islower (mp_obj_t self_in ) {
1575
- return str_uni_istype (IS_LOWER , self_in );
1549
+ return str_uni_istype (unichar_islower , self_in );
1576
1550
}
1577
1551
1578
1552
#if MICROPY_CPYTHON_COMPAT
0 commit comments