blob: 5dfade8282c7c42c59c57f7ea3e8614379b3bab8 [file] [log] [blame]
Guido van Rossumbe0e9421993-12-24 10:32:00 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumbe0e9421993-12-24 10:32:00 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossumb6775db1994-08-01 11:34:53 +000025#include "config.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +000026
Guido van Rossum7f7f2741995-02-10 17:01:56 +000027/* Convert a possibly signed character to a nonnegative int */
28/* XXX This assumes characters are 8 bits wide */
29#ifdef __CHAR_UNSIGNED__
30#define Py_CHARMASK(c) (c)
31#else
32#define Py_CHARMASK(c) ((c) & 0xff)
33#endif
34
Guido van Rossum5c2306c1995-01-17 16:31:21 +000035#include "rename2.h"
36
Guido van Rossumb6775db1994-08-01 11:34:53 +000037/* strtol and strtoul, renamed to avoid conflicts */
38
Guido van Rossumbe0e9421993-12-24 10:32:00 +000039/*
40** strtoul
41** This is a general purpose routine for converting
42** an ascii string to an integer in an arbitrary base.
43** Leading white space is ignored. If 'base' is zero
44** it looks for a leading 0, 0x or 0X to tell which
45** base. If these are absent it defaults to 10.
46** Base must be 0 or between 2 and 36 (inclusive).
47** If 'ptr' is non-NULL it will contain a pointer to
48** the end of the scan.
49** Errors due to bad pointers will probably result in
50** exceptions - we don't check for them.
51*/
52
53#include <ctype.h>
54#include <errno.h>
55
56unsigned long
Guido van Rossumb6775db1994-08-01 11:34:53 +000057mystrtoul(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +000058register char * str;
59char ** ptr;
60int base;
61{
62 register unsigned long result; /* return value of the function */
63 register int c; /* current input character */
64 register unsigned long temp; /* used in overflow testing */
65 int ovf; /* true if overflow occurred */
66
67 result = 0;
68 ovf = 0;
69
70/* catch silly bases */
71 if (base != 0 && (base < 2 || base > 36))
72 {
73 if (ptr)
74 *ptr = str;
75 return 0;
76 }
77
78/* skip leading white space */
Guido van Rossum7f7f2741995-02-10 17:01:56 +000079 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +000080 str++;
81
82/* check for leading 0 or 0x for auto-base or base 16 */
83 switch (base)
84 {
85 case 0: /* look for leading 0, 0x or 0X */
86 if (*str == '0')
87 {
88 str++;
89 if (*str == 'x' || *str == 'X')
90 {
91 str++;
92 base = 16;
93 }
94 else
95 base = 8;
96 }
97 else
98 base = 10;
99 break;
100
101 case 16: /* skip leading 0x or 0X */
102 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
103 str += 2;
104 break;
105 }
106
107/* do the conversion */
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000108 while (c = Py_CHARMASK(*str))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000109 {
110 if (isdigit(c) && c - '0' < base)
111 c -= '0';
112 else
113 {
114 if (isupper(c))
115 c = tolower(c);
116 if (c >= 'a' && c <= 'z')
117 c -= 'a' - 10;
118 else /* non-"digit" character */
119 break;
120 if (c >= base) /* non-"digit" character */
121 break;
122 }
123 temp = result;
124 result = result * base + c;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000125#ifndef MPW
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000126 if ((result - c) / base != temp) /* overflow */
127 ovf = 1;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000128#endif
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000129 str++;
130 }
131
132/* set pointer to point to the last character scanned */
133 if (ptr)
134 *ptr = str;
135 if (ovf)
136 {
137 result = ~0;
138 errno = ERANGE;
139 }
140 return result;
141}
142
143long
Guido van Rossumb6775db1994-08-01 11:34:53 +0000144mystrtol(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000145char * str;
146char ** ptr;
147int base;
148{
149 long result;
150 char sign;
151
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000152 while (*str && isspace(Py_CHARMASK(*str)))
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000153 str++;
154
155 sign = *str;
156 if (sign == '+' || sign == '-')
157 str++;
158
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159 result = (long) mystrtoul(str, ptr, base);
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000160
161 /* Signal overflow if the result appears negative,
162 except for the largest negative integer */
163 if (result < 0 && !(sign == '-' && result == -result)) {
164 errno = ERANGE;
165 result = 0x7fffffff;
166 }
167
168 if (sign == '-')
169 result = -result;
170
171 return result;
172}