1 /* Convert string representation of a number into an integer value.
3 Copyright (C) 1991-1992, 1994-1999, 2003, 2005-2007, 2009-2020 Free Software
6 NOTE: The canonical source of this file is maintained with the GNU C
7 Library. Bugs can be reported to bug-glibc@gnu.org.
9 This program is free software: you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>. */
23 # define USE_NUMBER_GROUPING
31 # define __set_errno(Val) errno = (Val)
39 #ifdef USE_NUMBER_GROUPING
40 # include "../locale/localeinfo.h"
43 /* Nonzero if we are defining 'strtoul' or 'strtoull', operating on
49 # define INT unsigned LONG int
52 /* Determine the name. */
53 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
57 # define strtol __wcstoull_l
59 # define strtol __wcstoul_l
63 # define strtol __strtoull_l
65 # define strtol __strtoul_l
71 # define strtol __wcstoll_l
73 # define strtol __wcstol_l
77 # define strtol __strtoll_l
79 # define strtol __strtol_l
87 # define strtol wcstoull
89 # define strtol wcstoul
93 # define strtol strtoull
95 # define strtol strtoul
101 # define strtol wcstoll
103 # define strtol wcstol
107 # define strtol strtoll
113 /* If QUAD is defined, we are defining 'strtoll' or 'strtoull',
114 operating on 'long long int's. */
116 # define LONG long long
117 # define STRTOL_LONG_MIN LLONG_MIN
118 # define STRTOL_LONG_MAX LLONG_MAX
119 # define STRTOL_ULONG_MAX ULLONG_MAX
120 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
121 /* Work around gcc bug with using this constant. */
122 static const unsigned long long int maxquad
= ULLONG_MAX
;
123 # undef STRTOL_ULONG_MAX
124 # define STRTOL_ULONG_MAX maxquad
128 # define STRTOL_LONG_MIN LONG_MIN
129 # define STRTOL_LONG_MAX LONG_MAX
130 # define STRTOL_ULONG_MAX ULONG_MAX
134 /* We use this code also for the extended locale handling where the
135 function gets as an additional argument the locale which has to be
136 used. To access the values we have to redefine the _NL_CURRENT
138 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
140 # define _NL_CURRENT(category, item) \
141 (current->values[_NL_ITEM_INDEX (item)].string)
142 # define LOCALE_PARAM , loc
143 # define LOCALE_PARAM_PROTO , __locale_t loc
145 # define LOCALE_PARAM
146 # define LOCALE_PARAM_PROTO
152 # define L_(Ch) L##Ch
153 # define UCHAR_TYPE wint_t
154 # define STRING_TYPE wchar_t
155 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
156 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
157 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
158 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
160 # define ISSPACE(Ch) iswspace (Ch)
161 # define ISALPHA(Ch) iswalpha (Ch)
162 # define TOUPPER(Ch) towupper (Ch)
166 # define UCHAR_TYPE unsigned char
167 # define STRING_TYPE char
168 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
169 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
170 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
171 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
173 # define ISSPACE(Ch) isspace (Ch)
174 # define ISALPHA(Ch) isalpha (Ch)
175 # define TOUPPER(Ch) toupper (Ch)
179 #define INTERNAL(X) INTERNAL1(X)
180 #define INTERNAL1(X) __##X##_internal
181 #define WEAKNAME(X) WEAKNAME1(X)
183 #ifdef USE_NUMBER_GROUPING
184 /* This file defines a function to check for correct grouping. */
185 # include "grouping.h"
190 /* Convert NPTR to an 'unsigned long int' or 'long int' in base BASE.
191 If BASE is 0 the base is determined by the presence of a leading
192 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
193 If BASE is < 2 or > 36, it is reset to 10.
194 If ENDPTR is not NULL, a pointer to the character after the last
195 one converted is stored in *ENDPTR. */
198 INTERNAL (strtol
) (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
199 int base
, int group LOCALE_PARAM_PROTO
)
202 register unsigned LONG
int cutoff
;
203 register unsigned int cutlim
;
204 register unsigned LONG
int i
;
205 register const STRING_TYPE
*s
;
206 register UCHAR_TYPE c
;
207 const STRING_TYPE
*save
, *end
;
210 #ifdef USE_NUMBER_GROUPING
211 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
212 struct locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
214 /* The thousands character of the current locale. */
215 wchar_t thousands
= L
'\0';
216 /* The numeric grouping specification of the current locale,
217 in the format described in <locale.h>. */
218 const char *grouping
;
222 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
223 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
227 /* Figure out the thousands separator character. */
228 # if defined _LIBC || defined _HAVE_BTOWC
229 thousands
= __btowc (*_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
));
230 if (thousands
== WEOF
)
233 if (thousands
== L
'\0')
241 if (base
< 0 || base
== 1 || base
> 36)
243 __set_errno (EINVAL
);
249 /* Skip white space. */
255 /* Check for a sign. */
261 else if (*s
== L_('+'))
269 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
272 if ((base
== 0 || base
== 16) && TOUPPER (s
[1]) == L_('X'))
283 /* Save the pointer so we can check later if anything happened. */
286 #ifdef USE_NUMBER_GROUPING
289 /* Find the end of the digit string and check its grouping. */
291 for (c
= *end
; c
!= L_('\0'); c
= *++end
)
292 if ((wchar_t) c
!= thousands
293 && ((wchar_t) c
< L_('0') || (wchar_t) c
> L_('9'))
294 && (!ISALPHA (c
) || (int) (TOUPPER (c
) - L_('A') + 10) >= base
))
299 end
= correctly_grouped_prefix (s
, end
, thousands
, grouping
);
305 cutoff
= STRTOL_ULONG_MAX
/ (unsigned LONG
int) base
;
306 cutlim
= STRTOL_ULONG_MAX
% (unsigned LONG
int) base
;
310 for (c
= *s
; c
!= L_('\0'); c
= *++s
)
314 if (c
>= L_('0') && c
<= L_('9'))
316 else if (ISALPHA (c
))
317 c
= TOUPPER (c
) - L_('A') + 10;
322 /* Check for overflow. */
323 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
327 i
*= (unsigned LONG
int) base
;
332 /* Check if anything actually happened. */
336 /* Store in ENDPTR the address of one character
337 past the last character we converted. */
339 *endptr
= (STRING_TYPE
*) s
;
342 /* Check for a value that is within the range of
343 'unsigned LONG int', but outside the range of 'LONG int'. */
346 ? -((unsigned LONG
int) (STRTOL_LONG_MIN
+ 1)) + 1
347 : (unsigned LONG
int) STRTOL_LONG_MAX
))
353 __set_errno (ERANGE
);
355 return STRTOL_ULONG_MAX
;
357 return negative
? STRTOL_LONG_MIN
: STRTOL_LONG_MAX
;
361 /* Return the result of the appropriate sign. */
362 return negative
? -i
: i
;
365 /* We must handle a special case here: the base is 0 or 16 and the
366 first two characters are '0' and 'x', but the rest are no
367 hexadecimal digits. This is no error case. We return 0 and
368 ENDPTR points to the 'x'. */
371 if (save
- nptr
>= 2 && TOUPPER (save
[-1]) == L_('X')
372 && save
[-2] == L_('0'))
373 *endptr
= (STRING_TYPE
*) &save
[-1];
375 /* There was no number to convert. */
376 *endptr
= (STRING_TYPE
*) nptr
;
382 /* External user entry point. */
389 strtol (const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
390 int base LOCALE_PARAM_PROTO
)
392 return INTERNAL (strtol
) (nptr
, endptr
, base
, 0 LOCALE_PARAM
);