1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2011 Jan Frederick Eick
5 Copyright (c) 2011 Christopher Jefferson
6 Copyright (c) 2006 Stephen Nutt
8 Distributed under the Boost Software License, Version 1.0. (See accompanying
9 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
11 #if !defined(BOOST_SPIRIT_X3_DETAIL_EXTRACT_INT_APRIL_17_2006_0816AM)
12 #define BOOST_SPIRIT_X3_DETAIL_EXTRACT_INT_APRIL_17_2006_0816AM
14 #include <boost/spirit/home/x3/support/unused.hpp>
15 #include <boost/spirit/home/x3/support/traits/attribute_type.hpp>
16 #include <boost/spirit/home/x3/support/traits/move_to.hpp>
17 #include <boost/spirit/home/x3/support/traits/numeric_traits.hpp>
18 #include <boost/spirit/home/support/char_encoding/ascii.hpp>
20 #include <boost/preprocessor/repetition/repeat.hpp>
21 #include <boost/preprocessor/iteration/local.hpp>
22 #include <boost/preprocessor/comparison/less.hpp>
23 #include <boost/preprocessor/control/if.hpp>
24 #include <boost/preprocessor/seq/elem.hpp>
26 #include <boost/utility/enable_if.hpp>
28 #include <boost/type_traits/is_integral.hpp>
29 #include <boost/type_traits/is_signed.hpp>
30 #include <boost/type_traits/make_unsigned.hpp>
32 #include <boost/mpl/bool.hpp>
33 #include <boost/mpl/and.hpp>
34 #include <boost/limits.hpp>
36 #include <iterator> // for std::iterator_traits
38 #if !defined(SPIRIT_NUMERICS_LOOP_UNROLL)
39 # define SPIRIT_NUMERICS_LOOP_UNROLL 3
42 namespace boost { namespace spirit { namespace x3 { namespace detail
44 ///////////////////////////////////////////////////////////////////////////
46 // The maximum radix digits that can be represented without
49 // template<typename T, unsigned Radix>
50 // struct digits_traits::value;
52 ///////////////////////////////////////////////////////////////////////////
53 template <typename T, unsigned Radix>
56 template <int Digits, unsigned Radix>
59 // lookup table for log2(x) : 2 <= x <= 36
60 #define BOOST_SPIRIT_X3_LOG2 (#error)(#error) \
61 (1000000)(1584960)(2000000)(2321920)(2584960)(2807350) \
62 (3000000)(3169920)(3321920)(3459430)(3584960)(3700430) \
63 (3807350)(3906890)(4000000)(4087460)(4169920)(4247920) \
64 (4321920)(4392310)(4459430)(4523560)(4584960)(4643850) \
65 (4700430)(4754880)(4807350)(4857980)(4906890)(4954190) \
66 (5000000)(5044390)(5087460)(5129280)(5169925) \
69 #define BOOST_PP_LOCAL_MACRO(Radix) \
70 template <int Digits> struct digits2_to_n<Digits, Radix> \
72 BOOST_STATIC_CONSTANT(int, value = static_cast<int>( \
73 (Digits * 1000000) / \
74 BOOST_PP_SEQ_ELEM(Radix, BOOST_SPIRIT_X3_LOG2))); \
78 #define BOOST_PP_LOCAL_LIMITS (2, 36)
79 #include BOOST_PP_LOCAL_ITERATE()
81 #undef BOOST_SPIRIT_X3_LOG2
83 template <typename T, unsigned Radix>
84 struct digits_traits : digits2_to_n<std::numeric_limits<T>::digits, Radix>
86 static_assert(std::numeric_limits<T>::radix == 2, "");
90 struct digits_traits<T, 10>
92 static int constexpr value = std::numeric_limits<T>::digits10;
95 ///////////////////////////////////////////////////////////////////////////
97 // Traits class for radix specific number conversion
99 // Test the validity of a single character:
101 // template<typename Char> static bool is_valid(Char ch);
103 // Convert a digit from character representation to binary
106 // template<typename Char> static int digit(Char ch);
108 ///////////////////////////////////////////////////////////////////////////
109 template <unsigned Radix>
112 template <typename Char>
113 inline static bool is_valid(Char ch)
115 return (ch >= '0' && ch <= (Radix > 10 ? '9' : static_cast<Char>('0' + Radix -1)))
116 || (Radix > 10 && ch >= 'a' && ch <= static_cast<Char>('a' + Radix -10 -1))
117 || (Radix > 10 && ch >= 'A' && ch <= static_cast<Char>('A' + Radix -10 -1));
120 template <typename Char>
121 inline static unsigned digit(Char ch)
123 return (Radix <= 10 || (ch >= '0' && ch <= '9'))
125 : char_encoding::ascii::tolower(ch) - 'a' + 10;
129 ///////////////////////////////////////////////////////////////////////////
130 // positive_accumulator/negative_accumulator: Accumulator policies for
131 // extracting integers. Use positive_accumulator if number is positive.
132 // Use negative_accumulator if number is negative.
133 ///////////////////////////////////////////////////////////////////////////
134 template <unsigned Radix>
135 struct positive_accumulator
137 template <typename T, typename Char>
138 inline static void add(T& n, Char ch, mpl::false_) // unchecked add
140 const int digit = radix_traits<Radix>::digit(ch);
141 n = n * T(Radix) + T(digit);
144 template <typename T, typename Char>
145 inline static bool add(T& n, Char ch, mpl::true_) // checked add
147 // Ensure n *= Radix will not overflow
148 T const max = (std::numeric_limits<T>::max)();
149 T const val = max / Radix;
155 // Ensure n += digit will not overflow
156 const int digit = radix_traits<Radix>::digit(ch);
157 if (tmp > max - digit)
160 n = tmp + static_cast<T>(digit);
165 template <unsigned Radix>
166 struct negative_accumulator
168 template <typename T, typename Char>
169 inline static void add(T& n, Char ch, mpl::false_) // unchecked subtract
171 const int digit = radix_traits<Radix>::digit(ch);
172 n = n * T(Radix) - T(digit);
175 template <typename T, typename Char>
176 inline static bool add(T& n, Char ch, mpl::true_) // checked subtract
178 // Ensure n *= Radix will not underflow
179 T const min = (std::numeric_limits<T>::min)();
180 T const val = min / T(Radix);
186 // Ensure n -= digit will not underflow
187 int const digit = radix_traits<Radix>::digit(ch);
188 if (tmp < min + digit)
191 n = tmp - static_cast<T>(digit);
196 ///////////////////////////////////////////////////////////////////////////
197 // Common code for extract_int::parse specializations
198 ///////////////////////////////////////////////////////////////////////////
199 template <unsigned Radix, typename Accumulator, int MaxDigits>
202 template <typename Char, typename T>
204 call(Char ch, std::size_t count, T& n, mpl::true_)
206 std::size_t constexpr
207 overflow_free = digits_traits<T, Radix>::value - 1;
209 if (count < overflow_free)
211 Accumulator::add(n, ch, mpl::false_());
215 if (!Accumulator::add(n, ch, mpl::true_()))
216 return false; // over/underflow!
221 template <typename Char, typename T>
223 call(Char ch, std::size_t /*count*/, T& n, mpl::false_)
225 // no need to check for overflow
226 Accumulator::add(n, ch, mpl::false_());
230 template <typename Char>
232 call(Char /*ch*/, std::size_t /*count*/, unused_type, mpl::false_)
237 template <typename Char, typename T>
239 call(Char ch, std::size_t count, T& n)
241 return call(ch, count, n
244 || (MaxDigits > digits_traits<T, Radix>::value)
246 && traits::check_overflow<T>::value
252 ///////////////////////////////////////////////////////////////////////////
253 // End of loop checking: check if the number of digits
254 // being parsed exceeds MaxDigits. Note: if MaxDigits == -1
255 // we don't do any checking.
256 ///////////////////////////////////////////////////////////////////////////
257 template <int MaxDigits>
258 struct check_max_digits
261 call(std::size_t count)
263 return count < MaxDigits; // bounded
268 struct check_max_digits<-1>
271 call(std::size_t /*count*/)
273 return true; // unbounded
277 ///////////////////////////////////////////////////////////////////////////
278 // extract_int: main code for extracting integers
279 ///////////////////////////////////////////////////////////////////////////
280 #define SPIRIT_NUMERIC_INNER_LOOP(z, x, data) \
281 if (!check_max_digits<MaxDigits>::call(count + leading_zeros) \
285 if (!radix_check::is_valid(ch) || !extractor::call(ch, count, val)) \
292 typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
293 , typename Accumulator = positive_accumulator<Radix>
294 , bool Accumulate = false
298 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
299 # pragma warning(push)
300 # pragma warning(disable: 4127) // conditional expression is constant
302 template <typename Iterator, typename Attribute>
306 , Iterator const& last
309 typedef radix_traits<Radix> radix_check;
310 typedef int_extractor<Radix, Accumulator, MaxDigits> extractor;
312 std::iterator_traits<Iterator>::value_type
316 std::size_t leading_zeros = 0;
319 // skip leading zeros
320 while (it != last && *it == '0' && leading_zeros < MaxDigits)
328 traits::attribute_type<Attribute>::type
331 attribute_type val = Accumulate ? attr : attribute_type(0);
332 std::size_t count = 0;
338 SPIRIT_NUMERICS_LOOP_UNROLL
339 , SPIRIT_NUMERIC_INNER_LOOP, _)
342 if (count + leading_zeros >= MinDigits)
344 traits::move_to(val, attr);
350 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
351 # pragma warning(pop)
354 template <typename Iterator>
358 , Iterator const& last
361 T n = 0; // must calculate value to detect over/underflow
362 return parse_main(first, last, n);
365 template <typename Iterator, typename Attribute>
369 , Iterator const& last
372 return parse_main(first, last, attr);
375 #undef SPIRIT_NUMERIC_INNER_LOOP
377 ///////////////////////////////////////////////////////////////////////////
378 // extract_int: main code for extracting integers
379 // common case where MinDigits == 1 and MaxDigits = -1
380 ///////////////////////////////////////////////////////////////////////////
381 #define SPIRIT_NUMERIC_INNER_LOOP(z, x, data) \
385 if (!radix_check::is_valid(ch)) \
387 if (!extractor::call(ch, count, val)) \
393 template <typename T, unsigned Radix, typename Accumulator, bool Accumulate>
394 struct extract_int<T, Radix, 1, -1, Accumulator, Accumulate>
396 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
397 # pragma warning(push)
398 # pragma warning(disable: 4127) // conditional expression is constant
400 template <typename Iterator, typename Attribute>
404 , Iterator const& last
407 typedef radix_traits<Radix> radix_check;
408 typedef int_extractor<Radix, Accumulator, -1> extractor;
410 std::iterator_traits<Iterator>::value_type
414 std::size_t count = 0;
417 // skip leading zeros
418 while (it != last && *it == '0')
426 if (count == 0) // must have at least one digit
435 traits::attribute_type<Attribute>::type
438 attribute_type val = Accumulate ? attr : attribute_type(0);
441 if (!radix_check::is_valid(ch) || !extractor::call(ch, 0, val))
443 if (count == 0) // must have at least one digit
445 traits::move_to(val, attr);
455 SPIRIT_NUMERICS_LOOP_UNROLL
456 , SPIRIT_NUMERIC_INNER_LOOP, _)
459 traits::move_to(val, attr);
463 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
464 # pragma warning(pop)
467 template <typename Iterator>
471 , Iterator const& last
474 T n = 0; // must calculate value to detect over/underflow
475 return parse_main(first, last, n);
478 template <typename Iterator, typename Attribute>
482 , Iterator const& last
485 return parse_main(first, last, attr);
489 #undef SPIRIT_NUMERIC_INNER_LOOP