strtol.hpp 8.9 KB

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