This commit was manufactured by cvs2svn to create tag 'cnrisync'.
[python/dscho.git] / Python / mystrtoul.c
bloba6462837899b688ca614dfca4e2d6f258dfa3a58
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 /* Convert a possibly signed character to a nonnegative int */
30 /* XXX This assumes characters are 8 bits wide */
31 #ifdef __CHAR_UNSIGNED__
32 #define Py_CHARMASK(c) (c)
33 #else
34 #define Py_CHARMASK(c) ((c) & 0xff)
35 #endif
37 #include "rename2.h"
39 /* strtol and strtoul, renamed to avoid conflicts */
42 ** strtoul
43 ** This is a general purpose routine for converting
44 ** an ascii string to an integer in an arbitrary base.
45 ** Leading white space is ignored. If 'base' is zero
46 ** it looks for a leading 0, 0x or 0X to tell which
47 ** base. If these are absent it defaults to 10.
48 ** Base must be 0 or between 2 and 36 (inclusive).
49 ** If 'ptr' is non-NULL it will contain a pointer to
50 ** the end of the scan.
51 ** Errors due to bad pointers will probably result in
52 ** exceptions - we don't check for them.
55 #include <ctype.h>
56 #include <errno.h>
58 unsigned long
59 mystrtoul(str, ptr, base)
60 register char * str;
61 char ** ptr;
62 int base;
64 register unsigned long result; /* return value of the function */
65 register int c; /* current input character */
66 register unsigned long temp; /* used in overflow testing */
67 int ovf; /* true if overflow occurred */
69 result = 0;
70 ovf = 0;
72 /* catch silly bases */
73 if (base != 0 && (base < 2 || base > 36))
75 if (ptr)
76 *ptr = str;
77 return 0;
80 /* skip leading white space */
81 while (*str && isspace(Py_CHARMASK(*str)))
82 str++;
84 /* check for leading 0 or 0x for auto-base or base 16 */
85 switch (base)
87 case 0: /* look for leading 0, 0x or 0X */
88 if (*str == '0')
90 str++;
91 if (*str == 'x' || *str == 'X')
93 str++;
94 base = 16;
96 else
97 base = 8;
99 else
100 base = 10;
101 break;
103 case 16: /* skip leading 0x or 0X */
104 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
105 str += 2;
106 break;
109 /* do the conversion */
110 while (c = Py_CHARMASK(*str))
112 if (isdigit(c) && c - '0' < base)
113 c -= '0';
114 else
116 if (isupper(c))
117 c = tolower(c);
118 if (c >= 'a' && c <= 'z')
119 c -= 'a' - 10;
120 else /* non-"digit" character */
121 break;
122 if (c >= base) /* non-"digit" character */
123 break;
125 temp = result;
126 result = result * base + c;
127 #ifndef MPW
128 if ((result - c) / base != temp) /* overflow */
129 ovf = 1;
130 #endif
131 str++;
134 /* set pointer to point to the last character scanned */
135 if (ptr)
136 *ptr = str;
137 if (ovf)
139 result = ~0;
140 errno = ERANGE;
142 return result;
145 long
146 mystrtol(str, ptr, base)
147 char * str;
148 char ** ptr;
149 int base;
151 long result;
152 char sign;
154 while (*str && isspace(Py_CHARMASK(*str)))
155 str++;
157 sign = *str;
158 if (sign == '+' || sign == '-')
159 str++;
161 result = (long) mystrtoul(str, ptr, base);
163 /* Signal overflow if the result appears negative,
164 except for the largest negative integer */
165 if (result < 0 && !(sign == '-' && result == -result)) {
166 errno = ERANGE;
167 result = 0x7fffffff;
170 if (sign == '-')
171 result = -result;
173 return result;