// Copyright (c) 2009-2016 Vladimir Batov. // Use, modification and distribution are subject to the Boost Software License, // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. #ifndef BOOST_CONVERT_STRTOL_CONVERTER_HPP #define BOOST_CONVERT_STRTOL_CONVERTER_HPP #include #include #include #include #include #include #include #include #if __GNUC__ == 4 && __GNUC_MINOR__ <= 2 namespace std { using ::strtold; // Tests indicated that gcc-4.2.1 does not have 'std::strtold' } #endif namespace boost { namespace cnv { struct strtol; }} /// @brief std::strtol-based extended converter /// @details The converter offers a fairly decent overall performance and moderate formatting facilities. struct boost::cnv::strtol : public boost::cnv::cnvbase { typedef boost::cnv::strtol this_type; typedef boost::cnv::cnvbase base_type; using base_type::operator(); private: friend struct boost::cnv::cnvbase; template void str_to(cnv::range v, optional< int_type>& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional< sint_type>& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional< lint_type>& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional< llint_type>& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional< uint_type>& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional< usint_type>& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional< ulint_type>& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional& r) const { str_to_i (v, r); } template void str_to(cnv::range v, optional< flt_type>& r) const { str_to_d (v, r); } template void str_to(cnv::range v, optional< dbl_type>& r) const { str_to_d (v, r); } template void str_to(cnv::range v, optional< ldbl_type>& r) const { str_to_d (v, r); } template cnv::range to_str ( int_type v, char_type* buf) const { return i_to_str(v, buf); } template cnv::range to_str ( uint_type v, char_type* buf) const { return i_to_str(v, buf); } template cnv::range to_str ( lint_type v, char_type* buf) const { return i_to_str(v, buf); } template cnv::range to_str ( ulint_type v, char_type* buf) const { return i_to_str(v, buf); } template cnv::range to_str ( llint_type v, char_type* buf) const { return i_to_str(v, buf); } template cnv::range to_str (ullint_type v, char_type* buf) const { return i_to_str(v, buf); } template cnv::range to_str ( dbl_type v, char_type* buf) const; template cnv::range i_to_str (in_type, char_type*) const; template void str_to_i (cnv::range, optional&) const; template void str_to_d (cnv::range, optional&) const; static double adjust_fraction (double, int); static int get_char (int v) { return (v < 10) ? (v += '0') : (v += 'A' - 10); } }; template boost::cnv::range boost::cnv::strtol::i_to_str(Type in_value, char_type* buf) const { // C1. Base=10 optimization improves performance 10% typedef typename boost::make_unsigned::type unsigned_type; char_type* beg = buf + bufsize_ / 2; char_type* end = beg; bool const is_negative = in_value < 0; unsigned_type value = static_cast(is_negative ? -in_value : in_value); if (base_ == 10) for (; value; *(--beg) = int(value % 10) + '0', value /= 10); //C1 else for (; value; *(--beg) = get_char(value % base_), value /= base_); if (beg == end) *(--beg) = '0'; if (is_negative) *(--beg) = '-'; return cnv::range(beg, end); } inline double boost::cnv::strtol::adjust_fraction(double fraction, int precision) { // C1. Bring forward the fraction coming right after precision digits. // That is, say, fraction=0.234567, precision=2. Then brought forward=23.4567 // C3. INT_MAX(4bytes)=2,147,483,647. So, 10^8 seems appropriate. If not, drop it down to 4. // C4. ::round() returns the integral value that is nearest to x, // with halfway cases rounded away from zero. Therefore, // round( 0.4) = 0 // round( 0.5) = 1 // round( 0.6) = 1 // round(-0.4) = 0 // round(-0.5) = -1 // round(-0.6) = -1 int const tens[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 }; for (int k = precision / 8; k; --k) fraction *= 100000000; //C3. fraction *= tens[precision % 8]; //C1 // return ::rint(fraction); //C4 return boost::math::round(fraction); //C4 } template inline boost::cnv::range boost::cnv::strtol::to_str(double value, char_type* buf) const { char_type* beg = buf + bufsize_ / 2; char_type* end = beg; char_type* ipos = end - 1; bool const is_negative = (value < 0) ? (value = -value, true) : false; double ipart = std::floor(value); double fpart = adjust_fraction(value - ipart, precision_); int precision = precision_; int const base = 10; for (; 1 <= ipart; ipart /= base) *(--beg) = get_char(int(ipart - std::floor(ipart / base) * base)); if (beg == end) *(--beg) = '0'; if (precision) *(end++) = '.'; for (char_type* fpos = end += precision; precision; --precision, fpart /= base) *(--fpos) = get_char(int(fpart - std::floor(fpart / base) * base)); if (1 <= fpart) { for (; beg <= ipos; --ipos) if (*ipos == '9') *ipos = '0'; else { ++*ipos; break; } if (ipos < beg) *(beg = ipos) = '1'; } if (is_negative) *(--beg) = '-'; return cnv::range(beg, end); } template void boost::cnv::strtol::str_to_i(cnv::range range, boost::optional& result_out) const { typedef typename boost::make_unsigned::type unsigned_type; typedef cnv::range range_type; typedef typename range_type::iterator iterator; iterator s = range.begin(); unsigned int ch = *s; bool const is_negative = ch == '-' ? (ch = *++s, true) : ch == '+' ? (ch = *++s, false) : false; bool const is_unsigned = boost::is_same::value; unsigned int base = base_; /**/ if (is_negative && is_unsigned) return; else if ((base == 0 || base == 16) && ch == '0' && (*++s == 'x' || *s == 'X')) ++s, base = 16; else if (base == 0) base = ch == '0' ? (++s, 8) : 10; unsigned_type const max = (std::numeric_limits::max)() + (is_negative ? 1 : 0); unsigned_type const cutoff = max / base; unsigned int const cutlim = max % base; unsigned_type result = 0; for (; s != range.sentry(); ++s) { ch = *s; /**/ if (std::isdigit(ch)) ch -= '0'; else if (std::isalpha(ch)) ch -= (std::isupper(ch) ? 'A' : 'a') - 10; else return; if (base <= ch || cutoff < result || (result == cutoff && cutlim < ch)) return; result *= base; result += ch; } result_out = is_negative ? -out_type(result) : out_type(result); } template void boost::cnv::strtol::str_to_d(cnv::range range, optional& result_out) const { // C2. Simply check if the end-of-string was reached -- *cnv_end == 0 // instead of traversing once with strlen() to find the end iterator // and then comparing to it as in // char const* end = str + strlen(str); // Unnecessary traversal! // bool const good = ... && cnv_end == end; typedef cnv::range range_type; typedef typename range_type::value_type ch_type; ch_type const* str = &*range.begin(); // Currently only works with 'char' char* cnv_end = 0; ldbl_type result = strtold(str, &cnv_end); bool good = result != -HUGE_VALL && result != HUGE_VALL && *cnv_end == 0/*C2*/; out_type max = (std::numeric_limits::max)(); if (good && -max <= result && result <= max) result_out = out_type(result); } #endif // BOOST_CONVERT_STRTOL_CONVERTER_HPP