strtol.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2009-2014 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. // C2. Old C-strings have an advantage over [begin, end) ranges. They do not need the 'end' iterator!
  29. // Instead, they provide a sentinel (0 terminator). Consequently, C strings can be traversed
  30. // without the need to compare if the 'end' has been reached (i.e. "for (; it != end; ++it)").
  31. // Instead, the current character is checked if it's 0 (i.e. "for (; *p; ++p)") which is faster.
  32. //
  33. // So, the implementation takes advantage of the fact. Namely, we simply check if *cnv_end == 0
  34. // instead of traversing once with strlen() to find the end iterator and then comparing to it as in
  35. //
  36. // char const* str_end = str + strlen(str); // Unnecessary traversal!
  37. // ...
  38. // bool const good = ... && cnv_end == str_end;
  39. typedef boost::cnv::strtol this_type;
  40. typedef boost::cnv::cnvbase<this_type> base_type;
  41. using base_type::operator();
  42. private:
  43. friend struct boost::cnv::cnvbase<this_type>;
  44. template<typename string_type> void str_to(cnv::range<string_type> v, optional< int_type>& r) const { str_to_i (v, r); }
  45. template<typename string_type> void str_to(cnv::range<string_type> v, optional< sint_type>& r) const { str_to_i (v, r); }
  46. template<typename string_type> void str_to(cnv::range<string_type> v, optional< lint_type>& r) const { str_to_i (v, r); }
  47. template<typename string_type> void str_to(cnv::range<string_type> v, optional< llint_type>& r) const { str_to_i (v, r); }
  48. template<typename string_type> void str_to(cnv::range<string_type> v, optional< uint_type>& r) const { str_to_i (v, r); }
  49. template<typename string_type> void str_to(cnv::range<string_type> v, optional< usint_type>& r) const { str_to_i (v, r); }
  50. template<typename string_type> void str_to(cnv::range<string_type> v, optional< ulint_type>& r) const { str_to_i (v, r); }
  51. template<typename string_type> void str_to(cnv::range<string_type> v, optional<ullint_type>& r) const { str_to_i (v, r); }
  52. template<typename string_type> void str_to(cnv::range<string_type> v, optional< flt_type>& r) const { str_to_d (v, r); }
  53. template<typename string_type> void str_to(cnv::range<string_type> v, optional< dbl_type>& r) const { str_to_d (v, r); }
  54. template<typename string_type> void str_to(cnv::range<string_type> v, optional< ldbl_type>& r) const { str_to_d (v, r); }
  55. template <typename char_type> cnv::range<char_type*> to_str ( int_type v, char_type* buf) const { return i_to_str(v, buf); }
  56. template <typename char_type> cnv::range<char_type*> to_str ( uint_type v, char_type* buf) const { return i_to_str(v, buf); }
  57. template <typename char_type> cnv::range<char_type*> to_str ( lint_type v, char_type* buf) const { return i_to_str(v, buf); }
  58. template <typename char_type> cnv::range<char_type*> to_str ( ulint_type v, char_type* buf) const { return i_to_str(v, buf); }
  59. template <typename char_type> cnv::range<char_type*> to_str ( llint_type v, char_type* buf) const { return i_to_str(v, buf); }
  60. template <typename char_type> cnv::range<char_type*> to_str (ullint_type v, char_type* buf) const { return i_to_str(v, buf); }
  61. template <typename char_type> cnv::range<char_type*> to_str ( dbl_type v, char_type* buf) const;
  62. template<typename char_type, typename in_type> cnv::range<char_type*> i_to_str (in_type, char_type*) const;
  63. template<typename string_type, typename out_type> void str_to_i (cnv::range<string_type>, optional<out_type>&) const;
  64. template<typename string_type, typename out_type> void str_to_d (cnv::range<string_type>, optional<out_type>&) const;
  65. static double adjust_fraction (double, int);
  66. static int get_char (int v) { return (v < 10) ? (v += '0') : (v += 'A' - 10); }
  67. };
  68. template<typename char_type, typename Type>
  69. boost::cnv::range<char_type*>
  70. boost::cnv::strtol::i_to_str(Type in_value, char_type* buf) const
  71. {
  72. // C1. Base=10 optimization improves performance 10%
  73. typedef typename boost::make_unsigned<Type>::type unsigned_type;
  74. char_type* beg = buf + bufsize_ / 2;
  75. char_type* end = beg;
  76. bool const is_negative = in_value < 0;
  77. unsigned_type value = static_cast<unsigned_type>(is_negative ? -in_value : in_value);
  78. if (base_ == 10) for (; value; *(--beg) = int(value % 10) + '0', value /= 10); //C1
  79. else for (; value; *(--beg) = get_char(value % base_), value /= base_);
  80. if (beg == end) *(--beg) = '0';
  81. if (is_negative) *(--beg) = '-';
  82. return cnv::range<char_type*>(beg, end);
  83. }
  84. inline
  85. double
  86. boost::cnv::strtol::adjust_fraction(double fraction, int precision)
  87. {
  88. // C1. Bring forward the fraction coming right after precision digits.
  89. // That is, say, fraction=0.234567, precision=2. Then brought forward=23.4567
  90. // C3. INT_MAX(4bytes)=2,147,483,647. So, 10^8 seems appropriate. If not, drop it down to 4.
  91. // C4. ::round() returns the integral value that is nearest to x,
  92. // with halfway cases rounded away from zero. Therefore,
  93. // round( 0.4) = 0
  94. // round( 0.5) = 1
  95. // round( 0.6) = 1
  96. // round(-0.4) = 0
  97. // round(-0.5) = -1
  98. // round(-0.6) = -1
  99. int const tens[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
  100. for (int k = precision / 8; k; --k) fraction *= 100000000; //C3.
  101. fraction *= tens[precision % 8]; //C1
  102. // return ::rint(fraction); //C4
  103. return boost::math::round(fraction); //C4
  104. }
  105. template <typename char_type>
  106. inline
  107. boost::cnv::range<char_type*>
  108. boost::cnv::strtol::to_str(double value, char_type* buf) const
  109. {
  110. char_type* beg = buf + bufsize_ / 2;
  111. char_type* end = beg;
  112. char_type* ipos = end - 1;
  113. bool const is_negative = (value < 0) ? (value = -value, true) : false;
  114. double ipart = std::floor(value);
  115. double fpart = adjust_fraction(value - ipart, precision_);
  116. int precision = precision_;
  117. int const base = 10;
  118. for (; 1 <= ipart; ipart /= base)
  119. *(--beg) = get_char(int(ipart - std::floor(ipart / base) * base));
  120. if (beg == end) *(--beg) = '0';
  121. if (precision) *(end++) = '.';
  122. for (char_type* fpos = end += precision; precision; --precision, fpart /= base)
  123. *(--fpos) = get_char(int(fpart - std::floor(fpart / base) * base));
  124. if (1 <= fpart)
  125. {
  126. for (; beg <= ipos; --ipos)
  127. if (*ipos == '9') *ipos = '0';
  128. else { ++*ipos; break; }
  129. if (ipos < beg)
  130. *(beg = ipos) = '1';
  131. }
  132. if (is_negative) *(--beg) = '-';
  133. return cnv::range<char_type*>(beg, end);
  134. }
  135. template<typename string_type, typename out_type>
  136. void
  137. boost::cnv::strtol::str_to_i(cnv::range<string_type> range, boost::optional<out_type>& result_out) const
  138. {
  139. typedef typename boost::make_unsigned<out_type>::type unsigned_type;
  140. typedef cnv::range<string_type> range_type;
  141. typedef typename range_type::iterator iterator;
  142. iterator s = range.begin();
  143. unsigned int ch = *s;
  144. bool const is_negative = ch == '-' ? (ch = *++s, true) : ch == '+' ? (ch = *++s, false) : false;
  145. bool const is_unsigned = boost::is_same<out_type, unsigned_type>::value;
  146. unsigned int base = base_;
  147. /**/ if (is_negative && is_unsigned) return;
  148. else if ((base == 0 || base == 16) && ch == '0' && (*++s == 'x' || *s == 'X')) ++s, base = 16;
  149. else if (base == 0) base = ch == '0' ? (++s, 8) : 10;
  150. unsigned_type const max = (std::numeric_limits<out_type>::max)() + (is_negative ? 1 : 0);
  151. unsigned_type const cutoff = max / base;
  152. unsigned int const cutlim = max % base;
  153. unsigned_type result = 0;
  154. for (; s != range.sentry(); ++s)
  155. {
  156. ch = *s;
  157. /**/ if (std::isdigit(ch)) ch -= '0';
  158. else if (std::isalpha(ch)) ch -= (std::isupper(ch) ? 'A' : 'a') - 10;
  159. else return;
  160. if (base <= ch || cutoff < result || (result == cutoff && cutlim < ch))
  161. return;
  162. result *= base;
  163. result += ch;
  164. }
  165. result_out = is_negative ? -out_type(result) : out_type(result);
  166. }
  167. template<typename string_type, typename out_type>
  168. void
  169. boost::cnv::strtol::str_to_d(cnv::range<string_type> range, optional<out_type>& result_out) const
  170. {
  171. typedef cnv::range<string_type> range_type;
  172. typedef typename range_type::value_type char_type;
  173. char_type const* str = &*range.begin(); // Currently only works with 'char'
  174. char* cnv_end = 0;
  175. ldbl_type const result = strtold(str, &cnv_end);
  176. bool const good = result != -HUGE_VALL && result != HUGE_VALL && *cnv_end == 0/*C2*/;
  177. out_type const max = (std::numeric_limits<out_type>::max)();
  178. if (good && -max <= result && result <= max)
  179. result_out = out_type(result);
  180. }
  181. #endif // BOOST_CONVERT_STRTOL_CONVERTER_HPP