~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/str2int.c

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  The result is a pointer to the first character after the number;
23
23
  trailing spaces will NOT be skipped.
24
24
 
25
 
  If an error is detected, the result will be NullS, the value put
 
25
  If an error is detected, the result will be (char *)0, the value put
26
26
  in val will be 0, and errno will be set to
27
27
        EDOM    if there are no digits
28
28
        ERANGE  if the result would overflow or otherwise fail to lie
37
37
  call has no problems.
38
38
*/
39
39
 
40
 
#include <my_global.h>
41
40
#include "m_string.h"
42
41
#include "m_ctype.h"
43
 
#include "my_sys.h"                     /* defines errno */
44
42
#include <errno.h>
45
43
 
46
44
#define char_val(X) (X >= '0' && X <= '9' ? X-'0' :\
65
63
  *val = 0;
66
64
 
67
65
  /*  Check that the radix is in the range 2..36  */
68
 
 
69
 
#ifndef DBUG_OFF
70
66
  if (radix < 2 || radix > 36) {
71
67
    errno=EDOM;
72
 
    return NullS;
 
68
    return (char *)0;
73
69
  }
74
 
#endif
75
70
 
76
71
  /*  The basic problem is: how do we handle the conversion of
77
72
      a number without resorting to machine-specific code to
82
77
      machines all, if +|n| is representable, so is -|n|, but on
83
78
      twos complement machines the converse is not true.  So the
84
79
      "maximum" representable number has a negative representative.
85
 
      Limit is set to min(-|lower|,-|upper|); this is the "largest"
 
80
      Limit is set to cmin(-|lower|,-|upper|); this is the "largest"
86
81
      number we are concerned with.     */
87
82
 
88
83
  /*  Calculate Limit using Scale as a scratch variable  */
97
92
      converted value (and the scale!) as *negative* numbers,
98
93
      so the sign is the opposite of what you might expect.
99
94
      */
100
 
  while (my_isspace(&my_charset_latin1,*src)) src++;
 
95
  while (my_isspace(&my_charset_utf8_general_ci,*src)) src++;
101
96
  sign = -1;
102
97
  if (*src == '+') src++; else
103
98
    if (*src == '-') src++, sign = 1;
120
115
 
121
116
  if (start == src) {
122
117
    errno=EDOM;
123
 
    return NullS;
 
118
    return (char *)0;
124
119
  }
125
120
 
126
121
  /*  The invariant we want to maintain is that src is just
140
135
  {
141
136
    if ((long) -(d=digits[n]) < limit) {
142
137
      errno=ERANGE;
143
 
      return NullS;
 
138
      return (char *)0;
144
139
    }
145
140
    limit = (limit+d)/radix, sofar += d*scale; scale *= radix;
146
141
  }
149
144
    if ((long) -(d=digits[n]) < limit)          /* get last digit */
150
145
    {
151
146
      errno=ERANGE;
152
 
      return NullS;
 
147
      return (char *)0;
153
148
    }
154
149
    sofar+=d*scale;
155
150
  }
166
161
    if (sofar < -LONG_MAX || (sofar= -sofar) > upper)
167
162
    {
168
163
      errno=ERANGE;
169
 
      return NullS;
 
164
      return (char *)0;
170
165
    }
171
166
  }
172
167
  else if (sofar < lower)
173
168
  {
174
169
    errno=ERANGE;
175
 
    return NullS;
 
170
    return (char *)0;
176
171
  }
177
172
  *val = sofar;
178
173
  errno=0;                      /* indicate that all went well */