Fix typo in dired-omit-case-fold-p
[emacs.git] / lib / strtol.c
blob6ef8a9624c9ee69f4d0f0879ecc4b12bcaaf706c
1 /* Convert string representation of a number into an integer value.
3 Copyright (C) 1991-1992, 1994-1999, 2003, 2005-2007, 2009-2016 Free Software
4 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
12 later version.
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/>. */
22 #ifdef _LIBC
23 # define USE_NUMBER_GROUPING
24 #else
25 # include <config.h>
26 #endif
28 #include <ctype.h>
29 #include <errno.h>
30 #ifndef __set_errno
31 # define __set_errno(Val) errno = (Val)
32 #endif
34 #include <limits.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #ifdef USE_NUMBER_GROUPING
40 # include "../locale/localeinfo.h"
41 #endif
43 /* Nonzero if we are defining 'strtoul' or 'strtoull', operating on
44 unsigned integers. */
45 #ifndef UNSIGNED
46 # define UNSIGNED 0
47 # define INT LONG int
48 #else
49 # define INT unsigned LONG int
50 #endif
52 /* Determine the name. */
53 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
54 # if UNSIGNED
55 # ifdef USE_WIDE_CHAR
56 # ifdef QUAD
57 # define strtol __wcstoull_l
58 # else
59 # define strtol __wcstoul_l
60 # endif
61 # else
62 # ifdef QUAD
63 # define strtol __strtoull_l
64 # else
65 # define strtol __strtoul_l
66 # endif
67 # endif
68 # else
69 # ifdef USE_WIDE_CHAR
70 # ifdef QUAD
71 # define strtol __wcstoll_l
72 # else
73 # define strtol __wcstol_l
74 # endif
75 # else
76 # ifdef QUAD
77 # define strtol __strtoll_l
78 # else
79 # define strtol __strtol_l
80 # endif
81 # endif
82 # endif
83 #else
84 # if UNSIGNED
85 # ifdef USE_WIDE_CHAR
86 # ifdef QUAD
87 # define strtol wcstoull
88 # else
89 # define strtol wcstoul
90 # endif
91 # else
92 # ifdef QUAD
93 # define strtol strtoull
94 # else
95 # define strtol strtoul
96 # endif
97 # endif
98 # else
99 # ifdef USE_WIDE_CHAR
100 # ifdef QUAD
101 # define strtol wcstoll
102 # else
103 # define strtol wcstol
104 # endif
105 # else
106 # ifdef QUAD
107 # define strtol strtoll
108 # endif
109 # endif
110 # endif
111 #endif
113 /* If QUAD is defined, we are defining 'strtoll' or 'strtoull',
114 operating on 'long long int's. */
115 #ifdef QUAD
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 the arithmetic type T is signed. */
125 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
127 /* Minimum and maximum values for integer types.
128 These macros have undefined behavior for signed types that either
129 have padding bits or do not use two's complement. If this is a
130 problem for you, please let us know how to fix it for your host. */
132 /* The maximum and minimum values for the integer type T. */
133 # define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
134 # define TYPE_MAXIMUM(t) \
135 ((t) (! TYPE_SIGNED (t) \
136 ? (t) -1 \
137 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
139 # ifndef ULLONG_MAX
140 # define ULLONG_MAX TYPE_MAXIMUM (unsigned long long)
141 # endif
142 # ifndef LLONG_MAX
143 # define LLONG_MAX TYPE_MAXIMUM (long long int)
144 # endif
145 # ifndef LLONG_MIN
146 # define LLONG_MIN TYPE_MINIMUM (long long int)
147 # endif
149 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
150 /* Work around gcc bug with using this constant. */
151 static const unsigned long long int maxquad = ULLONG_MAX;
152 # undef STRTOL_ULONG_MAX
153 # define STRTOL_ULONG_MAX maxquad
154 # endif
155 #else
156 # define LONG long
157 # define STRTOL_LONG_MIN LONG_MIN
158 # define STRTOL_LONG_MAX LONG_MAX
159 # define STRTOL_ULONG_MAX ULONG_MAX
160 #endif
163 /* We use this code also for the extended locale handling where the
164 function gets as an additional argument the locale which has to be
165 used. To access the values we have to redefine the _NL_CURRENT
166 macro. */
167 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
168 # undef _NL_CURRENT
169 # define _NL_CURRENT(category, item) \
170 (current->values[_NL_ITEM_INDEX (item)].string)
171 # define LOCALE_PARAM , loc
172 # define LOCALE_PARAM_PROTO , __locale_t loc
173 #else
174 # define LOCALE_PARAM
175 # define LOCALE_PARAM_PROTO
176 #endif
178 #ifdef USE_WIDE_CHAR
179 # include <wchar.h>
180 # include <wctype.h>
181 # define L_(Ch) L##Ch
182 # define UCHAR_TYPE wint_t
183 # define STRING_TYPE wchar_t
184 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
185 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
186 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
187 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
188 # else
189 # define ISSPACE(Ch) iswspace (Ch)
190 # define ISALPHA(Ch) iswalpha (Ch)
191 # define TOUPPER(Ch) towupper (Ch)
192 # endif
193 #else
194 # define L_(Ch) Ch
195 # define UCHAR_TYPE unsigned char
196 # define STRING_TYPE char
197 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
198 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
199 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
200 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
201 # else
202 # define ISSPACE(Ch) isspace (Ch)
203 # define ISALPHA(Ch) isalpha (Ch)
204 # define TOUPPER(Ch) toupper (Ch)
205 # endif
206 #endif
208 #define INTERNAL(X) INTERNAL1(X)
209 #define INTERNAL1(X) __##X##_internal
210 #define WEAKNAME(X) WEAKNAME1(X)
212 #ifdef USE_NUMBER_GROUPING
213 /* This file defines a function to check for correct grouping. */
214 # include "grouping.h"
215 #endif
219 /* Convert NPTR to an 'unsigned long int' or 'long int' in base BASE.
220 If BASE is 0 the base is determined by the presence of a leading
221 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
222 If BASE is < 2 or > 36, it is reset to 10.
223 If ENDPTR is not NULL, a pointer to the character after the last
224 one converted is stored in *ENDPTR. */
227 INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
228 int base, int group LOCALE_PARAM_PROTO)
230 int negative;
231 register unsigned LONG int cutoff;
232 register unsigned int cutlim;
233 register unsigned LONG int i;
234 register const STRING_TYPE *s;
235 register UCHAR_TYPE c;
236 const STRING_TYPE *save, *end;
237 int overflow;
239 #ifdef USE_NUMBER_GROUPING
240 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
241 struct locale_data *current = loc->__locales[LC_NUMERIC];
242 # endif
243 /* The thousands character of the current locale. */
244 wchar_t thousands = L'\0';
245 /* The numeric grouping specification of the current locale,
246 in the format described in <locale.h>. */
247 const char *grouping;
249 if (group)
251 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
252 if (*grouping <= 0 || *grouping == CHAR_MAX)
253 grouping = NULL;
254 else
256 /* Figure out the thousands separator character. */
257 # if defined _LIBC || defined _HAVE_BTOWC
258 thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
259 if (thousands == WEOF)
260 thousands = L'\0';
261 # endif
262 if (thousands == L'\0')
263 grouping = NULL;
266 else
267 grouping = NULL;
268 #endif
270 if (base < 0 || base == 1 || base > 36)
272 __set_errno (EINVAL);
273 return 0;
276 save = s = nptr;
278 /* Skip white space. */
279 while (ISSPACE (*s))
280 ++s;
281 if (*s == L_('\0'))
282 goto noconv;
284 /* Check for a sign. */
285 if (*s == L_('-'))
287 negative = 1;
288 ++s;
290 else if (*s == L_('+'))
292 negative = 0;
293 ++s;
295 else
296 negative = 0;
298 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
299 if (*s == L_('0'))
301 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
303 s += 2;
304 base = 16;
306 else if (base == 0)
307 base = 8;
309 else if (base == 0)
310 base = 10;
312 /* Save the pointer so we can check later if anything happened. */
313 save = s;
315 #ifdef USE_NUMBER_GROUPING
316 if (group)
318 /* Find the end of the digit string and check its grouping. */
319 end = s;
320 for (c = *end; c != L_('\0'); c = *++end)
321 if ((wchar_t) c != thousands
322 && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
323 && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
324 break;
325 if (*s == thousands)
326 end = s;
327 else
328 end = correctly_grouped_prefix (s, end, thousands, grouping);
330 else
331 #endif
332 end = NULL;
334 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
335 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
337 overflow = 0;
338 i = 0;
339 for (c = *s; c != L_('\0'); c = *++s)
341 if (s == end)
342 break;
343 if (c >= L_('0') && c <= L_('9'))
344 c -= L_('0');
345 else if (ISALPHA (c))
346 c = TOUPPER (c) - L_('A') + 10;
347 else
348 break;
349 if ((int) c >= base)
350 break;
351 /* Check for overflow. */
352 if (i > cutoff || (i == cutoff && c > cutlim))
353 overflow = 1;
354 else
356 i *= (unsigned LONG int) base;
357 i += c;
361 /* Check if anything actually happened. */
362 if (s == save)
363 goto noconv;
365 /* Store in ENDPTR the address of one character
366 past the last character we converted. */
367 if (endptr != NULL)
368 *endptr = (STRING_TYPE *) s;
370 #if !UNSIGNED
371 /* Check for a value that is within the range of
372 'unsigned LONG int', but outside the range of 'LONG int'. */
373 if (overflow == 0
374 && i > (negative
375 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
376 : (unsigned LONG int) STRTOL_LONG_MAX))
377 overflow = 1;
378 #endif
380 if (overflow)
382 __set_errno (ERANGE);
383 #if UNSIGNED
384 return STRTOL_ULONG_MAX;
385 #else
386 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
387 #endif
390 /* Return the result of the appropriate sign. */
391 return negative ? -i : i;
393 noconv:
394 /* We must handle a special case here: the base is 0 or 16 and the
395 first two characters are '0' and 'x', but the rest are no
396 hexadecimal digits. This is no error case. We return 0 and
397 ENDPTR points to the 'x'. */
398 if (endptr != NULL)
400 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
401 && save[-2] == L_('0'))
402 *endptr = (STRING_TYPE *) &save[-1];
403 else
404 /* There was no number to convert. */
405 *endptr = (STRING_TYPE *) nptr;
408 return 0L;
411 /* External user entry point. */
415 #ifdef weak_function
416 weak_function
417 #endif
418 strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
419 int base LOCALE_PARAM_PROTO)
421 return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);