fix various bugs in new integer parser framework
[musl] / src / stdlib / wcstoumax.c
1 #include <wchar.h>
2 #include <wctype.h>
3 #include <stdlib.h>
4 #include <inttypes.h>
5 #include <errno.h>
6 #include "intparse.h"
7
8 uintmax_t wcstoumax(const wchar_t *s, wchar_t **p, int base)
9 {
10         const wchar_t *s1 = s;
11         struct intparse ip = {0};
12
13         if (p) *p = (wchar_t *)s;
14
15         if (base && base-2U > 34) {
16                 errno = EINVAL;
17                 return 0;
18         }
19
20         for (; iswspace(*s); s++);
21
22         ip.base = base;
23         for (; __intparse(&ip, (char[]){(*s&-(*s<128U))}, 1); s++);
24
25         if (p && ip.err != EINVAL)
26                 *p = (wchar_t *)s1 + ip.cnt;
27
28         if (ip.err) {
29                 errno = ip.err;
30                 if (ip.err == EINVAL) return 0;
31                 return UINTMAX_MAX;
32         }
33
34         return ip.neg ? -ip.val : ip.val;
35 }