blob: cf23051a43155a4aefc8298a6b59bed33efc1c5c [file] [log] [blame]
Guido van Rossumbe0e9421993-12-24 10:32:00 +00001
Guido van Rossum1924a061998-12-18 22:02:37 +00002#include "Python.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +00003
Guido van Rossume32d1531998-07-07 21:32:53 +00004#if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
5#define _SGI_MP_SOURCE
6#endif
7
Guido van Rossum7f7f2741995-02-10 17:01:56 +00008/* Convert a possibly signed character to a nonnegative int */
9/* XXX This assumes characters are 8 bits wide */
10#ifdef __CHAR_UNSIGNED__
11#define Py_CHARMASK(c) (c)
12#else
13#define Py_CHARMASK(c) ((c) & 0xff)
14#endif
15
Guido van Rossumb6775db1994-08-01 11:34:53 +000016/* strtol and strtoul, renamed to avoid conflicts */
17
Thomas Wouters477c8d52006-05-27 19:21:47 +000018
19#include <ctype.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020#ifdef HAVE_ERRNO_H
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#include <errno.h>
22#endif
23
24/* Static overflow check values for bases 2 through 36.
25 * smallmax[base] is the largest unsigned long i such that
26 * i * base doesn't overflow unsigned long.
27 */
28static unsigned long smallmax[] = {
29 0, /* bases 0 and 1 are invalid */
30 0,
31 ULONG_MAX / 2,
32 ULONG_MAX / 3,
33 ULONG_MAX / 4,
34 ULONG_MAX / 5,
35 ULONG_MAX / 6,
36 ULONG_MAX / 7,
37 ULONG_MAX / 8,
38 ULONG_MAX / 9,
39 ULONG_MAX / 10,
40 ULONG_MAX / 11,
41 ULONG_MAX / 12,
42 ULONG_MAX / 13,
43 ULONG_MAX / 14,
44 ULONG_MAX / 15,
45 ULONG_MAX / 16,
46 ULONG_MAX / 17,
47 ULONG_MAX / 18,
48 ULONG_MAX / 19,
49 ULONG_MAX / 20,
50 ULONG_MAX / 21,
51 ULONG_MAX / 22,
52 ULONG_MAX / 23,
53 ULONG_MAX / 24,
54 ULONG_MAX / 25,
55 ULONG_MAX / 26,
56 ULONG_MAX / 27,
57 ULONG_MAX / 28,
58 ULONG_MAX / 29,
59 ULONG_MAX / 30,
60 ULONG_MAX / 31,
61 ULONG_MAX / 32,
62 ULONG_MAX / 33,
63 ULONG_MAX / 34,
64 ULONG_MAX / 35,
65 ULONG_MAX / 36,
66};
67
68/* maximum digits that can't ever overflow for bases 2 through 36,
69 * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)].
70 * Note that this is pessimistic if sizeof(long) > 4.
71 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#if SIZEOF_LONG == 4
Thomas Wouters477c8d52006-05-27 19:21:47 +000073static int digitlimit[] = {
74 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
75 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
76 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
77 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#elif SIZEOF_LONG == 8
79/* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
80static int digitlimit[] = {
81 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
82 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
83 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
84 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
85#else
86#error "Need table for SIZEOF_LONG"
87#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000088
Guido van Rossumbe0e9421993-12-24 10:32:00 +000089/*
90** strtoul
91** This is a general purpose routine for converting
92** an ascii string to an integer in an arbitrary base.
93** Leading white space is ignored. If 'base' is zero
Guido van Rossumcd16bf62007-06-13 18:07:49 +000094** it looks for a leading 0b, 0o or 0x to tell which
Guido van Rossumbe0e9421993-12-24 10:32:00 +000095** base. If these are absent it defaults to 10.
96** Base must be 0 or between 2 and 36 (inclusive).
97** If 'ptr' is non-NULL it will contain a pointer to
98** the end of the scan.
99** Errors due to bad pointers will probably result in
100** exceptions - we don't check for them.
101*/
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000102unsigned long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000103PyOS_strtoul(register char *str, char **ptr, int base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000104{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105 register unsigned long result = 0; /* return value of the function */
106 register int c; /* current input character */
107 register int ovlimit; /* required digits to overflow */
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000108
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109 /* skip leading white space */
110 while (*str && isspace(Py_CHARMASK(*str)))
111 ++str;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000112
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000113 /* check for leading 0b, 0o or 0x for auto-base or base 16 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114 switch (base) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000115 case 0: /* look for leading 0b, 0o or 0x */
116 if (*str == '0') {
117 ++str;
118 if (*str == 'x' || *str == 'X') {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119 ++str;
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000120 base = 16;
121 } else if (*str == 'o' || *str == 'O') {
122 ++str;
123 base = 8;
124 } else if (*str == 'b' || *str == 'B') {
125 ++str;
126 base = 2;
127 } else {
128 /* skip all zeroes... */
129 while (*str == '0')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000130 ++str;
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000131 while (isspace(Py_CHARMASK(*str)))
132 ++str;
133 if (ptr)
134 *ptr = str;
135 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000137 }
138 else
139 base = 10;
140 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000142 /* even with explicit base, skip leading 0? prefix */
143 case 16:
144 if (*str == '0') {
145 ++str;
146 if (*str == 'x' || *str == 'X')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147 ++str;
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000148 }
149 break;
150 case 8:
151 if (*str == '0') {
152 ++str;
153 if (*str == 'o' || *str == 'O')
154 ++str;
155 }
156 break;
157 case 2:
158 if(*str == '0') {
159 ++str;
160 if (*str == 'b' || *str == 'B')
161 ++str;
162 }
163 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000164 }
165
166 /* catch silly bases */
167 if (base < 2 || base > 36) {
168 if (ptr)
169 *ptr = str;
170 return 0;
171 }
172
173 /* skip leading zeroes */
174 while (*str == '0')
175 ++str;
176
177 /* base is guaranteed to be in [2, 36] at this point */
178 ovlimit = digitlimit[base];
179
180 /* do the conversion until non-digit character encountered */
181 while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
182 if (ovlimit > 0) /* no overflow check required */
183 result = result * base + c;
184 else { /* requires overflow check */
185 register unsigned long temp_result;
186
187 if (ovlimit < 0) /* guaranteed overflow */
188 goto overflowed;
189
190 /* there could be an overflow */
191 /* check overflow just from shifting */
192 if (result > smallmax[base])
193 goto overflowed;
194
195 result *= base;
196
197 /* check overflow from the digit's value */
198 temp_result = result + c;
199 if (temp_result < result)
200 goto overflowed;
201
202 result = temp_result;
203 }
204
205 ++str;
206 --ovlimit;
207 }
208
209 /* set pointer to point to the last character scanned */
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000210 if (ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000211 *ptr = str;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000212
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213 return result;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000214
Thomas Wouters477c8d52006-05-27 19:21:47 +0000215overflowed:
216 if (ptr) {
217 /* spool through remaining digit characters */
218 while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
219 ++str;
220 *ptr = str;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000221 }
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000222 errno = ERANGE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000223 return (unsigned long)-1;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000224}
225
Thomas Wouters89f507f2006-12-13 04:49:30 +0000226/* Checking for overflow in PyOS_strtol is a PITA; see comments
227 * about PY_ABS_LONG_MIN in longobject.c.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000228 */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000229#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000230
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000231long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232PyOS_strtol(char *str, char **ptr, int base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000233{
234 long result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000235 unsigned long uresult;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000236 char sign;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000237
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000238 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000239 str++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000240
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000241 sign = *str;
242 if (sign == '+' || sign == '-')
243 str++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000244
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245 uresult = PyOS_strtoul(str, ptr, base);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000247 if (uresult <= (unsigned long)LONG_MAX) {
248 result = (long)uresult;
249 if (sign == '-')
250 result = -result;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000251 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252 else if (sign == '-' && uresult == PY_ABS_LONG_MIN) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000253 result = LONG_MIN;
254 }
255 else {
256 errno = ERANGE;
257 result = LONG_MAX;
258 }
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000259 return result;
260}