(wrong string) ée

From: <git_AT_suckless.org>
Date: Wed, 2 Mar 2016 10:27:15 +0100 (CET)

commit ea2d6c93d28a9c5af94035057c52c36f0618ba2c
Author: Mattias Andrée <maandree_AT_kth.se>
AuthorDate: Wed Mar 2 09:11:48 2016 +0100
Commit: Mattias Andrée <maandree_AT_kth.se>
CommitDate: Wed Mar 2 09:12:31 2016 +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_AT_kth.se>

diff --git a/man/zstr.3 b/man/zstr.3
index 9fef75b..04cacd9 100644
--- a/man/zstr.3
+++ b/man/zstr.3
_AT_@ -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
new file mode 100644
index 0000000..cd1014a
--- /dev/null
+++ b/man/zstr_length.3
_AT_@ -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
deleted file mode 100644
index 3696378..0000000
--- a/man/zstr_length_positive.3
+++ /dev/null
_AT_@ -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
index df973fb..a544924 100644
--- a/src/internals.h
+++ b/src/internals.h
_AT_@ -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
new file mode 100644
index 0000000..4edabb0
--- /dev/null
+++ b/src/zstr_length.c
_AT_@ -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
index c39510f..3816411 100644
--- a/zahl.h
+++ b/zahl.h
_AT_@ -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. */
Received on Wed Mar 02 2016 - 10:27:15 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 02 2016 - 10:36:19 CET