]>
git.zerfleddert.de Git - micropolis/blob - src/tcl/compat/strtoul.c
4 * Source code for the "strtoul" library procedure.
6 * Copyright 1988 Regents of the University of California
7 * Permission to use, copy, modify, and distribute this
8 * software and its documentation for any purpose and without
9 * fee is hereby granted, provided that the above copyright
10 * notice appear in all copies. The University of California
11 * makes no representations about the suitability of this
12 * software for any purpose. It is provided "as is" without
13 * express or implied warranty.
17 static char rcsid
[] = "$Header: /sprite/src/lib/tcl/compat/RCS/strtoul.c,v 1.2 91/09/22 14:04:43 ouster Exp $ SPRITE (Berkeley)";
23 * The table below is used to convert from ASCII digits to a
24 * numerical equivalent. It maps from '0' through 'z' to integers
25 * (100 for non-digit characters).
28 static char cvtIn
[] = {
29 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* '0' - '9' */
30 100, 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,
34 100, 100, 100, 100, 100, 100, /* punctuation */
35 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'a' - 'z' */
36 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
37 30, 31, 32, 33, 34, 35};
40 *----------------------------------------------------------------------
44 * Convert an ASCII string into an integer.
47 * The return value is the integer equivalent of string. If endPtr
48 * is non-NULL, then *endPtr is filled in with the character
49 * after the last one that was part of the integer. If string
50 * doesn't contain a valid integer value, then zero is returned
51 * and *endPtr is set to string.
56 *----------------------------------------------------------------------
60 strtoul(string
, endPtr
, base
)
61 char *string
; /* String of ASCII digits, possibly
62 * preceded by white space. For bases
63 * greater than 10, either lower- or
64 * upper-case digits may be used.
66 char **endPtr
; /* Where to store address of terminating
67 * character, or NULL. */
68 int base
; /* Base for conversion. Must be less
69 * than 37. If 0, then the base is chosen
70 * from the leading characters of string:
71 * "0x" means hex, "0" means octal, anything
76 register unsigned long int result
= 0;
77 register unsigned digit
;
81 * Skip any leading blanks.
90 * If no base was provided, pick one from the leading characters
104 * Must set anyDigits here, otherwise "0" produces a
113 } else if (base
== 16) {
116 * Skip a leading "0x" from hex numbers.
119 if ((p
[0] == '0') && (p
[1] == 'x')) {
125 * Sorry this code is so messy, but speed seems important. Do
126 * different things for base 8, 10, 16, and other.
135 result
= (result
<< 3) + digit
;
138 } else if (base
== 10) {
144 result
= (10*result
) + digit
;
147 } else if (base
== 16) {
150 if (digit
> ('z' - '0')) {
153 digit
= cvtIn
[digit
];
157 result
= (result
<< 4) + digit
;
163 if (digit
> ('z' - '0')) {
166 digit
= cvtIn
[digit
];
170 result
= result
*base
+ digit
;
176 * See if there were any digits at all.