1 /* Convert string representation of a number into an integer value.
2 Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 # define USE_NUMBER_GROUPING
27 # define HAVE_LIMITS_H
48 #ifdef USE_NUMBER_GROUPING
49 # include "../locale/localeinfo.h"
52 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
58 # define INT unsigned LONG int
61 /* Determine the name. */
62 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
66 # define strtol __wcstoull_l
68 # define strtol __wcstoul_l
72 # define strtol __strtoull_l
74 # define strtol __strtoul_l
80 # define strtol __wcstoll_l
82 # define strtol __wcstol_l
86 # define strtol __strtoll_l
88 # define strtol __strtol_l
96 # define strtol wcstoull
98 # define strtol wcstoul
102 # define strtol strtoull
104 # define strtol strtoul
108 # ifdef USE_WIDE_CHAR
110 # define strtol wcstoll
112 # define strtol wcstol
116 # define strtol strtoll
122 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
123 operating on `long long int's. */
125 # define LONG long long
126 # define STRTOL_LONG_MIN LONG_LONG_MIN
127 # define STRTOL_LONG_MAX LONG_LONG_MAX
128 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
129 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
130 /* Work around gcc bug with using this constant. */
131 static const unsigned long long int maxquad
= ULONG_LONG_MAX
;
132 # undef STRTOL_ULONG_MAX
133 # define STRTOL_ULONG_MAX maxquad
139 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
142 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
144 # define STRTOL_LONG_MIN LONG_MIN
145 # define STRTOL_LONG_MAX LONG_MAX
146 # define STRTOL_ULONG_MAX ULONG_MAX
150 /* We use this code also for the extended locale handling where the
151 function gets as an additional argument the locale which has to be
152 used. To access the values we have to redefine the _NL_CURRENT
154 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
156 # define _NL_CURRENT(category, item) \
157 (current->values[_NL_ITEM_INDEX (item)].string)
158 # define LOCALE_PARAM , loc
159 # define LOCALE_PARAM_DECL __locale_t loc;
161 # define LOCALE_PARAM
162 # define LOCALE_PARAM_DECL
165 #if defined _LIBC || defined HAVE_WCHAR_H
171 # define L_(Ch) L##Ch
172 # define UCHAR_TYPE wint_t
173 # define STRING_TYPE wchar_t
174 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
175 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
176 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
177 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
179 # define ISSPACE(Ch) iswspace (Ch)
180 # define ISALPHA(Ch) iswalpha (Ch)
181 # define TOUPPER(Ch) towupper (Ch)
184 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
185 # define IN_CTYPE_DOMAIN(c) 1
187 # define IN_CTYPE_DOMAIN(c) isascii(c)
190 # define UCHAR_TYPE unsigned char
191 # define STRING_TYPE char
192 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
193 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
194 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
195 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
197 # define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
198 # define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
199 # define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
204 # define INTERNAL(X) INTERNAL1(X)
205 # define INTERNAL1(X) __##X##_internal
206 # define WEAKNAME(X) WEAKNAME1(X)
208 # define INTERNAL(X) __/**/X/**/_internal
211 #ifdef USE_NUMBER_GROUPING
212 /* This file defines a function to check for correct grouping. */
213 # include "grouping.h"
218 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
219 If BASE is 0 the base is determined by the presence of a leading
220 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
221 If BASE is < 2 or > 36, it is reset to 10.
222 If ENDPTR is not NULL, a pointer to the character after the last
223 one converted is stored in *ENDPTR. */
226 INTERNAL (strtol
) (nptr
, endptr
, base
, group LOCALE_PARAM
)
227 const STRING_TYPE
*nptr
;
228 STRING_TYPE
**endptr
;
234 register unsigned LONG
int cutoff
;
235 register unsigned int cutlim
;
236 register unsigned LONG
int i
;
237 register const STRING_TYPE
*s
;
238 register UCHAR_TYPE c
;
239 const STRING_TYPE
*save
, *end
;
241 #ifndef USE_WIDE_CHAR
245 #ifdef USE_NUMBER_GROUPING
246 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
247 struct locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
249 /* The thousands character of the current locale. */
250 # ifdef USE_WIDE_CHAR
251 wchar_t thousands
= L
'\0';
253 const char *thousands
= NULL
;
254 size_t thousands_len
= 0;
256 /* The numeric grouping specification of the current locale,
257 in the format described in <locale.h>. */
258 const char *grouping
;
260 if (__builtin_expect (group
, 0))
262 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
263 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
267 /* Figure out the thousands separator character. */
268 # ifdef USE_WIDE_CHAR
270 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
271 _NL_NUMERIC_THOUSANDS_SEP_WC
);
273 if (thousands
== L
'\0')
277 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
279 if (*thousands
== '\0')
291 if (base
< 0 || base
== 1 || base
> 36)
293 __set_errno (EINVAL
);
299 /* Skip white space. */
302 if (__builtin_expect (*s
== L_('\0'), 0))
305 /* Check for a sign. */
312 else if (*s
== L_('+'))
315 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
318 if ((base
== 0 || base
== 16) && TOUPPER (s
[1]) == L_('X'))
329 /* Save the pointer so we can check later if anything happened. */
332 #ifdef USE_NUMBER_GROUPING
336 if (__builtin_expect (grouping
!= NULL
, 0))
338 # ifndef USE_WIDE_CHAR
339 thousands_len
= strlen (thousands
);
342 /* Find the end of the digit string and check its grouping. */
345 # ifdef USE_WIDE_CHAR
348 ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
349 if (thousands
[cnt
] != end
[cnt
])
351 cnt
< thousands_len
; })
355 for (c
= *end
; c
!= L_('\0'); c
= *++end
)
356 if (((wchar_t) c
< L_('0') || (wchar_t) c
> L_('9'))
357 # ifdef USE_WIDE_CHAR
360 && ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
361 if (thousands
[cnt
] != end
[cnt
])
363 cnt
< thousands_len
; })
366 || (int) (TOUPPER (c
) - L_('A') + 10) >= base
))
369 end
= correctly_grouped_prefix (s
, end
, thousands
, grouping
);
376 cutoff
= STRTOL_ULONG_MAX
/ (unsigned LONG
int) base
;
377 cutlim
= STRTOL_ULONG_MAX
% (unsigned LONG
int) base
;
382 if (sizeof (long int) != sizeof (LONG
int))
384 unsigned long int j
= 0;
385 unsigned long int jmax
= ULONG_MAX
/ base
;
387 for (;c
!= L_('\0'); c
= *++s
)
391 if (c
>= L_('0') && c
<= L_('9'))
393 #ifdef USE_NUMBER_GROUPING
394 # ifdef USE_WIDE_CHAR
395 else if (grouping
&& c
== thousands
)
398 else if (thousands_len
)
400 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
401 if (thousands
[cnt
] != s
[cnt
])
403 if (cnt
== thousands_len
)
405 s
+= thousands_len
- 1;
409 c
= TOUPPER (c
) - L_('A') + 10;
415 else if (ISALPHA (c
))
416 c
= TOUPPER (c
) - L_('A') + 10;
421 /* Note that we never can have an overflow. */
424 /* We have an overflow. Now use the long representation. */
425 i
= (unsigned LONG
int) j
;
429 j
= j
* (unsigned long int) base
+ c
;
432 i
= (unsigned LONG
int) j
;
435 for (;c
!= L_('\0'); c
= *++s
)
439 if (c
>= L_('0') && c
<= L_('9'))
441 #ifdef USE_NUMBER_GROUPING
442 # ifdef USE_WIDE_CHAR
443 else if (grouping
&& c
== thousands
)
446 else if (thousands_len
)
448 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
449 if (thousands
[cnt
] != s
[cnt
])
451 if (cnt
== thousands_len
)
453 s
+= thousands_len
- 1;
457 c
= TOUPPER (c
) - L_('A') + 10;
463 else if (ISALPHA (c
))
464 c
= TOUPPER (c
) - L_('A') + 10;
469 /* Check for overflow. */
470 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
475 i
*= (unsigned LONG
int) base
;
480 /* Check if anything actually happened. */
484 /* Store in ENDPTR the address of one character
485 past the last character we converted. */
487 *endptr
= (STRING_TYPE
*) s
;
490 /* Check for a value that is within the range of
491 `unsigned LONG int', but outside the range of `LONG int'. */
494 ? -((unsigned LONG
int) (STRTOL_LONG_MIN
+ 1)) + 1
495 : (unsigned LONG
int) STRTOL_LONG_MAX
))
499 if (__builtin_expect (overflow
, 0))
501 __set_errno (ERANGE
);
503 return STRTOL_ULONG_MAX
;
505 return negative
? STRTOL_LONG_MIN
: STRTOL_LONG_MAX
;
509 /* Return the result of the appropriate sign. */
510 return negative
? -i
: i
;
513 /* We must handle a special case here: the base is 0 or 16 and the
514 first two characters are '0' and 'x', but the rest are no
515 hexadecimal digits. This is no error case. We return 0 and
516 ENDPTR points to the `x`. */
519 if (save
- nptr
>= 2 && TOUPPER (save
[-1]) == L_('X')
520 && save
[-2] == L_('0'))
521 *endptr
= (STRING_TYPE
*) &save
[-1];
523 /* There was no number to convert. */
524 *endptr
= (STRING_TYPE
*) nptr
;
530 /* External user entry point. */
534 # if defined (__STDC__) && __STDC__
535 # define PARAMS(Args) Args
537 # define PARAMS(Args) ()
541 INT strtol
PARAMS ((const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, int base
));
549 strtol (nptr
, endptr
, base LOCALE_PARAM
)
550 const STRING_TYPE
*nptr
;
551 STRING_TYPE
**endptr
;
555 return INTERNAL (strtol
) (nptr
, endptr
, base
, 0 LOCALE_PARAM
);