1 /* Convert string representing a number to integer value, using given locale.
2 Copyright (C) 1997, 2002, 2004 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 # 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
68 # define strtol_l wcstoul_l
72 # define strtol_l strtoull_l
74 # define strtol_l strtoul_l
80 # define strtol_l wcstoll_l
82 # define strtol_l wcstol_l
86 # define strtol_l strtoll_l
88 # define strtol_l strtol_l
93 #define __strtol_l __strtol_l2(strtol_l)
94 #define __strtol_l2(name) __strtol_l3(name)
95 #define __strtol_l3(name) __##name
98 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
99 operating on `long long int's. */
101 # define LONG long long
102 # define STRTOL_LONG_MIN LONG_LONG_MIN
103 # define STRTOL_LONG_MAX LONG_LONG_MAX
104 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
109 # define ULONG_MAX ((unsigned long int) ~(unsigned long int) 0)
112 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
114 # define STRTOL_LONG_MIN LONG_MIN
115 # define STRTOL_LONG_MAX LONG_MAX
116 # define STRTOL_ULONG_MAX ULONG_MAX
120 /* We use this code for the extended locale handling where the
121 function gets as an additional argument the locale which has to be
122 used. To access the values we have to redefine the _NL_CURRENT and
123 _NL_CURRENT_WORD macros. */
125 #define _NL_CURRENT(category, item) \
126 (current->values[_NL_ITEM_INDEX (item)].string)
127 #undef _NL_CURRENT_WORD
128 #define _NL_CURRENT_WORD(category, item) \
129 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
131 #if defined _LIBC || defined HAVE_WCHAR_H
137 # define L_(Ch) L##Ch
138 # define UCHAR_TYPE wint_t
139 # define STRING_TYPE wchar_t
140 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
141 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
142 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
145 || defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
146 # define IN_CTYPE_DOMAIN(c) 1
148 # define IN_CTYPE_DOMAIN(c) isascii(c)
151 # define UCHAR_TYPE unsigned char
152 # define STRING_TYPE char
153 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
154 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
155 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
158 #define INTERNAL(X) INTERNAL1(X)
159 #define INTERNAL1(X) __##X##_internal
160 #define WEAKNAME(X) WEAKNAME1(X)
162 #ifdef USE_NUMBER_GROUPING
163 /* This file defines a function to check for correct grouping. */
164 # include "grouping.h"
168 /* Define tables of maximum values and remainders in order to detect
169 overflow. Do this at compile-time in order to avoid the runtime
170 overhead of the division. */
172 #define DEF(TYPE, NAME) \
173 const TYPE NAME[] attribute_hidden \
174 __attribute__((section(".gnu.linkonce.r." #NAME))) = \
176 F(2), F(3), F(4), F(5), F(6), F(7), F(8), F(9), F(10), \
177 F(11), F(12), F(13), F(14), F(15), F(16), F(17), F(18), F(19), F(20), \
178 F(21), F(22), F(23), F(24), F(25), F(26), F(27), F(28), F(29), F(30), \
179 F(31), F(32), F(33), F(34), F(35), F(36) \
182 #define F(X) ULONG_MAX / X
183 DEF (unsigned long, __strtol_ul_max_tab
);
185 #if defined(QUAD) && __WORDSIZE == 32
186 # define F(X) ULONG_LONG_MAX / X
187 DEF (unsigned long long, __strtol_ull_max_tab
);
189 # define F(X) ULONG_LONG_MAX % X
190 DEF (unsigned char, __strtol_ull_rem_tab
);
193 # define F(X) ULONG_MAX % X
194 DEF (unsigned char, __strtol_ul_rem_tab
);
199 /* Define some more readable aliases for these arrays which correspond
200 to how they'll be used in the function below. */
201 #define jmax_tab __strtol_ul_max_tab
202 #if defined(QUAD) && __WORDSIZE == 32
203 # define cutoff_tab __strtol_ull_max_tab
204 # define cutlim_tab __strtol_ull_rem_tab
206 # define cutoff_tab __strtol_ul_max_tab
207 # define cutlim_tab __strtol_ul_rem_tab
211 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
212 If BASE is 0 the base is determined by the presence of a leading
213 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
214 If BASE is < 2 or > 36, it is reset to 10.
215 If ENDPTR is not NULL, a pointer to the character after the last
216 one converted is stored in *ENDPTR. */
219 INTERNAL (__strtol_l
) (nptr
, endptr
, base
, group
, loc
)
220 const STRING_TYPE
*nptr
;
221 STRING_TYPE
**endptr
;
227 register unsigned LONG
int cutoff
;
228 register unsigned int cutlim
;
229 register unsigned LONG
int i
;
230 register const STRING_TYPE
*s
;
231 register UCHAR_TYPE c
;
232 const STRING_TYPE
*save
, *end
;
234 #ifndef USE_WIDE_CHAR
238 #ifdef USE_NUMBER_GROUPING
239 struct locale_data
*current
= loc
->__locales
[LC_NUMERIC
];
240 /* The thousands character of the current locale. */
241 # ifdef USE_WIDE_CHAR
242 wchar_t thousands
= L
'\0';
244 const char *thousands
= NULL
;
245 size_t thousands_len
= 0;
247 /* The numeric grouping specification of the current locale,
248 in the format described in <locale.h>. */
249 const char *grouping
;
251 if (__builtin_expect (group
, 0))
253 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
254 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
258 /* Figure out the thousands separator character. */
259 # ifdef USE_WIDE_CHAR
261 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
262 _NL_NUMERIC_THOUSANDS_SEP_WC
);
264 if (thousands
== L
'\0')
268 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
270 if (*thousands
== '\0')
282 if (base
< 0 || base
== 1 || base
> 36)
284 __set_errno (EINVAL
);
290 /* Skip white space. */
293 if (__builtin_expect (*s
== L_('\0'), 0))
296 /* Check for a sign. */
303 else if (*s
== L_('+'))
306 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
309 if ((base
== 0 || base
== 16) && TOUPPER (s
[1]) == L_('X'))
320 /* Save the pointer so we can check later if anything happened. */
323 #ifdef USE_NUMBER_GROUPING
327 if (__builtin_expect (grouping
!= NULL
, 0))
329 # ifndef USE_WIDE_CHAR
330 thousands_len
= strlen (thousands
);
333 /* Find the end of the digit string and check its grouping. */
336 # ifdef USE_WIDE_CHAR
339 ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
340 if (thousands
[cnt
] != end
[cnt
])
342 cnt
< thousands_len
; })
346 for (c
= *end
; c
!= L_('\0'); c
= *++end
)
347 if (((STRING_TYPE
) c
< L_('0') || (STRING_TYPE
) c
> L_('9'))
348 # ifdef USE_WIDE_CHAR
349 && (wchar_t) c
!= thousands
351 && ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
352 if (thousands
[cnt
] != end
[cnt
])
354 cnt
< thousands_len
; })
357 || (int) (TOUPPER (c
) - L_('A') + 10) >= base
))
360 # ifdef USE_WIDE_CHAR
361 end
= __correctly_grouped_prefixwc (s
, end
, thousands
, grouping
);
363 end
= __correctly_grouped_prefixmb (s
, end
, thousands
, grouping
);
371 /* Avoid runtime division; lookup cutoff and limit. */
372 cutoff
= cutoff_tab
[base
- 2];
373 cutlim
= cutlim_tab
[base
- 2];
378 if (sizeof (long int) != sizeof (LONG
int))
380 unsigned long int j
= 0;
381 unsigned long int jmax
= jmax_tab
[base
- 2];
383 for (;c
!= L_('\0'); c
= *++s
)
387 if (c
>= L_('0') && c
<= L_('9'))
389 #ifdef USE_NUMBER_GROUPING
390 # ifdef USE_WIDE_CHAR
391 else if (grouping
&& (wchar_t) c
== thousands
)
394 else if (thousands_len
)
396 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
397 if (thousands
[cnt
] != s
[cnt
])
399 if (cnt
== thousands_len
)
401 s
+= thousands_len
- 1;
405 c
= TOUPPER (c
) - L_('A') + 10;
411 else if (ISALPHA (c
))
412 c
= TOUPPER (c
) - L_('A') + 10;
417 /* Note that we never can have an overflow. */
420 /* We have an overflow. Now use the long representation. */
421 i
= (unsigned LONG
int) j
;
425 j
= j
* (unsigned long int) base
+ c
;
428 i
= (unsigned LONG
int) j
;
431 for (;c
!= L_('\0'); c
= *++s
)
435 if (c
>= L_('0') && c
<= L_('9'))
437 #ifdef USE_NUMBER_GROUPING
438 # ifdef USE_WIDE_CHAR
439 else if (grouping
&& (wchar_t) c
== thousands
)
442 else if (thousands_len
)
444 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
445 if (thousands
[cnt
] != s
[cnt
])
447 if (cnt
== thousands_len
)
449 s
+= thousands_len
- 1;
453 c
= TOUPPER (c
) - L_('A') + 10;
459 else if (ISALPHA (c
))
460 c
= TOUPPER (c
) - L_('A') + 10;
465 /* Check for overflow. */
466 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
471 i
*= (unsigned LONG
int) base
;
476 /* Check if anything actually happened. */
480 /* Store in ENDPTR the address of one character
481 past the last character we converted. */
483 *endptr
= (STRING_TYPE
*) s
;
486 /* Check for a value that is within the range of
487 `unsigned LONG int', but outside the range of `LONG int'. */
490 ? -((unsigned LONG
int) (STRTOL_LONG_MIN
+ 1)) + 1
491 : (unsigned LONG
int) STRTOL_LONG_MAX
))
495 if (__builtin_expect (overflow
, 0))
497 __set_errno (ERANGE
);
499 return STRTOL_ULONG_MAX
;
501 return negative
? STRTOL_LONG_MIN
: STRTOL_LONG_MAX
;
505 /* Return the result of the appropriate sign. */
506 return negative
? -i
: i
;
509 /* We must handle a special case here: the base is 0 or 16 and the
510 first two characters are '0' and 'x', but the rest are no
511 hexadecimal digits. This is no error case. We return 0 and
512 ENDPTR points to the `x`. */
515 if (save
- nptr
>= 2 && TOUPPER (save
[-1]) == L_('X')
516 && save
[-2] == L_('0'))
517 *endptr
= (STRING_TYPE
*) &save
[-1];
519 /* There was no number to convert. */
520 *endptr
= (STRING_TYPE
*) nptr
;
525 #if defined _LIBC && !defined USE_WIDE_CHAR
526 libc_hidden_def (INTERNAL (__strtol_l
))
529 /* External user entry point. */
533 # if defined (__STDC__) && __STDC__
534 # define PARAMS(Args) Args
536 # define PARAMS(Args) ()
540 extern INT __strtol_l
PARAMS ((const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
549 __strtol_l (nptr
, endptr
, base
, loc
)
550 const STRING_TYPE
*nptr
;
551 STRING_TYPE
**endptr
;
555 return INTERNAL (__strtol_l
) (nptr
, endptr
, base
, 0, loc
);
557 weak_alias (__strtol_l
, strtol_l
)