~hartmut-php/drizzle/codestyle

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