diff options
-rw-r--r-- | src/internal/intparse.c | 116 | ||||
-rw-r--r-- | src/internal/intparse.h | 11 | ||||
-rw-r--r-- | src/stdlib/strtoimax.c | 38 | ||||
-rw-r--r-- | src/stdlib/strtol.c | 62 | ||||
-rw-r--r-- | src/stdlib/strtoll.c | 17 | ||||
-rw-r--r-- | src/stdlib/strtoul.c | 17 | ||||
-rw-r--r-- | src/stdlib/strtoull.c | 17 | ||||
-rw-r--r-- | src/stdlib/strtoumax.c | 34 | ||||
-rw-r--r-- | src/stdlib/wcstoimax.c | 39 | ||||
-rw-r--r-- | src/stdlib/wcstol.c | 85 | ||||
-rw-r--r-- | src/stdlib/wcstoll.c | 18 | ||||
-rw-r--r-- | src/stdlib/wcstoul.c | 18 | ||||
-rw-r--r-- | src/stdlib/wcstoull.c | 18 | ||||
-rw-r--r-- | src/stdlib/wcstoumax.c | 35 |
14 files changed, 120 insertions, 405 deletions
diff --git a/src/internal/intparse.c b/src/internal/intparse.c deleted file mode 100644 index fba38c0a..00000000 --- a/src/internal/intparse.c +++ /dev/null @@ -1,116 +0,0 @@ -#include <stdint.h> -#include <limits.h> -#include <stdlib.h> -#include <errno.h> -#include "intparse.h" - -/* Lookup table for digit values. -1==255>=36 -> invalid */ -static const unsigned char digits[] = { --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, --1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, -25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, --1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, -25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, --1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -}; - -#define SLIM (UINT_MAX/36-1) - -int __intparse(struct intparse *v, const void *buf, size_t n) -{ - const unsigned char *s = buf; - int d, b = v->base; - uintmax_t llim; - - v->cnt += n; - for (; n; n--, s++) switch (v->state) { - case 0: - v->err = EINVAL; - v->state++; - if (*s=='+' || *s=='-') { - v->neg = *s=='-'; - continue; - } - case 1: - v->state++; - if (*s=='0' && (!b || b==16)) continue; - if (!b) v->base = b = 10; - v->state++; - goto firstdigit; - case 2: - v->state++; - if ((!b || b==16) && (*s|32) == 'x') { - v->err = 0; - v->base = b = 16; - continue; - } - if (!b) v->base = b = 8; - goto seconddigit; - case 3: - firstdigit: - if (digits[*s] >= b) { - n++; - goto finished; - } - seconddigit: - v->err = 0; - v->state++; - case 4: - if (b==10) { - for (; n && *s-'0'<10U && v->small<=SLIM; n--, s++) - v->small = v->small * 10 + (*s-'0'); - } else if ((b&-b) == b) { - /* Compute bitshift for power-of-two bases - * using a De Bruijn B(2,3) sequence. */ - int bs = "\0\1\2\4\7\3\6\5"[(0x17*b)>>5&7]; - for (; n && (d=digits[*s])<b && v->small<=SLIM; n--, s++) - v->small = (v->small<<bs) + d; - } else { - for (; n && (d=digits[*s])<b && v->small<=SLIM; n--, s++) - v->small = v->small * b + d; - } - if (!n) return 1; - v->state++; - v->val = v->small; - case 5: - if (b==10) { - for (; n && *s-'0'<10U && v->val<=UINTMAX_MAX/10 && (*s-'0')<=UINTMAX_MAX-10*v->val; n--, s++) - v->val = v->val * 10 + (*s-'0'); - } else if ((b&-b) == b) { - int bs = "\0\1\2\4\7\3\6\5"[(0x17*b)>>5&7]; - llim = UINTMAX_MAX>>bs; - for (; n && (d=digits[*s])<b && v->val<=llim; n--, s++) - v->val = (v->val<<bs) + d; - } else { - llim = UINTMAX_MAX/b; - for (; n && (d=digits[*s])<b && v->val<=llim && d<=UINTMAX_MAX-b*v->val; n--, s++) - v->val = v->val * b + d; - } - if (!n) return 1; - if (d >= b) goto finished; - v->state++; - case 6: - if (n && digits[*s]<b) { - v->err = ERANGE; - v->val = UINTMAX_MAX; - n--; s++; - for (; n && digits[*s]<b; n--, s++); - } - if (!n) return 1; - goto finished; - } - return 1; -finished: - v->cnt -= n; - return 0; -} diff --git a/src/internal/intparse.h b/src/internal/intparse.h deleted file mode 100644 index 78e800d1..00000000 --- a/src/internal/intparse.h +++ /dev/null @@ -1,11 +0,0 @@ -#include <stdint.h> -#include <stddef.h> - -struct intparse { - uintmax_t val; - unsigned small; - size_t cnt; - char neg, base, state, err; -}; - -int __intparse(struct intparse *, const void *, size_t); diff --git a/src/stdlib/strtoimax.c b/src/stdlib/strtoimax.c deleted file mode 100644 index 671aa287..00000000 --- a/src/stdlib/strtoimax.c +++ /dev/null @@ -1,38 +0,0 @@ -#include <inttypes.h> -#include <errno.h> -#include <ctype.h> -#include "intparse.h" - -intmax_t strtoimax(const char *s1, char **p, int base) -{ - const unsigned char *s = (void *)s1; - struct intparse ip = {0}; - - if (p) *p = (char *)s1; - - if (base && base-2U > 34) { - errno = EINVAL; - return 0; - } - - for (; isspace(*s); s++); - - ip.base = base; - __intparse(&ip, s, SIZE_MAX); - - if (p && ip.err != EINVAL) - *p = (char *)s + ip.cnt; - - if (ip.err) { - errno = ip.err; - if (ip.err == EINVAL) return 0; - return ip.neg ? INTMAX_MIN : INTMAX_MAX; - } - - if (ip.val > INTMAX_MAX) { - if (!ip.neg || -ip.val != INTMAX_MIN) - errno = ERANGE; - return ip.neg ? INTMAX_MIN : INTMAX_MAX; - } - return ip.neg ? -ip.val : ip.val; -} diff --git a/src/stdlib/strtol.c b/src/stdlib/strtol.c index ace820af..4a949cb0 100644 --- a/src/stdlib/strtol.c +++ b/src/stdlib/strtol.c @@ -1,17 +1,53 @@ -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> +#include "stdio_impl.h" +#include "intscan.h" +#include "shgetc.h" -long strtol(const char *s, char **p, int base) +static unsigned long long strtox(const char *s, char **p, int base, unsigned long long lim) { - intmax_t x = strtoimax(s, p, base); - if (x > LONG_MAX) { - errno = ERANGE; - return LONG_MAX; - } else if (x < LONG_MIN) { - errno = ERANGE; - return LONG_MIN; + /* FIXME: use a helper function or macro to setup the FILE */ + FILE f; + f.flags = 0; + f.buf = f.rpos = (void *)s; + if ((size_t)s > (size_t)-1/2) + f.rend = (void *)-1; + else + f.rend = (unsigned char *)s+(size_t)-1/2; + f.lock = -1; + shlim(&f, 0); + unsigned long long y = __intscan(&f, base, 1, lim); + if (p) { + size_t cnt = shcnt(&f); + *p = (char *)s + cnt; } - return x; + return y; +} + +unsigned long long strtoull(const char *s, char **p, int base) +{ + return strtox(s, p, base, ULLONG_MAX); +} + +long long strtoll(const char *s, char **p, int base) +{ + return strtox(s, p, base, LLONG_MIN); +} + +unsigned long strtoul(const char *s, char **p, int base) +{ + return strtox(s, p, base, ULONG_MAX); +} + +long strtol(const char *s, char **p, int base) +{ + return strtox(s, p, base, 0UL+LONG_MIN); +} + +intmax_t strtoimax(const char *s, char **p, int base) +{ + return strtoll(s, p, base); +} + +uintmax_t strtoumax(const char *s, char **p, int base) +{ + return strtoull(s, p, base); } diff --git a/src/stdlib/strtoll.c b/src/stdlib/strtoll.c deleted file mode 100644 index 9ab66fd9..00000000 --- a/src/stdlib/strtoll.c +++ /dev/null @@ -1,17 +0,0 @@ -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> - -long long strtoll(const char *s, char **p, int base) -{ - intmax_t x = strtoimax(s, p, base); - if (x > LLONG_MAX) { - errno = ERANGE; - return LLONG_MAX; - } else if (x < LLONG_MIN) { - errno = ERANGE; - return LLONG_MIN; - } - return x; -} diff --git a/src/stdlib/strtoul.c b/src/stdlib/strtoul.c deleted file mode 100644 index 20d8bfb2..00000000 --- a/src/stdlib/strtoul.c +++ /dev/null @@ -1,17 +0,0 @@ -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> - -unsigned long strtoul(const char *s, char **p, int base) -{ - intmax_t x; - if (sizeof(intmax_t) == sizeof(long)) - return strtoumax(s, p, base); - x = strtoimax(s, p, base); - if (-x > ULONG_MAX || x > ULONG_MAX) { - errno = ERANGE; - return ULONG_MAX; - } - return x; -} diff --git a/src/stdlib/strtoull.c b/src/stdlib/strtoull.c deleted file mode 100644 index 5d1c4ee5..00000000 --- a/src/stdlib/strtoull.c +++ /dev/null @@ -1,17 +0,0 @@ -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> - -unsigned long long strtoull(const char *s, char **p, int base) -{ - intmax_t x; - if (sizeof(intmax_t) == sizeof(long long)) - return strtoumax(s, p, base); - x = strtoimax(s, p, base); - if (-x > ULLONG_MAX || x > ULLONG_MAX) { - errno = ERANGE; - return ULLONG_MAX; - } - return x; -} diff --git a/src/stdlib/strtoumax.c b/src/stdlib/strtoumax.c deleted file mode 100644 index a299dc04..00000000 --- a/src/stdlib/strtoumax.c +++ /dev/null @@ -1,34 +0,0 @@ -#include <inttypes.h> -#include <stdlib.h> -#include <errno.h> -#include <ctype.h> -#include "intparse.h" - -uintmax_t strtoumax(const char *s1, char **p, int base) -{ - const unsigned char *s = (void *)s1; - struct intparse ip = {0}; - - if (p) *p = (char *)s1; - - if (base && base-2U > 34) { - errno = EINVAL; - return 0; - } - - for (; isspace(*s); s++); - - ip.base = base; - __intparse(&ip, s, SIZE_MAX); - - if (p && ip.err != EINVAL) - *p = (char *)s + ip.cnt; - - if (ip.err) { - errno = ip.err; - if (ip.err == EINVAL) return 0; - return UINTMAX_MAX; - } - - return ip.neg ? -ip.val : ip.val; -} diff --git a/src/stdlib/wcstoimax.c b/src/stdlib/wcstoimax.c deleted file mode 100644 index 344fe3a3..00000000 --- a/src/stdlib/wcstoimax.c +++ /dev/null @@ -1,39 +0,0 @@ -#include <wchar.h> -#include <wctype.h> -#include <inttypes.h> -#include <errno.h> -#include "intparse.h" - -intmax_t wcstoimax(const wchar_t *s, wchar_t **p, int base) -{ - const wchar_t *s1 = s; - struct intparse ip = {0}; - - if (p) *p = (wchar_t *)s; - - if (base && base-2U > 34) { - errno = EINVAL; - return 0; - } - - for (; iswspace(*s); s++); - - ip.base = base; - for (; __intparse(&ip, (char[]){(*s&-(*s<128U))}, 1); s++); - - if (p && ip.err != EINVAL) - *p = (wchar_t *)s1 + ip.cnt; - - if (ip.err) { - errno = ip.err; - if (ip.err == EINVAL) return 0; - return ip.neg ? INTMAX_MIN : INTMAX_MAX; - } - - if (ip.val > INTMAX_MAX) { - if (!ip.neg || -ip.val != INTMAX_MIN) - errno = ERANGE; - return ip.neg ? INTMAX_MIN : INTMAX_MAX; - } - return ip.neg ? -ip.val : ip.val; -} diff --git a/src/stdlib/wcstol.c b/src/stdlib/wcstol.c index aad62e5b..cbdd8067 100644 --- a/src/stdlib/wcstol.c +++ b/src/stdlib/wcstol.c @@ -1,18 +1,75 @@ -#include <wchar.h> -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> +#include "stdio_impl.h" +#include "intscan.h" +#include "shgetc.h" -long wcstol(const wchar_t *s, wchar_t **p, int base) +/* This read function heavily cheats. It knows: + * (1) len will always be 1 + * (2) non-ascii characters don't matter */ + +static size_t do_read(FILE *f, unsigned char *buf, size_t len) +{ + size_t i; + const wchar_t *wcs = f->cookie; + + for (i=0; i<f->buf_size && wcs[i]; i++) + f->buf[i] = wcs[i] < 128 ? wcs[i] : '@'; + f->rpos = f->buf; + f->rend = f->buf + i; + f->cookie = (void *)(wcs+i); + + if (i && len) { + *buf = *f->rpos++; + return 1; + } + return 0; +} + +static unsigned long long wcstox(const wchar_t *s, wchar_t **p, int base, unsigned long long lim) { - intmax_t x = wcstoimax(s, p, base); - if (x > LONG_MAX) { - errno = ERANGE; - return LONG_MAX; - } else if (x < LONG_MIN) { - errno = ERANGE; - return LONG_MIN; + unsigned char buf[64]; + FILE f = {0}; + f.flags = 0; + f.rpos = f.rend = 0; + f.buf = buf; + f.buf_size = sizeof buf; + f.lock = -1; + f.read = do_read; + f.cookie = (void *)s; + shlim(&f, 0); + unsigned long long y = __intscan(&f, base, 1, lim); + if (p) { + size_t cnt = shcnt(&f); + *p = (wchar_t *)s + cnt; } - return x; + return y; +} + +unsigned long long wcstoull(const wchar_t *s, wchar_t **p, int base) +{ + return wcstox(s, p, base, ULLONG_MAX); +} + +long long wcstoll(const wchar_t *s, wchar_t **p, int base) +{ + return wcstox(s, p, base, LLONG_MIN); +} + +unsigned long wcstoul(const wchar_t *s, wchar_t **p, int base) +{ + return wcstox(s, p, base, ULONG_MAX); +} + +long wcstol(const wchar_t *s, wchar_t **p, int base) +{ + return wcstox(s, p, base, 0UL+LONG_MIN); +} + +intmax_t wcstoimax(const wchar_t *s, wchar_t **p, int base) +{ + return wcstoll(s, p, base); +} + +uintmax_t wcstoumax(const wchar_t *s, wchar_t **p, int base) +{ + return wcstoull(s, p, base); } diff --git a/src/stdlib/wcstoll.c b/src/stdlib/wcstoll.c deleted file mode 100644 index ddfea74b..00000000 --- a/src/stdlib/wcstoll.c +++ /dev/null @@ -1,18 +0,0 @@ -#include <wchar.h> -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> - -long long wcstoll(const wchar_t *s, wchar_t **p, int base) -{ - intmax_t x = wcstoimax(s, p, base); - if (x > LLONG_MAX) { - errno = ERANGE; - return LLONG_MAX; - } else if (x < LLONG_MIN) { - errno = ERANGE; - return LLONG_MIN; - } - return x; -} diff --git a/src/stdlib/wcstoul.c b/src/stdlib/wcstoul.c deleted file mode 100644 index 9cbec1a8..00000000 --- a/src/stdlib/wcstoul.c +++ /dev/null @@ -1,18 +0,0 @@ -#include <wchar.h> -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> - -unsigned long wcstoul(const wchar_t *s, wchar_t **p, int base) -{ - intmax_t x; - if (sizeof(intmax_t) == sizeof(long)) - return wcstoumax(s, p, base); - x = wcstoimax(s, p, base); - if (-x > ULONG_MAX || x > ULONG_MAX) { - errno = ERANGE; - return ULONG_MAX; - } - return x; -} diff --git a/src/stdlib/wcstoull.c b/src/stdlib/wcstoull.c deleted file mode 100644 index 48c557db..00000000 --- a/src/stdlib/wcstoull.c +++ /dev/null @@ -1,18 +0,0 @@ -#include <wchar.h> -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include <limits.h> - -unsigned long long wcstoull(const wchar_t *s, wchar_t **p, int base) -{ - intmax_t x; - if (sizeof(intmax_t) == sizeof(long long)) - return wcstoumax(s, p, base); - x = wcstoimax(s, p, base); - if (-x > ULLONG_MAX || x > ULLONG_MAX) { - errno = ERANGE; - return ULLONG_MAX; - } - return x; -} diff --git a/src/stdlib/wcstoumax.c b/src/stdlib/wcstoumax.c deleted file mode 100644 index cee5ff7f..00000000 --- a/src/stdlib/wcstoumax.c +++ /dev/null @@ -1,35 +0,0 @@ -#include <wchar.h> -#include <wctype.h> -#include <stdlib.h> -#include <inttypes.h> -#include <errno.h> -#include "intparse.h" - -uintmax_t wcstoumax(const wchar_t *s, wchar_t **p, int base) -{ - const wchar_t *s1 = s; - struct intparse ip = {0}; - - if (p) *p = (wchar_t *)s; - - if (base && base-2U > 34) { - errno = EINVAL; - return 0; - } - - for (; iswspace(*s); s++); - - ip.base = base; - for (; __intparse(&ip, (char[]){(*s&-(*s<128U))}, 1); s++); - - if (p && ip.err != EINVAL) - *p = (wchar_t *)s1 + ip.cnt; - - if (ip.err) { - errno = ip.err; - if (ip.err == EINVAL) return 0; - return UINTMAX_MAX; - } - - return ip.neg ? -ip.val : ip.val; -} |