summaryrefslogtreecommitdiff
path: root/src/libstddjb
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2019-07-11 16:19:36 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2019-07-11 16:19:36 +0000
commit9ec6602c9d3c1a8801d996aa3a56c779ce1c5f70 (patch)
tree5e545ff43b1e5890f0931c244a8b410eb036eb6c /src/libstddjb
parent846cd5b2b12947753f5fd3f02c49f7d828fb763d (diff)
downloadskalibs-9ec6602c9d3c1a8801d996aa3a56c779ce1c5f70.tar.xz
bits-fmtscan refactor, part one
Diffstat (limited to 'src/libstddjb')
-rw-r--r--src/libstddjb/fmtscan-internal.h101
-rw-r--r--src/libstddjb/int160_scan.c2
-rw-r--r--src/libstddjb/int16_fmt.c6
-rw-r--r--src/libstddjb/int16_scan.c11
-rw-r--r--src/libstddjb/int320_scan.c2
-rw-r--r--src/libstddjb/int32_fmt.c6
-rw-r--r--src/libstddjb/int32_scan.c11
-rw-r--r--src/libstddjb/int640_scan.c2
-rw-r--r--src/libstddjb/int64_fmt.c6
-rw-r--r--src/libstddjb/int64_fmt_generic.c10
-rw-r--r--src/libstddjb/int64_scan.c7
-rw-r--r--src/libstddjb/uint160_fmt.c6
-rw-r--r--src/libstddjb/uint160_scan.c2
-rw-r--r--src/libstddjb/uint16_fmt.c6
-rw-r--r--src/libstddjb/uint16_scan.c11
-rw-r--r--src/libstddjb/uint320_fmt.c6
-rw-r--r--src/libstddjb/uint320_scan.c2
-rw-r--r--src/libstddjb/uint32_fmt.c6
-rw-r--r--src/libstddjb/uint32_scan.c11
-rw-r--r--src/libstddjb/uint640_fmt.c6
-rw-r--r--src/libstddjb/uint640_scan.c2
-rw-r--r--src/libstddjb/uint64_fmt.c6
-rw-r--r--src/libstddjb/uint64_scan.c7
23 files changed, 71 insertions, 164 deletions
diff --git a/src/libstddjb/fmtscan-internal.h b/src/libstddjb/fmtscan-internal.h
index 5bbfa69..61b3e6a 100644
--- a/src/libstddjb/fmtscan-internal.h
+++ b/src/libstddjb/fmtscan-internal.h
@@ -7,76 +7,30 @@
#include <string.h>
#include <errno.h>
#include <limits.h>
+
+#include <skalibs/uint64.h>
#include <skalibs/fmtscan.h>
-#define SCANB(bits) \
-size_t uint##bits##_scan_base (char const *s, uint##bits##_t *u, unsigned int base) \
-{ \
- static uint##bits##_t const max = UINT##bits##_MAX ; \
- uint##bits##_t result = 0 ; \
- size_t pos = 0 ; \
- for (;; pos++) \
- { \
- unsigned char c = fmtscan_num(s[pos], base) ; \
- if ((c >= base) || (result > ((max - c) / base))) break ; \
- result = result * base + c ; \
- } \
- if (pos) *u = result ; \
- return pos ; \
-} \
+extern size_t uint64_scan_base_max (char const *, uint64_t *, uint8_t, uint64_t) ;
+extern size_t int64_scan_base_max (char const *, int64_t *, uint8_t, uint64_t) ;
#define SCANB0(bits) \
-size_t uint##bits##0_scan_base (char const *s, uint##bits##_t *u, unsigned int base) \
+size_t uint##bits##0_scan_base (char const *s, uint##bits##_t *u, uint8_t base) \
{ \
size_t pos = uint##bits##_scan_base(s, u, base) ; \
if (!pos) return (errno = EINVAL, 0) ; \
if (!s[pos]) return pos ; \
- errno = (fmtscan_num(s[pos], base) < base) ? EDOM : EINVAL ; \
+ errno = fmtscan_num(s[pos], base) < base ? ERANGE : EINVAL ; \
return 0 ; \
} \
-#define SCANS(bits) \
-size_t int##bits##_scan (char const *s, int##bits##_t *n) \
-{ \
- uint##bits##_t tmp ; \
- size_t r = 0 ; \
- unsigned int sign = 0 ; \
- if (*s == '-') \
- { \
- r = 1 + uint##bits##_scan(s+1, &tmp) ; \
- if (r == 1) return 0 ; \
- if (tmp == 0) *n = 0 ; \
- else \
- { \
- if (tmp-1 > -(INT##bits##_MIN+1)) \
- { \
- tmp /= 10 ; \
- r-- ; \
- } \
- *n = INT##bits##_MIN + (-(INT##bits##_MIN+1) - (tmp-1)) ; \
- } \
- return r ; \
- } \
- if (*s == '+') (s++, sign++) ; \
- r = uint##bits##_scan(s, &tmp) ; \
- if (!r) return 0 ; \
- r += sign ; \
- if (tmp > INT##bits##_MAX) \
- { \
- tmp /= 10 ; \
- r-- ; \
- } \
- *n = tmp ; \
- return r ; \
-} \
-
#define SCANS0(bits) \
-size_t int##bits##0_scan (char const *s, int##bits##_t *u) \
+size_t int##bits##0_scan_base (char const *s, int##bits##_t *d, uint8_t base) \
{ \
- size_t pos = int##bits##_scan(s, u) ; \
+ size_t pos = int##bits##_scan(s, d) ; \
if (!pos) return (errno = EINVAL, 0) ; \
if (!s[pos]) return pos ; \
- errno = (fmtscan_num(s[pos], 10) < 10) ? EDOM : EINVAL ; \
+ errno = (fmtscan_num(s[pos], base) < base) ? ERANGE : EINVAL ; \
return 0 ; \
} \
@@ -124,43 +78,6 @@ size_t uint##bits##_fmtlist (char *s, uint##bits##_t const *tab, size_t n) \
return len ; \
} \
-#define FMTB(bits) \
-size_t uint##bits##_fmt_base (char *s, uint##bits##_t x, unsigned int base) \
-{ \
- size_t len = 1 ; \
- { \
- uint##bits##_t q = x ; \
- while (q >= base) { len++ ; q /= base ; } \
- } \
- if (s) \
- { \
- s += len ; \
- do { *--s = fmtscan_asc(x % base) ; x /= base ; } while (x) ; \
- } \
- return len ; \
-} \
-
-#define FMTB0(bits) \
-size_t uint##bits##0_fmt_base (char *s, uint##bits##_t x, size_t n, unsigned int base) \
-{ \
- size_t r = n ; \
- size_t len = uint##bits##_fmt_base(0, x, base) ; \
- if (s) \
- { \
- while (n-- > len) *s++ = '0' ; \
- len = uint##bits##_fmt_base(s, x, base) ; \
- } \
- return len > r ? len : r ; \
-} \
-
-#define FMTS(bits) \
-size_t int##bits##_fmt (char *fmt, int##bits##_t n) \
-{ \
- if (n >= 0) return uint##bits##_fmt(fmt, n) ; \
- if (fmt) *fmt++ = '-' ; \
- return 1 + uint##bits##_fmt(fmt, -n) ; \
-} \
-
#define FMTSL(bits) \
size_t int##bits##_fmtlist (char *s, int##bits##_t const *tab, size_t n) \
{ \
diff --git a/src/libstddjb/int160_scan.c b/src/libstddjb/int160_scan.c
index f2ce735..95a1f3c 100644
--- a/src/libstddjb/int160_scan.c
+++ b/src/libstddjb/int160_scan.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint16.h>
+#include "fmtscan-internal.h"
SCANS0(16)
diff --git a/src/libstddjb/int16_fmt.c b/src/libstddjb/int16_fmt.c
deleted file mode 100644
index 6dff58b..0000000
--- a/src/libstddjb/int16_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint16.h>
-
-FMTS(16)
diff --git a/src/libstddjb/int16_scan.c b/src/libstddjb/int16_scan.c
index b20523d..a8e7e55 100644
--- a/src/libstddjb/int16_scan.c
+++ b/src/libstddjb/int16_scan.c
@@ -1,6 +1,13 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint16.h>
+#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
-SCANS(16)
+size_t int16_scan_base (char const *s, int16_t *d, uint8_t base)
+{
+ int64_t dd ;
+ size_t pos = int64_scan_base_max(s, &dd, base, UINT16_MAX) ;
+ if (pos) *d = (uint16_t)dd ;
+ return pos ;
+}
diff --git a/src/libstddjb/int320_scan.c b/src/libstddjb/int320_scan.c
index dea7c91..f2d288d 100644
--- a/src/libstddjb/int320_scan.c
+++ b/src/libstddjb/int320_scan.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint32.h>
+#include "fmtscan-internal.h"
SCANS0(32)
diff --git a/src/libstddjb/int32_fmt.c b/src/libstddjb/int32_fmt.c
deleted file mode 100644
index dc0ff1b..0000000
--- a/src/libstddjb/int32_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint32.h>
-
-FMTS(32)
diff --git a/src/libstddjb/int32_scan.c b/src/libstddjb/int32_scan.c
index ba4510a..7efcdd9 100644
--- a/src/libstddjb/int32_scan.c
+++ b/src/libstddjb/int32_scan.c
@@ -1,6 +1,13 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint32.h>
+#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
-SCANS(32)
+size_t int32_scan_base (char const *s, int32_t *d, uint8_t base)
+{
+ int64_t dd ;
+ size_t pos = int64_scan_base_max(s, &dd, base, UINT32_MAX) ;
+ if (pos) *d = (int32_t)dd ;
+ return pos ;
+}
diff --git a/src/libstddjb/int640_scan.c b/src/libstddjb/int640_scan.c
index 69e9f6d..88f0f58 100644
--- a/src/libstddjb/int640_scan.c
+++ b/src/libstddjb/int640_scan.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
SCANS0(64)
diff --git a/src/libstddjb/int64_fmt.c b/src/libstddjb/int64_fmt.c
deleted file mode 100644
index d2b7145..0000000
--- a/src/libstddjb/int64_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint64.h>
-
-FMTS(64)
diff --git a/src/libstddjb/int64_fmt_generic.c b/src/libstddjb/int64_fmt_generic.c
new file mode 100644
index 0000000..f57f915
--- /dev/null
+++ b/src/libstddjb/int64_fmt_generic.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <skalibs/uint64.h>
+
+size_t int64_fmt_generic (char *fmt, int64_t d, uint8_t base)
+{
+ if (d >= 0) return uint64_fmt_generic(fmt, (uint64_t)d, base) ;
+ if (fmt) *fmt++ = '-' ;
+ return 1 + uint64_fmt_generic(fmt, -(uint64_t)d, base) ;
+}
diff --git a/src/libstddjb/int64_scan.c b/src/libstddjb/int64_scan.c
index 934e84c..2631c0c 100644
--- a/src/libstddjb/int64_scan.c
+++ b/src/libstddjb/int64_scan.c
@@ -1,6 +1,9 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
-SCANS(64)
+size_t int64_scan_base (char const *s, int64_t *d, uint8_t base)
+{
+ return int64_scan_base_max(s, d, base, INT64_MAX) ;
+}
diff --git a/src/libstddjb/uint160_fmt.c b/src/libstddjb/uint160_fmt.c
deleted file mode 100644
index ccd2485..0000000
--- a/src/libstddjb/uint160_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint16.h>
-
-FMTB0(16)
diff --git a/src/libstddjb/uint160_scan.c b/src/libstddjb/uint160_scan.c
index 36d04f3..9a48514 100644
--- a/src/libstddjb/uint160_scan.c
+++ b/src/libstddjb/uint160_scan.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint16.h>
+#include "fmtscan-internal.h"
SCANB0(16)
diff --git a/src/libstddjb/uint16_fmt.c b/src/libstddjb/uint16_fmt.c
deleted file mode 100644
index 668c2bf..0000000
--- a/src/libstddjb/uint16_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint16.h>
-
-FMTB(16)
diff --git a/src/libstddjb/uint16_scan.c b/src/libstddjb/uint16_scan.c
index 4c0564e..4b23e64 100644
--- a/src/libstddjb/uint16_scan.c
+++ b/src/libstddjb/uint16_scan.c
@@ -1,6 +1,13 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint16.h>
+#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
-SCANB(16)
+size_t uint16_scan_base (char const *s, uint16_t *u, uint8_t base)
+{
+ uint64_t uu ;
+ size_t pos = uint64_scan_base_max(s, &uu, base, UINT16_MAX) ;
+ if (pos) *u = (uint16_t)uu ;
+ return pos ;
+}
diff --git a/src/libstddjb/uint320_fmt.c b/src/libstddjb/uint320_fmt.c
deleted file mode 100644
index 7de9833..0000000
--- a/src/libstddjb/uint320_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint32.h>
-
-FMTB0(32)
diff --git a/src/libstddjb/uint320_scan.c b/src/libstddjb/uint320_scan.c
index 2c63e65..103354c 100644
--- a/src/libstddjb/uint320_scan.c
+++ b/src/libstddjb/uint320_scan.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint32.h>
+#include "fmtscan-internal.h"
SCANB0(32)
diff --git a/src/libstddjb/uint32_fmt.c b/src/libstddjb/uint32_fmt.c
deleted file mode 100644
index 4364285..0000000
--- a/src/libstddjb/uint32_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint32.h>
-
-FMTB(32)
diff --git a/src/libstddjb/uint32_scan.c b/src/libstddjb/uint32_scan.c
index 58d43e6..672beec 100644
--- a/src/libstddjb/uint32_scan.c
+++ b/src/libstddjb/uint32_scan.c
@@ -1,6 +1,13 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint32.h>
+#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
-SCANB(32)
+size_t uint32_scan_base (char const *s, uint32_t *u, uint8_t base)
+{
+ uint64_t uu ;
+ size_t pos = uint64_scan_base_max(s, &uu, base, UINT32_MAX) ;
+ if (pos) *u = (uint32_t)uu ;
+ return pos ;
+}
diff --git a/src/libstddjb/uint640_fmt.c b/src/libstddjb/uint640_fmt.c
deleted file mode 100644
index dc9cfa5..0000000
--- a/src/libstddjb/uint640_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint64.h>
-
-FMTB0(64)
diff --git a/src/libstddjb/uint640_scan.c b/src/libstddjb/uint640_scan.c
index 28ae049..45c8e20 100644
--- a/src/libstddjb/uint640_scan.c
+++ b/src/libstddjb/uint640_scan.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
SCANB0(64)
diff --git a/src/libstddjb/uint64_fmt.c b/src/libstddjb/uint64_fmt.c
deleted file mode 100644
index 91c8178..0000000
--- a/src/libstddjb/uint64_fmt.c
+++ /dev/null
@@ -1,6 +0,0 @@
-/* ISC license. */
-
-#include "fmtscan-internal.h"
-#include <skalibs/uint64.h>
-
-FMTB(64)
diff --git a/src/libstddjb/uint64_scan.c b/src/libstddjb/uint64_scan.c
index 33744b1..b6afa8b 100644
--- a/src/libstddjb/uint64_scan.c
+++ b/src/libstddjb/uint64_scan.c
@@ -1,6 +1,9 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
-SCANB(64)
+size_t uint64_scan_base (char const *s, uint64_t *u, uint8_t base)
+{
+ return uint64_scan_base_max(s, u, base, UINT64_MAX) ;
+}