aboutsummaryrefslogtreecommitdiff
path: root/py/objint.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-10-11 13:11:32 +1100
committerDamien George <damien.p.george@gmail.com>2016-10-11 13:11:32 +1100
commit8bb7d958f173eec58892bc00115516c160f93243 (patch)
tree6e17bda1e05c52ef8d627c3ca253055bce895a1f /py/objint.c
parentdf3e5d2b2f8610d246fea348bb96e70f636183ea (diff)
py: Factor duplicated function to calculate size of formatted int.
Diffstat (limited to 'py/objint.c')
-rw-r--r--py/objint.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/py/objint.c b/py/objint.c
index 9f948a145..31067aaa5 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -162,14 +162,14 @@ STATIC const uint8_t log_base2_floor[] = {
4, 4, 4, 5
};
-STATIC uint int_as_str_size_formatted(uint base, const char *prefix, char comma) {
+size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma) {
if (base < 2 || base > 32) {
return 0;
}
- uint num_digits = sizeof(fmt_int_t) * 8 / log_base2_floor[base] + 1;
- uint num_commas = comma ? num_digits / 3: 0;
- uint prefix_len = prefix ? strlen(prefix) : 0;
+ size_t num_digits = num_bits / log_base2_floor[base] + 1;
+ size_t num_commas = comma ? num_digits / 3 : 0;
+ size_t prefix_len = prefix ? strlen(prefix) : 0;
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
}
@@ -211,7 +211,7 @@ char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size,
sign = '-';
}
- uint needed_size = int_as_str_size_formatted(base, prefix, comma);
+ uint 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);
*buf_size = needed_size;