1 /* Convert string representation of a number into an integer value.
3 Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2005,
4 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 NOTE: The canonical source of this file is maintained with the GNU C
7 Library. Bugs can be reported to bug-glibc@gnu.org.
9 This program is free software: you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 # define USE_NUMBER_GROUPING
31 # define __set_errno(Val) errno = (Val)
39 #ifdef USE_NUMBER_GROUPING
40 # include "../locale/localeinfo.h"
43 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
49 # define INT unsigned LONG int
52 /* Determine the name. */
53 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
57 # define strtol __wcstoull_l
59 # define strtol __wcstoul_l
63 # define strtol __strtoull_l
65 # define strtol __strtoul_l
71 # define strtol __wcstoll_l
73 # define strtol __wcstol_l
77 # define strtol __strtoll_l
79 # define strtol __strtol_l
87 # define strtol wcstoull
89 # define strtol wcstoul
93 # define strtol strtoull
95 # define strtol strtoul
101 # define strtol wcstoll
103 # define strtol wcstol
107 # define strtol strtoll
113 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
114 operating on `long long int's. */
116 # define LONG long long
117 # define STRTOL_LONG_MIN LLONG_MIN
118 # define STRTOL_LONG_MAX LLONG_MAX
119 # define STRTOL_ULONG_MAX ULLONG_MAX
121 /* The extra casts in the following macros work around compiler bugs,
122 e.g., in Cray C 5.0.3.0. */
124 /* True if negative values of the signed integer type T use two's
125 complement, ones' complement, or signed magnitude representation,
126 respectively. Much GNU code assumes two's complement, but some
127 people like to be portable to all possible C hosts. */
128 # define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
129 # define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
130 # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
132 /* True if the arithmetic type T is signed. */
133 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
135 /* The maximum and minimum values for the integer type T. These
136 macros have undefined behavior if T is signed and has padding bits.
137 If this is a problem for you, please let us know how to fix it for
139 # define TYPE_MINIMUM(t) \
140 ((t) (! TYPE_SIGNED (t) \
142 : TYPE_SIGNED_MAGNITUDE (t) \
144 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
145 # define TYPE_MAXIMUM(t) \
146 ((t) (! TYPE_SIGNED (t) \
148 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
151 # define ULLONG_MAX TYPE_MAXIMUM (unsigned long long)
154 # define LLONG_MAX TYPE_MAXIMUM (long long int)
157 # define LLONG_MIN TYPE_MINIMUM (long long int)
160 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
161 /* Work around gcc bug with using this constant. */
162 static const unsigned long long int maxquad = ULLONG_MAX;
163 # undef STRTOL_ULONG_MAX
164 # define STRTOL_ULONG_MAX maxquad
168 # define STRTOL_LONG_MIN LONG_MIN
169 # define STRTOL_LONG_MAX LONG_MAX
170 # define STRTOL_ULONG_MAX ULONG_MAX
174 /* We use this code also for the extended locale handling where the
175 function gets as an additional argument the locale which has to be
176 used. To access the values we have to redefine the _NL_CURRENT
178 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
180 # define _NL_CURRENT(category, item) \
181 (current->values[_NL_ITEM_INDEX (item)].string)
182 # define LOCALE_PARAM , loc
183 # define LOCALE_PARAM_PROTO , __locale_t loc
185 # define LOCALE_PARAM
186 # define LOCALE_PARAM_PROTO
193 # define L_(Ch) L##Ch
194 # define UCHAR_TYPE wint_t
195 # define STRING_TYPE wchar_t
196 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
197 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
198 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
199 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
201 # define ISSPACE(Ch) iswspace (Ch)
202 # define ISALPHA(Ch) iswalpha (Ch)
203 # define TOUPPER(Ch) towupper (Ch)
207 # define UCHAR_TYPE unsigned char
208 # define STRING_TYPE char
209 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
210 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
211 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
212 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
214 # define ISSPACE(Ch) isspace (Ch)
215 # define ISALPHA(Ch) isalpha (Ch)
216 # define TOUPPER(Ch) toupper (Ch)
220 #define INTERNAL(X) INTERNAL1(X)
221 #define INTERNAL1(X) __##X##_internal
222 #define WEAKNAME(X) WEAKNAME1(X)
224 #ifdef USE_NUMBER_GROUPING
225 /* This file defines a function to check for correct grouping. */
226 # include "grouping.h"
231 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
232 If BASE is 0 the base is determined by the presence of a leading
233 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
234 If BASE is < 2 or > 36, it is reset to 10.
235 If ENDPTR is not NULL, a pointer to the character after the last
236 one converted is stored in *ENDPTR. */
239 INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
240 int base, int group LOCALE_PARAM_PROTO)
243 register unsigned LONG int cutoff;
244 register unsigned int cutlim;
245 register unsigned LONG int i;
246 register const STRING_TYPE *s;
247 register UCHAR_TYPE c;
248 const STRING_TYPE *save, *end;
251 #ifdef USE_NUMBER_GROUPING
252 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
253 struct locale_data *current = loc->__locales[LC_NUMERIC];
255 /* The thousands character of the current locale. */
256 wchar_t thousands = L'\0';
257 /* The numeric grouping specification of the current locale,
258 in the format described in <locale.h>. */
259 const char *grouping;
263 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
264 if (*grouping <= 0 || *grouping == CHAR_MAX)
268 /* Figure out the thousands separator character. */
269 # if defined _LIBC || defined _HAVE_BTOWC
270 thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
271 if (thousands == WEOF)
274 if (thousands == L'\0')
282 if (base < 0 || base == 1 || base > 36)
284 __set_errno (EINVAL);
290 /* Skip white space. */
296 /* Check for a sign. */
302 else if (*s == L_('+'))
310 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
313 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
324 /* Save the pointer so we can check later if anything happened. */
327 #ifdef USE_NUMBER_GROUPING
330 /* Find the end of the digit string and check its grouping. */
332 for (c = *end; c != L_('\0'); c = *++end)
333 if ((wchar_t) c != thousands
334 && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
335 && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
340 end = correctly_grouped_prefix (s, end, thousands, grouping);
346 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
347 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
351 for (c = *s; c != L_('\0'); c = *++s)
355 if (c >= L_('0') && c <= L_('9'))
357 else if (ISALPHA (c))
358 c = TOUPPER (c) - L_('A') + 10;
363 /* Check for overflow. */
364 if (i > cutoff || (i == cutoff && c > cutlim))
368 i *= (unsigned LONG int) base;
373 /* Check if anything actually happened. */
377 /* Store in ENDPTR the address of one character
378 past the last character we converted. */
380 *endptr = (STRING_TYPE *) s;
383 /* Check for a value that is within the range of
384 `unsigned LONG int', but outside the range of `LONG int'. */
387 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
388 : (unsigned LONG int) STRTOL_LONG_MAX))
394 __set_errno (ERANGE);
396 return STRTOL_ULONG_MAX;
398 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
402 /* Return the result of the appropriate sign. */
403 return negative ? -i : i;
406 /* We must handle a special case here: the base is 0 or 16 and the
407 first two characters are '0' and 'x', but the rest are no
408 hexadecimal digits. This is no error case. We return 0 and
409 ENDPTR points to the `x`. */
412 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
413 && save[-2] == L_('0'))
414 *endptr = (STRING_TYPE *) &save[-1];
416 /* There was no number to convert. */
417 *endptr = (STRING_TYPE *) nptr;
423 /* External user entry point. */
430 strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
431 int base LOCALE_PARAM_PROTO)
433 return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);