py/objint: Simplify mp_int_format_size and remove unreachable code.
One never needs to format integers with a base larger than 16 (but code can be easily extended beyond this value if needed in the future).
This commit is contained in:
parent
44bf8e1f2b
commit
ea6a958393
10
py/objint.c
10
py/objint.c
|
@ -151,23 +151,21 @@ typedef mp_int_t fmt_int_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
STATIC const uint8_t log_base2_floor[] = {
|
STATIC const uint8_t log_base2_floor[] = {
|
||||||
0,
|
|
||||||
0, 1, 1, 2,
|
0, 1, 1, 2,
|
||||||
2, 2, 2, 3,
|
2, 2, 2, 3,
|
||||||
3, 3, 3, 3,
|
3, 3, 3, 3,
|
||||||
3, 3, 3, 4,
|
3, 3, 3, 4,
|
||||||
|
/* if needed, these are the values for higher bases
|
||||||
4, 4, 4, 4,
|
4, 4, 4, 4,
|
||||||
4, 4, 4, 4,
|
4, 4, 4, 4,
|
||||||
4, 4, 4, 4,
|
4, 4, 4, 4,
|
||||||
4, 4, 4, 5
|
4, 4, 4, 5
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
|
|
||||||
size_t mp_int_format_size(size_t num_bits, int 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) {
|
assert(2 <= base && base <= 16);
|
||||||
return 0;
|
size_t num_digits = num_bits / log_base2_floor[base - 1] + 1;
|
||||||
}
|
|
||||||
|
|
||||||
size_t num_digits = num_bits / log_base2_floor[base] + 1;
|
|
||||||
size_t num_commas = comma ? num_digits / 3 : 0;
|
size_t num_commas = comma ? num_digits / 3 : 0;
|
||||||
size_t prefix_len = prefix ? strlen(prefix) : 0;
|
size_t prefix_len = prefix ? strlen(prefix) : 0;
|
||||||
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
|
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
|
||||||
|
|
Loading…
Reference in New Issue