]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/convert/strtol.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / convert / strtol.hpp
CommitLineData
20effc67 1// Copyright (c) 2009-2020 Vladimir Batov.
7c673cae
FG
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>
7c673cae
FG
9#include <boost/math/special_functions/round.hpp>
10#include <limits>
7c673cae 11#include <climits>
20effc67 12#include <cstdlib>
7c673cae
FG
13
14namespace boost { namespace cnv
15{
16 struct strtol;
17}}
18
19/// @brief std::strtol-based extended converter
20/// @details The converter offers a fairly decent overall performance and moderate formatting facilities.
21
22struct boost::cnv::strtol : public boost::cnv::cnvbase<boost::cnv::strtol>
23{
20effc67
TL
24 using this_type = boost::cnv::strtol;
25 using base_type = boost::cnv::cnvbase<this_type>;
7c673cae
FG
26
27 using base_type::operator();
28
29 private:
30
31 friend struct boost::cnv::cnvbase<this_type>;
32
33 template<typename string_type> void str_to(cnv::range<string_type> v, optional< int_type>& r) const { str_to_i (v, r); }
34 template<typename string_type> void str_to(cnv::range<string_type> v, optional< sint_type>& r) const { str_to_i (v, r); }
35 template<typename string_type> void str_to(cnv::range<string_type> v, optional< lint_type>& r) const { str_to_i (v, r); }
36 template<typename string_type> void str_to(cnv::range<string_type> v, optional< llint_type>& r) const { str_to_i (v, r); }
37 template<typename string_type> void str_to(cnv::range<string_type> v, optional< uint_type>& r) const { str_to_i (v, r); }
38 template<typename string_type> void str_to(cnv::range<string_type> v, optional< usint_type>& r) const { str_to_i (v, r); }
39 template<typename string_type> void str_to(cnv::range<string_type> v, optional< ulint_type>& r) const { str_to_i (v, r); }
40 template<typename string_type> void str_to(cnv::range<string_type> v, optional<ullint_type>& r) const { str_to_i (v, r); }
41 template<typename string_type> void str_to(cnv::range<string_type> v, optional< flt_type>& r) const { str_to_d (v, r); }
42 template<typename string_type> void str_to(cnv::range<string_type> v, optional< dbl_type>& r) const { str_to_d (v, r); }
43 template<typename string_type> void str_to(cnv::range<string_type> v, optional< ldbl_type>& r) const { str_to_d (v, r); }
44
45 template <typename char_type> cnv::range<char_type*> to_str ( int_type v, char_type* buf) const { return i_to_str(v, buf); }
46 template <typename char_type> cnv::range<char_type*> to_str ( uint_type v, char_type* buf) const { return i_to_str(v, buf); }
47 template <typename char_type> cnv::range<char_type*> to_str ( lint_type v, char_type* buf) const { return i_to_str(v, buf); }
48 template <typename char_type> cnv::range<char_type*> to_str ( ulint_type v, char_type* buf) const { return i_to_str(v, buf); }
49 template <typename char_type> cnv::range<char_type*> to_str ( llint_type v, char_type* buf) const { return i_to_str(v, buf); }
50 template <typename char_type> cnv::range<char_type*> to_str (ullint_type v, char_type* buf) const { return i_to_str(v, buf); }
51 template <typename char_type> cnv::range<char_type*> to_str ( dbl_type v, char_type* buf) const;
52
53 template<typename char_type, typename in_type> cnv::range<char_type*> i_to_str (in_type, char_type*) const;
54 template<typename string_type, typename out_type> void str_to_i (cnv::range<string_type>, optional<out_type>&) const;
55 template<typename string_type, typename out_type> void str_to_d (cnv::range<string_type>, optional<out_type>&) const;
56
57 static double adjust_fraction (double, int);
58 static int get_char (int v) { return (v < 10) ? (v += '0') : (v += 'A' - 10); }
59};
60
61template<typename char_type, typename Type>
62boost::cnv::range<char_type*>
63boost::cnv::strtol::i_to_str(Type in_value, char_type* buf) const
64{
65 // C1. Base=10 optimization improves performance 10%
66
20effc67 67 using unsigned_type = typename std::make_unsigned<Type>::type;
7c673cae 68
b32b8144
FG
69 char_type* beg = buf + bufsize_ / 2;
70 char_type* end = beg;
71 bool const is_neg = std::is_signed<Type>::value && in_value < 0;
72 unsigned_type value = static_cast<unsigned_type>(is_neg ? -in_value : in_value);
92f5a8d4 73 int base = int(base_);
7c673cae 74
92f5a8d4
TL
75 if (base == 10) for (; value; *(--beg) = int(value % 10) + '0', value /= 10); //C1
76 else for (; value; *(--beg) = get_char(value % base), value /= base);
7c673cae 77
b32b8144
FG
78 if (beg == end) *(--beg) = '0';
79 if (is_neg) *(--beg) = '-';
7c673cae
FG
80
81 return cnv::range<char_type*>(beg, end);
82}
83
84inline
85double
86boost::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
100 int const tens[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
101
102 for (int k = precision / 8; k; --k) fraction *= 100000000; //C3.
103
104 fraction *= tens[precision % 8]; //C1
105
106// return ::rint(fraction); //C4
107 return boost::math::round(fraction); //C4
108}
109
110template <typename char_type>
111inline
112boost::cnv::range<char_type*>
113boost::cnv::strtol::to_str(double value, char_type* buf) const
114{
20effc67
TL
115 char_type* beg = buf + bufsize_ / 2;
116 char_type* end = beg;
117 char_type* ipos = end - 1;
118 bool is_negative = (value < 0) ? (value = -value, true) : false;
119 double ipart = std::floor(value);
120 double fpart = adjust_fraction(value - ipart, precision_);
121 int precision = precision_;
122 int const base = 10;
7c673cae
FG
123
124 for (; 1 <= ipart; ipart /= base)
125 *(--beg) = get_char(int(ipart - std::floor(ipart / base) * base));
126
127 if (beg == end) *(--beg) = '0';
128 if (precision) *(end++) = '.';
129
130 for (char_type* fpos = end += precision; precision; --precision, fpart /= base)
131 *(--fpos) = get_char(int(fpart - std::floor(fpart / base) * base));
132
133 if (1 <= fpart)
134 {
135 for (; beg <= ipos; --ipos)
136 if (*ipos == '9') *ipos = '0';
137 else { ++*ipos; break; }
138
139 if (ipos < beg)
140 *(beg = ipos) = '1';
141 }
142 if (is_negative) *(--beg) = '-';
143
144 return cnv::range<char_type*>(beg, end);
145}
146
147template<typename string_type, typename out_type>
148void
149boost::cnv::strtol::str_to_i(cnv::range<string_type> range, boost::optional<out_type>& result_out) const
150{
92f5a8d4 151 using uint_type = unsigned int;
20effc67 152 using unsigned_type = typename std::make_unsigned<out_type>::type;
92f5a8d4
TL
153 using range_type = cnv::range<string_type>;
154 using iterator = typename range_type::iterator;
7c673cae
FG
155
156 iterator s = range.begin();
92f5a8d4 157 uint_type ch = *s;
7c673cae 158 bool const is_negative = ch == '-' ? (ch = *++s, true) : ch == '+' ? (ch = *++s, false) : false;
20effc67 159 bool const is_unsigned = std::is_same<out_type, unsigned_type>::value;
92f5a8d4 160 uint_type base = uint_type(base_);
7c673cae
FG
161
162 /**/ if (is_negative && is_unsigned) return;
163 else if ((base == 0 || base == 16) && ch == '0' && (*++s == 'x' || *s == 'X')) ++s, base = 16;
164 else if (base == 0) base = ch == '0' ? (++s, 8) : 10;
165
92f5a8d4
TL
166 unsigned_type const max = (std::numeric_limits<out_type>::max)();
167 unsigned_type const umax = max + (is_negative ? 1 : 0);
168 unsigned_type const cutoff = umax / base;
169 uint_type const cutlim = umax % base;
7c673cae
FG
170 unsigned_type result = 0;
171
172 for (; s != range.sentry(); ++s)
173 {
174 ch = *s;
175
176 /**/ if (std::isdigit(ch)) ch -= '0';
177 else if (std::isalpha(ch)) ch -= (std::isupper(ch) ? 'A' : 'a') - 10;
178 else return;
179
180 if (base <= ch || cutoff < result || (result == cutoff && cutlim < ch))
181 return;
182
183 result *= base;
184 result += ch;
185 }
186 result_out = is_negative ? -out_type(result) : out_type(result);
187}
188
189template<typename string_type, typename out_type>
190void
191boost::cnv::strtol::str_to_d(cnv::range<string_type> range, optional<out_type>& result_out) const
192{
b32b8144
FG
193 // C1. Because of strtold() currently only works with 'char'
194 // C2. strtold() does not work with ranges.
195 // Consequently, we have to copy the supplied range into a string for strtold().
196 // C3. Check if the end-of-string was reached -- *cnv_end == 0.
7c673cae 197
20effc67
TL
198 using range_type = cnv::range<string_type>;
199 using ch_type = typename range_type::value_type;
7c673cae 200
b32b8144 201 size_t const sz = 128;
92f5a8d4 202 ch_type str[sz] = {0}; std::strncpy(str, &*range.begin(), (std::min)(sz - 1, range.size()));
b32b8144
FG
203 char* cnv_end = 0;
204 ldbl_type result = strtold(str, &cnv_end);
205 bool good = result != -HUGE_VALL && result != HUGE_VALL && *cnv_end == 0; //C3
206 out_type max = (std::numeric_limits<out_type>::max)();
7c673cae
FG
207
208 if (good && -max <= result && result <= max)
209 result_out = out_type(result);
210}
211
212#endif // BOOST_CONVERT_STRTOL_CONVERTER_HPP