8000 py/objstr: Make % (__mod__) formatting operator configurable. · andrewleech/micropython@2da5d41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2da5d41

Browse files
pfalcondpgeorge
authored andcommitted
py/objstr: Make % (__mod__) formatting operator configurable.
Default is enabled, disabled for minimal builds. Saves 1296 bytes on x86, 976 bytes on ARM.
1 parent b01f66c commit 2da5d41

File tree

5 files changed

+18
-0
lines changed

5 files changed

+18
-0
lines changed

ports/bare-arm/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define MICROPY_PY_BUILTINS_SET (0)
3030
#define MICROPY_PY_BUILTINS_SLICE (0)
3131
#define MICROPY_PY_BUILTINS_PROPERTY (0)
32+
#define MICROPY_PY_BUILTINS_STR_OP_MODULO (0)
3233
#define MICROPY_PY___FILE__ (0)
3334
#define MICROPY_PY_GC (0)
3435
#define MICROPY_PY_ARRAY (0)

ports/minimal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#define MICROPY_PY_BUILTINS_SLICE (0)
4141
#define MICROPY_PY_BUILTINS_PROPERTY (0)
4242
#define MICROPY_PY_BUILTINS_MIN_MAX (0)
43+
#define MICROPY_PY_BUILTINS_STR_OP_MODULO (0)
4344
#define MICROPY_PY___FILE__ (0)
4445
#define MICROPY_PY_GC (0)
4546
#define MICROPY_PY_ARRAY (0)

ports/unix/mpconfigport_minimal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#define MICROPY_PY_BUILTINS_REVERSED (0)
6666
#define MICROPY_PY_BUILTINS_SET (0)
6767
#define MICROPY_PY_BUILTINS_SLICE (0)
68+
#define MICROPY_PY_BUILTINS_STR_OP_MODULO (0)
6869
#define MICROPY_PY_BUILTINS_STR_UNICODE (0)
6970
#define MICROPY_PY_BUILTINS_PROPERTY (0)
7071
#define MICROPY_PY_BUILTINS_MIN_MAX (0)

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,11 @@ typedef double mp_float_t;
759759
#define MICROPY_PY_BUILTINS_STR_CENTER (0)
760760
#endif
761761

762+
// Whether str % (...) formatting operator provided
763+
#ifndef MICROPY_PY_BUILTINS_STR_OP_MODULO
764+
#define MICROPY_PY_BUILTINS_STR_OP_MODULO (1)
765+
#endif
766+
762767
// Whether str.partition()/str.rpartition() method provided
763768
#ifndef MICROPY_PY_BUILTINS_STR_PARTITION
764769
#define MICROPY_PY_BUILTINS_STR_PARTITION (0)

py/objstr.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
#include "py/runtime.h"
3535
#include "py/stackctrl.h"
3636

37+
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
3738
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict);
39+
#endif
3840

3941
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf);
4042
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
@@ -301,6 +303,7 @@ const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle,
301303
mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
302304
// check for modulo
303305
if (op == MP_BINARY_OP_MODULO) {
306+
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
304307
mp_obj_t *args = &rhs_in;
305308
size_t n_args = 1;
306309
mp_obj_t dict = MP_OBJ_NULL;
@@ -311,6 +314,9 @@ mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i
311314
dict = rhs_in;
312315
}
313316
return str_modulo_format(lhs_in, n_args, args, dict);
317+
#else
318+
return MP_OBJ_NULL;
319+
#endif
314320
}
315321

316322
// from now on we need lhs type and data, so extract them
@@ -915,6 +921,7 @@ STATIC bool arg_looks_numeric(mp_obj_t arg) {
915921
;
916922
}
917923

924+
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
918925
STATIC mp_obj_t arg_as_int(mp_obj_t arg) {
919926
#if MICROPY_PY_BUILTINS_FLOAT
920927
if (mp_obj_is_float(arg)) {
@@ -923,6 +930,7 @@ STATIC mp_obj_t arg_as_int(mp_obj_t arg) {
923930
#endif
924931
return arg;
925932
}
933+
#endif
926934

927935
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
928936
STATIC NORETURN void terse_str_format_value_error(void) {
@@ -1383,6 +1391,7 @@ mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
13831391
}
13841392
MP_DEFINE_CONST_FUN_OBJ_KW(str_format_obj, 1, mp_obj_str_format);
13851393

1394+
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
13861395
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict) {
13871396
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(pattern));
13881397

@@ -1578,6 +1587,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
15781587

15791588
return mp_obj_new_str_from_vstr(is_bytes ? &mp_type_bytes : &mp_type_str, &vstr);
15801589
}
1590+
#endif
15811591

15821592
// The implementation is optimized, returning the original string if there's
15831593
// nothing to replace.

0 commit comments

Comments
 (0)
0