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>
This commit is contained in:
Jeff Epler
2024-01-25 09:09:06 -06:00
committed by Damien George
parent f47e214cdc
commit 9032491efd
7 changed files with 32 additions and 21 deletions

View File

@@ -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 == '.') {