Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 1924a06 | 1998-12-18 22:02:37 +0000 | [diff] [blame] | 11 | #include "Python.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | e32d153 | 1998-07-07 21:32:53 +0000 | [diff] [blame] | 13 | #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE) |
| 14 | #define _SGI_MP_SOURCE |
| 15 | #endif |
| 16 | |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 17 | /* Convert a possibly signed character to a nonnegative int */ |
| 18 | /* XXX This assumes characters are 8 bits wide */ |
| 19 | #ifdef __CHAR_UNSIGNED__ |
| 20 | #define Py_CHARMASK(c) (c) |
| 21 | #else |
| 22 | #define Py_CHARMASK(c) ((c) & 0xff) |
| 23 | #endif |
| 24 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 25 | /* strtol and strtoul, renamed to avoid conflicts */ |
| 26 | |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 27 | /* |
| 28 | ** strtoul |
| 29 | ** This is a general purpose routine for converting |
| 30 | ** an ascii string to an integer in an arbitrary base. |
| 31 | ** Leading white space is ignored. If 'base' is zero |
| 32 | ** it looks for a leading 0, 0x or 0X to tell which |
| 33 | ** base. If these are absent it defaults to 10. |
| 34 | ** Base must be 0 or between 2 and 36 (inclusive). |
| 35 | ** If 'ptr' is non-NULL it will contain a pointer to |
| 36 | ** the end of the scan. |
| 37 | ** Errors due to bad pointers will probably result in |
| 38 | ** exceptions - we don't check for them. |
| 39 | */ |
| 40 | |
| 41 | #include <ctype.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 42 | #ifndef DONT_HAVE_ERRNO_H |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 43 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 44 | #endif |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 45 | |
| 46 | unsigned long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame^] | 47 | PyOS_strtoul(register char *str, char **ptr, int base) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 48 | { |
| 49 | register unsigned long result; /* return value of the function */ |
| 50 | register int c; /* current input character */ |
| 51 | register unsigned long temp; /* used in overflow testing */ |
| 52 | int ovf; /* true if overflow occurred */ |
| 53 | |
| 54 | result = 0; |
| 55 | ovf = 0; |
| 56 | |
| 57 | /* catch silly bases */ |
| 58 | if (base != 0 && (base < 2 || base > 36)) |
| 59 | { |
| 60 | if (ptr) |
| 61 | *ptr = str; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* skip leading white space */ |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 66 | while (*str && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 67 | str++; |
| 68 | |
| 69 | /* check for leading 0 or 0x for auto-base or base 16 */ |
| 70 | switch (base) |
| 71 | { |
| 72 | case 0: /* look for leading 0, 0x or 0X */ |
| 73 | if (*str == '0') |
| 74 | { |
| 75 | str++; |
| 76 | if (*str == 'x' || *str == 'X') |
| 77 | { |
| 78 | str++; |
| 79 | base = 16; |
| 80 | } |
| 81 | else |
| 82 | base = 8; |
| 83 | } |
| 84 | else |
| 85 | base = 10; |
| 86 | break; |
| 87 | |
| 88 | case 16: /* skip leading 0x or 0X */ |
| 89 | if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) |
| 90 | str += 2; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | /* do the conversion */ |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 95 | while ((c = Py_CHARMASK(*str)) != '\0') |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 96 | { |
| 97 | if (isdigit(c) && c - '0' < base) |
| 98 | c -= '0'; |
| 99 | else |
| 100 | { |
| 101 | if (isupper(c)) |
| 102 | c = tolower(c); |
| 103 | if (c >= 'a' && c <= 'z') |
| 104 | c -= 'a' - 10; |
| 105 | else /* non-"digit" character */ |
| 106 | break; |
| 107 | if (c >= base) /* non-"digit" character */ |
| 108 | break; |
| 109 | } |
| 110 | temp = result; |
| 111 | result = result * base + c; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 112 | #ifndef MPW |
Guido van Rossum | 330aafb | 1997-12-15 17:27:35 +0000 | [diff] [blame] | 113 | if(base == 10) { |
Guido van Rossum | 39b0f89 | 1998-04-10 21:52:06 +0000 | [diff] [blame] | 114 | if(((long)(result - c) / base != (long)temp)) /* overflow */ |
Guido van Rossum | 330aafb | 1997-12-15 17:27:35 +0000 | [diff] [blame] | 115 | ovf = 1; |
| 116 | } |
| 117 | else { |
| 118 | if ((result - c) / base != temp) /* overflow */ |
| 119 | ovf = 1; |
| 120 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 121 | #endif |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 122 | str++; |
| 123 | } |
| 124 | |
| 125 | /* set pointer to point to the last character scanned */ |
| 126 | if (ptr) |
| 127 | *ptr = str; |
| 128 | if (ovf) |
| 129 | { |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 130 | result = (unsigned long) ~0L; |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 131 | errno = ERANGE; |
| 132 | } |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame^] | 137 | PyOS_strtol(char *str, char **ptr, int base) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 138 | { |
| 139 | long result; |
| 140 | char sign; |
| 141 | |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 142 | while (*str && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 143 | str++; |
| 144 | |
| 145 | sign = *str; |
| 146 | if (sign == '+' || sign == '-') |
| 147 | str++; |
| 148 | |
Guido van Rossum | ee2373b | 1997-05-07 23:51:07 +0000 | [diff] [blame] | 149 | result = (long) PyOS_strtoul(str, ptr, base); |
Guido van Rossum | be0e942 | 1993-12-24 10:32:00 +0000 | [diff] [blame] | 150 | |
| 151 | /* Signal overflow if the result appears negative, |
| 152 | except for the largest negative integer */ |
| 153 | if (result < 0 && !(sign == '-' && result == -result)) { |
| 154 | errno = ERANGE; |
| 155 | result = 0x7fffffff; |
| 156 | } |
| 157 | |
| 158 | if (sign == '-') |
| 159 | result = -result; |
| 160 | |
| 161 | return result; |
| 162 | } |