OLD | NEW |
1 #include "stdio_impl.h" | 1 #include "stdio_impl.h" |
2 #include "intscan.h" | 2 #include "intscan.h" |
3 #include "shgetc.h" | 3 #include "shgetc.h" |
4 #include <inttypes.h> | 4 #include <inttypes.h> |
5 #include <limits.h> | 5 #include <limits.h> |
6 #include <wctype.h> | 6 #include <wctype.h> |
7 #include <wchar.h> | 7 #include <wchar.h> |
8 | 8 |
9 /* This read function heavily cheats. It knows: | 9 /* This read function heavily cheats. It knows: |
10 * (1) len will always be 1 | 10 * (1) len will always be 1 |
11 * (2) non-ascii characters don't matter */ | 11 * (2) non-ascii characters don't matter */ |
12 | 12 |
13 static size_t do_read(FILE *f, unsigned char *buf, size_t len) | 13 static size_t do_read(FILE* f, unsigned char* buf, size_t len) { |
14 { | 14 size_t i; |
15 » size_t i; | 15 const wchar_t* wcs = f->cookie; |
16 » const wchar_t *wcs = f->cookie; | |
17 | 16 |
18 » if (!wcs[0]) wcs=L"@"; | 17 if (!wcs[0]) |
19 » for (i=0; i<f->buf_size && wcs[i]; i++) | 18 wcs = L"@"; |
20 » » f->buf[i] = wcs[i] < 128 ? wcs[i] : '@'; | 19 for (i = 0; i < f->buf_size && wcs[i]; i++) |
21 » f->rpos = f->buf; | 20 f->buf[i] = wcs[i] < 128 ? wcs[i] : '@'; |
22 » f->rend = f->buf + i; | 21 f->rpos = f->buf; |
23 » f->cookie = (void *)(wcs+i); | 22 f->rend = f->buf + i; |
| 23 f->cookie = (void*)(wcs + i); |
24 | 24 |
25 » if (i && len) { | 25 if (i && len) { |
26 » » *buf = *f->rpos++; | 26 *buf = *f->rpos++; |
27 » » return 1; | 27 return 1; |
28 » } | 28 } |
29 » return 0; | 29 return 0; |
30 } | 30 } |
31 | 31 |
32 static unsigned long long wcstox(const wchar_t *s, wchar_t **p, int base, unsign
ed long long lim) | 32 static unsigned long long wcstox(const wchar_t* s, |
33 { | 33 wchar_t** p, |
34 » wchar_t *t = (wchar_t *)s; | 34 int base, |
35 » unsigned char buf[64]; | 35 unsigned long long lim) { |
36 » FILE f = {0}; | 36 wchar_t* t = (wchar_t*)s; |
37 » f.flags = 0; | 37 unsigned char buf[64]; |
38 » f.rpos = f.rend = 0; | 38 FILE f = {0}; |
39 » f.buf = buf + 4; | 39 f.flags = 0; |
40 » f.buf_size = sizeof buf - 4; | 40 f.rpos = f.rend = 0; |
41 » f.lock = -1; | 41 f.buf = buf + 4; |
42 » f.read = do_read; | 42 f.buf_size = sizeof buf - 4; |
43 » while (iswspace(*t)) t++; | 43 f.lock = -1; |
44 » f.cookie = (void *)t; | 44 f.read = do_read; |
45 » shlim(&f, 0); | 45 while (iswspace(*t)) |
46 » unsigned long long y = __intscan(&f, base, 1, lim); | 46 t++; |
47 » if (p) { | 47 f.cookie = (void*)t; |
48 » » size_t cnt = shcnt(&f); | 48 shlim(&f, 0); |
49 » » *p = cnt ? t + cnt : (wchar_t *)s; | 49 unsigned long long y = __intscan(&f, base, 1, lim); |
50 » } | 50 if (p) { |
51 » return y; | 51 size_t cnt = shcnt(&f); |
| 52 *p = cnt ? t + cnt : (wchar_t*)s; |
| 53 } |
| 54 return y; |
52 } | 55 } |
53 | 56 |
54 unsigned long long wcstoull(const wchar_t *restrict s, wchar_t **restrict p, int
base) | 57 unsigned long long wcstoull(const wchar_t* restrict s, |
55 { | 58 wchar_t** restrict p, |
56 » return wcstox(s, p, base, ULLONG_MAX); | 59 int base) { |
| 60 return wcstox(s, p, base, ULLONG_MAX); |
57 } | 61 } |
58 | 62 |
59 long long wcstoll(const wchar_t *restrict s, wchar_t **restrict p, int base) | 63 long long wcstoll(const wchar_t* restrict s, wchar_t** restrict p, int base) { |
60 { | 64 return wcstox(s, p, base, LLONG_MIN); |
61 » return wcstox(s, p, base, LLONG_MIN); | |
62 } | 65 } |
63 | 66 |
64 unsigned long wcstoul(const wchar_t *restrict s, wchar_t **restrict p, int base) | 67 unsigned long wcstoul(const wchar_t* restrict s, |
65 { | 68 wchar_t** restrict p, |
66 » return wcstox(s, p, base, ULONG_MAX); | 69 int base) { |
| 70 return wcstox(s, p, base, ULONG_MAX); |
67 } | 71 } |
68 | 72 |
69 long wcstol(const wchar_t *restrict s, wchar_t **restrict p, int base) | 73 long wcstol(const wchar_t* restrict s, wchar_t** restrict p, int base) { |
70 { | 74 return wcstox(s, p, base, 0UL + LONG_MIN); |
71 » return wcstox(s, p, base, 0UL+LONG_MIN); | |
72 } | 75 } |
73 | 76 |
74 intmax_t wcstoimax(const wchar_t *restrict s, wchar_t **restrict p, int base) | 77 intmax_t wcstoimax(const wchar_t* restrict s, wchar_t** restrict p, int base) { |
75 { | 78 return wcstoll(s, p, base); |
76 » return wcstoll(s, p, base); | |
77 } | 79 } |
78 | 80 |
79 uintmax_t wcstoumax(const wchar_t *restrict s, wchar_t **restrict p, int base) | 81 uintmax_t wcstoumax(const wchar_t* restrict s, wchar_t** restrict p, int base) { |
80 { | 82 return wcstoull(s, p, base); |
81 » return wcstoull(s, p, base); | |
82 } | 83 } |
OLD | NEW |