Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:02:36

0001 /*=============================================================================
0002     Copyright (c) 2001-2014 Joel de Guzman
0003     Copyright (c) 2001-2011 Hartmut Kaiser
0004 
0005     Distributed under the Boost Software License, Version 1.0. (See accompanying
0006     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 ==============================================================================*/
0008 #if !defined(BOOST_SPIRIT_X3_REAL_POLICIES_APRIL_17_2006_1158PM)
0009 #define BOOST_SPIRIT_X3_REAL_POLICIES_APRIL_17_2006_1158PM
0010 
0011 #include <boost/spirit/home/x3/string/detail/string_parse.hpp>
0012 #include <boost/spirit/home/x3/support/numeric_utils/extract_int.hpp>
0013 
0014 namespace boost { namespace spirit { namespace x3
0015 {
0016     ///////////////////////////////////////////////////////////////////////////
0017     //  Default (unsigned) real number policies
0018     ///////////////////////////////////////////////////////////////////////////
0019     template <typename T>
0020     struct ureal_policies
0021     {
0022         // trailing dot policy suggested by Gustavo Guerra
0023         static bool const allow_leading_dot = true;
0024         static bool const allow_trailing_dot = true;
0025         static bool const expect_dot = false;
0026 
0027         template <typename Iterator>
0028         static bool
0029         parse_sign(Iterator& /*first*/, Iterator const& /*last*/)
0030         {
0031             return false;
0032         }
0033 
0034         template <typename Iterator, typename Attribute>
0035         static bool
0036         parse_n(Iterator& first, Iterator const& last, Attribute& attr_)
0037         {
0038             return extract_uint<T, 10, 1, -1>::call(first, last, attr_);
0039         }
0040 
0041         template <typename Iterator>
0042         static bool
0043         parse_dot(Iterator& first, Iterator const& last)
0044         {
0045             if (first == last || *first != '.')
0046                 return false;
0047             ++first;
0048             return true;
0049         }
0050 
0051         template <typename Iterator, typename Attribute>
0052         static bool
0053         parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr_)
0054         {
0055             return extract_uint<T, 10, 1, -1, true>::call(first, last, attr_);
0056         }
0057 
0058         template <typename Iterator>
0059         static bool
0060         parse_exp(Iterator& first, Iterator const& last)
0061         {
0062             if (first == last || (*first != 'e' && *first != 'E'))
0063                 return false;
0064             ++first;
0065             return true;
0066         }
0067 
0068         template <typename Iterator>
0069         static bool
0070         parse_exp_n(Iterator& first, Iterator const& last, int& attr_)
0071         {
0072             return extract_int<int, 10, 1, -1>::call(first, last, attr_);
0073         }
0074 
0075         ///////////////////////////////////////////////////////////////////////
0076         //  The parse_nan() and parse_inf() functions get called whenever
0077         //  a number to parse does not start with a digit (after having
0078         //  successfully parsed an optional sign).
0079         //
0080         //  The functions should return true if a Nan or Inf has been found. In
0081         //  this case the attr should be set to the matched value (NaN or
0082         //  Inf). The optional sign will be automatically applied afterwards.
0083         //
0084         //  The default implementation below recognizes representations of NaN
0085         //  and Inf as mandated by the C99 Standard and as proposed for
0086         //  inclusion into the C++0x Standard: nan, nan(...), inf and infinity
0087         //  (the matching is performed case-insensitively).
0088         ///////////////////////////////////////////////////////////////////////
0089         template <typename Iterator, typename Attribute>
0090         static bool
0091         parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
0092         {
0093             if (first == last)
0094                 return false;   // end of input reached
0095 
0096             if (*first != 'n' && *first != 'N')
0097                 return false;   // not "nan"
0098 
0099             // nan[(...)] ?
0100             if (detail::string_parse("nan", "NAN", first, last, unused))
0101             {
0102                 if (first != last && *first == '(')
0103                 {
0104                     // skip trailing (...) part
0105                     Iterator i = first;
0106 
0107                     while (++i != last && *i != ')')
0108                         ;
0109                     if (i == last)
0110                         return false;     // no trailing ')' found, give up
0111 
0112                     first = ++i;
0113                 }
0114                 attr_ = std::numeric_limits<T>::quiet_NaN();
0115                 return true;
0116             }
0117             return false;
0118         }
0119 
0120         template <typename Iterator, typename Attribute>
0121         static bool
0122         parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
0123         {
0124             if (first == last)
0125                 return false;   // end of input reached
0126 
0127             if (*first != 'i' && *first != 'I')
0128                 return false;   // not "inf"
0129 
0130             // inf or infinity ?
0131             if (detail::string_parse("inf", "INF", first, last, unused))
0132             {
0133                 // skip allowed 'inity' part of infinity
0134                 detail::string_parse("inity", "INITY", first, last, unused);
0135                 attr_ = std::numeric_limits<T>::infinity();
0136                 return true;
0137             }
0138             return false;
0139         }
0140     };
0141 
0142     ///////////////////////////////////////////////////////////////////////////
0143     //  Default (signed) real number policies
0144     ///////////////////////////////////////////////////////////////////////////
0145     template <typename T>
0146     struct real_policies : ureal_policies<T>
0147     {
0148         template <typename Iterator>
0149         static bool
0150         parse_sign(Iterator& first, Iterator const& last)
0151         {
0152             return extract_sign(first, last);
0153         }
0154     };
0155 
0156     template <typename T>
0157     struct strict_ureal_policies : ureal_policies<T>
0158     {
0159         static bool const expect_dot = true;
0160     };
0161 
0162     template <typename T>
0163     struct strict_real_policies : real_policies<T>
0164     {
0165         static bool const expect_dot = true;
0166     };
0167 }}}
0168 
0169 #endif