summaryrefslogtreecommitdiff
path: root/py/mpprint.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2024-01-25 09:09:06 -0600
committerDamien George <damien@micropython.org>2025-05-13 12:16:35 +1000
commit9032491efd8a63e0b13dbcfb8579cde4791c03af (patch)
tree50245b32d8cba4f36c049a7c68cf11ff6606b5aa /py/mpprint.c
parentf47e214cdcf11c2067936cb4b4e4f9deab73f6fc (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/mpprint.c')
-rw-r--r--py/mpprint.c11
1 files changed, 4 insertions, 7 deletions
diff --git a/py/mpprint.c b/py/mpprint.c
index 291e4145f..00a5f944c 100644
--- a/py/mpprint.c
+++ b/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;