blob: fcd3e27f17f4e926fb0b3df457ffab5dd16cc9e9 [file] [log] [blame]
Victor Stinner03c40802023-09-03 16:54:271// strtol() and strtoul(), renamed to avoid conflicts.
2//
3// API:
4//
5// - PyOS_strtol(): convert string to C long integer.
6// - PyOS_strtoul(): convert string to C unsigned long integer.
7
Guido van Rossum1924a061998-12-18 22:02:378#include "Python.h"
Victor Stinner5f09bb02021-10-19 00:04:529#include "pycore_long.h" // _PyLong_DigitValue
Guido van Rossumb6775db1994-08-01 11:34:5310
Antoine Pitroua6a4dc82017-09-07 16:56:2411#if defined(__sgi) && !defined(_SGI_MP_SOURCE)
Victor Stinner03c40802023-09-03 16:54:2712# define _SGI_MP_SOURCE
Guido van Rossume32d1531998-07-07 21:32:5313#endif
14
Guido van Rossumb6775db1994-08-01 11:34:5315/* strtol and strtoul, renamed to avoid conflicts */
16
Thomas Wouters477c8d52006-05-27 19:21:4717
Thomas Wouters0e3f5912006-08-11 14:57:1218#ifdef HAVE_ERRNO_H
Victor Stinner03c40802023-09-03 16:54:2719# include <errno.h> // errno
Thomas Wouters477c8d52006-05-27 19:21:4720#endif
21
22/* Static overflow check values for bases 2 through 36.
23 * smallmax[base] is the largest unsigned long i such that
24 * i * base doesn't overflow unsigned long.
25 */
Serhiy Storchaka2d06e842015-12-25 17:53:1826static const unsigned long smallmax[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:2727 0, /* bases 0 and 1 are invalid */
28 0,
29 ULONG_MAX / 2,
30 ULONG_MAX / 3,
31 ULONG_MAX / 4,
32 ULONG_MAX / 5,
33 ULONG_MAX / 6,
34 ULONG_MAX / 7,
35 ULONG_MAX / 8,
36 ULONG_MAX / 9,
37 ULONG_MAX / 10,
38 ULONG_MAX / 11,
39 ULONG_MAX / 12,
40 ULONG_MAX / 13,
41 ULONG_MAX / 14,
42 ULONG_MAX / 15,
43 ULONG_MAX / 16,
44 ULONG_MAX / 17,
45 ULONG_MAX / 18,
46 ULONG_MAX / 19,
47 ULONG_MAX / 20,
48 ULONG_MAX / 21,
49 ULONG_MAX / 22,
50 ULONG_MAX / 23,
51 ULONG_MAX / 24,
52 ULONG_MAX / 25,
53 ULONG_MAX / 26,
54 ULONG_MAX / 27,
55 ULONG_MAX / 28,
56 ULONG_MAX / 29,
57 ULONG_MAX / 30,
58 ULONG_MAX / 31,
59 ULONG_MAX / 32,
60 ULONG_MAX / 33,
61 ULONG_MAX / 34,
62 ULONG_MAX / 35,
63 ULONG_MAX / 36,
Thomas Wouters477c8d52006-05-27 19:21:4764};
65
66/* maximum digits that can't ever overflow for bases 2 through 36,
67 * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)].
68 * Note that this is pessimistic if sizeof(long) > 4.
69 */
Thomas Wouters0e3f5912006-08-11 14:57:1270#if SIZEOF_LONG == 4
Serhiy Storchaka2d06e842015-12-25 17:53:1871static const int digitlimit[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:2772 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
73 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
74 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
75 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
Thomas Wouters0e3f5912006-08-11 14:57:1276#elif SIZEOF_LONG == 8
77/* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
Serhiy Storchaka2d06e842015-12-25 17:53:1878static const int digitlimit[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:2779 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
80 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
81 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
82 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
Thomas Wouters0e3f5912006-08-11 14:57:1283#else
Victor Stinner03c40802023-09-03 16:54:2784# error "Need table for SIZEOF_LONG"
Thomas Wouters0e3f5912006-08-11 14:57:1285#endif
Thomas Wouters477c8d52006-05-27 19:21:4786
Guido van Rossumbe0e9421993-12-24 10:32:0087/*
Antoine Pitrouf95a1b32010-05-09 15:52:2788** strtoul
89** This is a general purpose routine for converting
90** an ascii string to an integer in an arbitrary base.
91** Leading white space is ignored. If 'base' is zero
92** it looks for a leading 0b, 0o or 0x to tell which
93** base. If these are absent it defaults to 10.
94** Base must be 0 or between 2 and 36 (inclusive).
95** If 'ptr' is non-NULL it will contain a pointer to
96** the end of the scan.
97** Errors due to bad pointers will probably result in
98** exceptions - we don't check for them.
Guido van Rossumbe0e9421993-12-24 10:32:0099*/
Guido van Rossumbe0e9421993-12-24 10:32:00100unsigned long
Serhiy Storchakac6792272013-10-19 18:03:34101PyOS_strtoul(const char *str, char **ptr, int base)
Guido van Rossumbe0e9421993-12-24 10:32:00102{
Antoine Pitrou9ed5f272013-08-13 18:18:52103 unsigned long result = 0; /* return value of the function */
104 int c; /* current input character */
105 int ovlimit; /* required digits to overflow */
Guido van Rossumbe0e9421993-12-24 10:32:00106
Antoine Pitrouf95a1b32010-05-09 15:52:27107 /* skip leading white space */
Jordon Xu2ec70102019-09-10 16:04:08108 while (*str && Py_ISSPACE(*str))
Antoine Pitrouf95a1b32010-05-09 15:52:27109 ++str;
Guido van Rossumbe0e9421993-12-24 10:32:00110
Antoine Pitrouf95a1b32010-05-09 15:52:27111 /* check for leading 0b, 0o or 0x for auto-base or base 16 */
112 switch (base) {
113 case 0: /* look for leading 0b, 0o or 0x */
114 if (*str == '0') {
115 ++str;
116 if (*str == 'x' || *str == 'X') {
117 /* there must be at least one digit after 0x */
118 if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
119 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34120 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27121 return 0;
122 }
123 ++str;
124 base = 16;
125 } else if (*str == 'o' || *str == 'O') {
126 /* there must be at least one digit after 0o */
127 if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
128 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34129 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27130 return 0;
131 }
132 ++str;
133 base = 8;
134 } else if (*str == 'b' || *str == 'B') {
135 /* there must be at least one digit after 0b */
136 if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
137 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34138 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27139 return 0;
140 }
141 ++str;
142 base = 2;
143 } else {
144 /* skip all zeroes... */
145 while (*str == '0')
146 ++str;
Jordon Xu2ec70102019-09-10 16:04:08147 while (Py_ISSPACE(*str))
Antoine Pitrouf95a1b32010-05-09 15:52:27148 ++str;
149 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34150 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27151 return 0;
152 }
153 }
154 else
155 base = 10;
156 break;
Thomas Wouters477c8d52006-05-27 19:21:47157
Antoine Pitrouf95a1b32010-05-09 15:52:27158 /* even with explicit base, skip leading 0? prefix */
159 case 16:
160 if (*str == '0') {
161 ++str;
162 if (*str == 'x' || *str == 'X') {
163 /* there must be at least one digit after 0x */
164 if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
165 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34166 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27167 return 0;
168 }
169 ++str;
170 }
171 }
172 break;
173 case 8:
174 if (*str == '0') {
175 ++str;
176 if (*str == 'o' || *str == 'O') {
177 /* there must be at least one digit after 0o */
178 if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
179 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34180 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27181 return 0;
182 }
183 ++str;
184 }
185 }
186 break;
187 case 2:
188 if(*str == '0') {
189 ++str;
190 if (*str == 'b' || *str == 'B') {
191 /* there must be at least one digit after 0b */
192 if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
193 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34194 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27195 return 0;
196 }
197 ++str;
198 }
199 }
200 break;
201 }
Thomas Wouters477c8d52006-05-27 19:21:47202
Antoine Pitrouf95a1b32010-05-09 15:52:27203 /* catch silly bases */
204 if (base < 2 || base > 36) {
205 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34206 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27207 return 0;
208 }
Thomas Wouters477c8d52006-05-27 19:21:47209
Antoine Pitrouf95a1b32010-05-09 15:52:27210 /* skip leading zeroes */
211 while (*str == '0')
212 ++str;
Thomas Wouters477c8d52006-05-27 19:21:47213
Antoine Pitrouf95a1b32010-05-09 15:52:27214 /* base is guaranteed to be in [2, 36] at this point */
215 ovlimit = digitlimit[base];
Thomas Wouters477c8d52006-05-27 19:21:47216
Antoine Pitrouf95a1b32010-05-09 15:52:27217 /* do the conversion until non-digit character encountered */
218 while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
219 if (ovlimit > 0) /* no overflow check required */
220 result = result * base + c;
221 else { /* requires overflow check */
Antoine Pitrou9ed5f272013-08-13 18:18:52222 unsigned long temp_result;
Thomas Wouters477c8d52006-05-27 19:21:47223
Antoine Pitrouf95a1b32010-05-09 15:52:27224 if (ovlimit < 0) /* guaranteed overflow */
225 goto overflowed;
Thomas Wouters477c8d52006-05-27 19:21:47226
Antoine Pitrouf95a1b32010-05-09 15:52:27227 /* there could be an overflow */
228 /* check overflow just from shifting */
229 if (result > smallmax[base])
230 goto overflowed;
Thomas Wouters477c8d52006-05-27 19:21:47231
Antoine Pitrouf95a1b32010-05-09 15:52:27232 result *= base;
Thomas Wouters477c8d52006-05-27 19:21:47233
Antoine Pitrouf95a1b32010-05-09 15:52:27234 /* check overflow from the digit's value */
235 temp_result = result + c;
236 if (temp_result < result)
237 goto overflowed;
Thomas Wouters477c8d52006-05-27 19:21:47238
Antoine Pitrouf95a1b32010-05-09 15:52:27239 result = temp_result;
240 }
Thomas Wouters477c8d52006-05-27 19:21:47241
Antoine Pitrouf95a1b32010-05-09 15:52:27242 ++str;
243 --ovlimit;
244 }
Thomas Wouters477c8d52006-05-27 19:21:47245
Antoine Pitrouf95a1b32010-05-09 15:52:27246 /* set pointer to point to the last character scanned */
247 if (ptr)
Serhiy Storchakac6792272013-10-19 18:03:34248 *ptr = (char *)str;
Guido van Rossumbe0e9421993-12-24 10:32:00249
Antoine Pitrouf95a1b32010-05-09 15:52:27250 return result;
Guido van Rossumbe0e9421993-12-24 10:32:00251
Thomas Wouters477c8d52006-05-27 19:21:47252overflowed:
Antoine Pitrouf95a1b32010-05-09 15:52:27253 if (ptr) {
254 /* spool through remaining digit characters */
255 while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
256 ++str;
Serhiy Storchakac6792272013-10-19 18:03:34257 *ptr = (char *)str;
Antoine Pitrouf95a1b32010-05-09 15:52:27258 }
259 errno = ERANGE;
260 return (unsigned long)-1;
Guido van Rossumbe0e9421993-12-24 10:32:00261}
262
Thomas Wouters89f507f2006-12-13 04:49:30263/* Checking for overflow in PyOS_strtol is a PITA; see comments
264 * about PY_ABS_LONG_MIN in longobject.c.
Thomas Wouters0e3f5912006-08-11 14:57:12265 */
Antoine Pitrouf95a1b32010-05-09 15:52:27266#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
Thomas Wouters0e3f5912006-08-11 14:57:12267
Guido van Rossumbe0e9421993-12-24 10:32:00268long
Serhiy Storchakac6792272013-10-19 18:03:34269PyOS_strtol(const char *str, char **ptr, int base)
Guido van Rossumbe0e9421993-12-24 10:32:00270{
Antoine Pitrouf95a1b32010-05-09 15:52:27271 long result;
272 unsigned long uresult;
273 char sign;
Thomas Wouters477c8d52006-05-27 19:21:47274
Jordon Xu2ec70102019-09-10 16:04:08275 while (*str && Py_ISSPACE(*str))
Antoine Pitrouf95a1b32010-05-09 15:52:27276 str++;
Thomas Wouters477c8d52006-05-27 19:21:47277
Antoine Pitrouf95a1b32010-05-09 15:52:27278 sign = *str;
279 if (sign == '+' || sign == '-')
280 str++;
Thomas Wouters477c8d52006-05-27 19:21:47281
Antoine Pitrouf95a1b32010-05-09 15:52:27282 uresult = PyOS_strtoul(str, ptr, base);
Thomas Wouters477c8d52006-05-27 19:21:47283
Antoine Pitrouf95a1b32010-05-09 15:52:27284 if (uresult <= (unsigned long)LONG_MAX) {
285 result = (long)uresult;
286 if (sign == '-')
287 result = -result;
288 }
289 else if (sign == '-' && uresult == PY_ABS_LONG_MIN) {
290 result = LONG_MIN;
291 }
292 else {
293 errno = ERANGE;
294 result = LONG_MAX;
295 }
296 return result;
Guido van Rossumbe0e9421993-12-24 10:32:00297}