~drizzle-trunk/drizzle/development

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
25
  If an error is detected, the result will be NullS, the value put
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 <my_global.h>
41
#include "m_string.h"
42
#include "m_ctype.h"
43
#include "my_sys.h"			/* defines errno */
44
#include <errno.h>
45
46
#define char_val(X) (X >= '0' && X <= '9' ? X-'0' :\
47
		     X >= 'A' && X <= 'Z' ? X-'A'+10 :\
48
		     X >= 'a' && X <= 'z' ? X-'a'+10 :\
49
		     '\177')
50
51
char *str2int(register const char *src, register int radix, long int lower,
52
	      long int upper, long int *val)
53
{
54
  int sign;			/* is number negative (+1) or positive (-1) */
55
  int n;			/* number of digits yet to be converted */
56
  long limit;			/* "largest" possible valid input */
57
  long scale;			/* the amount to multiply next digit by */
58
  long sofar;			/* the running value */
59
  register int d;		/* (negative of) next digit */
60
  char *start;
61
  int digits[32];		/* Room for numbers */
62
63
  /*  Make sure *val is sensible in case of error  */
64
65
  *val = 0;
66
67
  /*  Check that the radix is in the range 2..36  */
68
  if (radix < 2 || radix > 36) {
69
    errno=EDOM;
70
    return NullS;
71
  }
72
73
  /*  The basic problem is: how do we handle the conversion of
74
      a number without resorting to machine-specific code to
75
      check for overflow?  Obviously, we have to ensure that
76
      no calculation can overflow.  We are guaranteed that the
77
      "lower" and "upper" arguments are valid machine integers.
78
      On sign-and-magnitude, twos-complement, and ones-complement
79
      machines all, if +|n| is representable, so is -|n|, but on
80
      twos complement machines the converse is not true.  So the
81
      "maximum" representable number has a negative representative.
82
      Limit is set to min(-|lower|,-|upper|); this is the "largest"
83
      number we are concerned with.	*/
84
85
  /*  Calculate Limit using Scale as a scratch variable  */
86
87
  if ((limit = lower) > 0) limit = -limit;
88
  if ((scale = upper) > 0) scale = -scale;
89
  if (scale < limit) limit = scale;
90
91
  /*  Skip leading spaces and check for a sign.
92
      Note: because on a 2s complement machine MinLong is a valid
93
      integer but |MinLong| is not, we have to keep the current
94
      converted value (and the scale!) as *negative* numbers,
95
      so the sign is the opposite of what you might expect.
96
      */
97
  while (my_isspace(&my_charset_latin1,*src)) src++;
98
  sign = -1;
99
  if (*src == '+') src++; else
100
    if (*src == '-') src++, sign = 1;
101
102
  /*  Skip leading zeros so that we never compute a power of radix
103
      in scale that we won't have a need for.  Otherwise sticking
104
      enough 0s in front of a number could cause the multiplication
105
      to overflow when it neededn't.
106
      */
107
  start=(char*) src;
108
  while (*src == '0') src++;
109
110
  /*  Move over the remaining digits.  We have to convert from left
111
      to left in order to avoid overflow.  Answer is after last digit.
112
      */
113
114
  for (n = 0; (digits[n]=char_val(*src)) < radix && n < 20; n++,src++) ;
115
116
  /*  Check that there is at least one digit  */
117
118
  if (start == src) {
119
    errno=EDOM;
120
    return NullS;
121
  }
122
123
  /*  The invariant we want to maintain is that src is just
124
      to the right of n digits, we've converted k digits to
125
      sofar, scale = -radix**k, and scale < sofar < 0.	Now
126
      if the final number is to be within the original
127
      Limit, we must have (to the left)*scale+sofar >= Limit,
128
      or (to the left)*scale >= Limit-sofar, i.e. the digits
129
      to the left of src must form an integer <= (Limit-sofar)/(scale).
130
      In particular, this is true of the next digit.  In our
131
      incremental calculation of Limit,
132
133
      IT IS VITAL that (-|N|)/(-|D|) = |N|/|D|
134
      */
135
136
  for (sofar = 0, scale = -1; --n >= 1;)
137
  {
138
    if ((long) -(d=digits[n]) < limit) {
139
      errno=ERANGE;
140
      return NullS;
141
    }
142
    limit = (limit+d)/radix, sofar += d*scale; scale *= radix;
143
  }
144
  if (n == 0)
145
  {
146
    if ((long) -(d=digits[n]) < limit)		/* get last digit */
147
    {
148
      errno=ERANGE;
149
      return NullS;
150
    }
151
    sofar+=d*scale;
152
  }
153
154
  /*  Now it might still happen that sofar = -32768 or its equivalent,
155
      so we can't just multiply by the sign and check that the result
156
      is in the range lower..upper.  All of this caution is a right
157
      pain in the neck.  If only there were a standard routine which
158
      says generate thus and such a signal on integer overflow...
159
      But not enough machines can do it *SIGH*.
160
      */
161
  if (sign < 0)
162
  {
163
    if (sofar < -LONG_MAX || (sofar= -sofar) > upper)
164
    {
165
      errno=ERANGE;
166
      return NullS;
167
    }
168
  }
169
  else if (sofar < lower)
170
  {
171
    errno=ERANGE;
172
    return NullS;
173
  }
174
  *val = sofar;
175
  errno=0;			/* indicate that all went well */
176
  return (char*) src;
177
}
178
179
	/* Theese are so slow compared with ordinary, optimized atoi */
180
181
#ifdef WANT_OUR_ATOI
182
183
int atoi(const char *src)
184
{
185
  long val;
186
  str2int(src, 10, (long) INT_MIN, (long) INT_MAX, &val);
187
  return (int) val;
188
}
189
190
191
long atol(const char *src)
192
{
193
  long val;
194
  str2int(src, 10, LONG_MIN, LONG_MAX, &val);
195
  return val;
196
}
197
198
#endif /* WANT_OUR_ATOI */