1 /* Convert string representation of a number into an integer value.
2 Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 # define USE_NUMBER_GROUPING
27 # define HAVE_LIMITS_H
36 # define __set_errno(Val) errno = (Val)
54 #ifdef USE_NUMBER_GROUPING
55 # include "../locale/localeinfo.h"
58 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
64 # define INT unsigned LONG int
67 /* Determine the name. */
68 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
72 # define strtol __wcstoull_l
74 # define strtol __wcstoul_l
78 # define strtol __strtoull_l
80 # define strtol __strtoul_l
86 # define strtol __wcstoll_l
88 # define strtol __wcstol_l
92 # define strtol __strtoll_l
94 # define strtol __strtol_l
100 # ifdef USE_WIDE_CHAR
102 # define strtol wcstoull
104 # define strtol wcstoul
108 # define strtol strtoull
110 # define strtol strtoul
114 # ifdef USE_WIDE_CHAR
116 # define strtol wcstoll
118 # define strtol wcstol
122 # define strtol strtoll
128 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
129 operating on `long long int's. */
131 # define LONG long long
132 # define STRTOL_LONG_MIN LONG_LONG_MIN
133 # define STRTOL_LONG_MAX LONG_LONG_MAX
134 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
135 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
136 /* Work around gcc bug with using this constant. */
137 static const unsigned long long int maxquad
= ULONG_LONG_MAX
;
138 # undef STRTOL_ULONG_MAX
139 # define STRTOL_ULONG_MAX maxquad
145 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
148 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
150 # define STRTOL_LONG_MIN LONG_MIN
151 # define STRTOL_LONG_MAX LONG_MAX
152 # define STRTOL_ULONG_MAX ULONG_MAX
156 /* We use this code also for the extended locale handling where the
157 function gets as an additional argument the locale which has to be
158 used. To access the values we have to redefine the _NL_CURRENT
160 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
162 # define _NL_CURRENT(category, item) \
163 (current->values[_NL_ITEM_INDEX (item)].string)
164 # define LOCALE_PARAM , loc
165 # define LOCALE_PARAM_DECL __locale_t loc;
167 # define LOCALE_PARAM
168 # define LOCALE_PARAM_DECL
171 #if defined _LIBC || defined HAVE_WCHAR_H
177 # define L_(Ch) L##Ch
178 # define UCHAR_TYPE wint_t
179 # define STRING_TYPE wchar_t
180 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
181 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
182 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
183 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
185 # define ISSPACE(Ch) iswspace (Ch)
186 # define ISALPHA(Ch) iswalpha (Ch)
187 # define TOUPPER(Ch) towupper (Ch)
190 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
191 # define IN_CTYPE_DOMAIN(c) 1
193 # define IN_CTYPE_DOMAIN(c) isascii(c)
196 # define UCHAR_TYPE unsigned char
197 # define STRING_TYPE char
198 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
199 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
200 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
201 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
203 # define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
204 # define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
205 # define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
210 # define INTERNAL(X) INTERNAL1(X)
211 # define INTERNAL1(X) __##X##_internal
212 # define WEAKNAME(X) WEAKNAME1(X)
214 # define INTERNAL(X) __/**/X/**/_internal
217 #ifdef USE_NUMBER_GROUPING
218 /* This file defines a function to check for correct grouping. */
219 # include "grouping.h"
224 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
225 If BASE is 0 the base is determined by the presence of a leading
226 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
227 If BASE is < 2 or > 36, it is reset to 10.
228 If ENDPTR is not NULL, a pointer to the character after the last
229 one converted is stored in *ENDPTR. */
232 INTERNAL (strtol
) (nptr
, endptr
, base
, group LOCALE_PARAM
)
233 const STRING_TYPE
*nptr
;
234 STRING_TYPE
**endptr
;
240 register unsigned LONG
int cutoff
;
241 register unsigned int cutlim
;
242 register unsigned LONG
int i
;
243 register const STRING_TYPE
*s
;
244 register UCHAR_TYPE c
;
245 const STRING_TYPE
*save
, *end
;
247 #ifndef USE_WIDE_CHAR
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 # ifdef USE_WIDE_CHAR
257 wchar_t thousands
= L
'\0';
259 const char *thousands
= NULL
;
260 size_t thousands_len
= 0;
262 /* The numeric grouping specification of the current locale,
263 in the format described in <locale.h>. */
264 const char *grouping
;
268 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
269 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
273 /* Figure out the thousands separator character. */
274 # ifdef USE_WIDE_CHAR
276 thousands
= _NL_CURRENT_WORD (LC_NUMERIC
,
277 _NL_NUMERIC_THOUSANDS_SEP_WC
);
279 if (thousands
== L
'\0')
283 thousands
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
285 if (*thousands
== '\0')
297 if (base
< 0 || base
== 1 || base
> 36)
299 __set_errno (EINVAL
);
305 /* Skip white space. */
311 /* Check for a sign. */
317 else if (*s
== L_('+'))
325 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
328 if ((base
== 0 || base
== 16) && TOUPPER (s
[1]) == L_('X'))
339 /* Save the pointer so we can check later if anything happened. */
342 #ifdef USE_NUMBER_GROUPING
348 # ifndef USE_WIDE_CHAR
349 thousands_len
= strlen (thousands
);
352 /* Find the end of the digit string and check its grouping. */
355 # ifdef USE_WIDE_CHAR
358 ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
359 if (thousands
[cnt
] != end
[cnt
])
361 cnt
< thousands_len
; })
365 for (c
= *end
; c
!= L_('\0'); c
= *++end
)
366 if (((wchar_t) c
< L_('0') || (wchar_t) c
> L_('9'))
367 # ifdef USE_WIDE_CHAR
370 && ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
371 if (thousands
[cnt
] != end
[cnt
])
373 cnt
< thousands_len
; })
376 || (int) (TOUPPER (c
) - L_('A') + 10) >= base
))
379 end
= correctly_grouped_prefix (s
, end
, thousands
, grouping
);
386 cutoff
= STRTOL_ULONG_MAX
/ (unsigned LONG
int) base
;
387 cutlim
= STRTOL_ULONG_MAX
% (unsigned LONG
int) base
;
392 if (sizeof (long int) != sizeof (LONG
int))
394 unsigned long int j
= 0;
395 unsigned long int jmax
= ULONG_MAX
/ base
;
397 for (;c
!= L_('\0'); c
= *++s
)
401 if (c
>= L_('0') && c
<= L_('9'))
403 #ifdef USE_NUMBER_GROUPING
404 # ifdef USE_WIDE_CHAR
405 else if (grouping
&& c
== thousands
)
408 else if (thousands_len
)
410 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
411 if (thousands
[cnt
] != s
[cnt
])
413 if (cnt
== thousands_len
)
415 s
+= thousands_len
- 1;
419 c
= TOUPPER (c
) - L_('A') + 10;
425 else if (ISALPHA (c
))
426 c
= TOUPPER (c
) - L_('A') + 10;
431 /* Note that we never can have an overflow. */
434 /* We have an overflow. Now use the long representation. */
435 i
= (unsigned LONG
int) j
;
439 j
= j
* (unsigned long int) base
+ c
;
442 i
= (unsigned LONG
int) j
;
445 for (;c
!= L_('\0'); c
= *++s
)
449 if (c
>= L_('0') && c
<= L_('9'))
451 #ifdef USE_NUMBER_GROUPING
452 # ifdef USE_WIDE_CHAR
453 else if (grouping
&& c
== thousands
)
456 else if (thousands_len
)
458 for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
459 if (thousands
[cnt
] != s
[cnt
])
461 if (cnt
== thousands_len
)
463 s
+= thousands_len
- 1;
467 c
= TOUPPER (c
) - L_('A') + 10;
473 else if (ISALPHA (c
))
474 c
= TOUPPER (c
) - L_('A') + 10;
479 /* Check for overflow. */
480 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
485 i
*= (unsigned LONG
int) base
;
490 /* Check if anything actually happened. */
494 /* Store in ENDPTR the address of one character
495 past the last character we converted. */
497 *endptr
= (STRING_TYPE
*) s
;
500 /* Check for a value that is within the range of
501 `unsigned LONG int', but outside the range of `LONG int'. */
504 ? -((unsigned LONG
int) (STRTOL_LONG_MIN
+ 1)) + 1
505 : (unsigned LONG
int) STRTOL_LONG_MAX
))
511 __set_errno (ERANGE
);
513 return STRTOL_ULONG_MAX
;
515 return negative
? STRTOL_LONG_MIN
: STRTOL_LONG_MAX
;
519 /* Return the result of the appropriate sign. */
520 return negative
? -i
: i
;
523 /* We must handle a special case here: the base is 0 or 16 and the
524 first two characters are '0' and 'x', but the rest are no
525 hexadecimal digits. This is no error case. We return 0 and
526 ENDPTR points to the `x`. */
529 if (save
- nptr
>= 2 && TOUPPER (save
[-1]) == L_('X')
530 && save
[-2] == L_('0'))
531 *endptr
= (STRING_TYPE
*) &save
[-1];
533 /* There was no number to convert. */
534 *endptr
= (STRING_TYPE
*) nptr
;
540 /* External user entry point. */
544 # if defined (__STDC__) && __STDC__
545 # define PARAMS(Args) Args
547 # define PARAMS(Args) ()
551 INT strtol
PARAMS ((const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
, int base
));
559 strtol (nptr
, endptr
, base LOCALE_PARAM
)
560 const STRING_TYPE
*nptr
;
561 STRING_TYPE
**endptr
;
565 return INTERNAL (strtol
) (nptr
, endptr
, base
, 0 LOCALE_PARAM
);