Mercurial > cpython-lru_cache-weakref
annotate Python/mystrtoul.c @ 13163:a09e8635534d legacy-trunk
Don't try to guess the name of a .def file -- if one is supplied, use it,
otherwise just generate an '/export:' option.
| author | Greg Ward <gward@python.net> |
|---|---|
| date | Thu, 29 Jun 2000 23:09:20 +0000 |
| parents | d834ecdf8c26 |
| children | 916fb024ce94 |
| rev | line source |
|---|---|
| 1449 | 1 /*********************************************************** |
|
2062
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
3 The Netherlands. |
| 1449 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 1449 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 1449 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
15 permission. |
| 1449 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 1449 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 9656 | 32 #include "Python.h" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
33 |
|
8634
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
34 #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
|
35 #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
|
36 #endif |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
37 |
| 2310 | 38 /* Convert a possibly signed character to a nonnegative int */ |
| 39 /* XXX This assumes characters are 8 bits wide */ | |
| 40 #ifdef __CHAR_UNSIGNED__ | |
| 41 #define Py_CHARMASK(c) (c) | |
| 42 #else | |
| 43 #define Py_CHARMASK(c) ((c) & 0xff) | |
| 44 #endif | |
| 45 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
46 /* 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
|
47 |
| 1449 | 48 /* |
| 49 ** strtoul | |
| 50 ** This is a general purpose routine for converting | |
| 51 ** an ascii string to an integer in an arbitrary base. | |
| 52 ** Leading white space is ignored. If 'base' is zero | |
| 53 ** it looks for a leading 0, 0x or 0X to tell which | |
| 54 ** base. If these are absent it defaults to 10. | |
| 55 ** Base must be 0 or between 2 and 36 (inclusive). | |
| 56 ** If 'ptr' is non-NULL it will contain a pointer to | |
| 57 ** the end of the scan. | |
| 58 ** Errors due to bad pointers will probably result in | |
| 59 ** exceptions - we don't check for them. | |
| 60 */ | |
| 61 | |
| 62 #include <ctype.h> | |
|
10530
d834ecdf8c26
Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents:
9656
diff
changeset
|
63 #ifndef DONT_HAVE_ERRNO_H |
| 1449 | 64 #include <errno.h> |
|
10530
d834ecdf8c26
Changes by Mark Hammond for Windows CE. Mostly of the form
Guido van Rossum <guido@python.org>
parents:
9656
diff
changeset
|
65 #endif |
| 1449 | 66 |
| 67 unsigned long | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
68 PyOS_strtoul(str, ptr, base) |
| 1449 | 69 register char * str; |
| 70 char ** ptr; | |
| 71 int base; | |
| 72 { | |
| 73 register unsigned long result; /* return value of the function */ | |
| 74 register int c; /* current input character */ | |
| 75 register unsigned long temp; /* used in overflow testing */ | |
| 76 int ovf; /* true if overflow occurred */ | |
| 77 | |
| 78 result = 0; | |
| 79 ovf = 0; | |
| 80 | |
| 81 /* catch silly bases */ | |
| 82 if (base != 0 && (base < 2 || base > 36)) | |
| 83 { | |
| 84 if (ptr) | |
| 85 *ptr = str; | |
| 86 return 0; | |
| 87 } | |
| 88 | |
| 89 /* skip leading white space */ | |
| 2310 | 90 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 91 str++; |
| 92 | |
| 93 /* check for leading 0 or 0x for auto-base or base 16 */ | |
| 94 switch (base) | |
| 95 { | |
| 96 case 0: /* look for leading 0, 0x or 0X */ | |
| 97 if (*str == '0') | |
| 98 { | |
| 99 str++; | |
| 100 if (*str == 'x' || *str == 'X') | |
| 101 { | |
| 102 str++; | |
| 103 base = 16; | |
| 104 } | |
| 105 else | |
| 106 base = 8; | |
| 107 } | |
| 108 else | |
| 109 base = 10; | |
| 110 break; | |
| 111 | |
| 112 case 16: /* skip leading 0x or 0X */ | |
| 113 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) | |
| 114 str += 2; | |
| 115 break; | |
| 116 } | |
| 117 | |
| 118 /* do the conversion */ | |
| 4596 | 119 while ((c = Py_CHARMASK(*str)) != '\0') |
| 1449 | 120 { |
| 121 if (isdigit(c) && c - '0' < base) | |
| 122 c -= '0'; | |
| 123 else | |
| 124 { | |
| 125 if (isupper(c)) | |
| 126 c = tolower(c); | |
| 127 if (c >= 'a' && c <= 'z') | |
| 128 c -= 'a' - 10; | |
| 129 else /* non-"digit" character */ | |
| 130 break; | |
| 131 if (c >= base) /* non-"digit" character */ | |
| 132 break; | |
| 133 } | |
| 134 temp = result; | |
| 135 result = result * base + c; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
136 #ifndef MPW |
|
6849
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
137 if(base == 10) { |
|
7979
cc47733b4dd2
Address warnings issued by the MSVC++ compiler
Guido van Rossum <guido@python.org>
parents:
6849
diff
changeset
|
138 if(((long)(result - c) / base != (long)temp)) /* overflow */ |
|
6849
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
139 ovf = 1; |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
140 } |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
141 else { |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
142 if ((result - c) / base != temp) /* overflow */ |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
143 ovf = 1; |
|
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
144 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
145 #endif |
| 1449 | 146 str++; |
| 147 } | |
| 148 | |
| 149 /* set pointer to point to the last character scanned */ | |
| 150 if (ptr) | |
| 151 *ptr = str; | |
| 152 if (ovf) | |
| 153 { | |
|
5287
5af766a1b531
Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents:
4596
diff
changeset
|
154 result = (unsigned long) ~0L; |
| 1449 | 155 errno = ERANGE; |
| 156 } | |
| 157 return result; | |
| 158 } | |
| 159 | |
| 160 long | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
161 PyOS_strtol(str, ptr, base) |
| 1449 | 162 char * str; |
| 163 char ** ptr; | |
| 164 int base; | |
| 165 { | |
| 166 long result; | |
| 167 char sign; | |
| 168 | |
| 2310 | 169 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 170 str++; |
| 171 | |
| 172 sign = *str; | |
| 173 if (sign == '+' || sign == '-') | |
| 174 str++; | |
| 175 | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
176 result = (long) PyOS_strtoul(str, ptr, base); |
| 1449 | 177 |
| 178 /* Signal overflow if the result appears negative, | |
| 179 except for the largest negative integer */ | |
| 180 if (result < 0 && !(sign == '-' && result == -result)) { | |
| 181 errno = ERANGE; | |
| 182 result = 0x7fffffff; | |
| 183 } | |
| 184 | |
| 185 if (sign == '-') | |
| 186 result = -result; | |
| 187 | |
| 188 return result; | |
| 189 } |
