1 /*=============================================================================
2 Copyright (c) 2001-2011 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(SPIRIT_NUMERIC_UTILS_APRIL_17_2006_0816AM)
12 #define SPIRIT_NUMERIC_UTILS_APRIL_17_2006_0816AM
18 #include <boost/detail/iterator.hpp>
19 #include <boost/spirit/home/support/unused.hpp>
20 #include <boost/spirit/home/qi/detail/attributes.hpp>
21 #include <boost/spirit/home/support/char_encoding/ascii.hpp>
22 #include <boost/spirit/home/support/numeric_traits.hpp>
23 #include <boost/preprocessor/repetition/repeat.hpp>
24 #include <boost/preprocessor/iteration/local.hpp>
25 #include <boost/preprocessor/comparison/less.hpp>
26 #include <boost/preprocessor/control/if.hpp>
27 #include <boost/preprocessor/seq/elem.hpp>
28 #include <boost/utility/enable_if.hpp>
29 #include <boost/type_traits/is_integral.hpp>
30 #include <boost/type_traits/is_signed.hpp>
31 #include <boost/mpl/bool.hpp>
32 #include <boost/mpl/and.hpp>
33 #include <boost/limits.hpp>
34 #include <boost/integer_traits.hpp>
36 #if defined(BOOST_MSVC)
37 # pragma warning(push)
38 # pragma warning(disable: 4127) // conditional expression is constant
41 #if !defined(SPIRIT_NUMERICS_LOOP_UNROLL)
42 # define SPIRIT_NUMERICS_LOOP_UNROLL 3
45 namespace boost { namespace spirit { namespace qi { namespace detail
47 ///////////////////////////////////////////////////////////////////////////
49 // The maximum radix digits that can be represented without
52 // template<typename T, unsigned Radix>
53 // struct digits_traits::value;
55 ///////////////////////////////////////////////////////////////////////////
56 template <typename T, unsigned Radix>
59 // lookup table for log2(x) : 2 <= x <= 36
60 #define BOOST_SPIRIT_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 <typename T> struct digits_traits<T, Radix> \
72 typedef std::numeric_limits<T> numeric_limits_type; \
73 BOOST_STATIC_CONSTANT(int, value = static_cast<int>( \
74 (numeric_limits_type::digits * 1000000) / \
75 BOOST_PP_SEQ_ELEM(Radix, BOOST_SPIRIT_LOG2))); \
79 #define BOOST_PP_LOCAL_LIMITS (2, 36)
80 #include BOOST_PP_LOCAL_ITERATE()
82 #undef BOOST_SPIRIT_LOG2
84 ///////////////////////////////////////////////////////////////////////////
86 // Traits class for radix specific number conversion
88 // Test the validity of a single character:
90 // template<typename Char> static bool is_valid(Char ch);
92 // Convert a digit from character representation to binary
95 // template<typename Char> static int digit(Char ch);
97 ///////////////////////////////////////////////////////////////////////////
98 template <unsigned Radix>
101 template <typename Char>
102 inline static bool is_valid(Char ch)
105 return (ch >= '0' && ch <= static_cast<Char>('0' + Radix -1));
106 return (ch >= '0' && ch <= '9')
107 || (ch >= 'a' && ch <= static_cast<Char>('a' + Radix -10 -1))
108 || (ch >= 'A' && ch <= static_cast<Char>('A' + Radix -10 -1));
111 template <typename Char>
112 inline static unsigned digit(Char ch)
114 if (Radix <= 10 || (ch >= '0' && ch <= '9'))
116 return spirit::char_encoding::ascii::tolower(ch) - 'a' + 10;
120 template <typename T, T Val>
123 BOOST_STATIC_CONSTEXPR T value = Val;
126 ///////////////////////////////////////////////////////////////////////////
127 // positive_accumulator/negative_accumulator: Accumulator policies for
128 // extracting integers. Use positive_accumulator if number is positive.
129 // Use negative_accumulator if number is negative.
130 ///////////////////////////////////////////////////////////////////////////
131 template <unsigned Radix>
132 struct positive_accumulator
134 template <typename T, typename Char>
135 inline static void add(T& n, Char ch, mpl::false_) // unchecked add
137 const int digit = radix_traits<Radix>::digit(ch);
138 n = n * T(Radix) + T(digit);
141 template <typename T, typename Char>
142 inline static bool add(T& n, Char ch, mpl::true_) // checked add
144 // Ensure n *= Radix will not overflow
145 typedef constexpr_int<T, boost::integer_traits<T>::const_max> max;
146 typedef constexpr_int<T, max::value / Radix> val;
153 // Ensure n += digit will not overflow
154 const int digit = radix_traits<Radix>::digit(ch);
155 if (n > max::value - digit)
158 n += static_cast<T>(digit);
163 template <unsigned Radix>
164 struct negative_accumulator
166 template <typename T, typename Char>
167 inline static void add(T& n, Char ch, mpl::false_) // unchecked subtract
169 const int digit = radix_traits<Radix>::digit(ch);
170 n = n * T(Radix) - T(digit);
173 template <typename T, typename Char>
174 inline static bool add(T& n, Char ch, mpl::true_) // checked subtract
176 // Ensure n *= Radix will not underflow
177 typedef constexpr_int<T, boost::integer_traits<T>::const_min> min;
178 typedef constexpr_int<T, (min::value + 1) / T(Radix)> val;
185 // Ensure n -= digit will not underflow
186 int const digit = radix_traits<Radix>::digit(ch);
187 if (n < min::value + digit)
190 n -= static_cast<T>(digit);
195 ///////////////////////////////////////////////////////////////////////////
196 // Common code for extract_int::parse specializations
197 ///////////////////////////////////////////////////////////////////////////
198 template <unsigned Radix, typename Accumulator, int MaxDigits, bool AlwaysCheckOverflow>
201 template <typename Char, typename T>
203 call(Char ch, std::size_t count, T& n, mpl::true_)
205 typedef constexpr_int<std::size_t, digits_traits<T, Radix>::value - 1> overflow_free;
207 if (!AlwaysCheckOverflow && (count < overflow_free::value))
209 Accumulator::add(n, ch, mpl::false_());
213 if (!Accumulator::add(n, ch, mpl::true_()))
214 return false; // over/underflow!
219 template <typename Char, typename T>
221 call(Char ch, std::size_t /*count*/, T& n, mpl::false_)
223 // no need to check for overflow
224 Accumulator::add(n, ch, mpl::false_());
228 template <typename Char>
230 call(Char /*ch*/, std::size_t /*count*/, unused_type, mpl::false_)
235 template <typename Char, typename T>
237 call(Char ch, std::size_t count, T& n)
239 return call(ch, count, n
242 || (MaxDigits > digits_traits<T, Radix>::value)
244 && traits::check_overflow<T>::value
250 ///////////////////////////////////////////////////////////////////////////
251 // End of loop checking: check if the number of digits
252 // being parsed exceeds MaxDigits. Note: if MaxDigits == -1
253 // we don't do any checking.
254 ///////////////////////////////////////////////////////////////////////////
255 template <int MaxDigits>
256 struct check_max_digits
259 call(std::size_t count)
261 return count < MaxDigits; // bounded
266 struct check_max_digits<-1>
269 call(std::size_t /*count*/)
271 return true; // unbounded
275 ///////////////////////////////////////////////////////////////////////////
276 // extract_int: main code for extracting integers
277 ///////////////////////////////////////////////////////////////////////////
278 #define SPIRIT_NUMERIC_INNER_LOOP(z, x, data) \
279 if (!check_max_digits<MaxDigits>::call(count + leading_zeros) \
285 if (!radix_check::is_valid(ch)) \
289 if (!extractor::call(ch, count, val)) \
291 if (IgnoreOverflowDigits) \
295 traits::assign_to(val, attr); \
296 return IgnoreOverflowDigits; \
303 typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
304 , typename Accumulator = positive_accumulator<Radix>
305 , bool Accumulate = false
306 , bool IgnoreOverflowDigits = false
310 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
311 # pragma warning(push)
312 # pragma warning(disable: 4127) // conditional expression is constant
314 template <typename Iterator, typename Attribute>
318 , Iterator const& last
321 typedef radix_traits<Radix> radix_check;
322 typedef int_extractor<Radix, Accumulator, MaxDigits, Accumulate> extractor;
324 boost::detail::iterator_traits<Iterator>::value_type
328 std::size_t leading_zeros = 0;
331 // skip leading zeros
332 while (it != last && *it == '0' && (MaxDigits < 0 || leading_zeros < static_cast< std::size_t >(MaxDigits)))
340 traits::attribute_type<Attribute>::type
343 attribute_type val = Accumulate ? attr : attribute_type(0);
344 std::size_t count = 0;
350 SPIRIT_NUMERICS_LOOP_UNROLL
351 , SPIRIT_NUMERIC_INNER_LOOP, _)
354 if (count + leading_zeros >= MinDigits)
356 traits::assign_to(val, attr);
362 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
363 # pragma warning(pop)
366 template <typename Iterator>
370 , Iterator const& last
373 T n = 0; // must calculate value to detect over/underflow
374 return parse_main(first, last, n);
377 template <typename Iterator, typename Attribute>
381 , Iterator const& last
384 return parse_main(first, last, attr);
387 #undef SPIRIT_NUMERIC_INNER_LOOP
389 ///////////////////////////////////////////////////////////////////////////
390 // extract_int: main code for extracting integers
391 // common case where MinDigits == 1 and MaxDigits = -1
392 ///////////////////////////////////////////////////////////////////////////
393 #define SPIRIT_NUMERIC_INNER_LOOP(z, x, data) \
399 if (!radix_check::is_valid(ch)) \
403 if (!extractor::call(ch, count, val)) \
405 traits::assign_to(val, attr); \
412 template <typename T, unsigned Radix, typename Accumulator, bool Accumulate>
413 struct extract_int<T, Radix, 1, -1, Accumulator, Accumulate>
415 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
416 # pragma warning(push)
417 # pragma warning(disable: 4127) // conditional expression is constant
419 template <typename Iterator, typename Attribute>
423 , Iterator const& last
426 typedef radix_traits<Radix> radix_check;
427 typedef int_extractor<Radix, Accumulator, -1, Accumulate> extractor;
429 boost::detail::iterator_traits<Iterator>::value_type
433 std::size_t count = 0;
436 // skip leading zeros
437 while (it != last && *it == '0')
445 if (count == 0) // must have at least one digit
447 traits::assign_to(0, attr);
454 traits::attribute_type<Attribute>::type
457 attribute_type val = Accumulate ? attr : attribute_type(0);
460 if (!radix_check::is_valid(ch) || !extractor::call(ch, 0, val))
462 if (count == 0) // must have at least one digit
464 traits::assign_to(val, attr);
469 // count = 0; $$$ verify: I think this is wrong $$$
474 SPIRIT_NUMERICS_LOOP_UNROLL
475 , SPIRIT_NUMERIC_INNER_LOOP, _)
478 traits::assign_to(val, attr);
482 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
483 # pragma warning(pop)
486 template <typename Iterator>
490 , Iterator const& last
493 T n = 0; // must calculate value to detect over/underflow
494 return parse_main(first, last, n);
497 template <typename Iterator, typename Attribute>
501 , Iterator const& last
504 return parse_main(first, last, attr);
508 #undef SPIRIT_NUMERIC_INNER_LOOP
510 ///////////////////////////////////////////////////////////////////////////
511 // Cast an signed integer to an unsigned integer
512 ///////////////////////////////////////////////////////////////////////////
513 template <typename T,
515 = mpl::and_<is_integral<T>, is_signed<T> >::value>
516 struct cast_unsigned;
518 template <typename T>
519 struct cast_unsigned<T, true>
521 typedef typename make_unsigned<T>::type unsigned_type;
522 typedef typename make_unsigned<T>::type& unsigned_type_ref;
524 inline static unsigned_type_ref call(T& n)
526 return unsigned_type_ref(n);
530 template <typename T>
531 struct cast_unsigned<T, false>
533 inline static T& call(T& n)
540 #if defined(BOOST_MSVC)
541 # pragma warning(pop)