1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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.
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.
23 ******************************************************************/
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)
34 #define Py_CHARMASK(c) ((c) & 0xff)
39 /* strtol and strtoul, renamed to avoid conflicts */
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.
59 mystrtoul(str
, ptr
, base
)
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 */
72 /* catch silly bases */
73 if (base
!= 0 && (base
< 2 || base
> 36))
80 /* skip leading white space */
81 while (*str
&& isspace(Py_CHARMASK(*str
)))
84 /* check for leading 0 or 0x for auto-base or base 16 */
87 case 0: /* look for leading 0, 0x or 0X */
91 if (*str
== 'x' || *str
== 'X')
103 case 16: /* skip leading 0x or 0X */
104 if (*str
== '0' && (*(str
+1) == 'x' || *(str
+1) == 'X'))
109 /* do the conversion */
110 while (c
= Py_CHARMASK(*str
))
112 if (isdigit(c
) && c
- '0' < base
)
118 if (c
>= 'a' && c
<= 'z')
120 else /* non-"digit" character */
122 if (c
>= base
) /* non-"digit" character */
126 result
= result
* base
+ c
;
128 if ((result
- c
) / base
!= temp
) /* overflow */
134 /* set pointer to point to the last character scanned */
146 mystrtol(str
, ptr
, base
)
154 while (*str
&& isspace(Py_CHARMASK(*str
)))
158 if (sign
== '+' || sign
== '-')
161 result
= (long) mystrtoul(str
, ptr
, base
);
163 /* Signal overflow if the result appears negative,
164 except for the largest negative integer */
165 if (result
< 0 && !(sign
== '-' && result
== -result
)) {