unsigned long long __intscan(FILE *f, unsigned base, int pok, unsigned long long lim) { const unsigned char *val = table+1; int c, neg=0; unsigned x; unsigned long long y; if (base > 36) { errno = EINVAL; return 0; } while (isspace((c=shgetc(f)))); if (c=='+' || c=='-') { neg = -(c=='-'); c = shgetc(f); } if ((base == 0 || base == 16) && c=='0') { c = shgetc(f); if ((c|32)=='x') { c = shgetc(f); if (val[c]>=16) { shunget(f); if (pok) shunget(f); else shlim(f, 0); return 0; } base = 16; } else if (base == 0) { base = 8; } } else { if (base == 0) base = 10; if (val[c] >= base) { shunget(f); shlim(f, 0); errno = EINVAL; return 0; } } if (base == 10) { for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f)) x = x*10 + (c-'0'); for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f)) y = y*10 + (c-'0'); if (c-'0'>=10U) goto done; } else if (!(base & base-1)) { int bs = "\0\1\2\4\7\3\6\5"[(0x17*base)>>5&7]; for (x=0; val[c]<base && x<=UINT_MAX/32; c=shgetc(f)) x = x<<bs | val[c]; for (y=x; val[c]<base && y<=ULLONG_MAX>>bs; c=shgetc(f)) y = y<<bs | val[c]; } else {
static long double strtox(const char *s, char **p, int prec) { char *t = (char *)s; while (isspace(*t)) t++; FILE f = { .buf = (void *)t, .rpos = (void *)t, .rend = (void *)-1, .lock = -1 }; shlim(&f, 0); long double y = __floatscan(&f, -1, prec, 1); off_t cnt = shcnt(&f); if (p) *p = cnt ? t + cnt : (char *)s; return y; } float strtof(const char *s, char **p) { return strtox(s, p, 0); } double strtod(const char *s, char **p) { return strtox(s, p, 1); } long double strtold(const char *s, char **p) { return strtox(s, p, 2); }
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; }
static unsigned long long wcstox(const wchar_t *s, wchar_t **p, int base, unsigned long long lim) { wchar_t *t = (wchar_t *)s; unsigned char buf[64]; FILE f = {0}; f.flags = 0; f.rpos = f.rend = 0; f.buf = buf + 4; f.buf_size = sizeof buf - 4; f.lock = -1; f.read = do_read; while (iswspace(*t)) t++; f.cookie = (void *)t; shlim(&f, 0); unsigned long long y = __intscan(&f, base, 1, lim); if (p) { size_t cnt = shcnt(&f); *p = cnt ? t + cnt : (wchar_t *)s; } return y; }