os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/compat/strtoul.c
First public contribution.
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.
12 * RCS: @(#) $Id: strtoul.c,v 1.5 2002/02/25 10:36:32 dkf Exp $
19 * The table below is used to convert from ASCII digits to a
20 * numerical equivalent. It maps from '0' through 'z' to integers
21 * (100 for non-digit characters).
24 static char cvtIn[] = {
25 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* '0' - '9' */
26 100, 100, 100, 100, 100, 100, 100, /* punctuation */
27 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'A' - 'Z' */
28 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
29 30, 31, 32, 33, 34, 35,
30 100, 100, 100, 100, 100, 100, /* punctuation */
31 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'a' - 'z' */
32 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
33 30, 31, 32, 33, 34, 35};
36 *----------------------------------------------------------------------
40 * Convert an ASCII string into an integer.
43 * The return value is the integer equivalent of string. If endPtr
44 * is non-NULL, then *endPtr is filled in with the character
45 * after the last one that was part of the integer. If string
46 * doesn't contain a valid integer value, then zero is returned
47 * and *endPtr is set to string.
52 *----------------------------------------------------------------------
56 strtoul(string, endPtr, base)
57 CONST char *string; /* String of ASCII digits, possibly
58 * preceded by white space. For bases
59 * greater than 10, either lower- or
60 * upper-case digits may be used.
62 char **endPtr; /* Where to store address of terminating
63 * character, or NULL. */
64 int base; /* Base for conversion. Must be less
65 * than 37. If 0, then the base is chosen
66 * from the leading characters of string:
67 * "0x" means hex, "0" means octal, anything
71 register CONST char *p;
72 register unsigned long int result = 0;
73 register unsigned digit;
79 * Skip any leading blanks.
83 while (isspace(UCHAR(*p))) {
96 * If no base was provided, pick one from the leading characters
104 if ((*p == 'x') || (*p == 'X')) {
110 * Must set anyDigits here, otherwise "0" produces a
119 } else if (base == 16) {
122 * Skip a leading "0x" from hex numbers.
125 if ((p[0] == '0') && ((p[1] == 'x') || (p[1] == 'X'))) {
131 * Sorry this code is so messy, but speed seems important. Do
132 * different things for base 8, 10, 16, and other.
136 unsigned long maxres = ULONG_MAX >> 3;
142 if (result > maxres) { overflow = 1; }
143 result = (result << 3);
144 if (digit > (ULONG_MAX - result)) { overflow = 1; }
148 } else if (base == 10) {
149 unsigned long maxres = ULONG_MAX / 10;
155 if (result > maxres) { overflow = 1; }
157 if (digit > (ULONG_MAX - result)) { overflow = 1; }
161 } else if (base == 16) {
162 unsigned long maxres = ULONG_MAX >> 4;
165 if (digit > ('z' - '0')) {
168 digit = cvtIn[digit];
172 if (result > maxres) { overflow = 1; }
173 result = (result << 4);
174 if (digit > (ULONG_MAX - result)) { overflow = 1; }
178 } else if ( base >= 2 && base <= 36 ) {
179 unsigned long maxres = ULONG_MAX / base;
182 if (digit > ('z' - '0')) {
185 digit = cvtIn[digit];
186 if (digit >= ( (unsigned) base )) {
189 if (result > maxres) { overflow = 1; }
191 if (digit > (ULONG_MAX - result)) { overflow = 1; }
198 * See if there were any digits at all.
206 /* unsafe, but required by the strtoul prototype */
207 *endPtr = (char *) p;