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.
16 wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
18 const wchar_t *s = nptr;
22 int neg = 0, any, cutlim;
25 * See strtol for comments as to the logic used.
29 } while (iswspace(c));
37 if ((base == 0 || base == 16) &&
38 c == L'0' && (*s == L'x' || *s == L'X'))
45 base = c == L'0' ? 8 : 10;
46 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
47 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
48 for (acc = 0, any = 0;; c = *s++)
53 c -= iswupper(c) ? L'A' - 10 : L'a' - 10;
58 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
74 *endptr = any ? (wchar_t *)s - 1 : (wchar_t *)nptr;
79 unsigned long wcstoul(const wchar_t *cp,wchar_t **endp,int base)
81 unsigned long result = 0,value;
88 if ((*cp == L'x') && iswxdigit(cp[1])) {
94 while (iswxdigit(*cp) && (value = iswdigit(*cp) ? *cp-L'0' : (iswlower(*cp)
95 ? towupper(*cp) : *cp)-L'A'+10) < base) {
96 result = result*base + value;
100 *endp = (wchar_t *)cp;