Skip to content

Commit 56eb25f

Browse files
committed
py/objstr: Make .partition()/.rpartition() methods configurable.
Default is disabled, enabled for unix port. Saves 600 bytes on x86.
1 parent a4aaf82 commit 56eb25f

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,11 @@ typedef double mp_float_t;
611611
#define MICROPY_PY_BUILTINS_STR_CENTER (0)
612612
#endif
613613

614+
// Whether str.partition()/str.rpartition() method provided
615+
#ifndef MICROPY_PY_BUILTINS_STR_PARTITION
616+
#define MICROPY_PY_BUILTINS_STR_PARTITION (0)
617+
#endif
618+
614619
// Whether str.splitlines() method provided
615620
#ifndef MICROPY_PY_BUILTINS_STR_SPLITLINES
616621
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (0)

py/objstr.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,7 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
16831683
return MP_OBJ_NEW_SMALL_INT(num_occurrences);
16841684
}
16851685

1686+
#if MICROPY_PY_BUILTINS_STR_PARTITION
16861687
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, mp_int_t direction) {
16871688
assert(MP_OBJ_IS_STR_OR_BYTES(self_in));
16881689
mp_obj_type_t *self_type = mp_obj_get_type(self_in);
@@ -1732,6 +1733,7 @@ STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
17321733
STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
17331734
return str_partitioner(self_in, arg, -1);
17341735
}
1736+
#endif
17351737

17361738
// Supposedly not too critical operations, so optimize for code size
17371739
STATIC mp_obj_t str_caseconv(unichar (*op)(unichar), mp_obj_t self_in) {
@@ -1875,8 +1877,10 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rstrip_obj, 1, 2, str_rstrip);
18751877
MP_DEFINE_CONST_FUN_OBJ_KW(str_format_obj, 1, mp_obj_str_format);
18761878
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
18771879
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
1880+
#if MICROPY_PY_BUILTINS_STR_PARTITION
18781881
MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
18791882
MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
1883+
#endif
18801884
MP_DEFINE_CONST_FUN_OBJ_1(str_lower_obj, str_lower);
18811885
MP_DEFINE_CONST_FUN_OBJ_1(str_upper_obj, str_upper);
18821886
MP_DEFINE_CONST_FUN_OBJ_1(str_isspace_obj, str_isspace);
@@ -1915,8 +1919,10 @@ STATIC const mp_rom_map_elem_t str8_locals_dict_table[] = {
19151919
{ MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) },
19161920
{ MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) },
19171921
{ MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
1922+
#if MICROPY_PY_BUILTINS_STR_PARTITION
19181923
{ MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
19191924
{ MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
1925+
#endif
19201926
#if MICROPY_PY_BUILTINS_STR_CENTER
19211927
{ MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) },
19221928
#endif

py/objstrunicode.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,10 @@ STATIC const mp_rom_map_elem_t struni_locals_dict_table[] = {
246246
{ MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) },
247247
{ MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) },
248248
{ MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
249+
#if MICROPY_PY_BUILTINS_STR_PARTITION
249250
{ MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
250251
{ MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
252+
#endif
251253
#if MICROPY_PY_BUILTINS_STR_CENTER
252254
{ MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) },
253255
#endif

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#define MICROPY_PY_DESCRIPTORS (1)
7474
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
7575
#define MICROPY_PY_BUILTINS_STR_CENTER (1)
76+
#define MICROPY_PY_BUILTINS_STR_PARTITION (1)
7677
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1)
7778
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
7879
#define MICROPY_PY_BUILTINS_FROZENSET (1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy