1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
3 #include <msvcrt/ctype.h>
4 #include <msvcrt/errno.h>
5 #include <msvcrt/stdlib.h>
8 * Convert a unicode string to an unsigned long integer.
10 * Ignores `locale' stuff. Assumes that the upper and lower case
11 * alphabets and digits are each contiguous.
14 wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
16 const wchar_t *s = nptr;
20 int neg = 0, any, cutlim;
23 * See strtol for comments as to the logic used.
27 } while (iswspace(c));
35 if ((base == 0 || base == 16) &&
36 c == L'0' && (*s == L'x' || *s == L'X'))
43 base = c == L'0' ? 8 : 10;
44 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
45 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
46 for (acc = 0, any = 0;; c = *s++)
51 c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
56 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
72 *endptr = any ? (wchar_t *)s - 1 : (wchar_t *)nptr;
77 unsigned long wcstoul(const wchar_t *cp,wchar_t **endp,int base)
79 unsigned long result = 0,value;
86 if ((*cp == L'x') && iswxdigit(cp[1])) {
92 while (iswxdigit(*cp) && (value = iswdigit(*cp) ? *cp-L'0' : (iswlower(*cp)
93 ? towupper(*cp) : *cp)-L'A'+10) < base) {
94 result = result*base + value;
98 *endp = (wchar_t *)cp;