4 * Source code for the "strtoul" library procedure.
6 * Copyright (c) 1988 The Regents of the University of California.
7 * Copyright (c) 1994 Sun Microsystems, Inc.
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
18 * The table below is used to convert from ASCII digits to a
19 * numerical equivalent. It maps from '0' through 'z' to integers
20 * (100 for non-digit characters).
23 static char cvtIn[] = {
24 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* '0' - '9' */
25 100, 100, 100, 100, 100, 100, 100, /* punctuation */
26 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'A' - 'Z' */
27 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
28 30, 31, 32, 33, 34, 35,
29 100, 100, 100, 100, 100, 100, /* punctuation */
30 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'a' - 'z' */
31 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
32 30, 31, 32, 33, 34, 35};
35 *----------------------------------------------------------------------
39 * Convert an ASCII string into an integer.
42 * The return value is the integer equivalent of string. If endPtr
43 * is non-NULL, then *endPtr is filled in with the character
44 * after the last one that was part of the integer. If string
45 * doesn't contain a valid integer value, then zero is returned
46 * and *endPtr is set to string.
51 *----------------------------------------------------------------------
55 strtoul(string, endPtr, base)
56 char *string; /* String of ASCII digits, possibly
57 * preceded by white space. For bases
58 * greater than 10, either lower- or
59 * upper-case digits may be used.
61 char **endPtr; /* Where to store address of terminating
62 * character, or NULL. */
63 int base; /* Base for conversion. Must be less
64 * than 37. If 0, then the base is chosen
65 * from the leading characters of string:
66 * "0x" means hex, "0" means octal, anything
71 register unsigned long int result = 0;
72 register unsigned digit;
76 * Skip any leading blanks.
85 * If no base was provided, pick one from the leading characters
99 * Must set anyDigits here, otherwise "0" produces a
108 } else if (base == 16) {
111 * Skip a leading "0x" from hex numbers.
114 if ((p[0] == '0') && (p[1] == 'x')) {
120 * Sorry this code is so messy, but speed seems important. Do
121 * different things for base 8, 10, 16, and other.
130 result = (result << 3) + digit;
133 } else if (base == 10) {
139 result = (10*result) + digit;
142 } else if (base == 16) {
145 if (digit > ('z' - '0')) {
148 digit = cvtIn[digit];
152 result = (result << 4) + digit;
158 if (digit > ('z' - '0')) {
161 digit = cvtIn[digit];
165 result = result*base + digit;
171 * See if there were any digits at all.