Update.
[glibc.git] / sysdeps / generic / strtol.c
blob1b267753d99436fc7a473be75666c214404cdd39
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
19 02111-1307 USA. */
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 errno
34 extern int errno;
35 #endif
36 #ifndef __set_errno
37 # define __set_errno(Val) errno = (Val)
38 #endif
40 #ifdef HAVE_LIMITS_H
41 # include <limits.h>
42 #endif
44 #ifdef STDC_HEADERS
45 # include <stddef.h>
46 # include <stdlib.h>
47 # include <string.h>
48 # include <locale.h>
49 #else
50 # ifndef NULL
51 # define NULL 0
52 # endif
53 #endif
55 #ifdef USE_NUMBER_GROUPING
56 # include "../locale/localeinfo.h"
57 #endif
59 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
60 unsigned integers. */
61 #ifndef UNSIGNED
62 # define UNSIGNED 0
63 # define INT LONG int
64 #else
65 # define INT unsigned LONG int
66 #endif
68 /* Determine the name. */
69 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
70 # if UNSIGNED
71 # ifdef USE_WIDE_CHAR
72 # ifdef QUAD
73 # define strtol __wcstoull_l
74 # else
75 # define strtol __wcstoul_l
76 # endif
77 # else
78 # ifdef QUAD
79 # define strtol __strtoull_l
80 # else
81 # define strtol __strtoul_l
82 # endif
83 # endif
84 # else
85 # ifdef USE_WIDE_CHAR
86 # ifdef QUAD
87 # define strtol __wcstoll_l
88 # else
89 # define strtol __wcstol_l
90 # endif
91 # else
92 # ifdef QUAD
93 # define strtol __strtoll_l
94 # else
95 # define strtol __strtol_l
96 # endif
97 # endif
98 # endif
99 #else
100 # if UNSIGNED
101 # ifdef USE_WIDE_CHAR
102 # ifdef QUAD
103 # define strtol wcstoull
104 # else
105 # define strtol wcstoul
106 # endif
107 # else
108 # ifdef QUAD
109 # define strtol strtoull
110 # else
111 # define strtol strtoul
112 # endif
113 # endif
114 # else
115 # ifdef USE_WIDE_CHAR
116 # ifdef QUAD
117 # define strtol wcstoll
118 # else
119 # define strtol wcstol
120 # endif
121 # else
122 # ifdef QUAD
123 # define strtol strtoll
124 # endif
125 # endif
126 # endif
127 #endif
129 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
130 operating on `long long int's. */
131 #ifdef QUAD
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
141 # endif
142 #else
143 # define LONG long
145 # ifndef ULONG_MAX
146 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
147 # endif
148 # ifndef LONG_MAX
149 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
150 # endif
151 # define STRTOL_LONG_MIN LONG_MIN
152 # define STRTOL_LONG_MAX LONG_MAX
153 # define STRTOL_ULONG_MAX ULONG_MAX
154 #endif
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
160 macro. */
161 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
162 # undef _NL_CURRENT
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;
167 #else
168 # define LOCALE_PARAM
169 # define LOCALE_PARAM_DECL
170 #endif
172 #if defined _LIBC || defined HAVE_WCHAR_H
173 # include <wchar.h>
174 #endif
176 #ifdef USE_WIDE_CHAR
177 # include <wctype.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)
185 # else
186 # define ISSPACE(Ch) iswspace (Ch)
187 # define ISALPHA(Ch) iswalpha (Ch)
188 # define TOUPPER(Ch) towupper (Ch)
189 # endif
190 # else
191 # if defined _LIBC \
192 || defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
193 # define IN_CTYPE_DOMAIN(c) 1
194 # else
195 # define IN_CTYPE_DOMAIN(c) isascii(c)
196 # endif
197 # define L_(Ch) Ch
198 # define UCHAR_TYPE unsigned char
199 # define STRING_TYPE char
200 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
201 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
202 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
203 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
204 # else
205 # define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
206 # define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
207 # define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
208 # endif
209 #endif
211 #ifdef __STDC__
212 # define INTERNAL(X) INTERNAL1(X)
213 # define INTERNAL1(X) __##X##_internal
214 # define WEAKNAME(X) WEAKNAME1(X)
215 #else
216 # define INTERNAL(X) __/**/X/**/_internal
217 #endif
219 #ifdef USE_NUMBER_GROUPING
220 /* This file defines a function to check for correct grouping. */
221 # include "grouping.h"
222 #endif
226 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
227 If BASE is 0 the base is determined by the presence of a leading
228 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
229 If BASE is < 2 or > 36, it is reset to 10.
230 If ENDPTR is not NULL, a pointer to the character after the last
231 one converted is stored in *ENDPTR. */
234 INTERNAL (strtol) (nptr, endptr, base, group LOCALE_PARAM)
235 const STRING_TYPE *nptr;
236 STRING_TYPE **endptr;
237 int base;
238 int group;
239 LOCALE_PARAM_DECL
241 int negative;
242 register unsigned LONG int cutoff;
243 register unsigned int cutlim;
244 register unsigned LONG int i;
245 register const STRING_TYPE *s;
246 register UCHAR_TYPE c;
247 const STRING_TYPE *save, *end;
248 int overflow;
249 #ifndef USE_WIDE_CHAR
250 size_t cnt;
251 #endif
253 #ifdef USE_NUMBER_GROUPING
254 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
255 struct locale_data *current = loc->__locales[LC_NUMERIC];
256 # endif
257 /* The thousands character of the current locale. */
258 # ifdef USE_WIDE_CHAR
259 wchar_t thousands = L'\0';
260 # else
261 const char *thousands = NULL;
262 size_t thousands_len = 0;
263 # endif
264 /* The numeric grouping specification of the current locale,
265 in the format described in <locale.h>. */
266 const char *grouping;
268 if (__builtin_expect (group, 0))
270 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
271 if (*grouping <= 0 || *grouping == CHAR_MAX)
272 grouping = NULL;
273 else
275 /* Figure out the thousands separator character. */
276 # ifdef USE_WIDE_CHAR
277 # ifdef _LIBC
278 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
279 _NL_NUMERIC_THOUSANDS_SEP_WC);
280 # endif
281 if (thousands == L'\0')
282 grouping = NULL;
283 # else
284 # ifdef _LIBC
285 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
286 # endif
287 if (*thousands == '\0')
289 thousands = NULL;
290 grouping = NULL;
292 # endif
295 else
296 grouping = NULL;
297 #endif
299 if (base < 0 || base == 1 || base > 36)
301 __set_errno (EINVAL);
302 return 0;
305 save = s = nptr;
307 /* Skip white space. */
308 while (ISSPACE (*s))
309 ++s;
310 if (__builtin_expect (*s == L_('\0'), 0))
311 goto noconv;
313 /* Check for a sign. */
314 negative = 0;
315 if (*s == L_('-'))
317 negative = 1;
318 ++s;
320 else if (*s == L_('+'))
321 ++s;
323 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
324 if (*s == L_('0'))
326 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
328 s += 2;
329 base = 16;
331 else if (base == 0)
332 base = 8;
334 else if (base == 0)
335 base = 10;
337 /* Save the pointer so we can check later if anything happened. */
338 save = s;
340 #ifdef USE_NUMBER_GROUPING
341 if (base != 10)
342 grouping = NULL;
344 if (__builtin_expect (grouping != NULL, 0))
346 # ifndef USE_WIDE_CHAR
347 thousands_len = strlen (thousands);
348 # endif
350 /* Find the end of the digit string and check its grouping. */
351 end = s;
352 if (
353 # ifdef USE_WIDE_CHAR
354 *s != thousands
355 # else
356 ({ for (cnt = 0; cnt < thousands_len; ++cnt)
357 if (thousands[cnt] != end[cnt])
358 break;
359 cnt < thousands_len; })
360 # endif
363 for (c = *end; c != L_('\0'); c = *++end)
364 if (((STRING_TYPE) c < L_('0') || (STRING_TYPE) c > L_('9'))
365 # ifdef USE_WIDE_CHAR
366 && (wchar_t) c != thousands
367 # else
368 && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
369 if (thousands[cnt] != end[cnt])
370 break;
371 cnt < thousands_len; })
372 # endif
373 && (!ISALPHA (c)
374 || (int) (TOUPPER (c) - L_('A') + 10) >= base))
375 break;
377 end = correctly_grouped_prefix (s, end, thousands, grouping);
380 else
381 #endif
382 end = NULL;
384 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
385 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
387 overflow = 0;
388 i = 0;
389 c = *s;
390 if (sizeof (long int) != sizeof (LONG int))
392 unsigned long int j = 0;
393 unsigned long int jmax = ULONG_MAX / base;
395 for (;c != L_('\0'); c = *++s)
397 if (s == end)
398 break;
399 if (c >= L_('0') && c <= L_('9'))
400 c -= L_('0');
401 #ifdef USE_NUMBER_GROUPING
402 # ifdef USE_WIDE_CHAR
403 else if (grouping && (wchar_t) c == thousands)
404 continue;
405 # else
406 else if (thousands_len)
408 for (cnt = 0; cnt < thousands_len; ++cnt)
409 if (thousands[cnt] != s[cnt])
410 break;
411 if (cnt == thousands_len)
413 s += thousands_len - 1;
414 continue;
416 if (ISALPHA (c))
417 c = TOUPPER (c) - L_('A') + 10;
418 else
419 break;
421 # endif
422 #endif
423 else if (ISALPHA (c))
424 c = TOUPPER (c) - L_('A') + 10;
425 else
426 break;
427 if ((int) c >= base)
428 break;
429 /* Note that we never can have an overflow. */
430 else if (j >= jmax)
432 /* We have an overflow. Now use the long representation. */
433 i = (unsigned LONG int) j;
434 goto use_long;
436 else
437 j = j * (unsigned long int) base + c;
440 i = (unsigned LONG int) j;
442 else
443 for (;c != L_('\0'); c = *++s)
445 if (s == end)
446 break;
447 if (c >= L_('0') && c <= L_('9'))
448 c -= L_('0');
449 #ifdef USE_NUMBER_GROUPING
450 # ifdef USE_WIDE_CHAR
451 else if (grouping && (wchar_t) c == thousands)
452 continue;
453 # else
454 else if (thousands_len)
456 for (cnt = 0; cnt < thousands_len; ++cnt)
457 if (thousands[cnt] != s[cnt])
458 break;
459 if (cnt == thousands_len)
461 s += thousands_len - 1;
462 continue;
464 if (ISALPHA (c))
465 c = TOUPPER (c) - L_('A') + 10;
466 else
467 break;
469 # endif
470 #endif
471 else if (ISALPHA (c))
472 c = TOUPPER (c) - L_('A') + 10;
473 else
474 break;
475 if ((int) c >= base)
476 break;
477 /* Check for overflow. */
478 if (i > cutoff || (i == cutoff && c > cutlim))
479 overflow = 1;
480 else
482 use_long:
483 i *= (unsigned LONG int) base;
484 i += c;
488 /* Check if anything actually happened. */
489 if (s == save)
490 goto noconv;
492 /* Store in ENDPTR the address of one character
493 past the last character we converted. */
494 if (endptr != NULL)
495 *endptr = (STRING_TYPE *) s;
497 #if !UNSIGNED
498 /* Check for a value that is within the range of
499 `unsigned LONG int', but outside the range of `LONG int'. */
500 if (overflow == 0
501 && i > (negative
502 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
503 : (unsigned LONG int) STRTOL_LONG_MAX))
504 overflow = 1;
505 #endif
507 if (__builtin_expect (overflow, 0))
509 __set_errno (ERANGE);
510 #if UNSIGNED
511 return STRTOL_ULONG_MAX;
512 #else
513 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
514 #endif
517 /* Return the result of the appropriate sign. */
518 return negative ? -i : i;
520 noconv:
521 /* We must handle a special case here: the base is 0 or 16 and the
522 first two characters are '0' and 'x', but the rest are no
523 hexadecimal digits. This is no error case. We return 0 and
524 ENDPTR points to the `x`. */
525 if (endptr != NULL)
527 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
528 && save[-2] == L_('0'))
529 *endptr = (STRING_TYPE *) &save[-1];
530 else
531 /* There was no number to convert. */
532 *endptr = (STRING_TYPE *) nptr;
535 return 0L;
537 #if defined _LIBC \
538 && !(defined USE_IN_EXTENDED_LOCALE_MODEL && defined USE_WIDE_CHAR)
539 libc_hidden_def (INTERNAL (strtol))
540 #endif
542 /* External user entry point. */
544 #if _LIBC - 0 == 0
545 # undef PARAMS
546 # if defined (__STDC__) && __STDC__
547 # define PARAMS(Args) Args
548 # else
549 # define PARAMS(Args) ()
550 # endif
552 /* Prototype. */
553 INT strtol PARAMS ((const STRING_TYPE *nptr, STRING_TYPE **endptr, int base));
554 #endif
558 #ifdef weak_function
559 weak_function
560 #endif
561 strtol (nptr, endptr, base LOCALE_PARAM)
562 const STRING_TYPE *nptr;
563 STRING_TYPE **endptr;
564 int base;
565 LOCALE_PARAM_DECL
567 return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);