Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 1924a06 | 1998-12-18 22:02:37 +0000 | [diff] [blame] | 2 | #include "Python.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | e32d153 | 1998-07-07 21:32:53 +0000 | [diff] [blame] | 4 | #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE) |
| 5 | #define _SGI_MP_SOURCE |
| 6 | #endif |
| 7 | |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 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) |
| 12 | #else |
| 13 | #define Py_CHARMASK(c) ((c) & 0xff) |
| 14 | #endif |
| 15 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 16 | /* strtol and strtoul, renamed to avoid conflicts */ |
| 17 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18 | |
| 19 | #include <ctype.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | #ifdef HAVE_ERRNO_H |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 21 | #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 | */ |
| 28 | static 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | #if SIZEOF_LONG == 4 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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 */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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 */ |
| 85 | #else |
| 86 | #error "Need table for SIZEOF_LONG" |
| 87 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 89 | /* |
| 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 Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 94 | ** it looks for a leading 0b, 0o or 0x to tell which |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 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. |
| 101 | */ |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 102 | unsigned long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 103 | PyOS_strtoul(register char *str, char **ptr, int base) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 104 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 108 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 109 | /* skip leading white space */ |
| 110 | while (*str && isspace(Py_CHARMASK(*str))) |
| 111 | ++str; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 113 | /* check for leading 0b, 0o or 0x for auto-base or base 16 */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 114 | switch (base) { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 115 | case 0: /* look for leading 0b, 0o or 0x */ |
| 116 | if (*str == '0') { |
| 117 | ++str; |
| 118 | if (*str == 'x' || *str == 'X') { |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 119 | /* there must be at least one digit after 0x */ |
| 120 | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) { |
| 121 | if (ptr) |
| 122 | *ptr = str; |
| 123 | return 0; |
| 124 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 125 | ++str; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 126 | base = 16; |
| 127 | } else if (*str == 'o' || *str == 'O') { |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 128 | /* there must be at least one digit after 0o */ |
| 129 | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) { |
| 130 | if (ptr) |
| 131 | *ptr = str; |
| 132 | return 0; |
| 133 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 134 | ++str; |
| 135 | base = 8; |
| 136 | } else if (*str == 'b' || *str == 'B') { |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 137 | /* there must be at least one digit after 0b */ |
| 138 | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) { |
| 139 | if (ptr) |
| 140 | *ptr = str; |
| 141 | return 0; |
| 142 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 143 | ++str; |
| 144 | base = 2; |
| 145 | } else { |
| 146 | /* skip all zeroes... */ |
| 147 | while (*str == '0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 148 | ++str; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 149 | while (isspace(Py_CHARMASK(*str))) |
| 150 | ++str; |
| 151 | if (ptr) |
| 152 | *ptr = str; |
| 153 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 154 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 155 | } |
| 156 | else |
| 157 | base = 10; |
| 158 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 160 | /* even with explicit base, skip leading 0? prefix */ |
| 161 | case 16: |
| 162 | if (*str == '0') { |
| 163 | ++str; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 164 | if (*str == 'x' || *str == 'X') { |
| 165 | /* there must be at least one digit after 0x */ |
| 166 | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) { |
| 167 | if (ptr) |
| 168 | *ptr = str; |
| 169 | return 0; |
| 170 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 171 | ++str; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 172 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 173 | } |
| 174 | break; |
| 175 | case 8: |
| 176 | if (*str == '0') { |
| 177 | ++str; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 178 | if (*str == 'o' || *str == 'O') { |
| 179 | /* there must be at least one digit after 0o */ |
| 180 | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) { |
| 181 | if (ptr) |
| 182 | *ptr = str; |
| 183 | return 0; |
| 184 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 185 | ++str; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 186 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 187 | } |
| 188 | break; |
| 189 | case 2: |
| 190 | if(*str == '0') { |
| 191 | ++str; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 192 | if (*str == 'b' || *str == 'B') { |
| 193 | /* there must be at least one digit after 0b */ |
| 194 | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) { |
| 195 | if (ptr) |
| 196 | *ptr = str; |
| 197 | return 0; |
| 198 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 199 | ++str; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame^] | 200 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 201 | } |
| 202 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* catch silly bases */ |
| 206 | if (base < 2 || base > 36) { |
| 207 | if (ptr) |
| 208 | *ptr = str; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* skip leading zeroes */ |
| 213 | while (*str == '0') |
| 214 | ++str; |
| 215 | |
| 216 | /* base is guaranteed to be in [2, 36] at this point */ |
| 217 | ovlimit = digitlimit[base]; |
| 218 | |
| 219 | /* do the conversion until non-digit character encountered */ |
| 220 | while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) { |
| 221 | if (ovlimit > 0) /* no overflow check required */ |
| 222 | result = result * base + c; |
| 223 | else { /* requires overflow check */ |
| 224 | register unsigned long temp_result; |
| 225 | |
| 226 | if (ovlimit < 0) /* guaranteed overflow */ |
| 227 | goto overflowed; |
| 228 | |
| 229 | /* there could be an overflow */ |
| 230 | /* check overflow just from shifting */ |
| 231 | if (result > smallmax[base]) |
| 232 | goto overflowed; |
| 233 | |
| 234 | result *= base; |
| 235 | |
| 236 | /* check overflow from the digit's value */ |
| 237 | temp_result = result + c; |
| 238 | if (temp_result < result) |
| 239 | goto overflowed; |
| 240 | |
| 241 | result = temp_result; |
| 242 | } |
| 243 | |
| 244 | ++str; |
| 245 | --ovlimit; |
| 246 | } |
| 247 | |
| 248 | /* set pointer to point to the last character scanned */ |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 249 | if (ptr) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 250 | *ptr = str; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 251 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 252 | return result; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 253 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 254 | overflowed: |
| 255 | if (ptr) { |
| 256 | /* spool through remaining digit characters */ |
| 257 | while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base) |
| 258 | ++str; |
| 259 | *ptr = str; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 260 | } |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 261 | errno = ERANGE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 262 | return (unsigned long)-1; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 265 | /* Checking for overflow in PyOS_strtol is a PITA; see comments |
| 266 | * about PY_ABS_LONG_MIN in longobject.c. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 267 | */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 268 | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 270 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 271 | PyOS_strtol(char *str, char **ptr, int base) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 272 | { |
| 273 | long result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 274 | unsigned long uresult; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 275 | char sign; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 277 | while (*str && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 278 | str++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 280 | sign = *str; |
| 281 | if (sign == '+' || sign == '-') |
| 282 | str++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 283 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 284 | uresult = PyOS_strtoul(str, ptr, base); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 285 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | if (uresult <= (unsigned long)LONG_MAX) { |
| 287 | result = (long)uresult; |
| 288 | if (sign == '-') |
| 289 | result = -result; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 290 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 291 | else if (sign == '-' && uresult == PY_ABS_LONG_MIN) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 292 | result = LONG_MIN; |
| 293 | } |
| 294 | else { |
| 295 | errno = ERANGE; |
| 296 | result = LONG_MAX; |
| 297 | } |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 298 | return result; |
| 299 | } |