1 /* Convert string representing a number to integer value, using given locale.
2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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, see
18 <https://www.gnu.org/licenses/>. */
26 # define USE_NUMBER_GROUPING
27 # define HAVE_LIMITS_H
33 # define __set_errno(Val) errno = (Val)
45 #include <bits/wordsize.h>
47 #ifdef USE_NUMBER_GROUPING
48 # include "../locale/localeinfo.h"
51 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
57 # define INT unsigned LONG int
60 /* Determine the name. */
64 # define strtol_l wcstoull_l
66 # define strtol_l wcstoul_l
70 # define strtol_l strtoull_l
72 # define strtol_l strtoul_l
78 # define strtol_l wcstoll_l
80 # define strtol_l wcstol_l
84 # define strtol_l strtoll_l
86 # define strtol_l strtol_l
91 #define __strtol_l __strtol_l2(strtol_l)
92 #define __strtol_l2(name) __strtol_l3(name)
93 #define __strtol_l3(name) __##name
96 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
97 operating on `long long int's. */
99 # define LONG long long
100 # define STRTOL_LONG_MIN LONG_LONG_MIN
101 # define STRTOL_LONG_MAX LONG_LONG_MAX
102 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
107 # define ULONG_MAX ((unsigned long int) ~(unsigned long int) 0)
110 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
112 # define STRTOL_LONG_MIN LONG_MIN
113 # define STRTOL_LONG_MAX LONG_MAX
114 # define STRTOL_ULONG_MAX ULONG_MAX
118 /* We use this code for the extended locale handling where the
119 function gets as an additional argument the locale which has to be
120 used. To access the values we have to redefine the _NL_CURRENT and
121 _NL_CURRENT_WORD macros. */
123 #define _NL_CURRENT(category, item) \
124 (current->values[_NL_ITEM_INDEX (item)].string)
125 #undef _NL_CURRENT_WORD
126 #define _NL_CURRENT_WORD(category, item) \
127 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
129 #if defined _LIBC || defined HAVE_WCHAR_H
135 # define L_(Ch) L##Ch
136 # define UCHAR_TYPE wint_t
137 # define STRING_TYPE wchar_t
138 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
139 # define ISALPHA(Ch) __iswalpha_l ((Ch), _nl_C_locobj_ptr)
140 # define TOUPPER(Ch) __towupper_l ((Ch), _nl_C_locobj_ptr)
143 || defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
144 # define IN_CTYPE_DOMAIN(c) 1
146 # define IN_CTYPE_DOMAIN(c) isascii(c)
149 # define UCHAR_TYPE unsigned char
150 # define STRING_TYPE char
151 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
152 # define ISALPHA(Ch) __isalpha_l ((Ch), _nl_C_locobj_ptr)
153 # define TOUPPER(Ch) __toupper_l ((Ch), _nl_C_locobj_ptr)
156 #define INTERNAL(X) INTERNAL1(X)
157 #define INTERNAL1(X) __##X##_internal
158 #define WEAKNAME(X) WEAKNAME1(X)
160 #ifdef USE_NUMBER_GROUPING
161 /* This file defines a function to check for correct grouping. */
162 # include "grouping.h"
166 /* Define tables of maximum values and remainders in order to detect
167 overflow. Do this at compile-time in order to avoid the runtime
168 overhead of the division. */
169 extern const unsigned long __strtol_ul_max_tab[] attribute_hidden;
170 extern const unsigned char __strtol_ul_rem_tab[] attribute_hidden;
171 #if defined(QUAD) && __WORDSIZE == 32
172 extern const unsigned long long __strtol_ull_max_tab[] attribute_hidden;
173 extern const unsigned char __strtol_ull_rem_tab[] attribute_hidden;
176 #define DEF(TYPE, NAME) \
177 const TYPE NAME[] attribute_hidden = \
179 F(2), F(3), F(4), F(5), F(6), F(7), F(8), F(9), F(10), \
180 F(11), F(12), F(13), F(14), F(15), F(16), F(17), F(18), F(19), F(20), \
181 F(21), F(22), F(23), F(24), F(25), F(26), F(27), F(28), F(29), F(30), \
182 F(31), F(32), F(33), F(34), F(35), F(36) \
185 #if !UNSIGNED && !defined (USE_WIDE_CHAR) && !defined (QUAD)
186 # define F(X) ULONG_MAX / X
187 DEF (unsigned long, __strtol_ul_max_tab);
189 # define F(X) ULONG_MAX % X
190 DEF (unsigned char, __strtol_ul_rem_tab);
193 #if !UNSIGNED && !defined (USE_WIDE_CHAR) && defined (QUAD) \
195 # define F(X) ULONG_LONG_MAX / X
196 DEF (unsigned long long, __strtol_ull_max_tab);
198 # define F(X) ULONG_LONG_MAX % X
199 DEF (unsigned char, __strtol_ull_rem_tab);
204 /* Define some more readable aliases for these arrays which correspond
205 to how they'll be used in the function below. */
206 #define jmax_tab __strtol_ul_max_tab
207 #if defined(QUAD) && __WORDSIZE == 32
208 # define cutoff_tab __strtol_ull_max_tab
209 # define cutlim_tab __strtol_ull_rem_tab
211 # define cutoff_tab __strtol_ul_max_tab
212 # define cutlim_tab __strtol_ul_rem_tab
216 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
217 If BASE is 0 the base is determined by the presence of a leading
218 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
219 If BASE is < 2 or > 36, it is reset to 10.
220 If ENDPTR is not NULL, a pointer to the character after the last
221 one converted is stored in *ENDPTR. */
224 INTERNAL (__strtol_l) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
225 int base, int group, locale_t loc)
228 unsigned LONG int cutoff;
231 const STRING_TYPE *s;
233 const STRING_TYPE *save, *end;
235 #ifndef USE_WIDE_CHAR
239 #ifdef USE_NUMBER_GROUPING
240 struct __locale_data *current = loc->__locales[LC_NUMERIC];
241 /* The thousands character of the current locale. */
242 # ifdef USE_WIDE_CHAR
243 wchar_t thousands = L'\0';
245 const char *thousands = NULL;
246 size_t thousands_len = 0;
248 /* The numeric grouping specification of the current locale,
249 in the format described in <locale.h>. */
250 const char *grouping;
252 if (__glibc_unlikely (group))
254 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
255 if (*grouping <= 0 || *grouping == CHAR_MAX)
259 /* Figure out the thousands separator character. */
260 # ifdef USE_WIDE_CHAR
262 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
263 _NL_NUMERIC_THOUSANDS_SEP_WC);
265 if (thousands == L'\0')
269 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
271 if (*thousands == '\0')
283 if (base < 0 || base == 1 || base > 36)
285 __set_errno (EINVAL);
291 /* Skip white space. */
294 if (__glibc_unlikely (*s == L_('\0')))
297 /* Check for a sign. */
304 else if (*s == L_('+'))
307 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
310 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
321 /* Save the pointer so we can check later if anything happened. */
324 #ifdef USE_NUMBER_GROUPING
328 if (__glibc_unlikely (grouping != NULL))
330 # ifndef USE_WIDE_CHAR
331 thousands_len = strlen (thousands);
334 /* Find the end of the digit string and check its grouping. */
337 # ifdef USE_WIDE_CHAR
340 ({ for (cnt = 0; cnt < thousands_len; ++cnt)
341 if (thousands[cnt] != end[cnt])
343 cnt < thousands_len; })
347 for (c = *end; c != L_('\0'); c = *++end)
348 if (((STRING_TYPE) c < L_('0') || (STRING_TYPE) c > L_('9'))
349 # ifdef USE_WIDE_CHAR
350 && (wchar_t) c != thousands
352 && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
353 if (thousands[cnt] != end[cnt])
355 cnt < thousands_len; })
358 || (int) (TOUPPER (c) - L_('A') + 10) >= base))
361 # ifdef USE_WIDE_CHAR
362 end = __correctly_grouped_prefixwc (s, end, thousands, grouping);
364 end = __correctly_grouped_prefixmb (s, end, thousands, grouping);
372 /* Avoid runtime division; lookup cutoff and limit. */
373 cutoff = cutoff_tab[base - 2];
374 cutlim = cutlim_tab[base - 2];
379 if (sizeof (long int) != sizeof (LONG int))
381 unsigned long int j = 0;
382 unsigned long int jmax = jmax_tab[base - 2];
384 for (;c != L_('\0'); c = *++s)
388 if (c >= L_('0') && c <= L_('9'))
390 #ifdef USE_NUMBER_GROUPING
391 # ifdef USE_WIDE_CHAR
392 else if (grouping && (wchar_t) c == thousands)
395 else if (thousands_len)
397 for (cnt = 0; cnt < thousands_len; ++cnt)
398 if (thousands[cnt] != s[cnt])
400 if (cnt == thousands_len)
402 s += thousands_len - 1;
406 c = TOUPPER (c) - L_('A') + 10;
412 else if (ISALPHA (c))
413 c = TOUPPER (c) - L_('A') + 10;
418 /* Note that we never can have an overflow. */
421 /* We have an overflow. Now use the long representation. */
422 i = (unsigned LONG int) j;
426 j = j * (unsigned long int) base + c;
429 i = (unsigned LONG int) j;
432 for (;c != L_('\0'); c = *++s)
436 if (c >= L_('0') && c <= L_('9'))
438 #ifdef USE_NUMBER_GROUPING
439 # ifdef USE_WIDE_CHAR
440 else if (grouping && (wchar_t) c == thousands)
443 else if (thousands_len)
445 for (cnt = 0; cnt < thousands_len; ++cnt)
446 if (thousands[cnt] != s[cnt])
448 if (cnt == thousands_len)
450 s += thousands_len - 1;
454 c = TOUPPER (c) - L_('A') + 10;
460 else if (ISALPHA (c))
461 c = TOUPPER (c) - L_('A') + 10;
466 /* Check for overflow. */
467 if (i > cutoff || (i == cutoff && c > cutlim))
472 i *= (unsigned LONG int) base;
477 /* Check if anything actually happened. */
481 /* Store in ENDPTR the address of one character
482 past the last character we converted. */
484 *endptr = (STRING_TYPE *) s;
487 /* Check for a value that is within the range of
488 `unsigned LONG int', but outside the range of `LONG int'. */
491 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
492 : (unsigned LONG int) STRTOL_LONG_MAX))
496 if (__glibc_unlikely (overflow))
498 __set_errno (ERANGE);
500 return STRTOL_ULONG_MAX;
502 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
506 /* Return the result of the appropriate sign. */
507 return negative ? -i : i;
510 /* We must handle a special case here: the base is 0 or 16 and the
511 first two characters are '0' and 'x', but the rest are no
512 hexadecimal digits. This is no error case. We return 0 and
513 ENDPTR points to the `x`. */
516 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
517 && save[-2] == L_('0'))
518 *endptr = (STRING_TYPE *) &save[-1];
520 /* There was no number to convert. */
521 *endptr = (STRING_TYPE *) nptr;
526 #if defined _LIBC && !defined USE_WIDE_CHAR
527 libc_hidden_def (INTERNAL (__strtol_l))
530 /* External user entry point. */
535 extern INT __strtol_l (const STRING_TYPE *nptr, STRING_TYPE **endptr,
544 __strtol_l (const STRING_TYPE *nptr, STRING_TYPE **endptr,
545 int base, locale_t loc)
547 return INTERNAL (__strtol_l) (nptr, endptr, base, 0, loc);
549 libc_hidden_def (__strtol_l)
550 weak_alias (__strtol_l, strtol_l)