commit ea2d6c93d28a9c5af94035057c52c36f0618ba2c
parent fe15c7e5b115cae4717f5481e7962cf376066387
Author: Mattias Andrée <maandree@kth.se>
Date: Wed, 2 Mar 2016 09:11:48 +0100
zstr_length_positive is safe for non-positive, hence rename to zstr_length; and add zstr_length
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat:
6 files changed, 59 insertions(+), 32 deletions(-)
diff --git a/man/zstr.3 b/man/zstr.3
@@ -31,6 +31,6 @@ is
.BR 0 ,
the string allocated by the function is returned.
.SH SEE ALSO
-.BR zstr_length_positive (3),
+.BR zstr_length (3),
.BR zsets (3),
.BR zload (3)
diff --git a/man/zstr_length.3 b/man/zstr_length.3
@@ -0,0 +1,24 @@
+.TH ZSTR_LENGTH 3 libzahl
+.SH NAME
+zstr_length - Predict the length of a string
+.SH SYNOPSIS
+.nf
+#include <zahl.h>
+
+size_t zstr_length(z_t \fIa\fP, unsigned long long int\fIradix\fP);
+.fi
+.SH DESCRIPTION
+.B zstr_length
+calculates the number of digits required to
+to represent the absolute value of
+.I a
+in the selected
+.IR radix .
+.SH RETURN VALUE
+The number of digits requires to represent
+.I a
+i the selected
+.I radix
+is returned.
+.SH SEE ALSO
+.BR zstr (3)
diff --git a/man/zstr_length_positive.3 b/man/zstr_length_positive.3
@@ -1,28 +0,0 @@
-.TH ZSTR_LENGTH_POSITIVE 3 libzahl
-.SH NAME
-zstr_length_positive - Predict the length of a string
-.SH SYNOPSIS
-.nf
-#include <zahl.h>
-
-size_t zstr_length_positive(z_t \fIa\fP, unsigned long long int\fIradix\fP);
-.fi
-.SH DESCRIPTION
-.B zstr_length_positive
-calculates the number of digits required to
-to represent
-.I a
-in the selected
-.IR radix .
-.P
-.I a
-must be a positive integer. If it is zero or negative,
-undefined behaviour is invoked.
-.SH RETURN VALUE
-The number of digits requires to represent
-.I a
-i the selected
-.I radix
-is returned.
-.SH SEE ALSO
-.BR zstr (3)
diff --git a/src/internals.h b/src/internals.h
@@ -9,7 +9,10 @@
#define BITS_IN_LAST_CHAR(bits) ((bits) & (BITS_PER_CHAR - 1))
#define LIST_TEMPS\
- X(libzahl_tmp_cmp)
+ X(libzahl_tmp_cmp)\
+ X(libzahl_tmp_str_num)\
+ X(libzahl_tmp_str_mag)\
+ X(libzahl_tmp_str_div)
#define X(x) extern z_t x;
LIST_TEMPS
diff --git a/src/zstr_length.c b/src/zstr_length.c
@@ -0,0 +1,28 @@
+/* See LICENSE file for copyright and license details. */
+#include "internals"
+
+#define num libzahl_tmp_str_num
+#define mag libzahl_tmp_str_mag
+#define div libzahl_tmp_str_div
+
+
+size_t
+zstr_length(z_t a, unsigned long long int radix)
+{
+ size_t size_total = 1, size_temp;
+ zset(num, a);
+ while (!zzero(num)) {
+ zsetu(mag, radix);
+ zset(div, mag);
+ size_temp = 1;
+ while (zcmpmag(mag, num) <= 0) {
+ zset(div, mag);
+ zsqr(mag, mag);
+ size_temp <<= 1;
+ }
+ size_temp >>= 1;
+ size_total += size_temp;
+ zdiv(num, num, div);
+ }
+ return size_total;
+}
diff --git a/zahl.h b/zahl.h
@@ -118,8 +118,8 @@ void zrand(z_t, enum zranddev, enum zranddist, z_t);
char *zstr(z_t, char *); /* Write a in decimal onto b. */
int zsets(z_t, const char *); /* a := b */
-/* Length of a in radix b, assuming a > 0. */
-size_t zstr_length_positive(z_t, unsigned long long int);
+/* Length of a in radix b. */
+size_t zstr_length(z_t, unsigned long long int);
/* Inline functions. */