use the new integer parser (FILE/shgetc based) for strtol, wcstol, etc.

This commit is contained in:
Rich Felker
2012-04-16 16:55:24 -04:00
parent 18efeb320b
commit 96e9773eb7
14 changed files with 120 additions and 405 deletions

View File

@ -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;
}

View File

@ -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"
static unsigned long long strtox(const char *s, char **p, int base, unsigned long long lim)
{
/* 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 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)
{
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;
}
return x;
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);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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"
/* 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)
{
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 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)
{
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;
}
return x;
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);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}