1 /* Convert string representation of a number into an integer value.
2 Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001,2002,2003
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 # define USE_NUMBER_GROUPING
28 # define HAVE_LIMITS_H
37 # define __set_errno(Val) errno = (Val)
55 #ifdef USE_NUMBER_GROUPING
56 # include "../locale/localeinfo.h"
59 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
65 # define INT unsigned LONG int
68 /* Determine the name. */
69 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
73 # define strtol __wcstoull_l
75 # define strtol __wcstoul_l
79 # define strtol __strtoull_l
81 # define strtol __strtoul_l
87 # define strtol __wcstoll_l
89 # define strtol __wcstol_l
93 # define strtol __strtoll_l
95 # define strtol __strtol_l
101 # ifdef USE_WIDE_CHAR
103 # define strtol wcstoull
105 # define strtol wcstoul
109 # define strtol strtoull
111 # define strtol strtoul
115 # ifdef USE_WIDE_CHAR
117 # define strtol wcstoll
119 # define strtol wcstol
123 # define strtol strtoll
129 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
130 operating on `long long int's. */
132 # define LONG long long
133 # define STRTOL_LONG_MIN LONG_LONG_MIN
134 # define STRTOL_LONG_MAX LONG_LONG_MAX
135 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
136 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
137 /* Work around gcc bug with using this constant. */
138 static const unsigned long long int maxquad = ULONG_LONG_MAX;
139 # undef STRTOL_ULONG_MAX
140 # define STRTOL_ULONG_MAX maxquad
146 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
149 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
151 # define STRTOL_LONG_MIN LONG_MIN
152 # define STRTOL_LONG_MAX LONG_MAX
153 # define STRTOL_ULONG_MAX ULONG_MAX
157 /* We use this code also for the extended locale handling where the
158 function gets as an additional argument the locale which has to be
159 used. To access the values we have to redefine the _NL_CURRENT
161 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
163 # define _NL_CURRENT(category, item) \
164 (current->values[_NL_ITEM_INDEX (item)].string)
165 # define LOCALE_PARAM , loc
166 # define LOCALE_PARAM_DECL __locale_t loc;
168 # define LOCALE_PARAM
169 # define LOCALE_PARAM_DECL
172 #if defined _LIBC || defined HAVE_WCHAR_H
178 # define L_(Ch) L##Ch
179 # define UCHAR_TYPE wint_t
180 # define STRING_TYPE wchar_t
181 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
182 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
183 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
184 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
186 # define ISSPACE(Ch) iswspace (Ch)
187 # define ISALPHA(Ch) iswalpha (Ch)
188 # define TOUPPER(Ch) towupper (Ch)
192 || defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
193 # define IN_CTYPE_DOMAIN(c) 1
195 # define IN_CTYPE_DOMAIN(c) isascii(c)
198 # define UCHAR_TYPE unsigned char
199 # define STRING_TYPE char
200 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
201 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
202 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
203 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
205 # define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
206 # define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
207 # define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
212 # define INTERNAL(X) INTERNAL1(X)
213 # define INTERNAL1(X) __##X##_internal
214 # define WEAKNAME(X) WEAKNAME1(X)
216 # define INTERNAL(X) __/**/X/**/_internal
219 #ifdef USE_NUMBER_GROUPING
220 /* This file defines a function to check for correct grouping. */
221 # include "grouping.h"
226 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
227 If BASE is 0 the base is determined by the presence of a leading
228 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
229 If BASE is < 2 or > 36, it is reset to 10.
230 If ENDPTR is not NULL, a pointer to the character after the last
231 one converted is stored in *ENDPTR. */
234 INTERNAL (strtol) (nptr, endptr, base, group LOCALE_PARAM)
235 const STRING_TYPE *nptr;
236 STRING_TYPE **endptr;
242 register unsigned LONG int cutoff;
243 register unsigned int cutlim;
244 register unsigned LONG int i;
245 register const STRING_TYPE *s;
246 register UCHAR_TYPE c;
247 const STRING_TYPE *save, *end;
249 #ifndef USE_WIDE_CHAR
253 #ifdef USE_NUMBER_GROUPING
254 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
255 struct locale_data *current = loc->__locales[LC_NUMERIC];
257 /* The thousands character of the current locale. */
258 # ifdef USE_WIDE_CHAR
259 wchar_t thousands = L'\0';
261 const char *thousands = NULL;
262 size_t thousands_len = 0;
264 /* The numeric grouping specification of the current locale,
265 in the format described in <locale.h>. */
266 const char *grouping;
268 if (__builtin_expect (group, 0))
270 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
271 if (*grouping <= 0 || *grouping == CHAR_MAX)
275 /* Figure out the thousands separator character. */
276 # ifdef USE_WIDE_CHAR
278 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
279 _NL_NUMERIC_THOUSANDS_SEP_WC);
281 if (thousands == L'\0')
285 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
287 if (*thousands == '\0')
299 if (base < 0 || base == 1 || base > 36)
301 __set_errno (EINVAL);
307 /* Skip white space. */
310 if (__builtin_expect (*s == L_('\0'), 0))
313 /* Check for a sign. */
320 else if (*s == L_('+'))
323 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
326 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
337 /* Save the pointer so we can check later if anything happened. */
340 #ifdef USE_NUMBER_GROUPING
344 if (__builtin_expect (grouping != NULL, 0))
346 # ifndef USE_WIDE_CHAR
347 thousands_len = strlen (thousands);
350 /* Find the end of the digit string and check its grouping. */
353 # ifdef USE_WIDE_CHAR
356 ({ for (cnt = 0; cnt < thousands_len; ++cnt)
357 if (thousands[cnt] != end[cnt])
359 cnt < thousands_len; })
363 for (c = *end; c != L_('\0'); c = *++end)
364 if (((STRING_TYPE) c < L_('0') || (STRING_TYPE) c > L_('9'))
365 # ifdef USE_WIDE_CHAR
366 && (wchar_t) c != thousands
368 && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
369 if (thousands[cnt] != end[cnt])
371 cnt < thousands_len; })
374 || (int) (TOUPPER (c) - L_('A') + 10) >= base))
377 # ifdef USE_WIDE_CHAR
378 end = __correctly_grouped_prefixwc (s, end, thousands, grouping);
380 end = __correctly_grouped_prefixmb (s, end, thousands, grouping);
388 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
389 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
394 if (sizeof (long int) != sizeof (LONG int))
396 unsigned long int j = 0;
397 unsigned long int jmax = ULONG_MAX / base;
399 for (;c != L_('\0'); c = *++s)
403 if (c >= L_('0') && c <= L_('9'))
405 #ifdef USE_NUMBER_GROUPING
406 # ifdef USE_WIDE_CHAR
407 else if (grouping && (wchar_t) c == thousands)
410 else if (thousands_len)
412 for (cnt = 0; cnt < thousands_len; ++cnt)
413 if (thousands[cnt] != s[cnt])
415 if (cnt == thousands_len)
417 s += thousands_len - 1;
421 c = TOUPPER (c) - L_('A') + 10;
427 else if (ISALPHA (c))
428 c = TOUPPER (c) - L_('A') + 10;
433 /* Note that we never can have an overflow. */
436 /* We have an overflow. Now use the long representation. */
437 i = (unsigned LONG int) j;
441 j = j * (unsigned long int) base + c;
444 i = (unsigned LONG int) j;
447 for (;c != L_('\0'); c = *++s)
451 if (c >= L_('0') && c <= L_('9'))
453 #ifdef USE_NUMBER_GROUPING
454 # ifdef USE_WIDE_CHAR
455 else if (grouping && (wchar_t) c == thousands)
458 else if (thousands_len)
460 for (cnt = 0; cnt < thousands_len; ++cnt)
461 if (thousands[cnt] != s[cnt])
463 if (cnt == thousands_len)
465 s += thousands_len - 1;
469 c = TOUPPER (c) - L_('A') + 10;
475 else if (ISALPHA (c))
476 c = TOUPPER (c) - L_('A') + 10;
481 /* Check for overflow. */
482 if (i > cutoff || (i == cutoff && c > cutlim))
487 i *= (unsigned LONG int) base;
492 /* Check if anything actually happened. */
496 /* Store in ENDPTR the address of one character
497 past the last character we converted. */
499 *endptr = (STRING_TYPE *) s;
502 /* Check for a value that is within the range of
503 `unsigned LONG int', but outside the range of `LONG int'. */
506 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
507 : (unsigned LONG int) STRTOL_LONG_MAX))
511 if (__builtin_expect (overflow, 0))
513 __set_errno (ERANGE);
515 return STRTOL_ULONG_MAX;
517 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
521 /* Return the result of the appropriate sign. */
522 return negative ? -i : i;
525 /* We must handle a special case here: the base is 0 or 16 and the
526 first two characters are '0' and 'x', but the rest are no
527 hexadecimal digits. This is no error case. We return 0 and
528 ENDPTR points to the `x`. */
531 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
532 && save[-2] == L_('0'))
533 *endptr = (STRING_TYPE *) &save[-1];
535 /* There was no number to convert. */
536 *endptr = (STRING_TYPE *) nptr;
542 && !(defined USE_IN_EXTENDED_LOCALE_MODEL && defined USE_WIDE_CHAR)
543 libc_hidden_def (INTERNAL (strtol))
546 /* External user entry point. */
550 # if defined (__STDC__) && __STDC__
551 # define PARAMS(Args) Args
553 # define PARAMS(Args) ()
557 INT strtol PARAMS ((const STRING_TYPE *nptr, STRING_TYPE **endptr, int base));
565 strtol (nptr, endptr, base LOCALE_PARAM)
566 const STRING_TYPE *nptr;
567 STRING_TYPE **endptr;
571 return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);