4 #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
8 /* strtol and strtoul, renamed to avoid conflicts */
16 /* Static overflow check values for bases 2 through 36.
17 * smallmax[base] is the largest unsigned long i such that
18 * i * base doesn't overflow unsigned long.
20 static unsigned long smallmax
[] = {
21 0, /* bases 0 and 1 are invalid */
60 /* maximum digits that can't ever overflow for bases 2 through 36,
61 * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)].
62 * Note that this is pessimistic if sizeof(long) > 4.
65 static int digitlimit
[] = {
66 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
67 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
68 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
69 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
70 #elif SIZEOF_LONG == 8
71 /* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
72 static int digitlimit
[] = {
73 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
74 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
75 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
76 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
78 #error "Need table for SIZEOF_LONG"
83 ** This is a general purpose routine for converting
84 ** an ascii string to an integer in an arbitrary base.
85 ** Leading white space is ignored. If 'base' is zero
86 ** it looks for a leading 0b, 0o or 0x to tell which
87 ** base. If these are absent it defaults to 10.
88 ** Base must be 0 or between 2 and 36 (inclusive).
89 ** If 'ptr' is non-NULL it will contain a pointer to
90 ** the end of the scan.
91 ** Errors due to bad pointers will probably result in
92 ** exceptions - we don't check for them.
95 PyOS_strtoul(register char *str
, char **ptr
, int base
)
97 register unsigned long result
= 0; /* return value of the function */
98 register int c
; /* current input character */
99 register int ovlimit
; /* required digits to overflow */
101 /* skip leading white space */
102 while (*str
&& isspace(Py_CHARMASK(*str
)))
105 /* check for leading 0b, 0o or 0x for auto-base or base 16 */
107 case 0: /* look for leading 0b, 0o or 0x */
110 if (*str
== 'x' || *str
== 'X') {
111 /* there must be at least one digit after 0x */
112 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 16) {
119 } else if (*str
== 'o' || *str
== 'O') {
120 /* there must be at least one digit after 0o */
121 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 8) {
128 } else if (*str
== 'b' || *str
== 'B') {
129 /* there must be at least one digit after 0b */
130 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 2) {
138 /* skip all zeroes... */
141 while (isspace(Py_CHARMASK(*str
)))
152 /* even with explicit base, skip leading 0? prefix */
156 if (*str
== 'x' || *str
== 'X') {
157 /* there must be at least one digit after 0x */
158 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 16) {
170 if (*str
== 'o' || *str
== 'O') {
171 /* there must be at least one digit after 0o */
172 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 8) {
184 if (*str
== 'b' || *str
== 'B') {
185 /* there must be at least one digit after 0b */
186 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 2) {
197 /* catch silly bases */
198 if (base
< 2 || base
> 36) {
204 /* skip leading zeroes */
208 /* base is guaranteed to be in [2, 36] at this point */
209 ovlimit
= digitlimit
[base
];
211 /* do the conversion until non-digit character encountered */
212 while ((c
= _PyLong_DigitValue
[Py_CHARMASK(*str
)]) < base
) {
213 if (ovlimit
> 0) /* no overflow check required */
214 result
= result
* base
+ c
;
215 else { /* requires overflow check */
216 register unsigned long temp_result
;
218 if (ovlimit
< 0) /* guaranteed overflow */
221 /* there could be an overflow */
222 /* check overflow just from shifting */
223 if (result
> smallmax
[base
])
228 /* check overflow from the digit's value */
229 temp_result
= result
+ c
;
230 if (temp_result
< result
)
233 result
= temp_result
;
240 /* set pointer to point to the last character scanned */
248 /* spool through remaining digit characters */
249 while (_PyLong_DigitValue
[Py_CHARMASK(*str
)] < base
)
254 return (unsigned long)-1;
257 /* Checking for overflow in PyOS_strtol is a PITA; see comments
258 * about PY_ABS_LONG_MIN in longobject.c.
260 #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
263 PyOS_strtol(char *str
, char **ptr
, int base
)
266 unsigned long uresult
;
269 while (*str
&& isspace(Py_CHARMASK(*str
)))
273 if (sign
== '+' || sign
== '-')
276 uresult
= PyOS_strtoul(str
, ptr
, base
);
278 if (uresult
<= (unsigned long)LONG_MAX
) {
279 result
= (long)uresult
;
283 else if (sign
== '-' && uresult
== PY_ABS_LONG_MIN
) {