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 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 2, or (at your option) 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, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 # define USE_NUMBER_GROUPING
37 # define __set_errno(Val) errno = (Val)
45 #ifdef USE_NUMBER_GROUPING
46 # include "../locale/localeinfo.h"
49 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
55 # define INT unsigned LONG int
58 /* Determine the name. */
59 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
63 # define strtol __wcstoull_l
65 # define strtol __wcstoul_l
69 # define strtol __strtoull_l
71 # define strtol __strtoul_l
77 # define strtol __wcstoll_l
79 # define strtol __wcstol_l
83 # define strtol __strtoll_l
85 # define strtol __strtol_l
93 # define strtol wcstoull
95 # define strtol wcstoul
99 # define strtol strtoull
101 # define strtol strtoul
105 # ifdef USE_WIDE_CHAR
107 # define strtol wcstoll
109 # define strtol wcstol
113 # define strtol strtoll
119 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
120 operating on `long long int's. */
122 # define LONG long long
123 # define STRTOL_LONG_MIN LONG_LONG_MIN
124 # define STRTOL_LONG_MAX LONG_LONG_MAX
125 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
127 /* The extra casts in the following macros work around compiler bugs,
128 e.g., in Cray C 5.0.3.0. */
130 /* True if negative values of the signed integer type T use twos
131 complement, ones complement, or signed magnitude representation,
132 respectively. Much GNU code assumes twos complement, but some
133 people like to be portable to all possible C hosts. */
134 #define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
135 #define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
136 #define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
138 /* True if the arithmetic type T is signed. */
139 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
141 /* The maximum and minimum values for the integer type T. These
142 macros have undefined behavior if T is signed and has padding bits
143 (i.e., bits that do not contribute to the value), or if T uses
144 signed-magnitude representation. If this is a problem for you,
145 please let us know how to fix it for your host. */
146 # define TYPE_MINIMUM(t) \
147 ((t) (! TYPE_SIGNED (t) \
149 : TYPE_SIGNED_MAGNITUDE (t) \
151 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
152 # define TYPE_MAXIMUM(t) \
153 ((t) (! TYPE_SIGNED (t) \
155 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
157 # ifndef ULONG_LONG_MAX
158 # define ULONG_LONG_MAX TYPE_MAXIMUM (unsigned long long)
160 # ifndef LONG_LONG_MAX
161 # define LONG_LONG_MAX TYPE_MAXIMUM (long long int)
163 # ifndef LONG_LONG_MIN
164 # define LONG_LONG_MIN TYPE_MINIMUM (long long int)
167 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
168 /* Work around gcc bug with using this constant. */
169 static const unsigned long long int maxquad = ULONG_LONG_MAX;
170 # undef STRTOL_ULONG_MAX
171 # define STRTOL_ULONG_MAX maxquad
175 # define STRTOL_LONG_MIN LONG_MIN
176 # define STRTOL_LONG_MAX LONG_MAX
177 # define STRTOL_ULONG_MAX ULONG_MAX
181 /* We use this code also for the extended locale handling where the
182 function gets as an additional argument the locale which has to be
183 used. To access the values we have to redefine the _NL_CURRENT
185 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
187 # define _NL_CURRENT(category, item) \
188 (current->values[_NL_ITEM_INDEX (item)].string)
189 # define LOCALE_PARAM , loc
190 # define LOCALE_PARAM_PROTO , __locale_t loc
192 # define LOCALE_PARAM
193 # define LOCALE_PARAM_PROTO
196 #if defined _LIBC || defined HAVE_WCHAR_H
202 # define L_(Ch) L##Ch
203 # define UCHAR_TYPE wint_t
204 # define STRING_TYPE wchar_t
205 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
206 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
207 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
208 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
210 # define ISSPACE(Ch) iswspace (Ch)
211 # define ISALPHA(Ch) iswalpha (Ch)
212 # define TOUPPER(Ch) towupper (Ch)
215 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
216 # define IN_CTYPE_DOMAIN(c) 1
218 # define IN_CTYPE_DOMAIN(c) isascii(c)
221 # define UCHAR_TYPE unsigned char
222 # define STRING_TYPE char
223 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
224 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
225 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
226 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
228 # define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
229 # define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
230 # define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
234 #define INTERNAL(X) INTERNAL1(X)
235 #define INTERNAL1(X) __##X##_internal
236 #define WEAKNAME(X) WEAKNAME1(X)
238 #ifdef USE_NUMBER_GROUPING
239 /* This file defines a function to check for correct grouping. */
240 # include "grouping.h"
245 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
246 If BASE is 0 the base is determined by the presence of a leading
247 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
248 If BASE is < 2 or > 36, it is reset to 10.
249 If ENDPTR is not NULL, a pointer to the character after the last
250 one converted is stored in *ENDPTR. */
253 INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
254 int base, int group LOCALE_PARAM_PROTO)
257 register unsigned LONG int cutoff;
258 register unsigned int cutlim;
259 register unsigned LONG int i;
260 register const STRING_TYPE *s;
261 register UCHAR_TYPE c;
262 const STRING_TYPE *save, *end;
265 #ifdef USE_NUMBER_GROUPING
266 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
267 struct locale_data *current = loc->__locales[LC_NUMERIC];
269 /* The thousands character of the current locale. */
270 wchar_t thousands = L'\0';
271 /* The numeric grouping specification of the current locale,
272 in the format described in <locale.h>. */
273 const char *grouping;
277 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
278 if (*grouping <= 0 || *grouping == CHAR_MAX)
282 /* Figure out the thousands separator character. */
283 # if defined _LIBC || defined _HAVE_BTOWC
284 thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
285 if (thousands == WEOF)
288 if (thousands == L'\0')
296 if (base < 0 || base == 1 || base > 36)
298 __set_errno (EINVAL);
304 /* Skip white space. */
310 /* Check for a sign. */
316 else if (*s == L_('+'))
324 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
327 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
338 /* Save the pointer so we can check later if anything happened. */
341 #ifdef USE_NUMBER_GROUPING
344 /* Find the end of the digit string and check its grouping. */
346 for (c = *end; c != L_('\0'); c = *++end)
347 if ((wchar_t) c != thousands
348 && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
349 && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
354 end = correctly_grouped_prefix (s, end, thousands, grouping);
360 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
361 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
365 for (c = *s; c != L_('\0'); c = *++s)
369 if (c >= L_('0') && c <= L_('9'))
371 else if (ISALPHA (c))
372 c = TOUPPER (c) - L_('A') + 10;
377 /* Check for overflow. */
378 if (i > cutoff || (i == cutoff && c > cutlim))
382 i *= (unsigned LONG int) base;
387 /* Check if anything actually happened. */
391 /* Store in ENDPTR the address of one character
392 past the last character we converted. */
394 *endptr = (STRING_TYPE *) s;
397 /* Check for a value that is within the range of
398 `unsigned LONG int', but outside the range of `LONG int'. */
401 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
402 : (unsigned LONG int) STRTOL_LONG_MAX))
408 __set_errno (ERANGE);
410 return STRTOL_ULONG_MAX;
412 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
416 /* Return the result of the appropriate sign. */
417 return negative ? -i : i;
420 /* We must handle a special case here: the base is 0 or 16 and the
421 first two characters are '0' and 'x', but the rest are no
422 hexadecimal digits. This is no error case. We return 0 and
423 ENDPTR points to the `x`. */
426 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
427 && save[-2] == L_('0'))
428 *endptr = (STRING_TYPE *) &save[-1];
430 /* There was no number to convert. */
431 *endptr = (STRING_TYPE *) nptr;
437 /* External user entry point. */
444 strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
445 int base LOCALE_PARAM_PROTO)
447 return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);