]>
Commit | Line | Data |
---|---|---|
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 | ||
43 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< int_type>& r) const { str_to_i (v, r); } | |
44 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< sint_type>& r) const { str_to_i (v, r); } | |
45 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< lint_type>& r) const { str_to_i (v, r); } | |
46 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< llint_type>& r) const { str_to_i (v, r); } | |
47 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< uint_type>& r) const { str_to_i (v, r); } | |
48 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< usint_type>& r) const { str_to_i (v, r); } | |
49 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< ulint_type>& r) const { str_to_i (v, r); } | |
50 | template<typename string_type> void str_to(cnv::range<string_type> v, optional<ullint_type>& r) const { str_to_i (v, r); } | |
51 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< flt_type>& r) const { str_to_d (v, r); } | |
52 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< dbl_type>& r) const { str_to_d (v, r); } | |
53 | template<typename string_type> void str_to(cnv::range<string_type> v, optional< ldbl_type>& r) const { str_to_d (v, r); } | |
54 | ||
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 | ||
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); | |
68 | 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*> | |
73 | 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_negative = in_value < 0; | |
82 | unsigned_type value = static_cast<unsigned_type>(is_negative ? -in_value : in_value); | |
83 | ||
84 | if (base_ == 10) for (; value; *(--beg) = int(value % 10) + '0', value /= 10); //C1 | |
85 | else for (; value; *(--beg) = get_char(value % base_), value /= base_); | |
86 | ||
87 | if (beg == end) *(--beg) = '0'; | |
88 | if (is_negative) *(--beg) = '-'; | |
89 | ||
90 | return cnv::range<char_type*>(beg, end); | |
91 | } | |
92 | ||
93 | inline | |
94 | double | |
95 | boost::cnv::strtol::adjust_fraction(double fraction, int precision) | |
96 | { | |
97 | // C1. Bring forward the fraction coming right after precision digits. | |
98 | // That is, say, fraction=0.234567, precision=2. Then brought forward=23.4567 | |
99 | // C3. INT_MAX(4bytes)=2,147,483,647. So, 10^8 seems appropriate. If not, drop it down to 4. | |
100 | // C4. ::round() returns the integral value that is nearest to x, | |
101 | // with halfway cases rounded away from zero. Therefore, | |
102 | // round( 0.4) = 0 | |
103 | // round( 0.5) = 1 | |
104 | // round( 0.6) = 1 | |
105 | // round(-0.4) = 0 | |
106 | // round(-0.5) = -1 | |
107 | // round(-0.6) = -1 | |
108 | ||
109 | int const tens[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 }; | |
110 | ||
111 | for (int k = precision / 8; k; --k) fraction *= 100000000; //C3. | |
112 | ||
113 | fraction *= tens[precision % 8]; //C1 | |
114 | ||
115 | // return ::rint(fraction); //C4 | |
116 | return boost::math::round(fraction); //C4 | |
117 | } | |
118 | ||
119 | template <typename char_type> | |
120 | inline | |
121 | boost::cnv::range<char_type*> | |
122 | boost::cnv::strtol::to_str(double value, char_type* buf) const | |
123 | { | |
124 | char_type* beg = buf + bufsize_ / 2; | |
125 | char_type* end = beg; | |
126 | char_type* ipos = end - 1; | |
127 | bool const is_negative = (value < 0) ? (value = -value, true) : false; | |
128 | double ipart = std::floor(value); | |
129 | double fpart = adjust_fraction(value - ipart, precision_); | |
130 | int precision = precision_; | |
131 | int const base = 10; | |
132 | ||
133 | for (; 1 <= ipart; ipart /= base) | |
134 | *(--beg) = get_char(int(ipart - std::floor(ipart / base) * base)); | |
135 | ||
136 | if (beg == end) *(--beg) = '0'; | |
137 | if (precision) *(end++) = '.'; | |
138 | ||
139 | for (char_type* fpos = end += precision; precision; --precision, fpart /= base) | |
140 | *(--fpos) = get_char(int(fpart - std::floor(fpart / base) * base)); | |
141 | ||
142 | if (1 <= fpart) | |
143 | { | |
144 | for (; beg <= ipos; --ipos) | |
145 | if (*ipos == '9') *ipos = '0'; | |
146 | else { ++*ipos; break; } | |
147 | ||
148 | if (ipos < beg) | |
149 | *(beg = ipos) = '1'; | |
150 | } | |
151 | if (is_negative) *(--beg) = '-'; | |
152 | ||
153 | return cnv::range<char_type*>(beg, end); | |
154 | } | |
155 | ||
156 | template<typename string_type, typename out_type> | |
157 | void | |
158 | boost::cnv::strtol::str_to_i(cnv::range<string_type> range, boost::optional<out_type>& result_out) const | |
159 | { | |
160 | typedef typename boost::make_unsigned<out_type>::type unsigned_type; | |
161 | typedef cnv::range<string_type> range_type; | |
162 | typedef typename range_type::iterator iterator; | |
163 | ||
164 | iterator s = range.begin(); | |
165 | unsigned int ch = *s; | |
166 | bool const is_negative = ch == '-' ? (ch = *++s, true) : ch == '+' ? (ch = *++s, false) : false; | |
167 | bool const is_unsigned = boost::is_same<out_type, unsigned_type>::value; | |
168 | unsigned int base = base_; | |
169 | ||
170 | /**/ if (is_negative && is_unsigned) return; | |
171 | else if ((base == 0 || base == 16) && ch == '0' && (*++s == 'x' || *s == 'X')) ++s, base = 16; | |
172 | else if (base == 0) base = ch == '0' ? (++s, 8) : 10; | |
173 | ||
174 | unsigned_type const max = (std::numeric_limits<out_type>::max)() + (is_negative ? 1 : 0); | |
175 | unsigned_type const cutoff = max / base; | |
176 | unsigned int const cutlim = max % base; | |
177 | unsigned_type result = 0; | |
178 | ||
179 | for (; s != range.sentry(); ++s) | |
180 | { | |
181 | ch = *s; | |
182 | ||
183 | /**/ if (std::isdigit(ch)) ch -= '0'; | |
184 | else if (std::isalpha(ch)) ch -= (std::isupper(ch) ? 'A' : 'a') - 10; | |
185 | else return; | |
186 | ||
187 | if (base <= ch || cutoff < result || (result == cutoff && cutlim < ch)) | |
188 | return; | |
189 | ||
190 | result *= base; | |
191 | result += ch; | |
192 | } | |
193 | result_out = is_negative ? -out_type(result) : out_type(result); | |
194 | } | |
195 | ||
196 | template<typename string_type, typename out_type> | |
197 | void | |
198 | boost::cnv::strtol::str_to_d(cnv::range<string_type> range, optional<out_type>& result_out) const | |
199 | { | |
200 | // C2. Simply check if the end-of-string was reached -- *cnv_end == 0 | |
201 | // instead of traversing once with strlen() to find the end iterator | |
202 | // and then comparing to it as in | |
203 | // char const* end = str + strlen(str); // Unnecessary traversal! | |
204 | // bool const good = ... && cnv_end == end; | |
205 | ||
206 | typedef cnv::range<string_type> range_type; | |
207 | typedef typename range_type::value_type ch_type; | |
208 | ||
209 | ch_type const* str = &*range.begin(); // Currently only works with 'char' | |
210 | char* cnv_end = 0; | |
211 | ldbl_type result = strtold(str, &cnv_end); | |
212 | bool good = result != -HUGE_VALL && result != HUGE_VALL && *cnv_end == 0/*C2*/; | |
213 | out_type max = (std::numeric_limits<out_type>::max)(); | |
214 | ||
215 | if (good && -max <= result && result <= max) | |
216 | result_out = out_type(result); | |
217 | } | |
218 | ||
219 | #endif // BOOST_CONVERT_STRTOL_CONVERTER_HPP |