xatonum.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 */
  100. unsigned long long bb_strtoull(const char *arg, char **endp, int base);
  101. long long bb_strtoll(const char *arg, char **endp, int base);
  102. #if ULONG_MAX == ULLONG_MAX
  103. static ALWAYS_INLINE
  104. unsigned long bb_strtoul(const char *arg, char **endp, int base)
  105. { return bb_strtoull(arg, endp, base); }
  106. static ALWAYS_INLINE
  107. long bb_strtol(const char *arg, char **endp, int base)
  108. { return bb_strtoll(arg, endp, base); }
  109. #else
  110. unsigned long bb_strtoul(const char *arg, char **endp, int base);
  111. long bb_strtol(const char *arg, char **endp, int base);
  112. #endif
  113. #if UINT_MAX == ULLONG_MAX
  114. static ALWAYS_INLINE
  115. unsigned bb_strtou(const char *arg, char **endp, int base)
  116. { return bb_strtoull(arg, endp, base); }
  117. static ALWAYS_INLINE
  118. int bb_strtoi(const char *arg, char **endp, int base)
  119. { return bb_strtoll(arg, endp, base); }
  120. #elif UINT_MAX == ULONG_MAX
  121. static ALWAYS_INLINE
  122. unsigned bb_strtou(const char *arg, char **endp, int base)
  123. { return bb_strtoul(arg, endp, base); }
  124. static ALWAYS_INLINE
  125. int bb_strtoi(const char *arg, char **endp, int base)
  126. { return bb_strtol(arg, endp, base); }
  127. #else
  128. unsigned bb_strtou(const char *arg, char **endp, int base);
  129. int bb_strtoi(const char *arg, char **endp, int base);
  130. #endif
  131. int BUG_bb_strtou32_unimplemented(void);
  132. static ALWAYS_INLINE
  133. uint32_t bb_strtou32(const char *arg, char **endp, int base)
  134. {
  135. if (sizeof(uint32_t) == sizeof(unsigned))
  136. return bb_strtou(arg, endp, base);
  137. if (sizeof(uint32_t) == sizeof(unsigned long))
  138. return bb_strtoul(arg, endp, base);
  139. return BUG_bb_strtou32_unimplemented();
  140. }
  141. /* Floating point */
  142. /* double bb_strtod(const char *arg, char **endp); */