3
0

xatonum.h 6.6 KB

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