strtol.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Convert string representation of a number into an integer value.
  2. Copyright (C) 1991-1992, 1994-1999, 2003, 2005-2007, 2009-2011 Free Software
  3. Foundation, Inc.
  4. NOTE: The canonical source of this file is maintained with the GNU C
  5. Library. Bugs can be reported to bug-glibc@gnu.org.
  6. This program is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3 of the License, or any
  9. later version.
  10. This program 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #ifdef _LIBC
  17. # define USE_NUMBER_GROUPING
  18. #else
  19. # include <config.h>
  20. #endif
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #ifndef __set_errno
  24. # define __set_errno(Val) errno = (Val)
  25. #endif
  26. #include <limits.h>
  27. #include <stddef.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifdef USE_NUMBER_GROUPING
  31. # include "../locale/localeinfo.h"
  32. #endif
  33. /* Nonzero if we are defining `strtoul' or `strtoull', operating on
  34. unsigned integers. */
  35. #ifndef UNSIGNED
  36. # define UNSIGNED 0
  37. # define INT LONG int
  38. #else
  39. # define INT unsigned LONG int
  40. #endif
  41. /* Determine the name. */
  42. #ifdef USE_IN_EXTENDED_LOCALE_MODEL
  43. # if UNSIGNED
  44. # ifdef USE_WIDE_CHAR
  45. # ifdef QUAD
  46. # define strtol __wcstoull_l
  47. # else
  48. # define strtol __wcstoul_l
  49. # endif
  50. # else
  51. # ifdef QUAD
  52. # define strtol __strtoull_l
  53. # else
  54. # define strtol __strtoul_l
  55. # endif
  56. # endif
  57. # else
  58. # ifdef USE_WIDE_CHAR
  59. # ifdef QUAD
  60. # define strtol __wcstoll_l
  61. # else
  62. # define strtol __wcstol_l
  63. # endif
  64. # else
  65. # ifdef QUAD
  66. # define strtol __strtoll_l
  67. # else
  68. # define strtol __strtol_l
  69. # endif
  70. # endif
  71. # endif
  72. #else
  73. # if UNSIGNED
  74. # ifdef USE_WIDE_CHAR
  75. # ifdef QUAD
  76. # define strtol wcstoull
  77. # else
  78. # define strtol wcstoul
  79. # endif
  80. # else
  81. # ifdef QUAD
  82. # define strtol strtoull
  83. # else
  84. # define strtol strtoul
  85. # endif
  86. # endif
  87. # else
  88. # ifdef USE_WIDE_CHAR
  89. # ifdef QUAD
  90. # define strtol wcstoll
  91. # else
  92. # define strtol wcstol
  93. # endif
  94. # else
  95. # ifdef QUAD
  96. # define strtol strtoll
  97. # endif
  98. # endif
  99. # endif
  100. #endif
  101. /* If QUAD is defined, we are defining `strtoll' or `strtoull',
  102. operating on `long long int's. */
  103. #ifdef QUAD
  104. # define LONG long long
  105. # define STRTOL_LONG_MIN LLONG_MIN
  106. # define STRTOL_LONG_MAX LLONG_MAX
  107. # define STRTOL_ULONG_MAX ULLONG_MAX
  108. /* The extra casts in the following macros work around compiler bugs,
  109. e.g., in Cray C 5.0.3.0. */
  110. /* True if negative values of the signed integer type T use two's
  111. complement, ones' complement, or signed magnitude representation,
  112. respectively. Much GNU code assumes two's complement, but some
  113. people like to be portable to all possible C hosts. */
  114. # define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
  115. # define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
  116. # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
  117. /* True if the arithmetic type T is signed. */
  118. # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  119. /* The maximum and minimum values for the integer type T. These
  120. macros have undefined behavior if T is signed and has padding bits.
  121. If this is a problem for you, please let us know how to fix it for
  122. your host. */
  123. # define TYPE_MINIMUM(t) \
  124. ((t) (! TYPE_SIGNED (t) \
  125. ? (t) 0 \
  126. : TYPE_SIGNED_MAGNITUDE (t) \
  127. ? ~ (t) 0 \
  128. : ~ TYPE_MAXIMUM (t)))
  129. # define TYPE_MAXIMUM(t) \
  130. ((t) (! TYPE_SIGNED (t) \
  131. ? (t) -1 \
  132. : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
  133. # ifndef ULLONG_MAX
  134. # define ULLONG_MAX TYPE_MAXIMUM (unsigned long long)
  135. # endif
  136. # ifndef LLONG_MAX
  137. # define LLONG_MAX TYPE_MAXIMUM (long long int)
  138. # endif
  139. # ifndef LLONG_MIN
  140. # define LLONG_MIN TYPE_MINIMUM (long long int)
  141. # endif
  142. # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
  143. /* Work around gcc bug with using this constant. */
  144. static const unsigned long long int maxquad = ULLONG_MAX;
  145. # undef STRTOL_ULONG_MAX
  146. # define STRTOL_ULONG_MAX maxquad
  147. # endif
  148. #else
  149. # define LONG long
  150. # define STRTOL_LONG_MIN LONG_MIN
  151. # define STRTOL_LONG_MAX LONG_MAX
  152. # define STRTOL_ULONG_MAX ULONG_MAX
  153. #endif
  154. /* We use this code also for the extended locale handling where the
  155. function gets as an additional argument the locale which has to be
  156. used. To access the values we have to redefine the _NL_CURRENT
  157. macro. */
  158. #ifdef USE_IN_EXTENDED_LOCALE_MODEL
  159. # undef _NL_CURRENT
  160. # define _NL_CURRENT(category, item) \
  161. (current->values[_NL_ITEM_INDEX (item)].string)
  162. # define LOCALE_PARAM , loc
  163. # define LOCALE_PARAM_PROTO , __locale_t loc
  164. #else
  165. # define LOCALE_PARAM
  166. # define LOCALE_PARAM_PROTO
  167. #endif
  168. #ifdef USE_WIDE_CHAR
  169. # include <wchar.h>
  170. # include <wctype.h>
  171. # define L_(Ch) L##Ch
  172. # define UCHAR_TYPE wint_t
  173. # define STRING_TYPE wchar_t
  174. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  175. # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
  176. # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
  177. # define TOUPPER(Ch) __towupper_l ((Ch), loc)
  178. # else
  179. # define ISSPACE(Ch) iswspace (Ch)
  180. # define ISALPHA(Ch) iswalpha (Ch)
  181. # define TOUPPER(Ch) towupper (Ch)
  182. # endif
  183. #else
  184. # define L_(Ch) Ch
  185. # define UCHAR_TYPE unsigned char
  186. # define STRING_TYPE char
  187. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  188. # define ISSPACE(Ch) __isspace_l ((Ch), loc)
  189. # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
  190. # define TOUPPER(Ch) __toupper_l ((Ch), loc)
  191. # else
  192. # define ISSPACE(Ch) isspace (Ch)
  193. # define ISALPHA(Ch) isalpha (Ch)
  194. # define TOUPPER(Ch) toupper (Ch)
  195. # endif
  196. #endif
  197. #define INTERNAL(X) INTERNAL1(X)
  198. #define INTERNAL1(X) __##X##_internal
  199. #define WEAKNAME(X) WEAKNAME1(X)
  200. #ifdef USE_NUMBER_GROUPING
  201. /* This file defines a function to check for correct grouping. */
  202. # include "grouping.h"
  203. #endif
  204. /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
  205. If BASE is 0 the base is determined by the presence of a leading
  206. zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
  207. If BASE is < 2 or > 36, it is reset to 10.
  208. If ENDPTR is not NULL, a pointer to the character after the last
  209. one converted is stored in *ENDPTR. */
  210. INT
  211. INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  212. int base, int group LOCALE_PARAM_PROTO)
  213. {
  214. int negative;
  215. register unsigned LONG int cutoff;
  216. register unsigned int cutlim;
  217. register unsigned LONG int i;
  218. register const STRING_TYPE *s;
  219. register UCHAR_TYPE c;
  220. const STRING_TYPE *save, *end;
  221. int overflow;
  222. #ifdef USE_NUMBER_GROUPING
  223. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  224. struct locale_data *current = loc->__locales[LC_NUMERIC];
  225. # endif
  226. /* The thousands character of the current locale. */
  227. wchar_t thousands = L'\0';
  228. /* The numeric grouping specification of the current locale,
  229. in the format described in <locale.h>. */
  230. const char *grouping;
  231. if (group)
  232. {
  233. grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
  234. if (*grouping <= 0 || *grouping == CHAR_MAX)
  235. grouping = NULL;
  236. else
  237. {
  238. /* Figure out the thousands separator character. */
  239. # if defined _LIBC || defined _HAVE_BTOWC
  240. thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
  241. if (thousands == WEOF)
  242. thousands = L'\0';
  243. # endif
  244. if (thousands == L'\0')
  245. grouping = NULL;
  246. }
  247. }
  248. else
  249. grouping = NULL;
  250. #endif
  251. if (base < 0 || base == 1 || base > 36)
  252. {
  253. __set_errno (EINVAL);
  254. return 0;
  255. }
  256. save = s = nptr;
  257. /* Skip white space. */
  258. while (ISSPACE (*s))
  259. ++s;
  260. if (*s == L_('\0'))
  261. goto noconv;
  262. /* Check for a sign. */
  263. if (*s == L_('-'))
  264. {
  265. negative = 1;
  266. ++s;
  267. }
  268. else if (*s == L_('+'))
  269. {
  270. negative = 0;
  271. ++s;
  272. }
  273. else
  274. negative = 0;
  275. /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
  276. if (*s == L_('0'))
  277. {
  278. if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
  279. {
  280. s += 2;
  281. base = 16;
  282. }
  283. else if (base == 0)
  284. base = 8;
  285. }
  286. else if (base == 0)
  287. base = 10;
  288. /* Save the pointer so we can check later if anything happened. */
  289. save = s;
  290. #ifdef USE_NUMBER_GROUPING
  291. if (group)
  292. {
  293. /* Find the end of the digit string and check its grouping. */
  294. end = s;
  295. for (c = *end; c != L_('\0'); c = *++end)
  296. if ((wchar_t) c != thousands
  297. && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
  298. && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
  299. break;
  300. if (*s == thousands)
  301. end = s;
  302. else
  303. end = correctly_grouped_prefix (s, end, thousands, grouping);
  304. }
  305. else
  306. #endif
  307. end = NULL;
  308. cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
  309. cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
  310. overflow = 0;
  311. i = 0;
  312. for (c = *s; c != L_('\0'); c = *++s)
  313. {
  314. if (s == end)
  315. break;
  316. if (c >= L_('0') && c <= L_('9'))
  317. c -= L_('0');
  318. else if (ISALPHA (c))
  319. c = TOUPPER (c) - L_('A') + 10;
  320. else
  321. break;
  322. if ((int) c >= base)
  323. break;
  324. /* Check for overflow. */
  325. if (i > cutoff || (i == cutoff && c > cutlim))
  326. overflow = 1;
  327. else
  328. {
  329. i *= (unsigned LONG int) base;
  330. i += c;
  331. }
  332. }
  333. /* Check if anything actually happened. */
  334. if (s == save)
  335. goto noconv;
  336. /* Store in ENDPTR the address of one character
  337. past the last character we converted. */
  338. if (endptr != NULL)
  339. *endptr = (STRING_TYPE *) s;
  340. #if !UNSIGNED
  341. /* Check for a value that is within the range of
  342. `unsigned LONG int', but outside the range of `LONG int'. */
  343. if (overflow == 0
  344. && i > (negative
  345. ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
  346. : (unsigned LONG int) STRTOL_LONG_MAX))
  347. overflow = 1;
  348. #endif
  349. if (overflow)
  350. {
  351. __set_errno (ERANGE);
  352. #if UNSIGNED
  353. return STRTOL_ULONG_MAX;
  354. #else
  355. return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
  356. #endif
  357. }
  358. /* Return the result of the appropriate sign. */
  359. return negative ? -i : i;
  360. noconv:
  361. /* We must handle a special case here: the base is 0 or 16 and the
  362. first two characters are '0' and 'x', but the rest are no
  363. hexadecimal digits. This is no error case. We return 0 and
  364. ENDPTR points to the `x`. */
  365. if (endptr != NULL)
  366. {
  367. if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
  368. && save[-2] == L_('0'))
  369. *endptr = (STRING_TYPE *) &save[-1];
  370. else
  371. /* There was no number to convert. */
  372. *endptr = (STRING_TYPE *) nptr;
  373. }
  374. return 0L;
  375. }
  376. /* External user entry point. */
  377. INT
  378. #ifdef weak_function
  379. weak_function
  380. #endif
  381. strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  382. int base LOCALE_PARAM_PROTO)
  383. {
  384. return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);
  385. }