xatonum.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ascii-to-numbers implementations for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. #if __GNUC_PREREQ(4,1)
  10. # pragma GCC visibility push(hidden)
  11. #endif
  12. /* Provides extern declarations of functions */
  13. #define DECLARE_STR_CONV(type, T, UT) \
  14. \
  15. unsigned type xstrto##UT##_range_sfx(const char *str, int b, unsigned type l, unsigned type u, const struct suffix_mult *sfx) FAST_FUNC; \
  16. unsigned type xstrto##UT##_range(const char *str, int b, unsigned type l, unsigned type u) FAST_FUNC; \
  17. unsigned type xstrto##UT##_sfx(const char *str, int b, const struct suffix_mult *sfx) FAST_FUNC; \
  18. unsigned type xstrto##UT(const char *str, int b) FAST_FUNC; \
  19. unsigned type xato##UT##_range_sfx(const char *str, unsigned type l, unsigned type u, const struct suffix_mult *sfx) FAST_FUNC; \
  20. unsigned type xato##UT##_range(const char *str, unsigned type l, unsigned type u) FAST_FUNC; \
  21. unsigned type xato##UT##_sfx(const char *str, const struct suffix_mult *sfx) FAST_FUNC; \
  22. unsigned type xato##UT(const char *str) FAST_FUNC; \
  23. type xstrto##T##_range_sfx(const char *str, int b, type l, type u, const struct suffix_mult *sfx) FAST_FUNC; \
  24. type xstrto##T##_range(const char *str, int b, type l, type u) FAST_FUNC; \
  25. type xato##T##_range_sfx(const char *str, type l, type u, const struct suffix_mult *sfx) FAST_FUNC; \
  26. type xato##T##_range(const char *str, type l, type u) FAST_FUNC; \
  27. type xato##T##_sfx(const char *str, const struct suffix_mult *sfx) FAST_FUNC; \
  28. type xato##T(const char *str) FAST_FUNC; \
  29. /* Unsigned long long functions always exist */
  30. DECLARE_STR_CONV(long long, ll, ull)
  31. /* Provides inline definitions of functions */
  32. /* (useful for mapping them to the type of the same width) */
  33. #define DEFINE_EQUIV_STR_CONV(narrow, N, W, UN, UW) \
  34. \
  35. static ALWAYS_INLINE \
  36. unsigned narrow xstrto##UN##_range_sfx(const char *str, int b, unsigned narrow l, unsigned narrow u, const struct suffix_mult *sfx) \
  37. { return xstrto##UW##_range_sfx(str, b, l, u, sfx); } \
  38. static ALWAYS_INLINE \
  39. unsigned narrow xstrto##UN##_range(const char *str, int b, unsigned narrow l, unsigned narrow u) \
  40. { return xstrto##UW##_range(str, b, l, u); } \
  41. static ALWAYS_INLINE \
  42. unsigned narrow xstrto##UN##_sfx(const char *str, int b, const struct suffix_mult *sfx) \
  43. { return xstrto##UW##_sfx(str, b, sfx); } \
  44. static ALWAYS_INLINE \
  45. unsigned narrow xstrto##UN(const char *str, int b) \
  46. { return xstrto##UW(str, b); } \
  47. static ALWAYS_INLINE \
  48. unsigned narrow xato##UN##_range_sfx(const char *str, unsigned narrow l, unsigned narrow u, const struct suffix_mult *sfx) \
  49. { return xato##UW##_range_sfx(str, l, u, sfx); } \
  50. static ALWAYS_INLINE \
  51. unsigned narrow xato##UN##_range(const char *str, unsigned narrow l, unsigned narrow u) \
  52. { return xato##UW##_range(str, l, u); } \
  53. static ALWAYS_INLINE \
  54. unsigned narrow xato##UN##_sfx(const char *str, const struct suffix_mult *sfx) \
  55. { return xato##UW##_sfx(str, sfx); } \
  56. static ALWAYS_INLINE \
  57. unsigned narrow xato##UN(const char *str) \
  58. { return xato##UW(str); } \
  59. static ALWAYS_INLINE \
  60. narrow xstrto##N##_range_sfx(const char *str, int b, narrow l, narrow u, const struct suffix_mult *sfx) \
  61. { return xstrto##W##_range_sfx(str, b, l, u, sfx); } \
  62. static ALWAYS_INLINE \
  63. narrow xstrto##N##_range(const char *str, int b, narrow l, narrow u) \
  64. { return xstrto##W##_range(str, b, l, u); } \
  65. static ALWAYS_INLINE \
  66. narrow xato##N##_range_sfx(const char *str, narrow l, narrow u, const struct suffix_mult *sfx) \
  67. { return xato##W##_range_sfx(str, l, u, sfx); } \
  68. static ALWAYS_INLINE \
  69. narrow xato##N##_range(const char *str, narrow l, narrow u) \
  70. { return xato##W##_range(str, l, u); } \
  71. static ALWAYS_INLINE \
  72. narrow xato##N##_sfx(const char *str, const struct suffix_mult *sfx) \
  73. { return xato##W##_sfx(str, sfx); } \
  74. static ALWAYS_INLINE \
  75. narrow xato##N(const char *str) \
  76. { return xato##W(str); } \
  77. /* If long == long long, then just map them one-to-one */
  78. #if ULONG_MAX == ULLONG_MAX
  79. DEFINE_EQUIV_STR_CONV(long, l, ll, ul, ull)
  80. #else
  81. /* Else provide extern defs */
  82. DECLARE_STR_CONV(long, l, ul)
  83. #endif
  84. /* Same for int -> [long] long */
  85. #if UINT_MAX == ULLONG_MAX
  86. DEFINE_EQUIV_STR_CONV(int, i, ll, u, ull)
  87. #elif UINT_MAX == ULONG_MAX
  88. DEFINE_EQUIV_STR_CONV(int, i, l, u, ul)
  89. #else
  90. DECLARE_STR_CONV(int, i, u)
  91. #endif
  92. /* Specialized */
  93. int BUG_xatou32_unimplemented(void);
  94. static ALWAYS_INLINE uint32_t xatou32(const char *numstr)
  95. {
  96. if (UINT_MAX == 0xffffffff)
  97. return xatou(numstr);
  98. if (ULONG_MAX == 0xffffffff)
  99. return xatoul(numstr);
  100. return BUG_xatou32_unimplemented();
  101. }
  102. /* Non-aborting kind of convertors: bb_strto[u][l]l */
  103. /* On exit: errno = 0 only if there was non-empty, '\0' terminated value
  104. * errno = EINVAL if value was not '\0' terminated, but otherwise ok
  105. * Return value is still valid, caller should just check whether end[0]
  106. * is a valid terminating char for particular case. OTOH, if caller
  107. * requires '\0' terminated input, [s]he can just check errno == 0.
  108. * errno = ERANGE if value had alphanumeric terminating char ("1234abcg").
  109. * errno = ERANGE if value is out of range, missing, etc.
  110. * errno = ERANGE if value had minus sign for strtouXX (even "-0" is not ok )
  111. * return value is all-ones in this case.
  112. */
  113. unsigned long long bb_strtoull(const char *arg, char **endp, int base) FAST_FUNC;
  114. long long bb_strtoll(const char *arg, char **endp, int base) FAST_FUNC;
  115. #if ULONG_MAX == ULLONG_MAX
  116. static ALWAYS_INLINE
  117. unsigned long bb_strtoul(const char *arg, char **endp, int base)
  118. { return bb_strtoull(arg, endp, base); }
  119. static ALWAYS_INLINE
  120. long bb_strtol(const char *arg, char **endp, int base)
  121. { return bb_strtoll(arg, endp, base); }
  122. #else
  123. unsigned long bb_strtoul(const char *arg, char **endp, int base) FAST_FUNC;
  124. long bb_strtol(const char *arg, char **endp, int base) FAST_FUNC;
  125. #endif
  126. #if UINT_MAX == ULLONG_MAX
  127. static ALWAYS_INLINE
  128. unsigned bb_strtou(const char *arg, char **endp, int base)
  129. { return bb_strtoull(arg, endp, base); }
  130. static ALWAYS_INLINE
  131. int bb_strtoi(const char *arg, char **endp, int base)
  132. { return bb_strtoll(arg, endp, base); }
  133. #elif UINT_MAX == ULONG_MAX
  134. static ALWAYS_INLINE
  135. unsigned bb_strtou(const char *arg, char **endp, int base)
  136. { return bb_strtoul(arg, endp, base); }
  137. static ALWAYS_INLINE
  138. int bb_strtoi(const char *arg, char **endp, int base)
  139. { return bb_strtol(arg, endp, base); }
  140. #else
  141. unsigned bb_strtou(const char *arg, char **endp, int base) FAST_FUNC;
  142. int bb_strtoi(const char *arg, char **endp, int base) FAST_FUNC;
  143. #endif
  144. int BUG_bb_strtou32_unimplemented(void);
  145. static ALWAYS_INLINE
  146. uint32_t bb_strtou32(const char *arg, char **endp, int base)
  147. {
  148. if (sizeof(uint32_t) == sizeof(unsigned))
  149. return bb_strtou(arg, endp, base);
  150. if (sizeof(uint32_t) == sizeof(unsigned long))
  151. return bb_strtoul(arg, endp, base);
  152. return BUG_bb_strtou32_unimplemented();
  153. }
  154. /* Floating point */
  155. double bb_strtod(const char *arg, char **endp) FAST_FUNC;
  156. #if __GNUC_PREREQ(4,1)
  157. # pragma GCC visibility pop
  158. #endif