BZ#11261 fixed by previous commit.
[glibc.git] / stdlib / strtol_l.c
blobdc6829f59cafffbd8762b0dcb12661e60cb86078
1 /* Convert string representing a number to integer value, using given locale.
2 Copyright (C) 1997-2013 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, see
18 <http://www.gnu.org/licenses/>. */
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #ifdef _LIBC
26 # define USE_NUMBER_GROUPING
27 # define STDC_HEADERS
28 # define HAVE_LIMITS_H
29 #endif
31 #include <ctype.h>
32 #include <errno.h>
33 #ifndef __set_errno
34 # define __set_errno(Val) errno = (Val)
35 #endif
37 #ifdef HAVE_LIMITS_H
38 # include <limits.h>
39 #endif
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <locale.h>
45 #include <xlocale.h>
46 #include <bits/wordsize.h>
48 #ifdef USE_NUMBER_GROUPING
49 # include "../locale/localeinfo.h"
50 #endif
52 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
53 unsigned integers. */
54 #ifndef UNSIGNED
55 # define UNSIGNED 0
56 # define INT LONG int
57 #else
58 # define INT unsigned LONG int
59 #endif
61 /* Determine the name. */
62 #if UNSIGNED
63 # ifdef USE_WIDE_CHAR
64 # ifdef QUAD
65 # define strtol_l wcstoull_l
66 # else
67 # define strtol_l wcstoul_l
68 # endif
69 # else
70 # ifdef QUAD
71 # define strtol_l strtoull_l
72 # else
73 # define strtol_l strtoul_l
74 # endif
75 # endif
76 #else
77 # ifdef USE_WIDE_CHAR
78 # ifdef QUAD
79 # define strtol_l wcstoll_l
80 # else
81 # define strtol_l wcstol_l
82 # endif
83 # else
84 # ifdef QUAD
85 # define strtol_l strtoll_l
86 # else
87 # define strtol_l strtol_l
88 # endif
89 # endif
90 #endif
92 #define __strtol_l __strtol_l2(strtol_l)
93 #define __strtol_l2(name) __strtol_l3(name)
94 #define __strtol_l3(name) __##name
97 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
98 operating on `long long int's. */
99 #ifdef QUAD
100 # define LONG long long
101 # define STRTOL_LONG_MIN LONG_LONG_MIN
102 # define STRTOL_LONG_MAX LONG_LONG_MAX
103 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
104 #else
105 # define LONG long
107 # ifndef ULONG_MAX
108 # define ULONG_MAX ((unsigned long int) ~(unsigned long int) 0)
109 # endif
110 # ifndef LONG_MAX
111 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
112 # endif
113 # define STRTOL_LONG_MIN LONG_MIN
114 # define STRTOL_LONG_MAX LONG_MAX
115 # define STRTOL_ULONG_MAX ULONG_MAX
116 #endif
119 /* We use this code for the extended locale handling where the
120 function gets as an additional argument the locale which has to be
121 used. To access the values we have to redefine the _NL_CURRENT and
122 _NL_CURRENT_WORD macros. */
123 #undef _NL_CURRENT
124 #define _NL_CURRENT(category, item) \
125 (current->values[_NL_ITEM_INDEX (item)].string)
126 #undef _NL_CURRENT_WORD
127 #define _NL_CURRENT_WORD(category, item) \
128 ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
130 #if defined _LIBC || defined HAVE_WCHAR_H
131 # include <wchar.h>
132 #endif
134 #ifdef USE_WIDE_CHAR
135 # include <wctype.h>
136 # define L_(Ch) L##Ch
137 # define UCHAR_TYPE wint_t
138 # define STRING_TYPE wchar_t
139 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
140 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
141 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
142 #else
143 # if defined _LIBC \
144 || defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
145 # define IN_CTYPE_DOMAIN(c) 1
146 # else
147 # define IN_CTYPE_DOMAIN(c) isascii(c)
148 # endif
149 # define L_(Ch) Ch
150 # define UCHAR_TYPE unsigned char
151 # define STRING_TYPE char
152 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
153 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
154 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
155 #endif
157 #define INTERNAL(X) INTERNAL1(X)
158 #define INTERNAL1(X) __##X##_internal
159 #define WEAKNAME(X) WEAKNAME1(X)
161 #ifdef USE_NUMBER_GROUPING
162 /* This file defines a function to check for correct grouping. */
163 # include "grouping.h"
164 #endif
167 /* Define tables of maximum values and remainders in order to detect
168 overflow. Do this at compile-time in order to avoid the runtime
169 overhead of the division. */
170 extern const unsigned long __strtol_ul_max_tab[] attribute_hidden;
171 extern const unsigned char __strtol_ul_rem_tab[] attribute_hidden;
172 #if defined(QUAD) && __WORDSIZE == 32
173 extern const unsigned long long __strtol_ull_max_tab[] attribute_hidden;
174 extern const unsigned char __strtol_ull_rem_tab[] attribute_hidden;
175 #endif
177 #define DEF(TYPE, NAME) \
178 const TYPE NAME[] attribute_hidden = \
180 F(2), F(3), F(4), F(5), F(6), F(7), F(8), F(9), F(10), \
181 F(11), F(12), F(13), F(14), F(15), F(16), F(17), F(18), F(19), F(20), \
182 F(21), F(22), F(23), F(24), F(25), F(26), F(27), F(28), F(29), F(30), \
183 F(31), F(32), F(33), F(34), F(35), F(36) \
186 #if !UNSIGNED && !defined (USE_WIDE_CHAR) && !defined (QUAD)
187 # define F(X) ULONG_MAX / X
188 DEF (unsigned long, __strtol_ul_max_tab);
189 # undef F
190 # define F(X) ULONG_MAX % X
191 DEF (unsigned char, __strtol_ul_rem_tab);
192 # undef F
193 #endif
194 #if !UNSIGNED && !defined (USE_WIDE_CHAR) && defined (QUAD) \
195 && __WORDSIZE == 32
196 # define F(X) ULONG_LONG_MAX / X
197 DEF (unsigned long long, __strtol_ull_max_tab);
198 # undef F
199 # define F(X) ULONG_LONG_MAX % X
200 DEF (unsigned char, __strtol_ull_rem_tab);
201 # undef F
202 #endif
203 #undef DEF
205 /* Define some more readable aliases for these arrays which correspond
206 to how they'll be used in the function below. */
207 #define jmax_tab __strtol_ul_max_tab
208 #if defined(QUAD) && __WORDSIZE == 32
209 # define cutoff_tab __strtol_ull_max_tab
210 # define cutlim_tab __strtol_ull_rem_tab
211 #else
212 # define cutoff_tab __strtol_ul_max_tab
213 # define cutlim_tab __strtol_ul_rem_tab
214 #endif
217 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
218 If BASE is 0 the base is determined by the presence of a leading
219 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
220 If BASE is < 2 or > 36, it is reset to 10.
221 If ENDPTR is not NULL, a pointer to the character after the last
222 one converted is stored in *ENDPTR. */
225 INTERNAL (__strtol_l) (nptr, endptr, base, group, loc)
226 const STRING_TYPE *nptr;
227 STRING_TYPE **endptr;
228 int base;
229 int group;
230 __locale_t loc;
232 int negative;
233 register unsigned LONG int cutoff;
234 register unsigned int cutlim;
235 register unsigned LONG int i;
236 register const STRING_TYPE *s;
237 register UCHAR_TYPE c;
238 const STRING_TYPE *save, *end;
239 int overflow;
240 #ifndef USE_WIDE_CHAR
241 size_t cnt;
242 #endif
244 #ifdef USE_NUMBER_GROUPING
245 struct __locale_data *current = loc->__locales[LC_NUMERIC];
246 /* The thousands character of the current locale. */
247 # ifdef USE_WIDE_CHAR
248 wchar_t thousands = L'\0';
249 # else
250 const char *thousands = NULL;
251 size_t thousands_len = 0;
252 # endif
253 /* The numeric grouping specification of the current locale,
254 in the format described in <locale.h>. */
255 const char *grouping;
257 if (__builtin_expect (group, 0))
259 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
260 if (*grouping <= 0 || *grouping == CHAR_MAX)
261 grouping = NULL;
262 else
264 /* Figure out the thousands separator character. */
265 # ifdef USE_WIDE_CHAR
266 # ifdef _LIBC
267 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
268 _NL_NUMERIC_THOUSANDS_SEP_WC);
269 # endif
270 if (thousands == L'\0')
271 grouping = NULL;
272 # else
273 # ifdef _LIBC
274 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
275 # endif
276 if (*thousands == '\0')
278 thousands = NULL;
279 grouping = NULL;
281 # endif
284 else
285 grouping = NULL;
286 #endif
288 if (base < 0 || base == 1 || base > 36)
290 __set_errno (EINVAL);
291 return 0;
294 save = s = nptr;
296 /* Skip white space. */
297 while (ISSPACE (*s))
298 ++s;
299 if (__builtin_expect (*s == L_('\0'), 0))
300 goto noconv;
302 /* Check for a sign. */
303 negative = 0;
304 if (*s == L_('-'))
306 negative = 1;
307 ++s;
309 else if (*s == L_('+'))
310 ++s;
312 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
313 if (*s == L_('0'))
315 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
317 s += 2;
318 base = 16;
320 else if (base == 0)
321 base = 8;
323 else if (base == 0)
324 base = 10;
326 /* Save the pointer so we can check later if anything happened. */
327 save = s;
329 #ifdef USE_NUMBER_GROUPING
330 if (base != 10)
331 grouping = NULL;
333 if (__builtin_expect (grouping != NULL, 0))
335 # ifndef USE_WIDE_CHAR
336 thousands_len = strlen (thousands);
337 # endif
339 /* Find the end of the digit string and check its grouping. */
340 end = s;
341 if (
342 # ifdef USE_WIDE_CHAR
343 *s != thousands
344 # else
345 ({ for (cnt = 0; cnt < thousands_len; ++cnt)
346 if (thousands[cnt] != end[cnt])
347 break;
348 cnt < thousands_len; })
349 # endif
352 for (c = *end; c != L_('\0'); c = *++end)
353 if (((STRING_TYPE) c < L_('0') || (STRING_TYPE) c > L_('9'))
354 # ifdef USE_WIDE_CHAR
355 && (wchar_t) c != thousands
356 # else
357 && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
358 if (thousands[cnt] != end[cnt])
359 break;
360 cnt < thousands_len; })
361 # endif
362 && (!ISALPHA (c)
363 || (int) (TOUPPER (c) - L_('A') + 10) >= base))
364 break;
366 # ifdef USE_WIDE_CHAR
367 end = __correctly_grouped_prefixwc (s, end, thousands, grouping);
368 # else
369 end = __correctly_grouped_prefixmb (s, end, thousands, grouping);
370 # endif
373 else
374 #endif
375 end = NULL;
377 /* Avoid runtime division; lookup cutoff and limit. */
378 cutoff = cutoff_tab[base - 2];
379 cutlim = cutlim_tab[base - 2];
381 overflow = 0;
382 i = 0;
383 c = *s;
384 if (sizeof (long int) != sizeof (LONG int))
386 unsigned long int j = 0;
387 unsigned long int jmax = jmax_tab[base - 2];
389 for (;c != L_('\0'); c = *++s)
391 if (s == end)
392 break;
393 if (c >= L_('0') && c <= L_('9'))
394 c -= L_('0');
395 #ifdef USE_NUMBER_GROUPING
396 # ifdef USE_WIDE_CHAR
397 else if (grouping && (wchar_t) c == thousands)
398 continue;
399 # else
400 else if (thousands_len)
402 for (cnt = 0; cnt < thousands_len; ++cnt)
403 if (thousands[cnt] != s[cnt])
404 break;
405 if (cnt == thousands_len)
407 s += thousands_len - 1;
408 continue;
410 if (ISALPHA (c))
411 c = TOUPPER (c) - L_('A') + 10;
412 else
413 break;
415 # endif
416 #endif
417 else if (ISALPHA (c))
418 c = TOUPPER (c) - L_('A') + 10;
419 else
420 break;
421 if ((int) c >= base)
422 break;
423 /* Note that we never can have an overflow. */
424 else if (j >= jmax)
426 /* We have an overflow. Now use the long representation. */
427 i = (unsigned LONG int) j;
428 goto use_long;
430 else
431 j = j * (unsigned long int) base + c;
434 i = (unsigned LONG int) j;
436 else
437 for (;c != L_('\0'); c = *++s)
439 if (s == end)
440 break;
441 if (c >= L_('0') && c <= L_('9'))
442 c -= L_('0');
443 #ifdef USE_NUMBER_GROUPING
444 # ifdef USE_WIDE_CHAR
445 else if (grouping && (wchar_t) c == thousands)
446 continue;
447 # else
448 else if (thousands_len)
450 for (cnt = 0; cnt < thousands_len; ++cnt)
451 if (thousands[cnt] != s[cnt])
452 break;
453 if (cnt == thousands_len)
455 s += thousands_len - 1;
456 continue;
458 if (ISALPHA (c))
459 c = TOUPPER (c) - L_('A') + 10;
460 else
461 break;
463 # endif
464 #endif
465 else if (ISALPHA (c))
466 c = TOUPPER (c) - L_('A') + 10;
467 else
468 break;
469 if ((int) c >= base)
470 break;
471 /* Check for overflow. */
472 if (i > cutoff || (i == cutoff && c > cutlim))
473 overflow = 1;
474 else
476 use_long:
477 i *= (unsigned LONG int) base;
478 i += c;
482 /* Check if anything actually happened. */
483 if (s == save)
484 goto noconv;
486 /* Store in ENDPTR the address of one character
487 past the last character we converted. */
488 if (endptr != NULL)
489 *endptr = (STRING_TYPE *) s;
491 #if !UNSIGNED
492 /* Check for a value that is within the range of
493 `unsigned LONG int', but outside the range of `LONG int'. */
494 if (overflow == 0
495 && i > (negative
496 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
497 : (unsigned LONG int) STRTOL_LONG_MAX))
498 overflow = 1;
499 #endif
501 if (__builtin_expect (overflow, 0))
503 __set_errno (ERANGE);
504 #if UNSIGNED
505 return STRTOL_ULONG_MAX;
506 #else
507 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
508 #endif
511 /* Return the result of the appropriate sign. */
512 return negative ? -i : i;
514 noconv:
515 /* We must handle a special case here: the base is 0 or 16 and the
516 first two characters are '0' and 'x', but the rest are no
517 hexadecimal digits. This is no error case. We return 0 and
518 ENDPTR points to the `x`. */
519 if (endptr != NULL)
521 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
522 && save[-2] == L_('0'))
523 *endptr = (STRING_TYPE *) &save[-1];
524 else
525 /* There was no number to convert. */
526 *endptr = (STRING_TYPE *) nptr;
529 return 0L;
531 #if defined _LIBC && !defined USE_WIDE_CHAR
532 libc_hidden_def (INTERNAL (__strtol_l))
533 #endif
535 /* External user entry point. */
537 #if _LIBC - 0 == 0
539 /* Prototype. */
540 extern INT __strtol_l (const STRING_TYPE *nptr, STRING_TYPE **endptr,
541 int base);
542 #endif
546 #ifdef weak_function
547 weak_function
548 #endif
549 __strtol_l (nptr, endptr, base, loc)
550 const STRING_TYPE *nptr;
551 STRING_TYPE **endptr;
552 int base;
553 __locale_t loc;
555 return INTERNAL (__strtol_l) (nptr, endptr, base, 0, loc);
557 libc_hidden_def (__strtol_l)
558 weak_alias (__strtol_l, strtol_l)