mirror of
https://github.com/micropython/micropython.git
synced 2026-01-08 21:20:13 +01:00
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;
|
||||
|
||||
Reference in New Issue
Block a user