blob: 380b37da90330f813e4435e8e05150308499bff2 [file] [log] [blame]
Guido van Rossumbe0e9421993-12-24 10:32:00 +00001
Guido van Rossum1924a061998-12-18 22:02:37 +00002#include "Python.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +00003
Guido van Rossume32d1531998-07-07 21:32:53 +00004#if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
5#define _SGI_MP_SOURCE
6#endif
7
Guido van Rossum7f7f2741995-02-10 17:01:56 +00008/* 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 Rossumb6775db1994-08-01 11:34:53 +000016/* strtol and strtoul, renamed to avoid conflicts */
17
Thomas Wouters477c8d52006-05-27 19:21:47 +000018
19#include <ctype.h>
20#ifndef DONT_HAVE_ERRNO_H
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 */
28static 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 */
72static int digitlimit[] = {
73 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
74 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
75 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
76 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
77
Guido van Rossumbe0e9421993-12-24 10:32:00 +000078/*
79** strtoul
80** This is a general purpose routine for converting
81** an ascii string to an integer in an arbitrary base.
82** Leading white space is ignored. If 'base' is zero
83** it looks for a leading 0, 0x or 0X to tell which
84** base. If these are absent it defaults to 10.
85** Base must be 0 or between 2 and 36 (inclusive).
86** If 'ptr' is non-NULL it will contain a pointer to
87** the end of the scan.
88** Errors due to bad pointers will probably result in
89** exceptions - we don't check for them.
90*/
Guido van Rossumbe0e9421993-12-24 10:32:00 +000091unsigned long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000092PyOS_strtoul(register char *str, char **ptr, int base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +000093{
Thomas Wouters477c8d52006-05-27 19:21:47 +000094 register unsigned long result = 0; /* return value of the function */
95 register int c; /* current input character */
96 register int ovlimit; /* required digits to overflow */
Guido van Rossumbe0e9421993-12-24 10:32:00 +000097
Thomas Wouters477c8d52006-05-27 19:21:47 +000098 /* skip leading white space */
99 while (*str && isspace(Py_CHARMASK(*str)))
100 ++str;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000101
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102 /* check for leading 0 or 0x for auto-base or base 16 */
103 switch (base) {
104 case 0: /* look for leading 0, 0x or 0X */
105 if (*str == '0') {
106 ++str;
107 if (*str == 'x' || *str == 'X') {
108 ++str;
109 base = 16;
110 }
111 else
112 base = 8;
113 }
114 else
115 base = 10;
116 break;
117
118 case 16: /* skip leading 0x or 0X */
119 if (*str == '0') {
120 ++str;
121 if (*str == 'x' || *str == 'X')
122 ++str;
123 }
124 break;
125 }
126
127 /* catch silly bases */
128 if (base < 2 || base > 36) {
129 if (ptr)
130 *ptr = str;
131 return 0;
132 }
133
134 /* skip leading zeroes */
135 while (*str == '0')
136 ++str;
137
138 /* base is guaranteed to be in [2, 36] at this point */
139 ovlimit = digitlimit[base];
140
141 /* do the conversion until non-digit character encountered */
142 while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
143 if (ovlimit > 0) /* no overflow check required */
144 result = result * base + c;
145 else { /* requires overflow check */
146 register unsigned long temp_result;
147
148 if (ovlimit < 0) /* guaranteed overflow */
149 goto overflowed;
150
151 /* there could be an overflow */
152 /* check overflow just from shifting */
153 if (result > smallmax[base])
154 goto overflowed;
155
156 result *= base;
157
158 /* check overflow from the digit's value */
159 temp_result = result + c;
160 if (temp_result < result)
161 goto overflowed;
162
163 result = temp_result;
164 }
165
166 ++str;
167 --ovlimit;
168 }
169
170 /* set pointer to point to the last character scanned */
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000171 if (ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000172 *ptr = str;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000173
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174 return result;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000175
Thomas Wouters477c8d52006-05-27 19:21:47 +0000176overflowed:
177 if (ptr) {
178 /* spool through remaining digit characters */
179 while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
180 ++str;
181 *ptr = str;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000182 }
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000183 errno = ERANGE;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000184 return (unsigned long)-1;
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000185}
186
187long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000188PyOS_strtol(char *str, char **ptr, int base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000189{
190 long result;
191 char sign;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000192
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000193 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000194 str++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000195
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000196 sign = *str;
197 if (sign == '+' || sign == '-')
198 str++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000199
Guido van Rossumee2373b1997-05-07 23:51:07 +0000200 result = (long) PyOS_strtoul(str, ptr, base);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000201
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000202 /* Signal overflow if the result appears negative,
203 except for the largest negative integer */
204 if (result < 0 && !(sign == '-' && result == -result)) {
205 errno = ERANGE;
206 result = 0x7fffffff;
207 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000208
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000209 if (sign == '-')
210 result = -result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000211
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000212 return result;
213}