1 /* Convert string representing a number to integer value, using given locale.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 #undef __GLIBC_USE_C2X_STRTOL
21 #define __GLIBC_USE_C2X_STRTOL 0
28 # define USE_NUMBER_GROUPING
29 # define HAVE_LIMITS_H
35 # define __set_errno(Val) errno = (Val)
47 #include <bits/wordsize.h>
49 #ifdef USE_NUMBER_GROUPING
50 # include "../locale/localeinfo.h"
53 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
59 # define INT unsigned LONG int
62 /* Determine the name. */
66 # define strtol_l wcstoull_l
67 # define __isoc23_strtol_l __isoc23_wcstoull_l
69 # define strtol_l wcstoul_l
70 # define __isoc23_strtol_l __isoc23_wcstoul_l
74 # define strtol_l strtoull_l
75 # define __isoc23_strtol_l __isoc23_strtoull_l
77 # define strtol_l strtoul_l
78 # define __isoc23_strtol_l __isoc23_strtoul_l
84 # define strtol_l wcstoll_l
85 # define __isoc23_strtol_l __isoc23_wcstoll_l
87 # define strtol_l wcstol_l
88 # define __isoc23_strtol_l __isoc23_wcstol_l
92 # define strtol_l strtoll_l
93 # define __isoc23_strtol_l __isoc23_strtoll_l
95 # define strtol_l strtol_l
96 # define __isoc23_strtol_l __isoc23_strtol_l
101 #define __strtol_l __strtol_l2(strtol_l)
102 #define __strtol_l2(name) __strtol_l3(name)
103 #define __strtol_l3(name) __##name
106 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
107 operating on `long long int's. */
109 # define LONG long long
110 # define STRTOL_LONG_MIN LONG_LONG_MIN
111 # define STRTOL_LONG_MAX LONG_LONG_MAX
112 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
117 # define ULONG_MAX ((unsigned long int) ~(unsigned long int) 0)
120 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
122 # define STRTOL_LONG_MIN LONG_MIN
123 # define STRTOL_LONG_MAX LONG_MAX
124 # define STRTOL_ULONG_MAX ULONG_MAX
128 /* We use this code for the extended locale handling where the
129 function gets as an additional argument the locale which has to be
130 used. To access the values we have to redefine the _NL_CURRENT and
131 _NL_CURRENT_WORD macros. */
133 #define _NL_CURRENT(category, item) \
134 (current->values[_NL_ITEM_INDEX (item)].string)
135 #undef _NL_CURRENT_WORD
136 #define _NL_CURRENT_WORD(category, item) \
137 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
139 #if defined _LIBC || defined HAVE_WCHAR_H
145 # define L_(Ch) L##Ch
146 # define UCHAR_TYPE wint_t
147 # define STRING_TYPE wchar_t
148 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
149 # define ISALPHA(Ch) __iswalpha_l ((Ch), _nl_C_locobj_ptr)
150 # define TOUPPER(Ch) __towupper_l ((Ch), _nl_C_locobj_ptr)
153 || defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
154 # define IN_CTYPE_DOMAIN(c) 1
156 # define IN_CTYPE_DOMAIN(c) isascii(c)
159 # define UCHAR_TYPE unsigned char
160 # define STRING_TYPE char
161 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
162 # define ISALPHA(Ch) __isalpha_l ((Ch), _nl_C_locobj_ptr)
163 # define TOUPPER(Ch) __toupper_l ((Ch), _nl_C_locobj_ptr)
166 #define INTERNAL(X) INTERNAL1(X)
167 #define INTERNAL1(X) __##X##_internal
168 #define WEAKNAME(X) WEAKNAME1(X)
170 #ifdef USE_NUMBER_GROUPING
171 /* This file defines a function to check for correct grouping. */
172 # include "grouping.h"
176 /* Define tables of maximum values and remainders in order to detect
177 overflow. Do this at compile-time in order to avoid the runtime
178 overhead of the division. */
179 extern const unsigned long __strtol_ul_max_tab
[] attribute_hidden
;
180 extern const unsigned char __strtol_ul_rem_tab
[] attribute_hidden
;
181 #if defined(QUAD) && __WORDSIZE == 32
182 extern const unsigned long long __strtol_ull_max_tab
[] attribute_hidden
;
183 extern const unsigned char __strtol_ull_rem_tab
[] attribute_hidden
;
186 #define DEF(TYPE, NAME) \
187 const TYPE NAME[] attribute_hidden = \
189 F(2), F(3), F(4), F(5), F(6), F(7), F(8), F(9), F(10), \
190 F(11), F(12), F(13), F(14), F(15), F(16), F(17), F(18), F(19), F(20), \
191 F(21), F(22), F(23), F(24), F(25), F(26), F(27), F(28), F(29), F(30), \
192 F(31), F(32), F(33), F(34), F(35), F(36) \
195 #if !UNSIGNED && !defined (USE_WIDE_CHAR) && !defined (QUAD)
196 # define F(X) ULONG_MAX / X
197 DEF (unsigned long, __strtol_ul_max_tab
);
199 # define F(X) ULONG_MAX % X
200 DEF (unsigned char, __strtol_ul_rem_tab
);
203 #if !UNSIGNED && !defined (USE_WIDE_CHAR) && defined (QUAD) \
205 # define F(X) ULONG_LONG_MAX / X
206 DEF (unsigned long long, __strtol_ull_max_tab
);
208 # define F(X) ULONG_LONG_MAX % X
209 DEF (unsigned char, __strtol_ull_rem_tab
);
214 /* Define some more readable aliases for these arrays which correspond
215 to how they'll be used in the function below. */
216 #define jmax_tab __strtol_ul_max_tab
217 #if defined(QUAD) && __WORDSIZE == 32
218 # define cutoff_tab __strtol_ull_max_tab
219 # define cutlim_tab __strtol_ull_rem_tab
221 # define cutoff_tab __strtol_ul_max_tab
222 # define cutlim_tab __strtol_ul_rem_tab
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 BIN_CST is true, binary constants starting "0b" or "0B" are accepted
232 If ENDPTR is not NULL, a pointer to the character after the last
233 one converted is stored in *ENDPTR. */
236 INTERNAL (__strtol_l
) (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
237 int base
, int group
, bool bin_cst
, locale_t loc
)
240 unsigned LONG
int cutoff
;
243 const STRING_TYPE
*s
;
245 const STRING_TYPE
*save
, *end
;
247 #ifndef USE_WIDE_CHAR
251 #ifdef USE_NUMBER_GROUPING
252 struct __locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
253 /* The thousands character of the current locale. */
254 # ifdef USE_WIDE_CHAR
255 wchar_t thousands
= L
'\0';
257 const char *thousands
= NULL
;
258 size_t thousands_len
= 0;
260 /* The numeric grouping specification of the current locale,
261 in the format described in <locale.h>. */
262 const char *grouping
;
264 if (__glibc_unlikely (group
))
266 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
267 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
271 /* Figure out the thousands separator character. */
272 # ifdef USE_WIDE_CHAR
274 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
275 _NL_NUMERIC_THOUSANDS_SEP_WC
);
277 if (thousands
== L
'\0')
281 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
283 if (*thousands
== '\0')
295 if (base
< 0 || base
== 1 || base
> 36)
297 __set_errno (EINVAL
);
303 /* Skip white space. */
306 if (__glibc_unlikely (*s
== L_('\0')))
309 /* Check for a sign. */
316 else if (*s
== L_('+'))
319 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
322 if ((base
== 0 || base
== 16) && TOUPPER (s
[1]) == L_('X'))
327 else if (bin_cst
&& (base
== 0 || base
== 2) && TOUPPER (s
[1]) == L_('B'))
338 /* Save the pointer so we can check later if anything happened. */
341 #ifdef USE_NUMBER_GROUPING
345 if (__glibc_unlikely (grouping
!= NULL
))
347 # ifndef USE_WIDE_CHAR
348 thousands_len
= strlen (thousands
);
351 /* Find the end of the digit string and check its grouping. */
354 # ifdef USE_WIDE_CHAR
357 ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
358 if (thousands
[cnt
] != end
[cnt
])
360 cnt
< thousands_len
; })
364 for (c
= *end
; c
!= L_('\0'); c
= *++end
)
365 if (((STRING_TYPE
) c
< L_('0') || (STRING_TYPE
) c
> L_('9'))
366 # ifdef USE_WIDE_CHAR
367 && (wchar_t) c
!= thousands
369 && ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
370 if (thousands
[cnt
] != end
[cnt
])
372 cnt
< thousands_len
; })
375 || (int) (TOUPPER (c
) - L_('A') + 10) >= base
))
378 # ifdef USE_WIDE_CHAR
379 end
= __correctly_grouped_prefixwc (s
, end
, thousands
, grouping
);
381 end
= __correctly_grouped_prefixmb (s
, end
, thousands
, grouping
);
389 /* Avoid runtime division; lookup cutoff and limit. */
390 cutoff
= cutoff_tab
[base
- 2];
391 cutlim
= cutlim_tab
[base
- 2];
396 if (sizeof (long int) != sizeof (LONG
int))
398 unsigned long int j
= 0;
399 unsigned long int jmax
= jmax_tab
[base
- 2];
401 for (;c
!= L_('\0'); c
= *++s
)
405 if (c
>= L_('0') && c
<= L_('9'))
407 #ifdef USE_NUMBER_GROUPING
408 # ifdef USE_WIDE_CHAR
409 else if (grouping
&& (wchar_t) c
== thousands
)
412 else if (thousands_len
)
414 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
415 if (thousands
[cnt
] != s
[cnt
])
417 if (cnt
== thousands_len
)
419 s
+= thousands_len
- 1;
423 c
= TOUPPER (c
) - L_('A') + 10;
429 else if (ISALPHA (c
))
430 c
= TOUPPER (c
) - L_('A') + 10;
435 /* Note that we never can have an overflow. */
438 /* We have an overflow. Now use the long representation. */
439 i
= (unsigned LONG
int) j
;
443 j
= j
* (unsigned long int) base
+ c
;
446 i
= (unsigned LONG
int) j
;
449 for (;c
!= L_('\0'); c
= *++s
)
453 if (c
>= L_('0') && c
<= L_('9'))
455 #ifdef USE_NUMBER_GROUPING
456 # ifdef USE_WIDE_CHAR
457 else if (grouping
&& (wchar_t) c
== thousands
)
460 else if (thousands_len
)
462 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
463 if (thousands
[cnt
] != s
[cnt
])
465 if (cnt
== thousands_len
)
467 s
+= thousands_len
- 1;
471 c
= TOUPPER (c
) - L_('A') + 10;
477 else if (ISALPHA (c
))
478 c
= TOUPPER (c
) - L_('A') + 10;
483 /* Check for overflow. */
484 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
489 i
*= (unsigned LONG
int) base
;
494 /* Check if anything actually happened. */
498 /* Store in ENDPTR the address of one character
499 past the last character we converted. */
501 *endptr
= (STRING_TYPE
*) s
;
504 /* Check for a value that is within the range of
505 `unsigned LONG int', but outside the range of `LONG int'. */
508 ? -((unsigned LONG
int) (STRTOL_LONG_MIN
+ 1)) + 1
509 : (unsigned LONG
int) STRTOL_LONG_MAX
))
513 if (__glibc_unlikely (overflow
))
515 __set_errno (ERANGE
);
517 return STRTOL_ULONG_MAX
;
519 return negative
? STRTOL_LONG_MIN
: STRTOL_LONG_MAX
;
523 /* Return the result of the appropriate sign. */
524 return negative
? -i
: i
;
527 /* We must handle a special case here: the base is 0 or 16 and the
528 first two characters are '0' and 'x', but the rest are no
529 hexadecimal digits. Likewise when the base is 0 or 2 and the
530 first two characters are '0' and 'b', but the rest are no binary
531 digits. This is no error case. We return 0 and ENDPTR points to
536 && (TOUPPER (save
[-1]) == L_('X')
537 || (bin_cst
&& TOUPPER (save
[-1]) == L_('B')))
538 && save
[-2] == L_('0'))
539 *endptr
= (STRING_TYPE
*) &save
[-1];
541 /* There was no number to convert. */
542 *endptr
= (STRING_TYPE
*) nptr
;
547 #if defined _LIBC && !defined USE_WIDE_CHAR
548 libc_hidden_def (INTERNAL (__strtol_l
))
551 /* External user entry point. */
556 extern INT
__strtol_l (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
565 __strtol_l (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
566 int base
, locale_t loc
)
568 return INTERNAL (__strtol_l
) (nptr
, endptr
, base
, 0, false, loc
);
570 libc_hidden_def (__strtol_l
)
571 weak_alias (__strtol_l
, strtol_l
)
574 __isoc23_strtol_l (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
575 int base
, locale_t loc
)
577 return INTERNAL (__strtol_l
) (nptr
, endptr
, base
, 0, true, loc
);
579 libc_hidden_def (__isoc23_strtol_l
)