diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/headers/uint64-defs | 8 | ||||
-rw-r--r-- | src/headers/uint64-include | 1 | ||||
-rw-r--r-- | src/headers/uint64-macros | 25 | ||||
-rw-r--r-- | src/libstddjb/int64_scan_base_max.c | 20 | ||||
-rw-r--r-- | src/libstddjb/uint640_fmt_generic.c | 21 | ||||
-rw-r--r-- | src/libstddjb/uint64_fmt_generic.c | 17 | ||||
-rw-r--r-- | src/libstddjb/uint64_scan_base_max.c | 21 |
7 files changed, 113 insertions, 0 deletions
diff --git a/src/headers/uint64-defs b/src/headers/uint64-defs new file mode 100644 index 0000000..551a382 --- /dev/null +++ b/src/headers/uint64-defs @@ -0,0 +1,8 @@ + +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_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/headers/uint64-include b/src/headers/uint64-include new file mode 100644 index 0000000..12a0936 --- /dev/null +++ b/src/headers/uint64-include @@ -0,0 +1 @@ +#include <skalibs/uint64.h> diff --git a/src/headers/uint64-macros b/src/headers/uint64-macros new file mode 100644 index 0000000..c186f07 --- /dev/null +++ b/src/headers/uint64-macros @@ -0,0 +1,25 @@ + +#ifndef INT_LEAST64_MIN +# define INT_LEAST64_MIN INT64_MIN +#endif + +#ifndef INT_FAST64_MIN +# define INT_FAST64_MIN INT64_MIN +#endif + +#ifndef INT_LEAST64_MAX +# define INT_LEAST64_MAX INT64_MAX +#endif + +#ifndef INT_FAST64_MAX +# define INT_FAST64_MAX INT64_MAX +#endif + +#ifndef UINT_LEAST64_MAX +# define UINT_LEAST64_MAX UINT64_MAX +#endif + +#ifndef UINT_FAST64_MAX +# define UINT_FAST64_MAX UINT64_MAX +#endif + diff --git a/src/libstddjb/int64_scan_base_max.c b/src/libstddjb/int64_scan_base_max.c new file mode 100644 index 0000000..69fb4bb --- /dev/null +++ b/src/libstddjb/int64_scan_base_max.c @@ -0,0 +1,20 @@ +/* ISC license. */ + +#include <skalibs/uint64.h> +#include "fmtscan-internal.h" + +size_t int64_scan_base_max (char const *s, int64_t *d, uint8_t base, uint64_t max) +{ + if (s[0] == '-') + { + uint64_t u ; + size_t pos = uint64_scan_base_max(s+1, &u, base, max+1) ; + return pos ? (*d = -u, 1 + pos) : 0 ; + } + else if (s[0] == '+') + { + size_t pos = uint64_scan_base_max(s+1, (uint64_t *)d, base, max) ; + return pos ? 1 + pos : 0 ; + } + else return uint64_scan_base_max(s, (uint64_t *)d, base, max) ; +} diff --git a/src/libstddjb/uint640_fmt_generic.c b/src/libstddjb/uint640_fmt_generic.c new file mode 100644 index 0000000..7ff02be --- /dev/null +++ b/src/libstddjb/uint640_fmt_generic.c @@ -0,0 +1,21 @@ +/* ISC license. */ + +#include <string.h> + +#include <skalibs/uint64.h> +#include <skalibs/fmtscan.h> + +size_t uint640_fmt_generic (char *s, uint64_t x, size_t n, uint8_t base) +{ + size_t len = uint64_fmt_generic(0, x, base) ; + if (s) + { + if (len < n) + { + memset(s, '0', n - len) ; + s += n - len ; + } + uint64_fmt_generic(s, x, base) ; + } + return len > n ? len : n ; +} diff --git a/src/libstddjb/uint64_fmt_generic.c b/src/libstddjb/uint64_fmt_generic.c new file mode 100644 index 0000000..54375a3 --- /dev/null +++ b/src/libstddjb/uint64_fmt_generic.c @@ -0,0 +1,17 @@ +/* ISC license. */ + +#include <skalibs/uint64.h> +#include <skalibs/fmtscan.h> + +size_t uint64_fmt_generic (char *s, uint64_t x, uint8_t base) +{ + size_t len = 1 ; + uint64_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 ; +} diff --git a/src/libstddjb/uint64_scan_base_max.c b/src/libstddjb/uint64_scan_base_max.c new file mode 100644 index 0000000..6debe0a --- /dev/null +++ b/src/libstddjb/uint64_scan_base_max.c @@ -0,0 +1,21 @@ +/* ISC license. */ + +#include <stddef.h> +#include <stdint.h> + +#include <skalibs/uint64.h> +#include <skalibs/fmtscan.h> + +size_t uint64_scan_base_max (char const *s, uint64_t *u, uint8_t base, uint64_t max) +{ + uint64_t result = 0 ; + size_t pos = 0 ; + for (;; pos++) + { + uint8_t c = fmtscan_num(s[pos], base) ; + if (c >= base || result > ((max - c) / base)) break ; + result = result * base + c ; + } + if (pos) *u = result ; + return pos ; +} |