Mercurial > cpython-lru_cache-weakref
annotate Python/mystrtoul.c @ 6719:faebc1940432 legacy-trunk
Understand -Xlinker option.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 02 Dec 1997 20:38:11 +0000 |
| parents | 9eaa2caed1c3 |
| children | 51f8d4da81e4 |
| 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 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
32 #include "config.h" |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
33 |
| 2310 | 34 /* Convert a possibly signed character to a nonnegative int */ |
| 35 /* XXX This assumes characters are 8 bits wide */ | |
| 36 #ifdef __CHAR_UNSIGNED__ | |
| 37 #define Py_CHARMASK(c) (c) | |
| 38 #else | |
| 39 #define Py_CHARMASK(c) ((c) & 0xff) | |
| 40 #endif | |
| 41 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
42 /* 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
|
43 |
| 1449 | 44 /* |
| 45 ** strtoul | |
| 46 ** This is a general purpose routine for converting | |
| 47 ** an ascii string to an integer in an arbitrary base. | |
| 48 ** Leading white space is ignored. If 'base' is zero | |
| 49 ** it looks for a leading 0, 0x or 0X to tell which | |
| 50 ** base. If these are absent it defaults to 10. | |
| 51 ** Base must be 0 or between 2 and 36 (inclusive). | |
| 52 ** If 'ptr' is non-NULL it will contain a pointer to | |
| 53 ** the end of the scan. | |
| 54 ** Errors due to bad pointers will probably result in | |
| 55 ** exceptions - we don't check for them. | |
| 56 */ | |
| 57 | |
| 58 #include <ctype.h> | |
| 59 #include <errno.h> | |
| 60 | |
| 61 unsigned long | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
62 PyOS_strtoul(str, ptr, base) |
| 1449 | 63 register char * str; |
| 64 char ** ptr; | |
| 65 int base; | |
| 66 { | |
| 67 register unsigned long result; /* return value of the function */ | |
| 68 register int c; /* current input character */ | |
| 69 register unsigned long temp; /* used in overflow testing */ | |
| 70 int ovf; /* true if overflow occurred */ | |
| 71 | |
| 72 result = 0; | |
| 73 ovf = 0; | |
| 74 | |
| 75 /* catch silly bases */ | |
| 76 if (base != 0 && (base < 2 || base > 36)) | |
| 77 { | |
| 78 if (ptr) | |
| 79 *ptr = str; | |
| 80 return 0; | |
| 81 } | |
| 82 | |
| 83 /* skip leading white space */ | |
| 2310 | 84 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 85 str++; |
| 86 | |
| 87 /* check for leading 0 or 0x for auto-base or base 16 */ | |
| 88 switch (base) | |
| 89 { | |
| 90 case 0: /* look for leading 0, 0x or 0X */ | |
| 91 if (*str == '0') | |
| 92 { | |
| 93 str++; | |
| 94 if (*str == 'x' || *str == 'X') | |
| 95 { | |
| 96 str++; | |
| 97 base = 16; | |
| 98 } | |
| 99 else | |
| 100 base = 8; | |
| 101 } | |
| 102 else | |
| 103 base = 10; | |
| 104 break; | |
| 105 | |
| 106 case 16: /* skip leading 0x or 0X */ | |
| 107 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) | |
| 108 str += 2; | |
| 109 break; | |
| 110 } | |
| 111 | |
| 112 /* do the conversion */ | |
| 4596 | 113 while ((c = Py_CHARMASK(*str)) != '\0') |
| 1449 | 114 { |
| 115 if (isdigit(c) && c - '0' < base) | |
| 116 c -= '0'; | |
| 117 else | |
| 118 { | |
| 119 if (isupper(c)) | |
| 120 c = tolower(c); | |
| 121 if (c >= 'a' && c <= 'z') | |
| 122 c -= 'a' - 10; | |
| 123 else /* non-"digit" character */ | |
| 124 break; | |
| 125 if (c >= base) /* non-"digit" character */ | |
| 126 break; | |
| 127 } | |
| 128 temp = result; | |
| 129 result = result * base + c; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
130 #ifndef MPW |
| 1449 | 131 if ((result - c) / base != temp) /* overflow */ |
| 132 ovf = 1; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
133 #endif |
| 1449 | 134 str++; |
| 135 } | |
| 136 | |
| 137 /* set pointer to point to the last character scanned */ | |
| 138 if (ptr) | |
| 139 *ptr = str; | |
| 140 if (ovf) | |
| 141 { | |
|
5287
5af766a1b531
Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents:
4596
diff
changeset
|
142 result = (unsigned long) ~0L; |
| 1449 | 143 errno = ERANGE; |
| 144 } | |
| 145 return result; | |
| 146 } | |
| 147 | |
| 148 long | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
149 PyOS_strtol(str, ptr, base) |
| 1449 | 150 char * str; |
| 151 char ** ptr; | |
| 152 int base; | |
| 153 { | |
| 154 long result; | |
| 155 char sign; | |
| 156 | |
| 2310 | 157 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 158 str++; |
| 159 | |
| 160 sign = *str; | |
| 161 if (sign == '+' || sign == '-') | |
| 162 str++; | |
| 163 | |
|
5440
9eaa2caed1c3
Oops, missed some renamings.
Guido van Rossum <guido@python.org>
parents:
5287
diff
changeset
|
164 result = (long) PyOS_strtoul(str, ptr, base); |
| 1449 | 165 |
| 166 /* Signal overflow if the result appears negative, | |
| 167 except for the largest negative integer */ | |
| 168 if (result < 0 && !(sign == '-' && result == -result)) { | |
| 169 errno = ERANGE; | |
| 170 result = 0x7fffffff; | |
| 171 } | |
| 172 | |
| 173 if (sign == '-') | |
| 174 result = -result; | |
| 175 | |
| 176 return result; | |
| 177 } |
