Mercurial > cpython-lru_cache-weakref
annotate Python/mystrtoul.c @ 3216:d00a79ebb027 legacy-trunk
Added missing imgpbm and imgsgi modules
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Thu, 12 Oct 1995 10:23:27 +0000 |
| parents | 07913fb8f92f |
| children | 318e689c220f |
| 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 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
25 #ifdef HAVE_CONFIG_H |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
26 #include "config.h" |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
27 #endif |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
28 |
| 2310 | 29 /* Convert a possibly signed character to a nonnegative int */ |
| 30 /* XXX This assumes characters are 8 bits wide */ | |
| 31 #ifdef __CHAR_UNSIGNED__ | |
| 32 #define Py_CHARMASK(c) (c) | |
| 33 #else | |
| 34 #define Py_CHARMASK(c) ((c) & 0xff) | |
| 35 #endif | |
| 36 | |
| 2155 | 37 #include "rename2.h" |
| 38 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
39 /* 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
|
40 |
| 1449 | 41 /* |
| 42 ** strtoul | |
| 43 ** This is a general purpose routine for converting | |
| 44 ** an ascii string to an integer in an arbitrary base. | |
| 45 ** Leading white space is ignored. If 'base' is zero | |
| 46 ** it looks for a leading 0, 0x or 0X to tell which | |
| 47 ** base. If these are absent it defaults to 10. | |
| 48 ** Base must be 0 or between 2 and 36 (inclusive). | |
| 49 ** If 'ptr' is non-NULL it will contain a pointer to | |
| 50 ** the end of the scan. | |
| 51 ** Errors due to bad pointers will probably result in | |
| 52 ** exceptions - we don't check for them. | |
| 53 */ | |
| 54 | |
| 55 #include <ctype.h> | |
| 56 #include <errno.h> | |
| 57 | |
| 58 unsigned long | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
59 mystrtoul(str, ptr, base) |
| 1449 | 60 register char * str; |
| 61 char ** ptr; | |
| 62 int base; | |
| 63 { | |
| 64 register unsigned long result; /* return value of the function */ | |
| 65 register int c; /* current input character */ | |
| 66 register unsigned long temp; /* used in overflow testing */ | |
| 67 int ovf; /* true if overflow occurred */ | |
| 68 | |
| 69 result = 0; | |
| 70 ovf = 0; | |
| 71 | |
| 72 /* catch silly bases */ | |
| 73 if (base != 0 && (base < 2 || base > 36)) | |
| 74 { | |
| 75 if (ptr) | |
| 76 *ptr = str; | |
| 77 return 0; | |
| 78 } | |
| 79 | |
| 80 /* skip leading white space */ | |
| 2310 | 81 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 82 str++; |
| 83 | |
| 84 /* check for leading 0 or 0x for auto-base or base 16 */ | |
| 85 switch (base) | |
| 86 { | |
| 87 case 0: /* look for leading 0, 0x or 0X */ | |
| 88 if (*str == '0') | |
| 89 { | |
| 90 str++; | |
| 91 if (*str == 'x' || *str == 'X') | |
| 92 { | |
| 93 str++; | |
| 94 base = 16; | |
| 95 } | |
| 96 else | |
| 97 base = 8; | |
| 98 } | |
| 99 else | |
| 100 base = 10; | |
| 101 break; | |
| 102 | |
| 103 case 16: /* skip leading 0x or 0X */ | |
| 104 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) | |
| 105 str += 2; | |
| 106 break; | |
| 107 } | |
| 108 | |
| 109 /* do the conversion */ | |
| 2310 | 110 while (c = Py_CHARMASK(*str)) |
| 1449 | 111 { |
| 112 if (isdigit(c) && c - '0' < base) | |
| 113 c -= '0'; | |
| 114 else | |
| 115 { | |
| 116 if (isupper(c)) | |
| 117 c = tolower(c); | |
| 118 if (c >= 'a' && c <= 'z') | |
| 119 c -= 'a' - 10; | |
| 120 else /* non-"digit" character */ | |
| 121 break; | |
| 122 if (c >= base) /* non-"digit" character */ | |
| 123 break; | |
| 124 } | |
| 125 temp = result; | |
| 126 result = result * base + c; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
127 #ifndef MPW |
| 1449 | 128 if ((result - c) / base != temp) /* overflow */ |
| 129 ovf = 1; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
130 #endif |
| 1449 | 131 str++; |
| 132 } | |
| 133 | |
| 134 /* set pointer to point to the last character scanned */ | |
| 135 if (ptr) | |
| 136 *ptr = str; | |
| 137 if (ovf) | |
| 138 { | |
| 139 result = ~0; | |
| 140 errno = ERANGE; | |
| 141 } | |
| 142 return result; | |
| 143 } | |
| 144 | |
| 145 long | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
146 mystrtol(str, ptr, base) |
| 1449 | 147 char * str; |
| 148 char ** ptr; | |
| 149 int base; | |
| 150 { | |
| 151 long result; | |
| 152 char sign; | |
| 153 | |
| 2310 | 154 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 155 str++; |
| 156 | |
| 157 sign = *str; | |
| 158 if (sign == '+' || sign == '-') | |
| 159 str++; | |
| 160 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
161 result = (long) mystrtoul(str, ptr, base); |
| 1449 | 162 |
| 163 /* Signal overflow if the result appears negative, | |
| 164 except for the largest negative integer */ | |
| 165 if (result < 0 && !(sign == '-' && result == -result)) { | |
| 166 errno = ERANGE; | |
| 167 result = 0x7fffffff; | |
| 168 } | |
| 169 | |
| 170 if (sign == '-') | |
| 171 result = -result; | |
| 172 | |
| 173 return result; | |
| 174 } |
