MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
str2int.c
1 /* Copyright (c) 2000-2003, 2006 MySQL AB
2  Use is subject to license terms.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 /*
18  str2int(src, radix, lower, upper, &val)
19  converts the string pointed to by src to an integer and stores it in
20  val. It skips leading spaces and tabs (but not newlines, formfeeds,
21  backspaces), then it accepts an optional sign and a sequence of digits
22  in the specified radix. The result should satisfy lower <= *val <= upper.
23  The result is a pointer to the first character after the number;
24  trailing spaces will NOT be skipped.
25 
26  If an error is detected, the result will be NullS, the value put
27  in val will be 0, and errno will be set to
28  EDOM if there are no digits
29  ERANGE if the result would overflow or otherwise fail to lie
30  within the specified bounds.
31  Check that the bounds are right for your machine.
32  This looks amazingly complicated for what you probably thought was an
33  easy task. Coping with integer overflow and the asymmetric range of
34  twos complement machines is anything but easy.
35 
36  So that users of atoi and atol can check whether an error occured,
37  I have taken a wholly unprecedented step: errno is CLEARED if this
38  call has no problems.
39 */
40 
41 #include <my_global.h>
42 #include "m_string.h"
43 #include "m_ctype.h"
44 #include "my_sys.h" /* defines errno */
45 #include <errno.h>
46 
47 #define char_val(X) (X >= '0' && X <= '9' ? X-'0' :\
48  X >= 'A' && X <= 'Z' ? X-'A'+10 :\
49  X >= 'a' && X <= 'z' ? X-'a'+10 :\
50  '\177')
51 
52 char *str2int(register const char *src, register int radix, long int lower,
53  long int upper, long int *val)
54 {
55  int sign; /* is number negative (+1) or positive (-1) */
56  int n; /* number of digits yet to be converted */
57  long limit; /* "largest" possible valid input */
58  long scale; /* the amount to multiply next digit by */
59  long sofar; /* the running value */
60  register int d; /* (negative of) next digit */
61  char *start;
62  int digits[32]; /* Room for numbers */
63 
64  /* Make sure *val is sensible in case of error */
65 
66  *val = 0;
67 
68  /* Check that the radix is in the range 2..36 */
69 
70 #ifndef DBUG_OFF
71  if (radix < 2 || radix > 36) {
72  errno=EDOM;
73  return NullS;
74  }
75 #endif
76 
77  /* The basic problem is: how do we handle the conversion of
78  a number without resorting to machine-specific code to
79  check for overflow? Obviously, we have to ensure that
80  no calculation can overflow. We are guaranteed that the
81  "lower" and "upper" arguments are valid machine integers.
82  On sign-and-magnitude, twos-complement, and ones-complement
83  machines all, if +|n| is representable, so is -|n|, but on
84  twos complement machines the converse is not true. So the
85  "maximum" representable number has a negative representative.
86  Limit is set to min(-|lower|,-|upper|); this is the "largest"
87  number we are concerned with. */
88 
89  /* Calculate Limit using Scale as a scratch variable */
90 
91  if ((limit = lower) > 0) limit = -limit;
92  if ((scale = upper) > 0) scale = -scale;
93  if (scale < limit) limit = scale;
94 
95  /* Skip leading spaces and check for a sign.
96  Note: because on a 2s complement machine MinLong is a valid
97  integer but |MinLong| is not, we have to keep the current
98  converted value (and the scale!) as *negative* numbers,
99  so the sign is the opposite of what you might expect.
100  */
101  while (my_isspace(&my_charset_latin1,*src)) src++;
102  sign = -1;
103  if (*src == '+') src++; else
104  if (*src == '-') src++, sign = 1;
105 
106  /* Skip leading zeros so that we never compute a power of radix
107  in scale that we won't have a need for. Otherwise sticking
108  enough 0s in front of a number could cause the multiplication
109  to overflow when it neededn't.
110  */
111  start=(char*) src;
112  while (*src == '0') src++;
113 
114  /* Move over the remaining digits. We have to convert from left
115  to left in order to avoid overflow. Answer is after last digit.
116  */
117 
118  for (n = 0; (digits[n]=char_val(*src)) < radix && n < 20; n++,src++) ;
119 
120  /* Check that there is at least one digit */
121 
122  if (start == src) {
123  errno=EDOM;
124  return NullS;
125  }
126 
127  /* The invariant we want to maintain is that src is just
128  to the right of n digits, we've converted k digits to
129  sofar, scale = -radix**k, and scale < sofar < 0. Now
130  if the final number is to be within the original
131  Limit, we must have (to the left)*scale+sofar >= Limit,
132  or (to the left)*scale >= Limit-sofar, i.e. the digits
133  to the left of src must form an integer <= (Limit-sofar)/(scale).
134  In particular, this is true of the next digit. In our
135  incremental calculation of Limit,
136 
137  IT IS VITAL that (-|N|)/(-|D|) = |N|/|D|
138  */
139 
140  for (sofar = 0, scale = -1; --n >= 1;)
141  {
142  if ((long) -(d=digits[n]) < limit) {
143  errno=ERANGE;
144  return NullS;
145  }
146  limit = (limit+d)/radix, sofar += d*scale; scale *= radix;
147  }
148  if (n == 0)
149  {
150  if ((long) -(d=digits[n]) < limit) /* get last digit */
151  {
152  errno=ERANGE;
153  return NullS;
154  }
155  sofar+=d*scale;
156  }
157 
158  /* Now it might still happen that sofar = -32768 or its equivalent,
159  so we can't just multiply by the sign and check that the result
160  is in the range lower..upper. All of this caution is a right
161  pain in the neck. If only there were a standard routine which
162  says generate thus and such a signal on integer overflow...
163  But not enough machines can do it *SIGH*.
164  */
165  if (sign < 0)
166  {
167  if (sofar < -LONG_MAX || (sofar= -sofar) > upper)
168  {
169  errno=ERANGE;
170  return NullS;
171  }
172  }
173  else if (sofar < lower)
174  {
175  errno=ERANGE;
176  return NullS;
177  }
178  *val = sofar;
179  errno=0; /* indicate that all went well */
180  return (char*) src;
181 }
182 
183  /* Theese are so slow compared with ordinary, optimized atoi */
184 
185 #ifdef WANT_OUR_ATOI
186 
187 int atoi(const char *src)
188 {
189  long val;
190  str2int(src, 10, (long) INT_MIN, (long) INT_MAX, &val);
191  return (int) val;
192 }
193 
194 
195 long atol(const char *src)
196 {
197  long val;
198  str2int(src, 10, LONG_MIN, LONG_MAX, &val);
199  return val;
200 }
201 
202 #endif /* WANT_OUR_ATOI */