strtol.hpp 9.0 KB

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