diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2019-07-11 16:19:36 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2019-07-11 16:19:36 +0000 |
commit | 9ec6602c9d3c1a8801d996aa3a56c779ce1c5f70 (patch) | |
tree | 5e545ff43b1e5890f0931c244a8b410eb036eb6c /src | |
parent | 846cd5b2b12947753f5fd3f02c49f7d828fb763d (diff) | |
download | skalibs-9ec6602c9d3c1a8801d996aa3a56c779ce1c5f70.tar.xz |
bits-fmtscan refactor, part one
Diffstat (limited to 'src')
28 files changed, 120 insertions, 175 deletions
diff --git a/src/headers/bits-header b/src/headers/bits-header index 58eee22..e96eecc 100644 --- a/src/headers/bits-header +++ b/src/headers/bits-header @@ -3,4 +3,5 @@ #ifndef UINT@BITS@_H #define UINT@BITS@_H -#include <sys/types.h> +#include <stddef.h> +#include <stdint.h> diff --git a/src/headers/bits-stdint b/src/headers/bits-stdint deleted file mode 100644 index 31da039..0000000 --- a/src/headers/bits-stdint +++ /dev/null @@ -1,2 +0,0 @@ -#include <stdint.h> - diff --git a/src/headers/bits-template b/src/headers/bits-template index b09036b..fa08dd9 100644 --- a/src/headers/bits-template +++ b/src/headers/bits-template @@ -10,9 +10,8 @@ extern uint@BITS@_t uint@BITS@_bswap (uint@BITS@_t) ; #define UINT@BITS@_XFMT @XFMT@ #define UINT@BITS@_BFMT @BFMT@ -extern size_t uint@BITS@_fmt_base (char *, uint@BITS@_t, unsigned int) ; -extern size_t uint@BITS@0_fmt_base (char *, uint@BITS@_t, size_t, unsigned int) ; - +#define uint@BITS@_fmt_base(s, u, base) uint64_fmt_generic(s, u, base) +#define uint@BITS@0_fmt_base(s, u, n, base) uint640_fmt_generic(s, u, n, base) #define uint@BITS@_fmt(s, u) uint@BITS@_fmt_base(s, (u), 10) #define uint@BITS@0_fmt(s, u, n) uint@BITS@0_fmt_base(s, u, (n), 10) #define uint@BITS@_ofmt(s, o) uint@BITS@_fmt_base(s, (o), 8) @@ -24,8 +23,8 @@ extern size_t uint@BITS@0_fmt_base (char *, uint@BITS@_t, size_t, unsigned int) extern size_t uint@BITS@_fmtlist (char *, uint@BITS@_t const *, size_t) ; -extern size_t uint@BITS@_scan_base (char const *, uint@BITS@_t *, unsigned int) ; -extern size_t uint@BITS@0_scan_base (char const *, uint@BITS@_t *, unsigned int) ; +extern size_t uint@BITS@_scan_base (char const *, uint@BITS@_t *, uint8_t) ; +extern size_t uint@BITS@0_scan_base (char const *, uint@BITS@_t *, uint8_t) ; #define uint@BITS@_scan(s, u) uint@BITS@_scan_base(s, (u), 10) #define uint@BITS@0_scan(s, u) uint@BITS@0_scan_base(s, (u), 10) @@ -38,8 +37,24 @@ extern size_t uint@BITS@0_scan_base (char const *, uint@BITS@_t *, unsigned int) extern size_t uint@BITS@_scanlist (uint@BITS@_t *, size_t, char const *, size_t *) ; -extern size_t int@BITS@_fmt (char *, int@BITS@_t) ; +#define int@BITS@_fmt_base(s, u, base) int64_fmt_generic(s, u, base) +#define int@BITS@_fmt(s, u) int@BITS@_fmt_base(s, (u), 10) +#define int@BITS@_ofmt(s, o) int@BITS@_fmt_base(s, (o), 8) +#define int@BITS@_xfmt(s, x) int@BITS@_fmt_base(s, (x), 16) +#define int@BITS@_bfmt(s, b) int@BITS@0_fmt_base(s, (b), 2) + extern size_t int@BITS@_fmtlist (char *, int@BITS@_t const *, size_t) ; -extern size_t int@BITS@_scan (char const *, int@BITS@_t *) ; -extern size_t int@BITS@0_scan (char const *, int@BITS@_t *) ; + +extern size_t int@BITS@_scan_base (char const *, int@BITS@_t *, uint8_t) ; +extern size_t int@BITS@0_scan_base (char const *, int@BITS@_t *, uint8_t) ; + +#define int@BITS@_scan(s, u) int@BITS@_scan_base(s, (u), 10) +#define int@BITS@0_scan(s, u) int@BITS@0_scan_base(s, (u), 10) +#define int@BITS@_oscan(s, u) int@BITS@_scan_base(s, (u), 8) +#define int@BITS@0_oscan(s, u) int@BITS@0_scan_base(s, (u), 8) +#define int@BITS@_xscan(s, u) int@BITS@_scan_base(s, (u), 16) +#define int@BITS@0_xscan(s, u) int@BITS@0_scan_base(s, (u), 16) +#define int@BITS@_bscan(s, u) int@BITS@_scan_base(s, (u), 2) +#define int@BITS@0_bscan(s, u) int@BITS@0_scan_base(s, (u), 2) + extern size_t int@BITS@_scanlist (int@BITS@_t *, size_t, char const *, size_t *) ; diff --git a/src/headers/uint64-noulong64 b/src/headers/uint64-noulong64 index 1a67c1d..fe61dda 100644 --- a/src/headers/uint64-noulong64 +++ b/src/headers/uint64-noulong64 @@ -1,3 +1,15 @@ typedef unsigned long long uint64_t ; +typedef signed long long int64_t ; +#ifndef INT64_MIN +# define INT64_MIN (-1-0x7fffffffffffffffll) +#endif + +#ifndef INT64_MAX +# define INT64_MAX 0x7fffffffffffffffll +#endif + +#ifndef UINT64_MAX +# define UINT64_MAX 0xffffffffffffffffull +#endif diff --git a/src/headers/uint64-ulong64 b/src/headers/uint64-ulong64 index 84ae235..f01c668 100644 --- a/src/headers/uint64-ulong64 +++ b/src/headers/uint64-ulong64 @@ -1,3 +1,15 @@ typedef unsigned long uint64_t ; +typedef long int64_t ; +#ifndef INT64_MIN +# define INT64_MIN (-1-0x7fffffffffffffffl) +#endif + +#ifndef INT64_MAX +# define INT64_MAX 0x7fffffffffffffffl +#endif + +#ifndef UINT64_MAX +# define UINT64_MAX 0xfffffffffffffffful +#endif 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) ; +} |