Bump version to 0.9.1.
[python/dscho.git] / Python / mystrtoul.c
blob29728e5682cb02ea5b98e374e09240831e84ad19
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.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 #include "Python.h"
13 #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
14 #define _SGI_MP_SOURCE
15 #endif
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)
21 #else
22 #define Py_CHARMASK(c) ((c) & 0xff)
23 #endif
25 /* strtol and strtoul, renamed to avoid conflicts */
28 ** strtoul
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.
41 #include <ctype.h>
42 #ifndef DONT_HAVE_ERRNO_H
43 #include <errno.h>
44 #endif
46 unsigned long
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 */
54 result = 0;
55 ovf = 0;
57 /* catch silly bases */
58 if (base != 0 && (base < 2 || base > 36))
60 if (ptr)
61 *ptr = str;
62 return 0;
65 /* skip leading white space */
66 while (*str && isspace(Py_CHARMASK(*str)))
67 str++;
69 /* check for leading 0 or 0x for auto-base or base 16 */
70 switch (base)
72 case 0: /* look for leading 0, 0x or 0X */
73 if (*str == '0')
75 str++;
76 if (*str == 'x' || *str == 'X')
78 str++;
79 base = 16;
81 else
82 base = 8;
84 else
85 base = 10;
86 break;
88 case 16: /* skip leading 0x or 0X */
89 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
90 str += 2;
91 break;
94 /* do the conversion */
95 while ((c = Py_CHARMASK(*str)) != '\0')
97 if (isdigit(c) && c - '0' < base)
98 c -= '0';
99 else
101 if (isupper(c))
102 c = tolower(c);
103 if (c >= 'a' && c <= 'z')
104 c -= 'a' - 10;
105 else /* non-"digit" character */
106 break;
107 if (c >= base) /* non-"digit" character */
108 break;
110 temp = result;
111 result = result * base + c;
112 #ifndef MPW
113 if(base == 10) {
114 if(((long)(result - c) / base != (long)temp)) /* overflow */
115 ovf = 1;
117 else {
118 if ((result - c) / base != temp) /* overflow */
119 ovf = 1;
121 #endif
122 str++;
125 /* set pointer to point to the last character scanned */
126 if (ptr)
127 *ptr = str;
128 if (ovf)
130 result = (unsigned long) ~0L;
131 errno = ERANGE;
133 return result;
136 long
137 PyOS_strtol(char *str, char **ptr, int base)
139 long result;
140 char sign;
142 while (*str && isspace(Py_CHARMASK(*str)))
143 str++;
145 sign = *str;
146 if (sign == '+' || sign == '-')
147 str++;
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)) {
154 errno = ERANGE;
155 result = 0x7fffffff;
158 if (sign == '-')
159 result = -result;
161 return result;