/Python/mystrtoul.c

http://unladen-swallow.googlecode.com/ · C · 285 lines · 210 code · 32 blank · 43 comment · 83 complexity · 0cd7dc64cfd9ca820f6c99bc3095eea5 MD5 · raw file

  1. #include "Python.h"
  2. #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
  3. #define _SGI_MP_SOURCE
  4. #endif
  5. /* strtol and strtoul, renamed to avoid conflicts */
  6. #include <ctype.h>
  7. #ifdef HAVE_ERRNO_H
  8. #include <errno.h>
  9. #endif
  10. /* Static overflow check values for bases 2 through 36.
  11. * smallmax[base] is the largest unsigned long i such that
  12. * i * base doesn't overflow unsigned long.
  13. */
  14. static unsigned long smallmax[] = {
  15. 0, /* bases 0 and 1 are invalid */
  16. 0,
  17. ULONG_MAX / 2,
  18. ULONG_MAX / 3,
  19. ULONG_MAX / 4,
  20. ULONG_MAX / 5,
  21. ULONG_MAX / 6,
  22. ULONG_MAX / 7,
  23. ULONG_MAX / 8,
  24. ULONG_MAX / 9,
  25. ULONG_MAX / 10,
  26. ULONG_MAX / 11,
  27. ULONG_MAX / 12,
  28. ULONG_MAX / 13,
  29. ULONG_MAX / 14,
  30. ULONG_MAX / 15,
  31. ULONG_MAX / 16,
  32. ULONG_MAX / 17,
  33. ULONG_MAX / 18,
  34. ULONG_MAX / 19,
  35. ULONG_MAX / 20,
  36. ULONG_MAX / 21,
  37. ULONG_MAX / 22,
  38. ULONG_MAX / 23,
  39. ULONG_MAX / 24,
  40. ULONG_MAX / 25,
  41. ULONG_MAX / 26,
  42. ULONG_MAX / 27,
  43. ULONG_MAX / 28,
  44. ULONG_MAX / 29,
  45. ULONG_MAX / 30,
  46. ULONG_MAX / 31,
  47. ULONG_MAX / 32,
  48. ULONG_MAX / 33,
  49. ULONG_MAX / 34,
  50. ULONG_MAX / 35,
  51. ULONG_MAX / 36,
  52. };
  53. /* maximum digits that can't ever overflow for bases 2 through 36,
  54. * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)].
  55. * Note that this is pessimistic if sizeof(long) > 4.
  56. */
  57. #if SIZEOF_LONG == 4
  58. static int digitlimit[] = {
  59. 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
  60. 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
  61. 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
  62. 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
  63. #elif SIZEOF_LONG == 8
  64. /* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
  65. static int digitlimit[] = {
  66. 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
  67. 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
  68. 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
  69. 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
  70. #else
  71. #error "Need table for SIZEOF_LONG"
  72. #endif
  73. /*
  74. ** strtoul
  75. ** This is a general purpose routine for converting
  76. ** an ascii string to an integer in an arbitrary base.
  77. ** Leading white space is ignored. If 'base' is zero
  78. ** it looks for a leading 0, 0b, 0B, 0o, 0O, 0x or 0X
  79. ** to tell which base. If these are absent it defaults
  80. ** to 10. Base must be 0 or between 2 and 36 (inclusive).
  81. ** If 'ptr' is non-NULL it will contain a pointer to
  82. ** the end of the scan.
  83. ** Errors due to bad pointers will probably result in
  84. ** exceptions - we don't check for them.
  85. */
  86. unsigned long
  87. PyOS_strtoul(register char *str, char **ptr, int base)
  88. {
  89. register unsigned long result = 0; /* return value of the function */
  90. register int c; /* current input character */
  91. register int ovlimit; /* required digits to overflow */
  92. /* skip leading white space */
  93. while (*str && isspace(Py_CHARMASK(*str)))
  94. ++str;
  95. /* check for leading 0 or 0x for auto-base or base 16 */
  96. switch (base) {
  97. case 0: /* look for leading 0, 0b, 0o or 0x */
  98. if (*str == '0') {
  99. ++str;
  100. if (*str == 'x' || *str == 'X') {
  101. /* there must be at least one digit after 0x */
  102. if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
  103. if (ptr)
  104. *ptr = str;
  105. return 0;
  106. }
  107. ++str;
  108. base = 16;
  109. } else if (*str == 'o' || *str == 'O') {
  110. /* there must be at least one digit after 0o */
  111. if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
  112. if (ptr)
  113. *ptr = str;
  114. return 0;
  115. }
  116. ++str;
  117. base = 8;
  118. } else if (*str == 'b' || *str == 'B') {
  119. /* there must be at least one digit after 0b */
  120. if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
  121. if (ptr)
  122. *ptr = str;
  123. return 0;
  124. }
  125. ++str;
  126. base = 2;
  127. } else {
  128. base = 8;
  129. }
  130. }
  131. else
  132. base = 10;
  133. break;
  134. case 2: /* skip leading 0b or 0B */
  135. if (*str == '0') {
  136. ++str;
  137. if (*str == 'b' || *str == 'B') {
  138. /* there must be at least one digit after 0b */
  139. if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
  140. if (ptr)
  141. *ptr = str;
  142. return 0;
  143. }
  144. ++str;
  145. }
  146. }
  147. break;
  148. case 8: /* skip leading 0o or 0O */
  149. if (*str == '0') {
  150. ++str;
  151. if (*str == 'o' || *str == 'O') {
  152. /* there must be at least one digit after 0o */
  153. if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
  154. if (ptr)
  155. *ptr = str;
  156. return 0;
  157. }
  158. ++str;
  159. }
  160. }
  161. break;
  162. case 16: /* skip leading 0x or 0X */
  163. if (*str == '0') {
  164. ++str;
  165. if (*str == 'x' || *str == 'X') {
  166. /* there must be at least one digit after 0x */
  167. if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
  168. if (ptr)
  169. *ptr = str;
  170. return 0;
  171. }
  172. ++str;
  173. }
  174. }
  175. break;
  176. }
  177. /* catch silly bases */
  178. if (base < 2 || base > 36) {
  179. if (ptr)
  180. *ptr = str;
  181. return 0;
  182. }
  183. /* skip leading zeroes */
  184. while (*str == '0')
  185. ++str;
  186. /* base is guaranteed to be in [2, 36] at this point */
  187. ovlimit = digitlimit[base];
  188. /* do the conversion until non-digit character encountered */
  189. while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
  190. if (ovlimit > 0) /* no overflow check required */
  191. result = result * base + c;
  192. else { /* requires overflow check */
  193. register unsigned long temp_result;
  194. if (ovlimit < 0) /* guaranteed overflow */
  195. goto overflowed;
  196. /* there could be an overflow */
  197. /* check overflow just from shifting */
  198. if (result > smallmax[base])
  199. goto overflowed;
  200. result *= base;
  201. /* check overflow from the digit's value */
  202. temp_result = result + c;
  203. if (temp_result < result)
  204. goto overflowed;
  205. result = temp_result;
  206. }
  207. ++str;
  208. --ovlimit;
  209. }
  210. /* set pointer to point to the last character scanned */
  211. if (ptr)
  212. *ptr = str;
  213. return result;
  214. overflowed:
  215. if (ptr) {
  216. /* spool through remaining digit characters */
  217. while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
  218. ++str;
  219. *ptr = str;
  220. }
  221. errno = ERANGE;
  222. return (unsigned long)-1;
  223. }
  224. /* Checking for overflow in PyOS_strtol is a PITA; see comments
  225. * about PY_ABS_LONG_MIN in longobject.c.
  226. */
  227. #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
  228. long
  229. PyOS_strtol(char *str, char **ptr, int base)
  230. {
  231. long result;
  232. unsigned long uresult;
  233. char sign;
  234. while (*str && isspace(Py_CHARMASK(*str)))
  235. str++;
  236. sign = *str;
  237. if (sign == '+' || sign == '-')
  238. str++;
  239. uresult = PyOS_strtoul(str, ptr, base);
  240. if (uresult <= (unsigned long)LONG_MAX) {
  241. result = (long)uresult;
  242. if (sign == '-')
  243. result = -result;
  244. }
  245. else if (sign == '-' && uresult == PY_ABS_LONG_MIN) {
  246. result = LONG_MIN;
  247. }
  248. else {
  249. errno = ERANGE;
  250. result = LONG_MAX;
  251. }
  252. return result;
  253. }