Share more duplicated functions
[reactos.git] / reactos / lib / string / strtoul.c
1 #include <string.h>
2 #include <limits.h>
3 #include <ctype.h>
4
5 /*
6 * Convert a string to an unsigned long integer.
7 *
8 * Ignores `locale' stuff. Assumes that the upper and lower case
9 * alphabets and digits are each contiguous.
10 *
11 * @implemented
12 */
13 unsigned long
14 strtoul(const char *nptr, char **endptr, int base)
15 {
16 const char *s = nptr;
17 unsigned long acc;
18 int c;
19 unsigned long cutoff;
20 int neg = 0, any, cutlim;
21
22 /*
23 * See strtol for comments as to the logic used.
24 */
25 do {
26 c = *s++;
27 } while (isspace(c));
28 if (c == '-')
29 {
30 neg = 1;
31 c = *s++;
32 }
33 else if (c == '+')
34 c = *s++;
35 if ((base == 0 || base == 16) &&
36 c == '0' && (*s == 'x' || *s == 'X'))
37 {
38 c = s[1];
39 s += 2;
40 base = 16;
41 }
42 if (base == 0)
43 base = c == '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++)
47 {
48 if (isdigit(c))
49 c -= '0';
50 else if (isalpha(c))
51 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
52 else
53 break;
54 if (c >= base)
55 break;
56 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
57 any = -1;
58 else {
59 any = 1;
60 acc *= base;
61 acc += c;
62 }
63 }
64 if (any < 0)
65 {
66 acc = ULONG_MAX;
67 }
68 else if (neg)
69 acc = -acc;
70 if (endptr != 0)
71 *endptr = any ? (char *)s - 1 : (char *)nptr;
72 return acc;
73 }