strtol.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* Convert string representation of a number into an integer value.
  2. Copyright (C) 1991-1992, 1994-1999, 2003, 2005-2007, 2009-2017 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 the arithmetic type T is signed. */
  111. # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  112. /* Minimum and maximum values for integer types.
  113. These macros have undefined behavior for signed types that either
  114. have padding bits or do not use two's complement. If this is a
  115. problem for you, please let us know how to fix it for your host. */
  116. /* The maximum and minimum values for the integer type T. */
  117. # define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  118. # define TYPE_MAXIMUM(t) \
  119. ((t) (! TYPE_SIGNED (t) \
  120. ? (t) -1 \
  121. : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
  122. # ifndef ULLONG_MAX
  123. # define ULLONG_MAX TYPE_MAXIMUM (unsigned long long)
  124. # endif
  125. # ifndef LLONG_MAX
  126. # define LLONG_MAX TYPE_MAXIMUM (long long int)
  127. # endif
  128. # ifndef LLONG_MIN
  129. # define LLONG_MIN TYPE_MINIMUM (long long int)
  130. # endif
  131. # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
  132. /* Work around gcc bug with using this constant. */
  133. static const unsigned long long int maxquad = ULLONG_MAX;
  134. # undef STRTOL_ULONG_MAX
  135. # define STRTOL_ULONG_MAX maxquad
  136. # endif
  137. #else
  138. # define LONG long
  139. # define STRTOL_LONG_MIN LONG_MIN
  140. # define STRTOL_LONG_MAX LONG_MAX
  141. # define STRTOL_ULONG_MAX ULONG_MAX
  142. #endif
  143. /* We use this code also for the extended locale handling where the
  144. function gets as an additional argument the locale which has to be
  145. used. To access the values we have to redefine the _NL_CURRENT
  146. macro. */
  147. #ifdef USE_IN_EXTENDED_LOCALE_MODEL
  148. # undef _NL_CURRENT
  149. # define _NL_CURRENT(category, item) \
  150. (current->values[_NL_ITEM_INDEX (item)].string)
  151. # define LOCALE_PARAM , loc
  152. # define LOCALE_PARAM_PROTO , __locale_t loc
  153. #else
  154. # define LOCALE_PARAM
  155. # define LOCALE_PARAM_PROTO
  156. #endif
  157. #ifdef USE_WIDE_CHAR
  158. # include <wchar.h>
  159. # include <wctype.h>
  160. # define L_(Ch) L##Ch
  161. # define UCHAR_TYPE wint_t
  162. # define STRING_TYPE wchar_t
  163. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  164. # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
  165. # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
  166. # define TOUPPER(Ch) __towupper_l ((Ch), loc)
  167. # else
  168. # define ISSPACE(Ch) iswspace (Ch)
  169. # define ISALPHA(Ch) iswalpha (Ch)
  170. # define TOUPPER(Ch) towupper (Ch)
  171. # endif
  172. #else
  173. # define L_(Ch) Ch
  174. # define UCHAR_TYPE unsigned char
  175. # define STRING_TYPE char
  176. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  177. # define ISSPACE(Ch) __isspace_l ((Ch), loc)
  178. # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
  179. # define TOUPPER(Ch) __toupper_l ((Ch), loc)
  180. # else
  181. # define ISSPACE(Ch) isspace (Ch)
  182. # define ISALPHA(Ch) isalpha (Ch)
  183. # define TOUPPER(Ch) toupper (Ch)
  184. # endif
  185. #endif
  186. #define INTERNAL(X) INTERNAL1(X)
  187. #define INTERNAL1(X) __##X##_internal
  188. #define WEAKNAME(X) WEAKNAME1(X)
  189. #ifdef USE_NUMBER_GROUPING
  190. /* This file defines a function to check for correct grouping. */
  191. # include "grouping.h"
  192. #endif
  193. /* Convert NPTR to an 'unsigned long int' or 'long int' in base BASE.
  194. If BASE is 0 the base is determined by the presence of a leading
  195. zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
  196. If BASE is < 2 or > 36, it is reset to 10.
  197. If ENDPTR is not NULL, a pointer to the character after the last
  198. one converted is stored in *ENDPTR. */
  199. INT
  200. INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  201. int base, int group LOCALE_PARAM_PROTO)
  202. {
  203. int negative;
  204. register unsigned LONG int cutoff;
  205. register unsigned int cutlim;
  206. register unsigned LONG int i;
  207. register const STRING_TYPE *s;
  208. register UCHAR_TYPE c;
  209. const STRING_TYPE *save, *end;
  210. int overflow;
  211. #ifdef USE_NUMBER_GROUPING
  212. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  213. struct locale_data *current = loc->__locales[LC_NUMERIC];
  214. # endif
  215. /* The thousands character of the current locale. */
  216. wchar_t thousands = L'\0';
  217. /* The numeric grouping specification of the current locale,
  218. in the format described in <locale.h>. */
  219. const char *grouping;
  220. if (group)
  221. {
  222. grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
  223. if (*grouping <= 0 || *grouping == CHAR_MAX)
  224. grouping = NULL;
  225. else
  226. {
  227. /* Figure out the thousands separator character. */
  228. # if defined _LIBC || defined _HAVE_BTOWC
  229. thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
  230. if (thousands == WEOF)
  231. thousands = L'\0';
  232. # endif
  233. if (thousands == L'\0')
  234. grouping = NULL;
  235. }
  236. }
  237. else
  238. grouping = NULL;
  239. #endif
  240. if (base < 0 || base == 1 || base > 36)
  241. {
  242. __set_errno (EINVAL);
  243. return 0;
  244. }
  245. save = s = nptr;
  246. /* Skip white space. */
  247. while (ISSPACE (*s))
  248. ++s;
  249. if (*s == L_('\0'))
  250. goto noconv;
  251. /* Check for a sign. */
  252. if (*s == L_('-'))
  253. {
  254. negative = 1;
  255. ++s;
  256. }
  257. else if (*s == L_('+'))
  258. {
  259. negative = 0;
  260. ++s;
  261. }
  262. else
  263. negative = 0;
  264. /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
  265. if (*s == L_('0'))
  266. {
  267. if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
  268. {
  269. s += 2;
  270. base = 16;
  271. }
  272. else if (base == 0)
  273. base = 8;
  274. }
  275. else if (base == 0)
  276. base = 10;
  277. /* Save the pointer so we can check later if anything happened. */
  278. save = s;
  279. #ifdef USE_NUMBER_GROUPING
  280. if (group)
  281. {
  282. /* Find the end of the digit string and check its grouping. */
  283. end = s;
  284. for (c = *end; c != L_('\0'); c = *++end)
  285. if ((wchar_t) c != thousands
  286. && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
  287. && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
  288. break;
  289. if (*s == thousands)
  290. end = s;
  291. else
  292. end = correctly_grouped_prefix (s, end, thousands, grouping);
  293. }
  294. else
  295. #endif
  296. end = NULL;
  297. cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
  298. cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
  299. overflow = 0;
  300. i = 0;
  301. for (c = *s; c != L_('\0'); c = *++s)
  302. {
  303. if (s == end)
  304. break;
  305. if (c >= L_('0') && c <= L_('9'))
  306. c -= L_('0');
  307. else if (ISALPHA (c))
  308. c = TOUPPER (c) - L_('A') + 10;
  309. else
  310. break;
  311. if ((int) c >= base)
  312. break;
  313. /* Check for overflow. */
  314. if (i > cutoff || (i == cutoff && c > cutlim))
  315. overflow = 1;
  316. else
  317. {
  318. i *= (unsigned LONG int) base;
  319. i += c;
  320. }
  321. }
  322. /* Check if anything actually happened. */
  323. if (s == save)
  324. goto noconv;
  325. /* Store in ENDPTR the address of one character
  326. past the last character we converted. */
  327. if (endptr != NULL)
  328. *endptr = (STRING_TYPE *) s;
  329. #if !UNSIGNED
  330. /* Check for a value that is within the range of
  331. 'unsigned LONG int', but outside the range of 'LONG int'. */
  332. if (overflow == 0
  333. && i > (negative
  334. ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
  335. : (unsigned LONG int) STRTOL_LONG_MAX))
  336. overflow = 1;
  337. #endif
  338. if (overflow)
  339. {
  340. __set_errno (ERANGE);
  341. #if UNSIGNED
  342. return STRTOL_ULONG_MAX;
  343. #else
  344. return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
  345. #endif
  346. }
  347. /* Return the result of the appropriate sign. */
  348. return negative ? -i : i;
  349. noconv:
  350. /* We must handle a special case here: the base is 0 or 16 and the
  351. first two characters are '0' and 'x', but the rest are no
  352. hexadecimal digits. This is no error case. We return 0 and
  353. ENDPTR points to the 'x'. */
  354. if (endptr != NULL)
  355. {
  356. if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
  357. && save[-2] == L_('0'))
  358. *endptr = (STRING_TYPE *) &save[-1];
  359. else
  360. /* There was no number to convert. */
  361. *endptr = (STRING_TYPE *) nptr;
  362. }
  363. return 0L;
  364. }
  365. /* External user entry point. */
  366. INT
  367. #ifdef weak_function
  368. weak_function
  369. #endif
  370. strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  371. int base LOCALE_PARAM_PROTO)
  372. {
  373. return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);
  374. }