summaryrefslogtreecommitdiff
path: root/py/mpz.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/mpz.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/mpz.c')
-rw-r--r--py/mpz.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/py/mpz.c b/py/mpz.c
index 084aebda9..471bd1598 100644
--- a/py/mpz.c
+++ b/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;
}