1 /* Convert string representation of a number into an integer value.
2 Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
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)
191 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
192 # define IN_CTYPE_DOMAIN(c) 1
194 # define IN_CTYPE_DOMAIN(c) isascii(c)
197 # define UCHAR_TYPE unsigned char
198 # define STRING_TYPE char
199 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
200 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
201 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
202 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
204 # define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
205 # define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
206 # define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
211 # define INTERNAL(X) INTERNAL1(X)
212 # define INTERNAL1(X) __##X##_internal
213 # define WEAKNAME(X) WEAKNAME1(X)
215 # define INTERNAL(X) __/**/X/**/_internal
218 #ifdef USE_NUMBER_GROUPING
219 /* This file defines a function to check for correct grouping. */
220 # include "grouping.h"
225 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
226 If BASE is 0 the base is determined by the presence of a leading
227 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
228 If BASE is < 2 or > 36, it is reset to 10.
229 If ENDPTR is not NULL, a pointer to the character after the last
230 one converted is stored in *ENDPTR. */
233 INTERNAL (strtol
) (nptr
, endptr
, base
, group LOCALE_PARAM
)
234 const STRING_TYPE
*nptr
;
235 STRING_TYPE
**endptr
;
241 register unsigned LONG
int cutoff
;
242 register unsigned int cutlim
;
243 register unsigned LONG
int i
;
244 register const STRING_TYPE
*s
;
245 register UCHAR_TYPE c
;
246 const STRING_TYPE
*save
, *end
;
248 #ifndef USE_WIDE_CHAR
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 # ifdef USE_WIDE_CHAR
258 wchar_t thousands
= L
'\0';
260 const char *thousands
= NULL
;
261 size_t thousands_len
= 0;
263 /* The numeric grouping specification of the current locale,
264 in the format described in <locale.h>. */
265 const char *grouping
;
267 if (__builtin_expect (group
, 0))
269 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
270 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
274 /* Figure out the thousands separator character. */
275 # ifdef USE_WIDE_CHAR
277 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
278 _NL_NUMERIC_THOUSANDS_SEP_WC
);
280 if (thousands
== L
'\0')
284 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
286 if (*thousands
== '\0')
298 if (base
< 0 || base
== 1 || base
> 36)
300 __set_errno (EINVAL
);
306 /* Skip white space. */
309 if (__builtin_expect (*s
== L_('\0'), 0))
312 /* Check for a sign. */
319 else if (*s
== L_('+'))
322 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
325 if ((base
== 0 || base
== 16) && TOUPPER (s
[1]) == L_('X'))
336 /* Save the pointer so we can check later if anything happened. */
339 #ifdef USE_NUMBER_GROUPING
343 if (__builtin_expect (grouping
!= NULL
, 0))
345 # ifndef USE_WIDE_CHAR
346 thousands_len
= strlen (thousands
);
349 /* Find the end of the digit string and check its grouping. */
352 # ifdef USE_WIDE_CHAR
355 ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
356 if (thousands
[cnt
] != end
[cnt
])
358 cnt
< thousands_len
; })
362 for (c
= *end
; c
!= L_('\0'); c
= *++end
)
363 if (((wchar_t) c
< L_('0') || (wchar_t) c
> L_('9'))
364 # ifdef USE_WIDE_CHAR
367 && ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
368 if (thousands
[cnt
] != end
[cnt
])
370 cnt
< thousands_len
; })
373 || (int) (TOUPPER (c
) - L_('A') + 10) >= base
))
376 end
= correctly_grouped_prefix (s
, end
, thousands
, grouping
);
383 cutoff
= STRTOL_ULONG_MAX
/ (unsigned LONG
int) base
;
384 cutlim
= STRTOL_ULONG_MAX
% (unsigned LONG
int) base
;
389 if (sizeof (long int) != sizeof (LONG
int))
391 unsigned long int j
= 0;
392 unsigned long int jmax
= ULONG_MAX
/ base
;
394 for (;c
!= L_('\0'); c
= *++s
)
398 if (c
>= L_('0') && c
<= L_('9'))
400 #ifdef USE_NUMBER_GROUPING
401 # ifdef USE_WIDE_CHAR
402 else if (grouping
&& c
== thousands
)
405 else if (thousands_len
)
407 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
408 if (thousands
[cnt
] != s
[cnt
])
410 if (cnt
== thousands_len
)
412 s
+= thousands_len
- 1;
416 c
= TOUPPER (c
) - L_('A') + 10;
422 else if (ISALPHA (c
))
423 c
= TOUPPER (c
) - L_('A') + 10;
428 /* Note that we never can have an overflow. */
431 /* We have an overflow. Now use the long representation. */
432 i
= (unsigned LONG
int) j
;
436 j
= j
* (unsigned long int) base
+ c
;
439 i
= (unsigned LONG
int) j
;
442 for (;c
!= L_('\0'); c
= *++s
)
446 if (c
>= L_('0') && c
<= L_('9'))
448 #ifdef USE_NUMBER_GROUPING
449 # ifdef USE_WIDE_CHAR
450 else if (grouping
&& c
== thousands
)
453 else if (thousands_len
)
455 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
456 if (thousands
[cnt
] != s
[cnt
])
458 if (cnt
== thousands_len
)
460 s
+= thousands_len
- 1;
464 c
= TOUPPER (c
) - L_('A') + 10;
470 else if (ISALPHA (c
))
471 c
= TOUPPER (c
) - L_('A') + 10;
476 /* Check for overflow. */
477 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
482 i
*= (unsigned LONG
int) base
;
487 /* Check if anything actually happened. */
491 /* Store in ENDPTR the address of one character
492 past the last character we converted. */
494 *endptr
= (STRING_TYPE
*) s
;
497 /* Check for a value that is within the range of
498 `unsigned LONG int', but outside the range of `LONG int'. */
501 ? -((unsigned LONG
int) (STRTOL_LONG_MIN
+ 1)) + 1
502 : (unsigned LONG
int) STRTOL_LONG_MAX
))
506 if (__builtin_expect (overflow
, 0))
508 __set_errno (ERANGE
);
510 return STRTOL_ULONG_MAX
;
512 return negative
? STRTOL_LONG_MIN
: STRTOL_LONG_MAX
;
516 /* Return the result of the appropriate sign. */
517 return negative
? -i
: i
;
520 /* We must handle a special case here: the base is 0 or 16 and the
521 first two characters are '0' and 'x', but the rest are no
522 hexadecimal digits. This is no error case. We return 0 and
523 ENDPTR points to the `x`. */
526 if (save
- nptr
>= 2 && TOUPPER (save
[-1]) == L_('X')
527 && save
[-2] == L_('0'))
528 *endptr
= (STRING_TYPE
*) &save
[-1];
530 /* There was no number to convert. */
531 *endptr
= (STRING_TYPE
*) nptr
;
537 && !(defined USE_IN_EXTENDED_LOCALE_MODEL && defined USE_WIDE_CHAR)
538 libc_hidden_def (INTERNAL (strtol
))
541 /* External user entry point. */
545 # if defined (__STDC__) && __STDC__
546 # define PARAMS(Args) Args
548 # define PARAMS(Args) ()
552 INT strtol
PARAMS ((const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, int base
));
560 strtol (nptr
, endptr
, base LOCALE_PARAM
)
561 const STRING_TYPE
*nptr
;
562 STRING_TYPE
**endptr
;
566 return INTERNAL (strtol
) (nptr
, endptr
, base
, 0 LOCALE_PARAM
);