1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
13 #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
14 #define _SGI_MP_SOURCE
17 /* Convert a possibly signed character to a nonnegative int */
18 /* XXX This assumes characters are 8 bits wide */
19 #ifdef __CHAR_UNSIGNED__
20 #define Py_CHARMASK(c) (c)
22 #define Py_CHARMASK(c) ((c) & 0xff)
25 /* strtol and strtoul, renamed to avoid conflicts */
29 ** This is a general purpose routine for converting
30 ** an ascii string to an integer in an arbitrary base.
31 ** Leading white space is ignored. If 'base' is zero
32 ** it looks for a leading 0, 0x or 0X to tell which
33 ** base. If these are absent it defaults to 10.
34 ** Base must be 0 or between 2 and 36 (inclusive).
35 ** If 'ptr' is non-NULL it will contain a pointer to
36 ** the end of the scan.
37 ** Errors due to bad pointers will probably result in
38 ** exceptions - we don't check for them.
42 #ifndef DONT_HAVE_ERRNO_H
47 PyOS_strtoul(register char *str
, char **ptr
, int base
)
49 register unsigned long result
; /* return value of the function */
50 register int c
; /* current input character */
51 register unsigned long temp
; /* used in overflow testing */
52 int ovf
; /* true if overflow occurred */
57 /* catch silly bases */
58 if (base
!= 0 && (base
< 2 || base
> 36))
65 /* skip leading white space */
66 while (*str
&& isspace(Py_CHARMASK(*str
)))
69 /* check for leading 0 or 0x for auto-base or base 16 */
72 case 0: /* look for leading 0, 0x or 0X */
76 if (*str
== 'x' || *str
== 'X')
88 case 16: /* skip leading 0x or 0X */
89 if (*str
== '0' && (*(str
+1) == 'x' || *(str
+1) == 'X'))
94 /* do the conversion */
95 while ((c
= Py_CHARMASK(*str
)) != '\0')
97 if (isdigit(c
) && c
- '0' < base
)
103 if (c
>= 'a' && c
<= 'z')
105 else /* non-"digit" character */
107 if (c
>= base
) /* non-"digit" character */
111 result
= result
* base
+ c
;
114 if(((long)(result
- c
) / base
!= (long)temp
)) /* overflow */
118 if ((result
- c
) / base
!= temp
) /* overflow */
125 /* set pointer to point to the last character scanned */
130 result
= (unsigned long) ~0L;
137 PyOS_strtol(char *str
, char **ptr
, int base
)
142 while (*str
&& isspace(Py_CHARMASK(*str
)))
146 if (sign
== '+' || sign
== '-')
149 result
= (long) PyOS_strtoul(str
, ptr
, base
);
151 /* Signal overflow if the result appears negative,
152 except for the largest negative integer */
153 if (result
< 0 && !(sign
== '-' && result
== -result
)) {