libzahl

big integer library
git clone git://git.suckless.org/libzahl
Log | Files | Refs | README | LICENSE

commit e82b5e111223c34deb1e200c9f7cc2ecff9efb01
parent faf2cebb48ec44a14b025bbe858b66efb78577fa
Author: Mattias Andrée <maandree@kth.se>
Date:   Mon,  2 May 2016 01:02:41 +0200

zstr: do not calculate the exact output size, calculate something easier

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
Msrc/zstr.c | 14+++++---------
1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/zstr.c b/src/zstr.c @@ -70,15 +70,11 @@ zstr(z_t a, char *b, size_t n) } if (!n) { - /* This is not the most efficient way to handle this. It should - * be faster to allocate buffers that sprintint_fix and - * sprintint_min print to, and then allocate `b` and copy the - * buffers in reverse order into `b`. However, that is an overly - * complicated solution. You probably already know the maximum - * length or do not care about performance. Another disadvantage - * with calculating the length before-hand, means that it is not - * possible to reallocate `b` if it is too small. */ - n = zstr_length(a, 10); + /* Calculate a value that is at least the number of + * digits required to store the string. The overshoot + * is not too signicant. */ + n = (20 * BITS_PER_CHAR / 64 + (BITS_PER_CHAR == 8)) * a->used; + /* Note, depends on a ≠ as ensure above. */ } if (unlikely(!b) && unlikely(!(b = libzahl_temp_allocation = malloc(n + 1))))