1/* Copyright (c) 2000 TXT DataKonsult Ab & Monty Program Ab
2 Copyright (c) 2009-2011, Monty Program Ab
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must the following disclaimer in
12 the documentation and/or other materials provided with the
13 distribution.
14
15 THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 SUCH DAMAGE.
27*/
28
29/*
30 str2int(src, radix, lower, upper, &val)
31 converts the string pointed to by src to an integer and stores it in
32 val. It skips leading spaces and tabs (but not newlines, formfeeds,
33 backspaces), then it accepts an optional sign and a sequence of digits
34 in the specified radix. The result should satisfy lower <= *val <= upper.
35 The result is a pointer to the first character after the number;
36 trailing spaces will NOT be skipped.
37
38 If an error is detected, the result will be NullS, the value put
39 in val will be 0, and errno will be set to
40 EDOM if there are no digits
41 ERANGE if the result would overflow or otherwise fail to lie
42 within the specified bounds.
43 Check that the bounds are right for your machine.
44 This looks amazingly complicated for what you probably thought was an
45 easy task. Coping with integer overflow and the asymmetric range of
46 twos complement machines is anything but easy.
47
48 So that users of atoi and atol can check whether an error occurred,
49 I have taken a wholly unprecedented step: errno is CLEARED if this
50 call has no problems.
51*/
52
53#include "strings_def.h"
54#include <m_ctype.h>
55#include "my_sys.h" /* defines errno */
56#include <errno.h>
57
58#define char_val(X) (X >= '0' && X <= '9' ? X-'0' :\
59 X >= 'A' && X <= 'Z' ? X-'A'+10 :\
60 X >= 'a' && X <= 'z' ? X-'a'+10 :\
61 '\177')
62
63char *str2int(register const char *src, register int radix, long int lower,
64 long int upper, long int *val)
65{
66 int sign; /* is number negative (+1) or positive (-1) */
67 int n; /* number of digits yet to be converted */
68 long limit; /* "largest" possible valid input */
69 long scale; /* the amount to multiply next digit by */
70 long sofar; /* the running value */
71 register int d; /* (negative of) next digit */
72 char *start;
73 int digits[32]; /* Room for numbers */
74
75 /* Make sure *val is sensible in case of error */
76
77 *val = 0;
78
79 /* Check that the radix is in the range 2..36 */
80
81#ifndef DBUG_OFF
82 if (radix < 2 || radix > 36) {
83 errno=EDOM;
84 return NullS;
85 }
86#endif
87
88 /* The basic problem is: how do we handle the conversion of
89 a number without resorting to machine-specific code to
90 check for overflow? Obviously, we have to ensure that
91 no calculation can overflow. We are guaranteed that the
92 "lower" and "upper" arguments are valid machine integers.
93 On sign-and-magnitude, twos-complement, and ones-complement
94 machines all, if +|n| is representable, so is -|n|, but on
95 twos complement machines the converse is not true. So the
96 "maximum" representable number has a negative representative.
97 Limit is set to MY_MIN(-|lower|,-|upper|); this is the "largest"
98 number we are concerned with. */
99
100 /* Calculate Limit using Scale as a scratch variable */
101
102 if ((limit = lower) > 0) limit = -limit;
103 if ((scale = upper) > 0) scale = -scale;
104 if (scale < limit) limit = scale;
105
106 /* Skip leading spaces and check for a sign.
107 Note: because on a 2s complement machine MinLong is a valid
108 integer but |MinLong| is not, we have to keep the current
109 converted value (and the scale!) as *negative* numbers,
110 so the sign is the opposite of what you might expect.
111 */
112 while (my_isspace(&my_charset_latin1,*src)) src++;
113 sign = -1;
114 if (*src == '+') src++; else
115 if (*src == '-') src++, sign = 1;
116
117 /* Skip leading zeros so that we never compute a power of radix
118 in scale that we won't have a need for. Otherwise sticking
119 enough 0s in front of a number could cause the multiplication
120 to overflow when it neededn't.
121 */
122 start=(char*) src;
123 while (*src == '0') src++;
124
125 /* Move over the remaining digits. We have to convert from left
126 to left in order to avoid overflow. Answer is after last digit.
127 */
128
129 for (n = 0; (digits[n]=char_val(*src)) < radix && n < 20; n++,src++) ;
130
131 /* Check that there is at least one digit */
132
133 if (start == src) {
134 errno=EDOM;
135 return NullS;
136 }
137
138 /* The invariant we want to maintain is that src is just
139 to the right of n digits, we've converted k digits to
140 sofar, scale = -radix**k, and scale < sofar < 0. Now
141 if the final number is to be within the original
142 Limit, we must have (to the left)*scale+sofar >= Limit,
143 or (to the left)*scale >= Limit-sofar, i.e. the digits
144 to the left of src must form an integer <= (Limit-sofar)/(scale).
145 In particular, this is true of the next digit. In our
146 incremental calculation of Limit,
147
148 IT IS VITAL that (-|N|)/(-|D|) = |N|/|D|
149 */
150
151 for (sofar = 0, scale = -1; --n >= 1;)
152 {
153 if ((long) -(d=digits[n]) < limit) {
154 errno=ERANGE;
155 return NullS;
156 }
157 limit = (limit+d)/radix, sofar += d*scale; scale *= radix;
158 }
159 if (n == 0)
160 {
161 if ((long) -(d=digits[n]) < limit) /* get last digit */
162 {
163 errno=ERANGE;
164 return NullS;
165 }
166 sofar+=d*scale;
167 }
168
169 /* Now it might still happen that sofar = -32768 or its equivalent,
170 so we can't just multiply by the sign and check that the result
171 is in the range lower..upper. All of this caution is a right
172 pain in the neck. If only there were a standard routine which
173 says generate thus and such a signal on integer overflow...
174 But not enough machines can do it *SIGH*.
175 */
176 if (sign < 0)
177 {
178 if (sofar < -LONG_MAX || (sofar= -sofar) > upper)
179 {
180 errno=ERANGE;
181 return NullS;
182 }
183 }
184 else if (sofar < lower)
185 {
186 errno=ERANGE;
187 return NullS;
188 }
189 *val = sofar;
190 errno=0; /* indicate that all went well */
191 return (char*) src;
192}
193
194 /* These are so slow compared with ordinary, optimized atoi */
195
196#ifdef WANT_OUR_ATOI
197
198int atoi(const char *src)
199{
200 long val;
201 str2int(src, 10, (long) INT_MIN, (long) INT_MAX, &val);
202 return (int) val;
203}
204
205
206long atol(const char *src)
207{
208 long val;
209 str2int(src, 10, LONG_MIN, LONG_MAX, &val);
210 return val;
211}
212
213#endif /* WANT_OUR_ATOI */
214