@@ -469,6 +469,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
469
469
// return joined string
470
470
return mp_obj_new_str_from_vstr (self_type , & vstr );
471
471
}
472
+ MP_DEFINE_CONST_FUN_OBJ_2 (str_join_obj , str_join );
472
473
473
474
mp_obj_t mp_obj_str_split (size_t n_args , const mp_obj_t * args ) {
474
475
const mp_obj_type_t * self_type = mp_obj_get_type (args [0 ]);
@@ -544,6 +545,7 @@ mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args) {
544
545
545
546
return res ;
546
547
}
548
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_split_obj , 1 , 3 , mp_obj_str_split );
547
549
548
550
#if MICROPY_PY_BUILTINS_STR_SPLITLINES
549
551
STATIC mp_obj_t str_splitlines (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
@@ -589,6 +591,7 @@ STATIC mp_obj_t str_splitlines(size_t n_args, const mp_obj_t *pos_args, mp_map_t
589
591
590
592
return res ;
591
593
}
594
+ MP_DEFINE_CONST_FUN_OBJ_KW (str_splitlines_obj , 1 , str_splitlines );
592
595
#endif
593
596
594
597
STATIC mp_obj_t str_rsplit (size_t n_args , const mp_obj_t * args ) {
@@ -656,6 +659,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) {
656
659
657
660
return MP_OBJ_FROM_PTR (res );
658
661
}
662
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rsplit_obj , 1 , 3 , str_rsplit );
659
663
660
664
STATIC mp_obj_t str_finder (size_t n_args , const mp_obj_t * args , int direction , bool is_index ) {
661
665
const mp_obj_type_t * self_type = mp_obj_get_type (args [0 ]);
@@ -700,18 +704,22 @@ STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b
700
704
STATIC mp_obj_t str_find (size_t n_args , const mp_obj_t * args ) {
701
705
return str_finder (n_args , args , 1 , false);
702
706
}
707
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_find_obj , 2 , 4 , str_find );
703
708
704
709
STATIC mp_obj_t str_rfind (size_t n_args , const mp_obj_t * args ) {
705
710
return str_finder (n_args , args , -1 , false);
706
711
}
712
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rfind_obj , 2 , 4 , str_rfind );
707
713
708
714
STATIC mp_obj_t str_index (size_t n_args , const mp_obj_t * args ) {
709
715
return str_finder (n_args , args , 1 , true);
710
716
}
717
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_index_obj , 2 , 4 , str_index );
711
718
712
719
STATIC mp_obj_t str_rindex (size_t n_args , const mp_obj_t * args ) {
713
720
return str_finder (n_args , args , -1 , true);
714
721
}
722
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rindex_obj , 2 , 4 , str_rindex );
715
723
716
724
// TODO: (Much) more variety in args
717
725
STATIC mp_obj_t str_startswith (size_t n_args , const mp_obj_t * args ) {
@@ -727,6 +735,7 @@ STATIC mp_obj_t str_startswith(size_t n_args, const mp_obj_t *args) {
727
735
}
728
736
return mp_obj_new_bool (memcmp (start , prefix , prefix_len ) == 0 );
729
737
}
738
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_startswith_obj , 2 , 3 , str_startswith );
730
739
731
740
STATIC mp_obj_t str_endswith (size_t n_args , const mp_obj_t * args ) {
732
741
GET_STR_DATA_LEN (args [0 ], str , str_len );
@@ -740,6 +749,7 @@ STATIC mp_obj_t str_endswith(size_t n_args, const mp_obj_t *args) {
740
749
}
741
750
return mp_obj_new_bool (memcmp (str + (str_len - suffix_len ), suffix , suffix_len ) == 0 );
742
751
}
752
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_endswith_obj , 2 , 3 , str_endswith );
743
753
744
754
enum { LSTRIP , RSTRIP , STRIP };
745
755
@@ -817,14 +827,17 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) {
817
827
STATIC mp_obj_t str_strip (size_t n_args , const mp_obj_t * args ) {
818
828
return str_uni_strip (STRIP , n_args , args );
819
829
}
830
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_strip_obj , 1 , 2 , str_strip );
820
831
821
832
STATIC mp_obj_t str_lstrip (size_t n_args , const mp_obj_t * args ) {
822
833
return str_uni_strip (LSTRIP , n_args , args );
823
834
}
835
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_lstrip_obj , 1 , 2 , str_lstrip );
824
836
825
837
STATIC mp_obj_t str_rstrip (size_t n_args , const mp_obj_t * args ) {
826
838
return str_uni_strip (RSTRIP , n_args , args );
827
839
}
840
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rstrip_obj , 1 , 2 , str_rstrip );
828
841
829
842
#if MICROPY_PY_BUILTINS_STR_CENTER
830
843
STATIC mp_obj_t str_center (mp_obj_t str_in , mp_obj_t width_in ) {
@@ -841,6 +854,7 @@ STATIC mp_obj_t str_center(mp_obj_t str_in, mp_obj_t width_in) {
841
854
memcpy (vstr .buf + left , str , str_len );
842
855
return mp_obj_new_str_from_vstr (mp_obj_get_type (str_in ), & vstr );
843
856
}
857
+ MP_DEFINE_CONST_FUN_OBJ_2 (str_center_obj , str_center );
844
858
#endif
845
859
846
860
// Takes an int arg, but only parses unsigned numbers, and only changes
@@ -1346,6 +1360,7 @@ mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
1346
1360
vstr_t vstr = mp_obj_str_format_helper ((const char * )str , (const char * )str + len , & arg_i , n_args , args , kwargs );
1347
1361
return mp_obj_new_str_from_vstr (& mp_type_str , & vstr );
1348
1362
}
1363
+ MP_DEFINE_CONST_FUN_OBJ_KW (str_format_obj , 1 , mp_obj_str_format );
1349
1364
1350
1365
STATIC mp_obj_t str_modulo_format (mp_obj_t pattern , size_t n_args , const mp_obj_t * args , mp_obj_t dict ) {
1351
1366
mp_check_self (MP_OBJ_IS_STR_OR_BYTES (pattern ));
@@ -1649,6 +1664,7 @@ STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
1649
1664
1650
1665
return mp_obj_new_str_from_vstr (self_type , & vstr );
1651
1666
}
1667
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_replace_obj , 3 , 4 , str_replace );
1652
1668
1653
1669
STATIC mp_obj_t str_count (size_t n_args , const mp_obj_t * args ) {
1654
1670
const mp_obj_type_t * self_type = mp_obj_get_type (args [0 ]);
@@ -1689,6 +1705,7 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
1689
1705
1690
1706
return MP_OBJ_NEW_SMALL_INT (num_occurrences );
1691
1707
}
1708
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_count_obj , 2 , 4 , str_count );
1692
1709
1693
1710
#if MICROPY_PY_BUILTINS_STR_PARTITION
1694
1711
STATIC mp_obj_t str_partitioner (mp_obj_t self_in , mp_obj_t arg , int direction ) {
@@ -1736,10 +1753,12 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) {
1736
1753
STATIC mp_obj_t str_partition (mp_obj_t self_in , mp_obj_t arg ) {
1737
1754
return str_partitioner (self_in , arg , 1 );
1738
1755
}
1756
+ MP_DEFINE_CONST_FUN_OBJ_2 (str_partition_obj , str_partition );
1739
1757
1740
1758
STATIC mp_obj_t str_rpartition (mp_obj_t self_in , mp_obj_t arg ) {
1741
1759
return str_partitioner (self_in , arg , -1 );
1742
1760
}
1761
+ MP_DEFINE_CONST_FUN_OBJ_2 (str_rpartition_obj , str_rpartition );
1743
1762
#endif
1744
1763
1745
1764
// Supposedly not too critical operations, so optimize for code size
@@ -1757,10 +1776,12 @@ STATIC mp_obj_t str_caseconv(unichar (*op)(unichar), mp_obj_t self_in) {
1757
1776
STATIC mp_obj_t str_lower (mp_obj_t self_in ) {
1758
1777
return str_caseconv (unichar_tolower , self_in );
1759
1778
}
1779
+ MP_DEFINE_CONST_FUN_OBJ_1 (str_lower_obj , str_lower );
1760
1780
1761
1781
STATIC mp_obj_t str_upper (mp_obj_t self_in ) {
1762
1782
return str_caseconv (unichar_toupper , self_in );
1763
1783
}
1784
+ MP_DEFINE_CONST_FUN_OBJ_1 (str_upper_obj , str_upper );
1764
1785
1765
1786
STATIC mp_obj_t str_uni_istype (bool (* f )(unichar ), mp_obj_t self_in ) {
1766
1787
GET_STR_DATA_LEN (self_in , self_data , self_len );
@@ -1798,22 +1819,27 @@ STATIC mp_obj_t str_uni_istype(bool (*f)(unichar), mp_obj_t self_in) {
1798
1819
STATIC mp_obj_t str_isspace (mp_obj_t self_in ) {
1799
1820
return str_uni_istype (unichar_isspace , self_in );
1800
1821
}
1822
+ MP_DEFINE_CONST_FUN_OBJ_1 (str_isspace_obj , str_isspace );
1801
1823
1802
1824
STATIC mp_obj_t str_isalpha (mp_obj_t self_in ) {
1803
1825
return str_uni_istype (unichar_isalpha , self_in );
1804
1826
}
1827
+ MP_DEFINE_CONST_FUN_OBJ_1 (str_isalpha_obj , str_isalpha );
1805
1828
1806
1829
STATIC mp_obj_t str_isdigit (mp_obj_t self_in ) {
1807
1830
return str_uni_istype (unichar_isdigit , self_in );
1808
1831
}
1832
+ MP_DEFINE_CONST_FUN_OBJ_1 (str_isdigit_obj , str_isdigit );
1809
1833
1810
1834
STATIC mp_obj_t str_isupper (mp_obj_t self_in ) {
1811
1835
return str_uni_istype (unichar_isupper , self_in );
1812
1836
}
1837
+ MP_DEFINE_CONST_FUN_OBJ_1 (str_isupper_obj , str_isupper );
1813
1838
1814
1839
STATIC mp_obj_t str_islower (mp_obj_t self_in ) {
1815
1840
return str_uni_istype (unichar_islower , self_in );
1816
1841
}
1842
+ MP_DEFINE_CONST_FUN_OBJ_1 (str_islower_obj , str_islower );
1817
1843
1818
1844
#if MICROPY_CPYTHON_COMPAT
1819
1845
// These methods are superfluous in the presence of str() and bytes()
@@ -1829,6 +1855,7 @@ STATIC mp_obj_t bytes_decode(size_t n_args, const mp_obj_t *args) {
1829
1855
}
1830
1856
return mp_obj_str_make_new (& mp_type_str , n_args , 0 , args );
1831
1857
}
1858
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (bytes_decode_obj , 1 , 3 , bytes_decode );
1832
1859
1833
1860
// TODO: should accept kwargs too
1834
1861
STATIC mp_obj_t str_encode (size_t n_args , const mp_obj_t * args ) {
@@ -1841,6 +1868,7 @@ STATIC mp_obj_t str_encode(size_t n_args, const mp_obj_t *args) {
1841
1868
}
1842
1869
return bytes_make_new (NULL , n_args , 0 , args );
1843
1870
}
1871
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_encode_obj , 1 , 3 , str_encode );
1844
1872
#endif
1845
1873
1846
1874
mp_int_t mp_obj_str_get_buffer (mp_obj_t self_in , mp_buffer_info_t * bufinfo , mp_uint_t flags ) {
@@ -1859,43 +1887,6 @@ mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_u
1859
1887
}
1860
1888
}
1861
1889
1862
- #if MICROPY_CPYTHON_COMPAT
1863
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (bytes_decode_obj , 1 , 3 , bytes_decode );
1864
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_encode_obj , 1 , 3 , str_encode );
1865
- #endif
1866
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_find_obj , 2 , 4 , str_find );
1867
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rfind_obj , 2 , 4 , str_rfind );
1868
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_index_obj , 2 , 4 , str_index );
1869
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rindex_obj , 2 , 4 , str_rindex );
1870
- MP_DEFINE_CONST_FUN_OBJ_2 (str_join_obj , str_join );
1871
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_split_obj , 1 , 3 , mp_obj_str_split );
1872
- #if MICROPY_PY_BUILTINS_STR_SPLITLINES
1873
- MP_DEFINE_CONST_FUN_OBJ_KW (str_splitlines_obj , 1 , str_splitlines );
1874
- #endif
1875
- #if MICROPY_PY_BUILTINS_STR_CENTER
1876
- MP_DEFINE_CONST_FUN_OBJ_2 (str_center_obj , str_center );
1877
- #endif
1878
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rsplit_obj , 1 , 3 , str_rsplit );
1879
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_startswith_obj , 2 , 3 , str_startswith );
1880
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_endswith_obj , 2 , 3 , str_endswith );
1881
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_strip_obj , 1 , 2 , str_strip );
1882
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_lstrip_obj , 1 , 2 , str_lstrip );
1883
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rstrip_obj , 1 , 2 , str_rstrip );
1884
- MP_DEFINE_CONST_FUN_OBJ_KW (str_format_obj , 1 , mp_obj_str_format );
1885
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_replace_obj , 3 , 4 , str_replace );
1886
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_count_obj , 2 , 4 , str_count );
1887
- #if MICROPY_PY_BUILTINS_STR_PARTITION
1888
- MP_DEFINE_CONST_FUN_OBJ_2 (str_partition_obj , str_partition );
1889
- MP_DEFINE_CONST_FUN_OBJ_2 (str_rpartition_obj , str_rpartition );
1890
- #endif
1891
- MP_DEFINE_CONST_FUN_OBJ_1 (str_lower_obj , str_lower );
1892
- MP_DEFINE_CONST_FUN_OBJ_1 (str_upper_obj , str_upper );
1893
- MP_DEFINE_CONST_FUN_OBJ_1 (str_isspace_obj , str_isspace );
1894
- MP_DEFINE_CONST_FUN_OBJ_1 (str_isalpha_obj , str_isalpha );
1895
- MP_DEFINE_CONST_FUN_OBJ_1 (str_isdigit_obj , str_isdigit );
1896
- MP_DEFINE_CONST_FUN_OBJ_1 (str_isupper_obj , str_isupper );
1897
- MP_DEFINE_CONST_FUN_OBJ_1 (str_islower_obj , str_islower );
1898
-
1899
1890
STATIC const mp_rom_map_elem_t str8_locals_dict_table [] = {
1900
1891
#if MICROPY_CPYTHON_COMPAT
1901
1892
{ MP_ROM_QSTR (MP_QSTR_decode ), MP_ROM_PTR (& bytes_decode_obj ) },
0 commit comments