1 /*=============================================================================
2 Copyright (c) 2001-2019 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #ifndef BOOST_SPIRIT_QI_NUMERIC_REAL_POLICIES_HPP
9 #define BOOST_SPIRIT_QI_NUMERIC_REAL_POLICIES_HPP
15 #include <boost/spirit/home/qi/numeric/numeric_utils.hpp>
16 #include <boost/spirit/home/qi/detail/string_parse.hpp>
17 #include <boost/type_traits/is_floating_point.hpp>
19 namespace boost { namespace spirit { namespace traits
21 // So that we won't exceed the capacity of the underlying type T,
22 // we limit the number of digits parsed to its max_digits10.
23 // By default, the value is -1 which tells spirit to parse an
24 // unbounded number of digits.
26 template <typename T, typename Enable = void>
29 static int const value = -1; // unbounded
34 , typename enable_if_c<(is_floating_point<T>::value)>::type>
36 static int const digits = std::numeric_limits<T>::digits;
37 static int const value = 2 + (digits * 30103l) / 100000l;
41 namespace boost { namespace spirit { namespace qi
43 ///////////////////////////////////////////////////////////////////////////
44 // Default (unsigned) real number policies
45 ///////////////////////////////////////////////////////////////////////////
50 typedef mpl::int_<2> version;
52 // trailing dot policy suggested by Gustavo Guerra
53 static bool const allow_leading_dot = true;
54 static bool const allow_trailing_dot = true;
55 static bool const expect_dot = false;
57 template <typename Iterator>
59 parse_sign(Iterator& /*first*/, Iterator const& /*last*/)
64 template <typename Iterator, typename Attribute>
66 parse_n(Iterator& first, Iterator const& last, Attribute& attr_)
68 typedef extract_uint<Attribute, 10, 1
69 , traits::max_digits10<T>::value // See notes on max_digits10 above
72 return extract_uint::call(first, last, attr_);
75 // ignore_excess_digits (required for version > 1 API)
76 template <typename Iterator>
78 ignore_excess_digits(Iterator& first, Iterator const& last)
80 Iterator save = first;
81 if (extract_uint<unused_type, 10, 1, -1>::call(first, last, unused))
82 return static_cast<std::size_t>(std::distance(save, first));
86 template <typename Iterator>
88 parse_dot(Iterator& first, Iterator const& last)
90 if (first == last || *first != '.')
96 template <typename Iterator, typename Attribute>
98 parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr_, int& frac_digits)
100 Iterator savef = first;
101 bool r = extract_uint<Attribute, 10, 1, -1, true, true>::call(first, last, attr_);
104 #if defined(_MSC_VER) && _MSC_VER < 1900
105 # pragma warning(push)
106 # pragma warning(disable: 4127) // conditional expression is constant
108 // Optimization note: don't compute frac_digits if T is
109 // an unused_type. This should be optimized away by the compiler.
110 if (!is_same<T, unused_type>::value)
112 static_cast<int>(std::distance(savef, first));
113 #if defined(_MSC_VER) && _MSC_VER < 1900
114 # pragma warning(pop)
116 // ignore extra (non-significant digits)
117 extract_uint<unused_type, 10, 1, -1>::call(first, last, unused);
122 template <typename Iterator>
124 parse_exp(Iterator& first, Iterator const& last)
126 if (first == last || (*first != 'e' && *first != 'E'))
132 template <typename Iterator>
134 parse_exp_n(Iterator& first, Iterator const& last, int& attr_)
136 return extract_int<int, 10, 1, -1>::call(first, last, attr_);
139 ///////////////////////////////////////////////////////////////////////
140 // The parse_nan() and parse_inf() functions get called whenever
141 // a number to parse does not start with a digit (after having
142 // successfully parsed an optional sign).
144 // The functions should return true if a Nan or Inf has been found. In
145 // this case the attr should be set to the matched value (NaN or
146 // Inf). The optional sign will be automatically applied afterwards.
148 // The default implementation below recognizes representations of NaN
149 // and Inf as mandated by the C99 Standard and as proposed for
150 // inclusion into the C++0x Standard: nan, nan(...), inf and infinity
151 // (the matching is performed case-insensitively).
152 ///////////////////////////////////////////////////////////////////////
153 template <typename Iterator, typename Attribute>
155 parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
158 return false; // end of input reached
160 if (*first != 'n' && *first != 'N')
161 return false; // not "nan"
164 if (detail::string_parse("nan", "NAN", first, last, unused))
166 if (first != last && *first == '(')
168 // skip trailing (...) part
171 while (++i != last && *i != ')')
174 return false; // no trailing ')' found, give up
178 attr_ = std::numeric_limits<T>::quiet_NaN();
184 template <typename Iterator, typename Attribute>
186 parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
189 return false; // end of input reached
191 if (*first != 'i' && *first != 'I')
192 return false; // not "inf"
195 if (detail::string_parse("inf", "INF", first, last, unused))
197 // skip allowed 'inity' part of infinity
198 detail::string_parse("inity", "INITY", first, last, unused);
199 attr_ = std::numeric_limits<T>::infinity();
206 ///////////////////////////////////////////////////////////////////////////
207 // Default (signed) real number policies
208 ///////////////////////////////////////////////////////////////////////////
209 template <typename T>
210 struct real_policies : ureal_policies<T>
212 template <typename Iterator>
214 parse_sign(Iterator& first, Iterator const& last)
216 return extract_sign(first, last);
220 template <typename T>
221 struct strict_ureal_policies : ureal_policies<T>
223 static bool const expect_dot = true;
226 template <typename T>
227 struct strict_real_policies : real_policies<T>
229 static bool const expect_dot = true;