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