• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BOOST_CONVERT_STRTOL_CONVERTER_HPP
6 #define BOOST_CONVERT_STRTOL_CONVERTER_HPP
7 
8 #include <boost/convert/base.hpp>
9 #include <boost/type_traits/make_unsigned.hpp>
10 #include <boost/type_traits/is_same.hpp>
11 #include <boost/math/special_functions/round.hpp>
12 #include <limits>
13 #include <cmath>
14 #include <cstdlib>
15 #include <climits>
16 
17 #if __GNUC__ == 4 && __GNUC_MINOR__ <= 2
18 namespace std
19 {
20     using ::strtold; // Tests indicated that gcc-4.2.1 does not have 'std::strtold'
21 }
22 #endif
23 
24 namespace boost { namespace cnv
25 {
26     struct strtol;
27 }}
28 
29 /// @brief std::strtol-based extended converter
30 /// @details The converter offers a fairly decent overall performance and moderate formatting facilities.
31 
32 struct boost::cnv::strtol : public boost::cnv::cnvbase<boost::cnv::strtol>
33 {
34     typedef boost::cnv::strtol             this_type;
35     typedef boost::cnv::cnvbase<this_type> base_type;
36 
37     using base_type::operator();
38 
39     private:
40 
41     friend struct boost::cnv::cnvbase<this_type>;
42 
str_toboost::cnv::strtol43     template<typename string_type> void str_to(cnv::range<string_type> v, optional<   int_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol44     template<typename string_type> void str_to(cnv::range<string_type> v, optional<  sint_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol45     template<typename string_type> void str_to(cnv::range<string_type> v, optional<  lint_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol46     template<typename string_type> void str_to(cnv::range<string_type> v, optional< llint_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol47     template<typename string_type> void str_to(cnv::range<string_type> v, optional<  uint_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol48     template<typename string_type> void str_to(cnv::range<string_type> v, optional< usint_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol49     template<typename string_type> void str_to(cnv::range<string_type> v, optional< ulint_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol50     template<typename string_type> void str_to(cnv::range<string_type> v, optional<ullint_type>& r) const { str_to_i (v, r); }
str_toboost::cnv::strtol51     template<typename string_type> void str_to(cnv::range<string_type> v, optional<   flt_type>& r) const { str_to_d (v, r); }
str_toboost::cnv::strtol52     template<typename string_type> void str_to(cnv::range<string_type> v, optional<   dbl_type>& r) const { str_to_d (v, r); }
str_toboost::cnv::strtol53     template<typename string_type> void str_to(cnv::range<string_type> v, optional<  ldbl_type>& r) const { str_to_d (v, r); }
54 
to_strboost::cnv::strtol55     template <typename char_type> cnv::range<char_type*> to_str (   int_type v, char_type* buf) const { return i_to_str(v, buf); }
to_strboost::cnv::strtol56     template <typename char_type> cnv::range<char_type*> to_str (  uint_type v, char_type* buf) const { return i_to_str(v, buf); }
to_strboost::cnv::strtol57     template <typename char_type> cnv::range<char_type*> to_str (  lint_type v, char_type* buf) const { return i_to_str(v, buf); }
to_strboost::cnv::strtol58     template <typename char_type> cnv::range<char_type*> to_str ( ulint_type v, char_type* buf) const { return i_to_str(v, buf); }
to_strboost::cnv::strtol59     template <typename char_type> cnv::range<char_type*> to_str ( llint_type v, char_type* buf) const { return i_to_str(v, buf); }
to_strboost::cnv::strtol60     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 
63     template<typename char_type, typename in_type> cnv::range<char_type*> i_to_str (in_type, char_type*) const;
64     template<typename string_type, typename out_type> void                str_to_i (cnv::range<string_type>, optional<out_type>&) const;
65     template<typename string_type, typename out_type> void                str_to_d (cnv::range<string_type>, optional<out_type>&) const;
66 
67     static double adjust_fraction (double, int);
get_charboost::cnv::strtol68     static int           get_char (int v) { return (v < 10) ? (v += '0') : (v += 'A' - 10); }
69 };
70 
71 template<typename char_type, typename Type>
72 boost::cnv::range<char_type*>
i_to_str(Type in_value,char_type * buf) const73 boost::cnv::strtol::i_to_str(Type in_value, char_type* buf) const
74 {
75     // C1. Base=10 optimization improves performance 10%
76 
77     typedef typename boost::make_unsigned<Type>::type unsigned_type;
78 
79     char_type*      beg = buf + bufsize_ / 2;
80     char_type*      end = beg;
81     bool const   is_neg = std::is_signed<Type>::value && in_value < 0;
82     unsigned_type value = static_cast<unsigned_type>(is_neg ? -in_value : in_value);
83     int            base = int(base_);
84 
85     if (base == 10) for (; value; *(--beg) = int(value % 10) + '0', value /= 10); //C1
86     else            for (; value; *(--beg) = get_char(value % base), value /= base);
87 
88     if (beg == end) *(--beg) = '0';
89     if (is_neg)     *(--beg) = '-';
90 
91     return cnv::range<char_type*>(beg, end);
92 }
93 
94 inline
95 double
adjust_fraction(double fraction,int precision)96 boost::cnv::strtol::adjust_fraction(double fraction, int precision)
97 {
98     // C1. Bring forward the fraction coming right after precision digits.
99     //     That is, say, fraction=0.234567, precision=2. Then brought forward=23.4567
100     // C3. INT_MAX(4bytes)=2,147,483,647. So, 10^8 seems appropriate. If not, drop it down to 4.
101     // C4. ::round() returns the integral value that is nearest to x,
102     //     with halfway cases rounded away from zero. Therefore,
103     //          round( 0.4) =  0
104     //          round( 0.5) =  1
105     //          round( 0.6) =  1
106     //          round(-0.4) =  0
107     //          round(-0.5) = -1
108     //          round(-0.6) = -1
109 
110     int const tens[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
111 
112     for (int k = precision / 8; k; --k) fraction *= 100000000; //C3.
113 
114     fraction *= tens[precision % 8]; //C1
115 
116 //  return ::rint(fraction); //C4
117     return boost::math::round(fraction); //C4
118 }
119 
120 template <typename char_type>
121 inline
122 boost::cnv::range<char_type*>
to_str(double value,char_type * buf) const123 boost::cnv::strtol::to_str(double value, char_type* buf) const
124 {
125     char_type*         beg = buf + bufsize_ / 2;
126     char_type*         end = beg;
127     char_type*        ipos = end - 1;
128     bool const is_negative = (value < 0) ? (value = -value, true) : false;
129     double           ipart = std::floor(value);
130     double           fpart = adjust_fraction(value - ipart, precision_);
131     int          precision = precision_;
132     int const         base = 10;
133 
134     for (; 1 <= ipart; ipart /= base)
135         *(--beg) = get_char(int(ipart - std::floor(ipart / base) * base));
136 
137     if (beg == end) *(--beg) = '0';
138     if (precision)  *(end++) = '.';
139 
140     for (char_type* fpos = end += precision; precision; --precision, fpart /= base)
141         *(--fpos) = get_char(int(fpart - std::floor(fpart / base) * base));
142 
143     if (1 <= fpart)
144     {
145         for (; beg <= ipos; --ipos)
146             if (*ipos == '9') *ipos = '0';
147             else { ++*ipos; break; }
148 
149         if (ipos < beg)
150             *(beg = ipos) = '1';
151     }
152     if (is_negative) *(--beg) = '-';
153 
154     return cnv::range<char_type*>(beg, end);
155 }
156 
157 template<typename string_type, typename out_type>
158 void
str_to_i(cnv::range<string_type> range,boost::optional<out_type> & result_out) const159 boost::cnv::strtol::str_to_i(cnv::range<string_type> range, boost::optional<out_type>& result_out) const
160 {
161     using     uint_type = unsigned int;
162     using unsigned_type = typename boost::make_unsigned<out_type>::type;
163     using    range_type = cnv::range<string_type>;
164     using      iterator = typename range_type::iterator;
165 
166     iterator             s = range.begin();
167     uint_type           ch = *s;
168     bool const is_negative = ch == '-' ? (ch = *++s, true) : ch == '+' ? (ch = *++s, false) : false;
169     bool const is_unsigned = boost::is_same<out_type, unsigned_type>::value;
170     uint_type         base = uint_type(base_);
171 
172     /**/ if (is_negative && is_unsigned) return;
173     else if ((base == 0 || base == 16) && ch == '0' && (*++s == 'x' || *s == 'X')) ++s, base = 16;
174     else if (base == 0) base = ch == '0' ? (++s, 8) : 10;
175 
176     unsigned_type const    max = (std::numeric_limits<out_type>::max)();
177     unsigned_type const   umax = max + (is_negative ? 1 : 0);
178     unsigned_type const cutoff = umax / base;
179     uint_type     const cutlim = umax % base;
180     unsigned_type       result = 0;
181 
182     for (; s != range.sentry(); ++s)
183     {
184         ch = *s;
185 
186         /**/ if (std::isdigit(ch)) ch -= '0';
187         else if (std::isalpha(ch)) ch -= (std::isupper(ch) ? 'A' : 'a') - 10;
188         else return;
189 
190         if (base <= ch || cutoff < result || (result == cutoff && cutlim < ch))
191             return;
192 
193         result *= base;
194         result += ch;
195     }
196     result_out = is_negative ? -out_type(result) : out_type(result);
197 }
198 
199 template<typename string_type, typename out_type>
200 void
str_to_d(cnv::range<string_type> range,optional<out_type> & result_out) const201 boost::cnv::strtol::str_to_d(cnv::range<string_type> range, optional<out_type>& result_out) const
202 {
203     // C1. Because of strtold() currently only works with 'char'
204     // C2. strtold() does not work with ranges.
205     //     Consequently, we have to copy the supplied range into a string for strtold().
206     // C3. Check if the end-of-string was reached -- *cnv_end == 0.
207 
208     typedef cnv::range<string_type>      range_type;
209     typedef typename range_type::value_type ch_type;
210 
211     size_t const  sz = 128;
212     ch_type  str[sz] = {0}; std::strncpy(str, &*range.begin(), (std::min)(sz - 1, range.size()));
213     char*    cnv_end = 0;
214     ldbl_type result = strtold(str, &cnv_end);
215     bool        good = result != -HUGE_VALL && result != HUGE_VALL && *cnv_end == 0; //C3
216     out_type     max = (std::numeric_limits<out_type>::max)();
217 
218     if (good && -max <= result && result <= max)
219         result_out = out_type(result);
220 }
221 
222 #endif // BOOST_CONVERT_STRTOL_CONVERTER_HPP
223