real_policies.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*=============================================================================
  2. Copyright (c) 2001-2019 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(SPIRIT_REAL_POLICIES_APRIL_17_2006_1158PM)
  8. #define SPIRIT_REAL_POLICIES_APRIL_17_2006_1158PM
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/qi/numeric/numeric_utils.hpp>
  13. #include <boost/spirit/home/qi/detail/string_parse.hpp>
  14. #include <boost/type_traits/is_floating_point.hpp>
  15. namespace boost { namespace spirit { namespace traits
  16. {
  17. // So that we won't exceed the capacity of the underlying type T,
  18. // we limit the number of digits parsed to its max_digits10.
  19. // By default, the value is -1 which tells spirit to parse an
  20. // unbounded number of digits.
  21. template <typename T, typename Enable = void>
  22. struct max_digits10
  23. {
  24. static int const value = -1; // unbounded
  25. };
  26. template <typename T>
  27. struct max_digits10<T
  28. , typename enable_if_c<(is_floating_point<T>::value)>::type>
  29. {
  30. static int const digits = std::numeric_limits<T>::digits;
  31. static int const value = 2 + (digits * 30103l) / 100000l;
  32. };
  33. }}}
  34. namespace boost { namespace spirit { namespace qi
  35. {
  36. ///////////////////////////////////////////////////////////////////////////
  37. // Default (unsigned) real number policies
  38. ///////////////////////////////////////////////////////////////////////////
  39. template <typename T>
  40. struct ureal_policies
  41. {
  42. // Versioning
  43. typedef mpl::int_<2> version;
  44. // trailing dot policy suggested by Gustavo Guerra
  45. static bool const allow_leading_dot = true;
  46. static bool const allow_trailing_dot = true;
  47. static bool const expect_dot = false;
  48. template <typename Iterator>
  49. static bool
  50. parse_sign(Iterator& /*first*/, Iterator const& /*last*/)
  51. {
  52. return false;
  53. }
  54. template <typename Iterator, typename Attribute>
  55. static bool
  56. parse_n(Iterator& first, Iterator const& last, Attribute& attr_)
  57. {
  58. typedef extract_uint<Attribute, 10, 1
  59. , traits::max_digits10<T>::value // See notes on max_digits10 above
  60. , false, true>
  61. extract_uint;
  62. return extract_uint::call(first, last, attr_);
  63. }
  64. // ignore_excess_digits (required for version > 1 API)
  65. template <typename Iterator>
  66. static std::size_t
  67. ignore_excess_digits(Iterator& first, Iterator const& last)
  68. {
  69. Iterator save = first;
  70. if (extract_uint<unused_type, 10, 1, -1>::call(first, last, unused))
  71. return std::distance(save, first);
  72. return 0;
  73. }
  74. template <typename Iterator>
  75. static bool
  76. parse_dot(Iterator& first, Iterator const& last)
  77. {
  78. if (first == last || *first != '.')
  79. return false;
  80. ++first;
  81. return true;
  82. }
  83. template <typename Iterator, typename Attribute>
  84. static bool
  85. parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr_, int& frac_digits)
  86. {
  87. Iterator savef = first;
  88. bool r = extract_uint<Attribute, 10, 1, -1, true, true>::call(first, last, attr_);
  89. if (r)
  90. {
  91. #if defined(_MSC_VER) && _MSC_VER < 1900
  92. # pragma warning(push)
  93. # pragma warning(disable: 4127) // conditional expression is constant
  94. #endif
  95. // Optimization note: don't compute frac_digits if T is
  96. // an unused_type. This should be optimized away by the compiler.
  97. if (!is_same<T, unused_type>::value)
  98. frac_digits =
  99. static_cast<int>(std::distance(savef, first));
  100. #if defined(_MSC_VER) && _MSC_VER < 1900
  101. # pragma warning(pop)
  102. #endif
  103. // ignore extra (non-significant digits)
  104. extract_uint<unused_type, 10, 1, -1>::call(first, last, unused);
  105. }
  106. return r;
  107. }
  108. template <typename Iterator>
  109. static bool
  110. parse_exp(Iterator& first, Iterator const& last)
  111. {
  112. if (first == last || (*first != 'e' && *first != 'E'))
  113. return false;
  114. ++first;
  115. return true;
  116. }
  117. template <typename Iterator>
  118. static bool
  119. parse_exp_n(Iterator& first, Iterator const& last, int& attr_)
  120. {
  121. return extract_int<int, 10, 1, -1>::call(first, last, attr_);
  122. }
  123. ///////////////////////////////////////////////////////////////////////
  124. // The parse_nan() and parse_inf() functions get called whenever
  125. // a number to parse does not start with a digit (after having
  126. // successfully parsed an optional sign).
  127. //
  128. // The functions should return true if a Nan or Inf has been found. In
  129. // this case the attr should be set to the matched value (NaN or
  130. // Inf). The optional sign will be automatically applied afterwards.
  131. //
  132. // The default implementation below recognizes representations of NaN
  133. // and Inf as mandated by the C99 Standard and as proposed for
  134. // inclusion into the C++0x Standard: nan, nan(...), inf and infinity
  135. // (the matching is performed case-insensitively).
  136. ///////////////////////////////////////////////////////////////////////
  137. template <typename Iterator, typename Attribute>
  138. static bool
  139. parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
  140. {
  141. if (first == last)
  142. return false; // end of input reached
  143. if (*first != 'n' && *first != 'N')
  144. return false; // not "nan"
  145. // nan[(...)] ?
  146. if (detail::string_parse("nan", "NAN", first, last, unused))
  147. {
  148. if (first != last && *first == '(')
  149. {
  150. // skip trailing (...) part
  151. Iterator i = first;
  152. while (++i != last && *i != ')')
  153. ;
  154. if (i == last)
  155. return false; // no trailing ')' found, give up
  156. first = ++i;
  157. }
  158. attr_ = std::numeric_limits<T>::quiet_NaN();
  159. return true;
  160. }
  161. return false;
  162. }
  163. template <typename Iterator, typename Attribute>
  164. static bool
  165. parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
  166. {
  167. if (first == last)
  168. return false; // end of input reached
  169. if (*first != 'i' && *first != 'I')
  170. return false; // not "inf"
  171. // inf or infinity ?
  172. if (detail::string_parse("inf", "INF", first, last, unused))
  173. {
  174. // skip allowed 'inity' part of infinity
  175. detail::string_parse("inity", "INITY", first, last, unused);
  176. attr_ = std::numeric_limits<T>::infinity();
  177. return true;
  178. }
  179. return false;
  180. }
  181. };
  182. ///////////////////////////////////////////////////////////////////////////
  183. // Default (signed) real number policies
  184. ///////////////////////////////////////////////////////////////////////////
  185. template <typename T>
  186. struct real_policies : ureal_policies<T>
  187. {
  188. template <typename Iterator>
  189. static bool
  190. parse_sign(Iterator& first, Iterator const& last)
  191. {
  192. return extract_sign(first, last);
  193. }
  194. };
  195. template <typename T>
  196. struct strict_ureal_policies : ureal_policies<T>
  197. {
  198. static bool const expect_dot = true;
  199. };
  200. template <typename T>
  201. struct strict_real_policies : real_policies<T>
  202. {
  203. static bool const expect_dot = true;
  204. };
  205. }}}
  206. #endif