strtol.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (c) 2009-2016 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifndef BOOST_CONVERT_STRTOL_CONVERTER_HPP
  5. #define BOOST_CONVERT_STRTOL_CONVERTER_HPP
  6. #include <boost/convert/base.hpp>
  7. #include <boost/type_traits/make_unsigned.hpp>
  8. #include <boost/type_traits/is_same.hpp>
  9. #include <boost/math/special_functions/round.hpp>
  10. #include <limits>
  11. #include <cmath>
  12. #include <cstdlib>
  13. #include <climits>
  14. #if __GNUC__ == 4 && __GNUC_MINOR__ <= 2
  15. namespace std
  16. {
  17. using ::strtold; // Tests indicated that gcc-4.2.1 does not have 'std::strtold'
  18. }
  19. #endif
  20. namespace boost { namespace cnv
  21. {
  22. struct strtol;
  23. }}
  24. /// @brief std::strtol-based extended converter
  25. /// @details The converter offers a fairly decent overall performance and moderate formatting facilities.
  26. struct boost::cnv::strtol : public boost::cnv::cnvbase<boost::cnv::strtol>
  27. {
  28. typedef boost::cnv::strtol this_type;
  29. typedef boost::cnv::cnvbase<this_type> base_type;
  30. using base_type::operator();
  31. private:
  32. friend struct boost::cnv::cnvbase<this_type>;
  33. template<typename string_type> void str_to(cnv::range<string_type> v, optional< int_type>& r) const { str_to_i (v, r); }
  34. template<typename string_type> void str_to(cnv::range<string_type> v, optional< sint_type>& r) const { str_to_i (v, r); }
  35. template<typename string_type> void str_to(cnv::range<string_type> v, optional< lint_type>& r) const { str_to_i (v, r); }
  36. template<typename string_type> void str_to(cnv::range<string_type> v, optional< llint_type>& r) const { str_to_i (v, r); }
  37. template<typename string_type> void str_to(cnv::range<string_type> v, optional< uint_type>& r) const { str_to_i (v, r); }
  38. template<typename string_type> void str_to(cnv::range<string_type> v, optional< usint_type>& r) const { str_to_i (v, r); }
  39. template<typename string_type> void str_to(cnv::range<string_type> v, optional< ulint_type>& r) const { str_to_i (v, r); }
  40. template<typename string_type> void str_to(cnv::range<string_type> v, optional<ullint_type>& r) const { str_to_i (v, r); }
  41. template<typename string_type> void str_to(cnv::range<string_type> v, optional< flt_type>& r) const { str_to_d (v, r); }
  42. template<typename string_type> void str_to(cnv::range<string_type> v, optional< dbl_type>& r) const { str_to_d (v, r); }
  43. template<typename string_type> void str_to(cnv::range<string_type> v, optional< ldbl_type>& r) const { str_to_d (v, r); }
  44. template <typename char_type> cnv::range<char_type*> to_str ( int_type v, char_type* buf) const { return i_to_str(v, buf); }
  45. template <typename char_type> cnv::range<char_type*> to_str ( uint_type v, char_type* buf) const { return i_to_str(v, buf); }
  46. template <typename char_type> cnv::range<char_type*> to_str ( lint_type v, char_type* buf) const { return i_to_str(v, buf); }
  47. template <typename char_type> cnv::range<char_type*> to_str ( ulint_type v, char_type* buf) const { return i_to_str(v, buf); }
  48. template <typename char_type> cnv::range<char_type*> to_str ( llint_type v, char_type* buf) const { return i_to_str(v, buf); }
  49. template <typename char_type> cnv::range<char_type*> to_str (ullint_type v, char_type* buf) const { return i_to_str(v, buf); }
  50. template <typename char_type> cnv::range<char_type*> to_str ( dbl_type v, char_type* buf) const;
  51. template<typename char_type, typename in_type> cnv::range<char_type*> i_to_str (in_type, char_type*) const;
  52. template<typename string_type, typename out_type> void str_to_i (cnv::range<string_type>, optional<out_type>&) const;
  53. template<typename string_type, typename out_type> void str_to_d (cnv::range<string_type>, optional<out_type>&) const;
  54. static double adjust_fraction (double, int);
  55. static int get_char (int v) { return (v < 10) ? (v += '0') : (v += 'A' - 10); }
  56. };
  57. template<typename char_type, typename Type>
  58. boost::cnv::range<char_type*>
  59. boost::cnv::strtol::i_to_str(Type in_value, char_type* buf) const
  60. {
  61. // C1. Base=10 optimization improves performance 10%
  62. typedef typename boost::make_unsigned<Type>::type unsigned_type;
  63. char_type* beg = buf + bufsize_ / 2;
  64. char_type* end = beg;
  65. bool const is_neg = std::is_signed<Type>::value && in_value < 0;
  66. unsigned_type value = static_cast<unsigned_type>(is_neg ? -in_value : in_value);
  67. int base = int(base_);
  68. if (base == 10) for (; value; *(--beg) = int(value % 10) + '0', value /= 10); //C1
  69. else for (; value; *(--beg) = get_char(value % base), value /= base);
  70. if (beg == end) *(--beg) = '0';
  71. if (is_neg) *(--beg) = '-';
  72. return cnv::range<char_type*>(beg, end);
  73. }
  74. inline
  75. double
  76. boost::cnv::strtol::adjust_fraction(double fraction, int precision)
  77. {
  78. // C1. Bring forward the fraction coming right after precision digits.
  79. // That is, say, fraction=0.234567, precision=2. Then brought forward=23.4567
  80. // C3. INT_MAX(4bytes)=2,147,483,647. So, 10^8 seems appropriate. If not, drop it down to 4.
  81. // C4. ::round() returns the integral value that is nearest to x,
  82. // with halfway cases rounded away from zero. Therefore,
  83. // round( 0.4) = 0
  84. // round( 0.5) = 1
  85. // round( 0.6) = 1
  86. // round(-0.4) = 0
  87. // round(-0.5) = -1
  88. // round(-0.6) = -1
  89. int const tens[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
  90. for (int k = precision / 8; k; --k) fraction *= 100000000; //C3.
  91. fraction *= tens[precision % 8]; //C1
  92. // return ::rint(fraction); //C4
  93. return boost::math::round(fraction); //C4
  94. }
  95. template <typename char_type>
  96. inline
  97. boost::cnv::range<char_type*>
  98. boost::cnv::strtol::to_str(double value, char_type* buf) const
  99. {
  100. char_type* beg = buf + bufsize_ / 2;
  101. char_type* end = beg;
  102. char_type* ipos = end - 1;
  103. bool const is_negative = (value < 0) ? (value = -value, true) : false;
  104. double ipart = std::floor(value);
  105. double fpart = adjust_fraction(value - ipart, precision_);
  106. int precision = precision_;
  107. int const base = 10;
  108. for (; 1 <= ipart; ipart /= base)
  109. *(--beg) = get_char(int(ipart - std::floor(ipart / base) * base));
  110. if (beg == end) *(--beg) = '0';
  111. if (precision) *(end++) = '.';
  112. for (char_type* fpos = end += precision; precision; --precision, fpart /= base)
  113. *(--fpos) = get_char(int(fpart - std::floor(fpart / base) * base));
  114. if (1 <= fpart)
  115. {
  116. for (; beg <= ipos; --ipos)
  117. if (*ipos == '9') *ipos = '0';
  118. else { ++*ipos; break; }
  119. if (ipos < beg)
  120. *(beg = ipos) = '1';
  121. }
  122. if (is_negative) *(--beg) = '-';
  123. return cnv::range<char_type*>(beg, end);
  124. }
  125. template<typename string_type, typename out_type>
  126. void
  127. boost::cnv::strtol::str_to_i(cnv::range<string_type> range, boost::optional<out_type>& result_out) const
  128. {
  129. using uint_type = unsigned int;
  130. using unsigned_type = typename boost::make_unsigned<out_type>::type;
  131. using range_type = cnv::range<string_type>;
  132. using iterator = typename range_type::iterator;
  133. iterator s = range.begin();
  134. uint_type ch = *s;
  135. bool const is_negative = ch == '-' ? (ch = *++s, true) : ch == '+' ? (ch = *++s, false) : false;
  136. bool const is_unsigned = boost::is_same<out_type, unsigned_type>::value;
  137. uint_type base = uint_type(base_);
  138. /**/ if (is_negative && is_unsigned) return;
  139. else if ((base == 0 || base == 16) && ch == '0' && (*++s == 'x' || *s == 'X')) ++s, base = 16;
  140. else if (base == 0) base = ch == '0' ? (++s, 8) : 10;
  141. unsigned_type const max = (std::numeric_limits<out_type>::max)();
  142. unsigned_type const umax = max + (is_negative ? 1 : 0);
  143. unsigned_type const cutoff = umax / base;
  144. uint_type const cutlim = umax % base;
  145. unsigned_type result = 0;
  146. for (; s != range.sentry(); ++s)
  147. {
  148. ch = *s;
  149. /**/ if (std::isdigit(ch)) ch -= '0';
  150. else if (std::isalpha(ch)) ch -= (std::isupper(ch) ? 'A' : 'a') - 10;
  151. else return;
  152. if (base <= ch || cutoff < result || (result == cutoff && cutlim < ch))
  153. return;
  154. result *= base;
  155. result += ch;
  156. }
  157. result_out = is_negative ? -out_type(result) : out_type(result);
  158. }
  159. template<typename string_type, typename out_type>
  160. void
  161. boost::cnv::strtol::str_to_d(cnv::range<string_type> range, optional<out_type>& result_out) const
  162. {
  163. // C1. Because of strtold() currently only works with 'char'
  164. // C2. strtold() does not work with ranges.
  165. // Consequently, we have to copy the supplied range into a string for strtold().
  166. // C3. Check if the end-of-string was reached -- *cnv_end == 0.
  167. typedef cnv::range<string_type> range_type;
  168. typedef typename range_type::value_type ch_type;
  169. size_t const sz = 128;
  170. ch_type str[sz] = {0}; std::strncpy(str, &*range.begin(), (std::min)(sz - 1, range.size()));
  171. char* cnv_end = 0;
  172. ldbl_type result = strtold(str, &cnv_end);
  173. bool good = result != -HUGE_VALL && result != HUGE_VALL && *cnv_end == 0; //C3
  174. out_type max = (std::numeric_limits<out_type>::max)();
  175. if (good && -max <= result && result <= max)
  176. result_out = out_type(result);
  177. }
  178. #endif // BOOST_CONVERT_STRTOL_CONVERTER_HPP