Mercurial > cpython-lru_cache-weakref
annotate Python/mystrtoul.c @ 38897:53b04c19fe41 legacy-trunk
Link to LibRef module documentation
| author | Andrew M. Kuchling <amk@amk.ca> |
|---|---|
| date | Wed, 21 Jun 2006 17:10:18 +0000 |
| parents | 42808526511c |
| children | 95f9e0184555 |
| rev | line source |
|---|---|
| 1449 | 1 |
| 9656 | 2 #include "Python.h" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
3 |
|
8634
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
4 #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE) |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
5 #define _SGI_MP_SOURCE |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
6 #endif |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
7 |
| 2310 | 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 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
16 /* strtol and strtoul, renamed to avoid conflicts */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
17 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
18 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
19 #include <ctype.h> |
|
38716
42808526511c
Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
38301
diff
changeset
|
20 #ifdef HAVE_ERRNO_H |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
21 #include <errno.h> |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
22 #endif |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
23 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
24 /* Static overflow check values for bases 2 through 36. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
25 * smallmax[base] is the largest unsigned long i such that |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
26 * i * base doesn't overflow unsigned long. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
27 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
28 static unsigned long smallmax[] = { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
29 0, /* bases 0 and 1 are invalid */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
30 0, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
31 ULONG_MAX / 2, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
32 ULONG_MAX / 3, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
33 ULONG_MAX / 4, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
34 ULONG_MAX / 5, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
35 ULONG_MAX / 6, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
36 ULONG_MAX / 7, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
37 ULONG_MAX / 8, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
38 ULONG_MAX / 9, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
39 ULONG_MAX / 10, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
40 ULONG_MAX / 11, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
41 ULONG_MAX / 12, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
42 ULONG_MAX / 13, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
43 ULONG_MAX / 14, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
44 ULONG_MAX / 15, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
45 ULONG_MAX / 16, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
46 ULONG_MAX / 17, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
47 ULONG_MAX / 18, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
48 ULONG_MAX / 19, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
49 ULONG_MAX / 20, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
50 ULONG_MAX / 21, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
51 ULONG_MAX / 22, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
52 ULONG_MAX / 23, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
53 ULONG_MAX / 24, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
54 ULONG_MAX / 25, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
55 ULONG_MAX / 26, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
56 ULONG_MAX / 27, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
57 ULONG_MAX / 28, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
58 ULONG_MAX / 29, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
59 ULONG_MAX / 30, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
60 ULONG_MAX / 31, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
61 ULONG_MAX / 32, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
62 ULONG_MAX / 33, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
63 ULONG_MAX / 34, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
64 ULONG_MAX / 35, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
65 ULONG_MAX / 36, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
66 }; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
67 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
68 /* maximum digits that can't ever overflow for bases 2 through 36, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
69 * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)]. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
70 * Note that this is pessimistic if sizeof(long) > 4. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
71 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
72 static int digitlimit[] = { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
73 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
74 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
75 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
76 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
77 |
| 1449 | 78 /* |
| 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 */ | |
| 91 unsigned long | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
92 PyOS_strtoul(register char *str, char **ptr, int base) |
| 1449 | 93 { |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
94 register unsigned long result = 0; /* return value of the function */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
95 register int c; /* current input character */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
96 register int ovlimit; /* required digits to overflow */ |
| 1449 | 97 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
98 /* skip leading white space */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
99 while (*str && isspace(Py_CHARMASK(*str))) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
100 ++str; |
| 1449 | 101 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
102 /* check for leading 0 or 0x for auto-base or base 16 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
103 switch (base) { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
104 case 0: /* look for leading 0, 0x or 0X */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
105 if (*str == '0') { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
106 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
107 if (*str == 'x' || *str == 'X') { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
108 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
109 base = 16; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
110 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
111 else |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
112 base = 8; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
113 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
114 else |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
115 base = 10; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
116 break; |
| 1449 | 117 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
118 case 16: /* skip leading 0x or 0X */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
119 if (*str == '0') { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
120 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
121 if (*str == 'x' || *str == 'X') |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
122 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
123 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
124 break; |
| 1449 | 125 } |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
126 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
127 /* catch silly bases */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
128 if (base < 2 || base > 36) { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
129 if (ptr) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
130 *ptr = str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
131 return 0; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
132 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
133 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
134 /* skip leading zeroes */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
135 while (*str == '0') |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
136 ++str; |
| 1449 | 137 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
138 /* base is guaranteed to be in [2, 36] at this point */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
139 ovlimit = digitlimit[base]; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
140 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
141 /* do the conversion until non-digit character encountered */ |
|
38301
dccbcbbd6720
A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents:
38258
diff
changeset
|
142 while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) { |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
143 if (ovlimit > 0) /* no overflow check required */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
144 result = result * base + c; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
145 else { /* requires overflow check */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
146 register unsigned long temp_result; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
147 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
148 if (ovlimit < 0) /* guaranteed overflow */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
149 goto overflowed; |
| 1449 | 150 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
151 /* there could be an overflow */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
152 /* check overflow just from shifting */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
153 if (result > smallmax[base]) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
154 goto overflowed; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
155 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
156 result *= base; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
157 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
158 /* check overflow from the digit's value */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
159 temp_result = result + c; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
160 if (temp_result < result) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
161 goto overflowed; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
162 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
163 result = temp_result; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
164 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
165 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
166 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
167 --ovlimit; |
| 1449 | 168 } |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
169 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
170 /* set pointer to point to the last character scanned */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
171 if (ptr) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
172 *ptr = str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
173 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
174 return result; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
175 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
176 overflowed: |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
177 if (ptr) { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
178 /* spool through remaining digit characters */ |
|
38301
dccbcbbd6720
A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents:
38258
diff
changeset
|
179 while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base) |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
180 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
181 *ptr = str; |
|
6849
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
182 } |
| 1449 | 183 errno = ERANGE; |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
184 return (unsigned long)-1; |
| 1449 | 185 } |
| 186 | |
| 187 long | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
188 PyOS_strtol(char *str, char **ptr, int base) |
| 1449 | 189 { |
| 190 long result; | |
| 191 char sign; | |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
192 |
| 2310 | 193 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 194 str++; |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
195 |
| 1449 | 196 sign = *str; |
| 197 if (sign == '+' || sign == '-') | |
| 198 str++; | |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
199 |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
200 result = (long) PyOS_strtoul(str, ptr, base); |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
201 |
| 1449 | 202 /* 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 } | |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
208 |
| 1449 | 209 if (sign == '-') |
| 210 result = -result; | |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
211 |
| 1449 | 212 return result; |
| 213 } |
