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:
committed by
Damien George
parent
f47e214cdc
commit
9032491efd
11
py/mpprint.c
11
py/mpprint.c
@@ -58,7 +58,7 @@ int mp_print_str(const mp_print_t *print, const char *str) {
|
||||
return len;
|
||||
}
|
||||
|
||||
int mp_print_strn(const mp_print_t *print, const char *str, size_t len, int flags, char fill, int width) {
|
||||
int mp_print_strn(const mp_print_t *print, const char *str, size_t len, unsigned int flags, char fill, int width) {
|
||||
int left_pad = 0;
|
||||
int right_pad = 0;
|
||||
int pad = width - len;
|
||||
@@ -201,7 +201,7 @@ static int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base,
|
||||
return len;
|
||||
}
|
||||
|
||||
int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec) {
|
||||
int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, unsigned int base, int base_char, int flags, char fill, int width, int prec) {
|
||||
// These are the only values for "base" that are required to be supported by this
|
||||
// function, since Python only allows the user to format integers in these bases.
|
||||
// If needed this function could be generalised to handle other values.
|
||||
@@ -248,10 +248,7 @@ int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char
|
||||
int prefix_len = prefix - prefix_buf;
|
||||
prefix = prefix_buf;
|
||||
|
||||
char comma = '\0';
|
||||
if (flags & PF_FLAG_SHOW_COMMA) {
|
||||
comma = ',';
|
||||
}
|
||||
char comma = flags >> PF_FLAG_SEP_POS;
|
||||
|
||||
// The size of this buffer is rather arbitrary. If it's not large
|
||||
// enough, a dynamic one will be allocated.
|
||||
@@ -340,7 +337,7 @@ int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char
|
||||
}
|
||||
|
||||
#if MICROPY_PY_BUILTINS_FLOAT
|
||||
int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, char fill, int width, int prec) {
|
||||
int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, unsigned int flags, char fill, int width, int prec) {
|
||||
char buf[32];
|
||||
char sign = '\0';
|
||||
int chrs = 0;
|
||||
|
||||
14
py/mpprint.h
14
py/mpprint.h
@@ -33,11 +33,11 @@
|
||||
#define PF_FLAG_SPACE_SIGN (0x004)
|
||||
#define PF_FLAG_NO_TRAILZ (0x008)
|
||||
#define PF_FLAG_SHOW_PREFIX (0x010)
|
||||
#define PF_FLAG_SHOW_COMMA (0x020)
|
||||
#define PF_FLAG_PAD_AFTER_SIGN (0x040)
|
||||
#define PF_FLAG_CENTER_ADJUST (0x080)
|
||||
#define PF_FLAG_ADD_PERCENT (0x100)
|
||||
#define PF_FLAG_SHOW_OCTAL_LETTER (0x200)
|
||||
#define PF_FLAG_PAD_AFTER_SIGN (0x020)
|
||||
#define PF_FLAG_CENTER_ADJUST (0x040)
|
||||
#define PF_FLAG_ADD_PERCENT (0x080)
|
||||
#define PF_FLAG_SHOW_OCTAL_LETTER (0x100)
|
||||
#define PF_FLAG_SEP_POS (9) // must be above all the above PF_FLAGs
|
||||
|
||||
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
|
||||
#define MP_PYTHON_PRINTER &mp_sys_stdout_print
|
||||
@@ -69,9 +69,9 @@ extern const mp_print_t mp_sys_stdout_print;
|
||||
#endif
|
||||
|
||||
int mp_print_str(const mp_print_t *print, const char *str);
|
||||
int mp_print_strn(const mp_print_t *print, const char *str, size_t len, int flags, char fill, int width);
|
||||
int mp_print_strn(const mp_print_t *print, const char *str, size_t len, unsigned int flags, char fill, int width);
|
||||
#if MICROPY_PY_BUILTINS_FLOAT
|
||||
int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, char fill, int width, int prec);
|
||||
int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, unsigned int flags, char fill, int width, int prec);
|
||||
#endif
|
||||
|
||||
int mp_printf(const mp_print_t *print, const char *fmt, ...);
|
||||
|
||||
4
py/mpz.c
4
py/mpz.c
@@ -1672,6 +1672,8 @@ size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, ch
|
||||
|
||||
size_t ilen = i->len;
|
||||
|
||||
int n_comma = (base == 10) ? 3 : 4;
|
||||
|
||||
char *s = str;
|
||||
if (ilen == 0) {
|
||||
if (prefix) {
|
||||
@@ -1717,7 +1719,7 @@ size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, ch
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!done && comma && (s - last_comma) == 3) {
|
||||
if (!done && comma && (s - last_comma) == n_comma) {
|
||||
*s++ = comma;
|
||||
last_comma = s;
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ static const uint8_t log_base2_floor[] = {
|
||||
size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma) {
|
||||
assert(2 <= base && base <= 16);
|
||||
size_t num_digits = num_bits / log_base2_floor[base - 1] + 1;
|
||||
size_t num_commas = comma ? num_digits / 3 : 0;
|
||||
size_t num_commas = comma ? (base == 10 ? num_digits / 3 : num_digits / 4): 0;
|
||||
size_t prefix_len = prefix ? strlen(prefix) : 0;
|
||||
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
|
||||
}
|
||||
@@ -251,6 +251,7 @@ char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_co
|
||||
sign = '-';
|
||||
}
|
||||
|
||||
int n_comma = (base == 10) ? 3 : 4;
|
||||
size_t needed_size = mp_int_format_size(sizeof(fmt_int_t) * 8, base, prefix, comma);
|
||||
if (needed_size > *buf_size) {
|
||||
*buf = m_new(char, needed_size);
|
||||
@@ -275,7 +276,7 @@ char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_co
|
||||
c += '0';
|
||||
}
|
||||
*(--b) = c;
|
||||
if (comma && num != 0 && b > str && (last_comma - b) == 3) {
|
||||
if (comma && num != 0 && b > str && (last_comma - b) == n_comma) {
|
||||
*(--b) = comma;
|
||||
last_comma = b;
|
||||
}
|
||||
|
||||
@@ -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 == '.') {
|
||||
|
||||
@@ -128,7 +128,7 @@ void mp_event_wait_indefinite(void);
|
||||
void mp_event_wait_ms(mp_uint_t timeout_ms);
|
||||
|
||||
// extra printing method specifically for mp_obj_t's which are integral type
|
||||
int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec);
|
||||
int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, unsigned base, int base_char, int flags, char fill, int width, int prec);
|
||||
|
||||
void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig);
|
||||
static inline void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw) {
|
||||
|
||||
@@ -22,7 +22,17 @@ test("{:4o}", 123)
|
||||
test("{:4x}", 123)
|
||||
test("{:4X}", 123)
|
||||
|
||||
test("{:4,d}", 1)
|
||||
test("{:4_d}", 1)
|
||||
test("{:4_o}", 1)
|
||||
test("{:4_b}", 1)
|
||||
test("{:4_x}", 1)
|
||||
|
||||
test("{:4,d}", 12345678)
|
||||
test("{:4_d}", 12345678)
|
||||
test("{:4_o}", 12345678)
|
||||
test("{:4_b}", 12345678)
|
||||
test("{:4_x}", 12345678)
|
||||
|
||||
test("{:#4b}", 10)
|
||||
test("{:#4o}", 123)
|
||||
|
||||
Reference in New Issue
Block a user