diff options
| author | Jeff Epler <jepler@gmail.com> | 2024-01-25 09:09:06 -0600 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-05-13 12:16:35 +1000 |
| commit | 9032491efd8a63e0b13dbcfb8579cde4791c03af (patch) | |
| tree | 50245b32d8cba4f36c049a7c68cf11ff6606b5aa /py/objstr.c | |
| parent | f47e214cdcf11c2067936cb4b4e4f9deab73f6fc (diff) | |
py/objstr: Add support for the :_b/o/x specifier in str.format.
This groups non-decimal values by fours, such as bbb_bbbb_bbbb. It also
supports `{:_d}` to use underscore for decimal numbers (grouped in threes).
Use of incorrect ":,b" is not diagnosed.
Thanks to @dpgeorge for the suggestion to reduce code size.
Signed-off-by: Jeff Epler <jepler@gmail.com>
Diffstat (limited to 'py/objstr.c')
| -rw-r--r-- | py/objstr.c | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/py/objstr.c b/py/objstr.c index fda31d531..c81fc682f 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -1184,7 +1184,7 @@ static vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar int width = -1; int precision = -1; char type = '\0'; - int flags = 0; + unsigned int flags = 0; if (format_spec) { // The format specifier (from http://docs.python.org/2/library/string.html#formatspec) @@ -1229,8 +1229,9 @@ static vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar } } s = str_to_int(s, stop, &width); - if (*s == ',') { - flags |= PF_FLAG_SHOW_COMMA; + if (*s == ',' || *s == '_') { + MP_STATIC_ASSERT((unsigned)'_' << PF_FLAG_SEP_POS >> PF_FLAG_SEP_POS == '_'); + flags |= (unsigned)*s << PF_FLAG_SEP_POS; s++; } if (*s == '.') { |
