1 /* Convert string representation of a number into an integer value.
3 Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2005,
5 Free Software Foundation, Inc.
7 NOTE: The canonical source of this file is maintained with the GNU C
8 Library. Bugs can be reported to bug-glibc@gnu.org.
10 This program is free software: you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 3 of the License, or any
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 # define USE_NUMBER_GROUPING
32 # define __set_errno(Val) errno = (Val)
40 #ifdef USE_NUMBER_GROUPING
41 # include "../locale/localeinfo.h"
44 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
50 # define INT unsigned LONG int
53 /* Determine the name. */
54 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
58 # define strtol __wcstoull_l
60 # define strtol __wcstoul_l
64 # define strtol __strtoull_l
66 # define strtol __strtoul_l
72 # define strtol __wcstoll_l
74 # define strtol __wcstol_l
78 # define strtol __strtoll_l
80 # define strtol __strtol_l
88 # define strtol wcstoull
90 # define strtol wcstoul
94 # define strtol strtoull
96 # define strtol strtoul
100 # ifdef USE_WIDE_CHAR
102 # define strtol wcstoll
104 # define strtol wcstol
108 # define strtol strtoll
114 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
115 operating on `long long int's. */
117 # define LONG long long
118 # define STRTOL_LONG_MIN LONG_LONG_MIN
119 # define STRTOL_LONG_MAX LONG_LONG_MAX
120 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
122 /* The extra casts in the following macros work around compiler bugs,
123 e.g., in Cray C 5.0.3.0. */
125 /* True if negative values of the signed integer type T use two's
126 complement, ones' complement, or signed magnitude representation,
127 respectively. Much GNU code assumes two's complement, but some
128 people like to be portable to all possible C hosts. */
129 # define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
130 # define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
131 # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
133 /* True if the arithmetic type T is signed. */
134 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
136 /* The maximum and minimum values for the integer type T. These
137 macros have undefined behavior if T is signed and has padding bits.
138 If this is a problem for you, please let us know how to fix it for
140 # define TYPE_MINIMUM(t) \
141 ((t) (! TYPE_SIGNED (t) \
143 : TYPE_SIGNED_MAGNITUDE (t) \
145 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
146 # define TYPE_MAXIMUM(t) \
147 ((t) (! TYPE_SIGNED (t) \
149 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
151 # ifndef ULONG_LONG_MAX
152 # define ULONG_LONG_MAX TYPE_MAXIMUM (unsigned long long)
154 # ifndef LONG_LONG_MAX
155 # define LONG_LONG_MAX TYPE_MAXIMUM (long long int)
157 # ifndef LONG_LONG_MIN
158 # define LONG_LONG_MIN TYPE_MINIMUM (long long int)
161 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
162 /* Work around gcc bug with using this constant. */
163 static const unsigned long long int maxquad = ULONG_LONG_MAX;
164 # undef STRTOL_ULONG_MAX
165 # define STRTOL_ULONG_MAX maxquad
169 # define STRTOL_LONG_MIN LONG_MIN
170 # define STRTOL_LONG_MAX LONG_MAX
171 # define STRTOL_ULONG_MAX ULONG_MAX
175 /* We use this code also for the extended locale handling where the
176 function gets as an additional argument the locale which has to be
177 used. To access the values we have to redefine the _NL_CURRENT
179 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
181 # define _NL_CURRENT(category, item) \
182 (current->values[_NL_ITEM_INDEX (item)].string)
183 # define LOCALE_PARAM , loc
184 # define LOCALE_PARAM_PROTO , __locale_t loc
186 # define LOCALE_PARAM
187 # define LOCALE_PARAM_PROTO
194 # define L_(Ch) L##Ch
195 # define UCHAR_TYPE wint_t
196 # define STRING_TYPE wchar_t
197 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
198 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
199 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
200 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
202 # define ISSPACE(Ch) iswspace (Ch)
203 # define ISALPHA(Ch) iswalpha (Ch)
204 # define TOUPPER(Ch) towupper (Ch)
208 # define UCHAR_TYPE unsigned char
209 # define STRING_TYPE char
210 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
211 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
212 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
213 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
215 # define ISSPACE(Ch) isspace (Ch)
216 # define ISALPHA(Ch) isalpha (Ch)
217 # define TOUPPER(Ch) toupper (Ch)
221 #define INTERNAL(X) INTERNAL1(X)
222 #define INTERNAL1(X) __##X##_internal
223 #define WEAKNAME(X) WEAKNAME1(X)
225 #ifdef USE_NUMBER_GROUPING
226 /* This file defines a function to check for correct grouping. */
227 # include "grouping.h"
232 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
233 If BASE is 0 the base is determined by the presence of a leading
234 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
235 If BASE is < 2 or > 36, it is reset to 10.
236 If ENDPTR is not NULL, a pointer to the character after the last
237 one converted is stored in *ENDPTR. */
240 INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
241 int base, int group LOCALE_PARAM_PROTO)
244 register unsigned LONG int cutoff;
245 register unsigned int cutlim;
246 register unsigned LONG int i;
247 register const STRING_TYPE *s;
248 register UCHAR_TYPE c;
249 const STRING_TYPE *save, *end;
252 #ifdef USE_NUMBER_GROUPING
253 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
254 struct locale_data *current = loc->__locales[LC_NUMERIC];
256 /* The thousands character of the current locale. */
257 wchar_t thousands = L'\0';
258 /* The numeric grouping specification of the current locale,
259 in the format described in <locale.h>. */
260 const char *grouping;
264 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
265 if (*grouping <= 0 || *grouping == CHAR_MAX)
269 /* Figure out the thousands separator character. */
270 # if defined _LIBC || defined _HAVE_BTOWC
271 thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
272 if (thousands == WEOF)
275 if (thousands == L'\0')
283 if (base < 0 || base == 1 || base > 36)
285 __set_errno (EINVAL);
291 /* Skip white space. */
297 /* Check for a sign. */
303 else if (*s == L_('+'))
311 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
314 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
325 /* Save the pointer so we can check later if anything happened. */
328 #ifdef USE_NUMBER_GROUPING
331 /* Find the end of the digit string and check its grouping. */
333 for (c = *end; c != L_('\0'); c = *++end)
334 if ((wchar_t) c != thousands
335 && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
336 && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
341 end = correctly_grouped_prefix (s, end, thousands, grouping);
347 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
348 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
352 for (c = *s; c != L_('\0'); c = *++s)
356 if (c >= L_('0') && c <= L_('9'))
358 else if (ISALPHA (c))
359 c = TOUPPER (c) - L_('A') + 10;
364 /* Check for overflow. */
365 if (i > cutoff || (i == cutoff && c > cutlim))
369 i *= (unsigned LONG int) base;
374 /* Check if anything actually happened. */
378 /* Store in ENDPTR the address of one character
379 past the last character we converted. */
381 *endptr = (STRING_TYPE *) s;
384 /* Check for a value that is within the range of
385 `unsigned LONG int', but outside the range of `LONG int'. */
388 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
389 : (unsigned LONG int) STRTOL_LONG_MAX))
395 __set_errno (ERANGE);
397 return STRTOL_ULONG_MAX;
399 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
403 /* Return the result of the appropriate sign. */
404 return negative ? -i : i;
407 /* We must handle a special case here: the base is 0 or 16 and the
408 first two characters are '0' and 'x', but the rest are no
409 hexadecimal digits. This is no error case. We return 0 and
410 ENDPTR points to the `x`. */
413 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
414 && save[-2] == L_('0'))
415 *endptr = (STRING_TYPE *) &save[-1];
417 /* There was no number to convert. */
418 *endptr = (STRING_TYPE *) nptr;
424 /* External user entry point. */
431 strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
432 int base LOCALE_PARAM_PROTO)
434 return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);