Skip to content

Commit 65417c5

Browse files
committed
py/objstr: Move uPy function wrappers to just after the C function.
This matches the coding/layout style of all the other objects.
1 parent 90c1d54 commit 65417c5

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

py/objstr.c

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
469469
// return joined string
470470
return mp_obj_new_str_from_vstr(self_type, &vstr);
471471
}
472+
MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
472473

473474
mp_obj_t mp_obj_str_split(size_t n_args, const mp_obj_t *args) {
474475
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) {
544545

545546
return res;
546547
}
548+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_split_obj, 1, 3, mp_obj_str_split);
547549

548550
#if MICROPY_PY_BUILTINS_STR_SPLITLINES
549551
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
589591

590592
return res;
591593
}
594+
MP_DEFINE_CONST_FUN_OBJ_KW(str_splitlines_obj, 1, str_splitlines);
592595
#endif
593596

594597
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) {
656659

657660
return MP_OBJ_FROM_PTR(res);
658661
}
662+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit);
659663

660664
STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, bool is_index) {
661665
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
700704
STATIC mp_obj_t str_find(size_t n_args, const mp_obj_t *args) {
701705
return str_finder(n_args, args, 1, false);
702706
}
707+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
703708

704709
STATIC mp_obj_t str_rfind(size_t n_args, const mp_obj_t *args) {
705710
return str_finder(n_args, args, -1, false);
706711
}
712+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj, 2, 4, str_rfind);
707713

708714
STATIC mp_obj_t str_index(size_t n_args, const mp_obj_t *args) {
709715
return str_finder(n_args, args, 1, true);
710716
}
717+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_index_obj, 2, 4, str_index);
711718

712719
STATIC mp_obj_t str_rindex(size_t n_args, const mp_obj_t *args) {
713720
return str_finder(n_args, args, -1, true);
714721
}
722+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rindex_obj, 2, 4, str_rindex);
715723

716724
// TODO: (Much) more variety in args
717725
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) {
727735
}
728736
return mp_obj_new_bool(memcmp(start, prefix, prefix_len) == 0);
729737
}
738+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_startswith_obj, 2, 3, str_startswith);
730739

731740
STATIC mp_obj_t str_endswith(size_t n_args, const mp_obj_t *args) {
732741
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) {
740749
}
741750
return mp_obj_new_bool(memcmp(str + (str_len - suffix_len), suffix, suffix_len) == 0);
742751
}
752+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
743753

744754
enum { LSTRIP, RSTRIP, STRIP };
745755

@@ -817,14 +827,17 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) {
817827
STATIC mp_obj_t str_strip(size_t n_args, const mp_obj_t *args) {
818828
return str_uni_strip(STRIP, n_args, args);
819829
}
830+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
820831

821832
STATIC mp_obj_t str_lstrip(size_t n_args, const mp_obj_t *args) {
822833
return str_uni_strip(LSTRIP, n_args, args);
823834
}
835+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_lstrip_obj, 1, 2, str_lstrip);
824836

825837
STATIC mp_obj_t str_rstrip(size_t n_args, const mp_obj_t *args) {
826838
return str_uni_strip(RSTRIP, n_args, args);
827839
}
840+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rstrip_obj, 1, 2, str_rstrip);
828841

829842
#if MICROPY_PY_BUILTINS_STR_CENTER
830843
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) {
841854
memcpy(vstr.buf + left, str, str_len);
842855
return mp_obj_new_str_from_vstr(mp_obj_get_type(str_in), &vstr);
843856
}
857+
MP_DEFINE_CONST_FUN_OBJ_2(str_center_obj, str_center);
844858
#endif
845859

846860
// 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
13461360
vstr_t vstr = mp_obj_str_format_helper((const char*)str, (const char*)str + len, &arg_i, n_args, args, kwargs);
13471361
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
13481362
}
1363+
MP_DEFINE_CONST_FUN_OBJ_KW(str_format_obj, 1, mp_obj_str_format);
13491364

13501365
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict) {
13511366
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) {
16491664

16501665
return mp_obj_new_str_from_vstr(self_type, &vstr);
16511666
}
1667+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
16521668

16531669
STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
16541670
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) {
16891705

16901706
return MP_OBJ_NEW_SMALL_INT(num_occurrences);
16911707
}
1708+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
16921709

16931710
#if MICROPY_PY_BUILTINS_STR_PARTITION
16941711
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) {
17361753
STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
17371754
return str_partitioner(self_in, arg, 1);
17381755
}
1756+
MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
17391757

17401758
STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
17411759
return str_partitioner(self_in, arg, -1);
17421760
}
1761+
MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
17431762
#endif
17441763

17451764
// 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) {
17571776
STATIC mp_obj_t str_lower(mp_obj_t self_in) {
17581777
return str_caseconv(unichar_tolower, self_in);
17591778
}
1779+
MP_DEFINE_CONST_FUN_OBJ_1(str_lower_obj, str_lower);
17601780

17611781
STATIC mp_obj_t str_upper(mp_obj_t self_in) {
17621782
return str_caseconv(unichar_toupper, self_in);
17631783
}
1784+
MP_DEFINE_CONST_FUN_OBJ_1(str_upper_obj, str_upper);
17641785

17651786
STATIC mp_obj_t str_uni_istype(bool (*f)(unichar), mp_obj_t self_in) {
17661787
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) {
17981819
STATIC mp_obj_t str_isspace(mp_obj_t self_in) {
17991820
return str_uni_istype(unichar_isspace, self_in);
18001821
}
1822+
MP_DEFINE_CONST_FUN_OBJ_1(str_isspace_obj, str_isspace);
18011823

18021824
STATIC mp_obj_t str_isalpha(mp_obj_t self_in) {
18031825
return str_uni_istype(unichar_isalpha, self_in);
18041826
}
1827+
MP_DEFINE_CONST_FUN_OBJ_1(str_isalpha_obj, str_isalpha);
18051828

18061829
STATIC mp_obj_t str_isdigit(mp_obj_t self_in) {
18071830
return str_uni_istype(unichar_isdigit, self_in);
18081831
}
1832+
MP_DEFINE_CONST_FUN_OBJ_1(str_isdigit_obj, str_isdigit);
18091833

18101834
STATIC mp_obj_t str_isupper(mp_obj_t self_in) {
18111835
return str_uni_istype(unichar_isupper, self_in);
18121836
}
1837+
MP_DEFINE_CONST_FUN_OBJ_1(str_isupper_obj, str_isupper);
18131838

18141839
STATIC mp_obj_t str_islower(mp_obj_t self_in) {
18151840
return str_uni_istype(unichar_islower, self_in);
18161841
}
1842+
MP_DEFINE_CONST_FUN_OBJ_1(str_islower_obj, str_islower);
18171843

18181844
#if MICROPY_CPYTHON_COMPAT
18191845
// 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) {
18291855
}
18301856
return mp_obj_str_make_new(&mp_type_str, n_args, 0, args);
18311857
}
1858+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bytes_decode_obj, 1, 3, bytes_decode);
18321859

18331860
// TODO: should accept kwargs too
18341861
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) {
18411868
}
18421869
return bytes_make_new(NULL, n_args, 0, args);
18431870
}
1871+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj, 1, 3, str_encode);
18441872
#endif
18451873

18461874
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
18591887
}
18601888
}
18611889

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-
18991890
STATIC const mp_rom_map_elem_t str8_locals_dict_table[] = {
19001891
#if MICROPY_CPYTHON_COMPAT
19011892
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&bytes_decode_obj) },

0 commit comments

Comments
 (0)