commit 34f0e76ca0135b2b302e2b079482a4038149b079
parent fd7dc9d1d7311b05e92464de1fc4eab6f2407b51
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Fri, 19 Jul 2019 12:34:44 +0000
bits-fmtscan refactor, part two
Diffstat:
8 files changed, 40 insertions(+), 18 deletions(-)
diff --git a/src/headers/uint64-defs b/src/headers/uint64-defs
@@ -2,6 +2,7 @@
extern size_t uint64_fmt_generic (char *, uint64_t, uint8_t) ;
extern size_t uint640_fmt_generic (char *, uint64_t, size_t, uint8_t) ;
extern size_t int64_fmt_generic (char *, int64_t, uint8_t) ;
+extern size_t uint64_fmtlist_generic (char *, void const *, size_t, uint8_t, uint64_t (*)(void const *, size_t)) ;
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) ;
diff --git a/src/libstddjb/fmtscan-internal.h b/src/libstddjb/fmtscan-internal.h
@@ -11,9 +11,6 @@
#include <skalibs/uint64.h>
#include <skalibs/fmtscan.h>
-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, uint8_t base) \
{ \
@@ -65,17 +62,13 @@ size_t int##bits##_scanlist (int##bits##_t *tab, size_t max, char const *s, size
} \
#define FMTL(bits) \
+static uint64_t get (void const *tab, size_t i) \
+{ \
+ return ((uint##bits##_t const *)tab)[i] ; \
+} \
size_t uint##bits##_fmtlist (char *s, uint##bits##_t const *tab, size_t n) \
{ \
- size_t i = 0, len = 0 ; \
- for (; i < n ; i++) \
- { \
- size_t w = uint##bits##_fmt(s, tab[i]) ; \
- len += w ; \
- if (s) s += w ; \
- if (i < n-1) { len++ ; if (s) *s++ = ',' ; } \
- } \
- return len ; \
+ return uint64_fmtlist_generic(s, tab, n, 10, &get) ; \
} \
#define FMTSL(bits) \
diff --git a/src/libstddjb/uint16_fmtlist.c b/src/libstddjb/uint16_fmtlist.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint16.h>
+#include "fmtscan-internal.h"
FMTL(16)
diff --git a/src/libstddjb/uint32_fmtlist.c b/src/libstddjb/uint32_fmtlist.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint32.h>
+#include "fmtscan-internal.h"
FMTL(32)
diff --git a/src/libstddjb/uint640_scan_base_max.c b/src/libstddjb/uint640_scan_base_max.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <errno.h>
+
+#include <skalibs/uint64.h>
+#include <skalibs/fmtscan.h>
+
+size_t uint640_scan_base_max (char const *s, uint64_t *u, uint8_t base, uint64_t max)
+{
+ size_t pos = uint640_scan_base_max(s, u, base, max) ;
+ if (!pos) return (errno = EINVAL, 0) ;
+ if (!s[pos]) return pos ;
+ errno = fmtscan_num(s[pos], base) < base ? ERANGE : EINVAL ;
+ return 0 ;
+}
diff --git a/src/libstddjb/uint64_fmtlist.c b/src/libstddjb/uint64_fmtlist.c
@@ -1,6 +1,6 @@
/* ISC license. */
-#include "fmtscan-internal.h"
#include <skalibs/uint64.h>
+#include "fmtscan-internal.h"
FMTL(64)
diff --git a/src/libstddjb/uint64_fmtlist_generic.c b/src/libstddjb/uint64_fmtlist_generic.c
@@ -0,0 +1,16 @@
+/* ISC license. */
+
+#include <skalibs/uint64.h>
+
+size_t uint64_fmtlist_generic (char *s, void const *tab, size_t n, uint8_t base, uint64_t (*get)(void const *, size_t))
+{
+ size_t i = 0, len = 0 ;
+ for (; i < n ; i++)
+ {
+ size_t w = uint64_fmt_generic(s, (*get)(tab, i), base) ;
+ len += w ;
+ if (s) s += w ;
+ if (i < n-1) { len++ ; if (s) *s++ = ',' ; }
+ }
+ return len ;
+}
diff --git a/src/libstddjb/uint64_scan_base_max.c b/src/libstddjb/uint64_scan_base_max.c
@@ -1,8 +1,5 @@
/* ISC license. */
-#include <stddef.h>
-#include <stdint.h>
-
#include <skalibs/uint64.h>
#include <skalibs/fmtscan.h>