4 #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
8 /* 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)
13 #define Py_CHARMASK(c) ((c) & 0xff)
16 /* strtol and strtoul, renamed to avoid conflicts */
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.
28 static unsigned long smallmax
[] = {
29 0, /* bases 0 and 1 are invalid */
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.
73 static 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 */
78 #elif SIZEOF_LONG == 8
79 /* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
80 static 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 */
86 #error "Need table for SIZEOF_LONG"
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
94 ** it looks for a leading 0, 0x or 0X to tell which
95 ** 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.
103 PyOS_strtoul(register char *str
, char **ptr
, int base
)
105 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 */
109 /* skip leading white space */
110 while (*str
&& isspace(Py_CHARMASK(*str
)))
113 /* check for leading 0 or 0x for auto-base or base 16 */
115 case 0: /* look for leading 0, 0x or 0X */
118 if (*str
== 'x' || *str
== 'X') {
119 /* there must be at least one digit after 0x */
120 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 16) {
135 case 16: /* skip leading 0x or 0X */
138 if (*str
== 'x' || *str
== 'X') {
139 /* there must be at least one digit after 0x */
140 if (_PyLong_DigitValue
[Py_CHARMASK(str
[1])] >= 16) {
151 /* catch silly bases */
152 if (base
< 2 || base
> 36) {
158 /* skip leading zeroes */
162 /* base is guaranteed to be in [2, 36] at this point */
163 ovlimit
= digitlimit
[base
];
165 /* do the conversion until non-digit character encountered */
166 while ((c
= _PyLong_DigitValue
[Py_CHARMASK(*str
)]) < base
) {
167 if (ovlimit
> 0) /* no overflow check required */
168 result
= result
* base
+ c
;
169 else { /* requires overflow check */
170 register unsigned long temp_result
;
172 if (ovlimit
< 0) /* guaranteed overflow */
175 /* there could be an overflow */
176 /* check overflow just from shifting */
177 if (result
> smallmax
[base
])
182 /* check overflow from the digit's value */
183 temp_result
= result
+ c
;
184 if (temp_result
< result
)
187 result
= temp_result
;
194 /* set pointer to point to the last character scanned */
202 /* spool through remaining digit characters */
203 while (_PyLong_DigitValue
[Py_CHARMASK(*str
)] < base
)
208 return (unsigned long)-1;
211 /* Checking for overflow in PyOS_strtol is a PITA; see comments
212 * about PY_ABS_LONG_MIN in longobject.c.
214 #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
217 PyOS_strtol(char *str
, char **ptr
, int base
)
220 unsigned long uresult
;
223 while (*str
&& isspace(Py_CHARMASK(*str
)))
227 if (sign
== '+' || sign
== '-')
230 uresult
= PyOS_strtoul(str
, ptr
, base
);
232 if (uresult
<= (unsigned long)LONG_MAX
) {
233 result
= (long)uresult
;
237 else if (sign
== '-' && uresult
== PY_ABS_LONG_MIN
) {