real_policies.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 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. namespace boost { namespace spirit { namespace qi
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. // Default (unsigned) real number policies
  18. ///////////////////////////////////////////////////////////////////////////
  19. template <typename T>
  20. struct ureal_policies
  21. {
  22. // trailing dot policy suggested by Gustavo Guerra
  23. static bool const allow_leading_dot = true;
  24. static bool const allow_trailing_dot = true;
  25. static bool const expect_dot = false;
  26. template <typename Iterator>
  27. static bool
  28. parse_sign(Iterator& /*first*/, Iterator const& /*last*/)
  29. {
  30. return false;
  31. }
  32. template <typename Iterator, typename Attribute>
  33. static bool
  34. parse_n(Iterator& first, Iterator const& last, Attribute& attr_)
  35. {
  36. return extract_uint<Attribute, 10, 1, -1>::call(first, last, attr_);
  37. }
  38. template <typename Iterator>
  39. static bool
  40. parse_dot(Iterator& first, Iterator const& last)
  41. {
  42. if (first == last || *first != '.')
  43. return false;
  44. ++first;
  45. return true;
  46. }
  47. template <typename Iterator, typename Attribute>
  48. static bool
  49. parse_frac_n(Iterator& first, Iterator const& last, Attribute& attr_, int& frac_digits)
  50. {
  51. Iterator savef = first;
  52. bool r = extract_uint<Attribute, 10, 1, -1, true, true>::call(first, last, attr_);
  53. if (r)
  54. {
  55. // Optimization note: don't compute frac_digits if T is
  56. // an unused_type. This should be optimized away by the compiler.
  57. if (!is_same<T, unused_type>::value)
  58. frac_digits =
  59. static_cast<int>(std::distance(savef, first));
  60. // ignore extra (non-significant digits)
  61. extract_uint<unused_type, 10, 1, -1>::call(first, last, unused);
  62. }
  63. return r;
  64. }
  65. template <typename Iterator>
  66. static bool
  67. parse_exp(Iterator& first, Iterator const& last)
  68. {
  69. if (first == last || (*first != 'e' && *first != 'E'))
  70. return false;
  71. ++first;
  72. return true;
  73. }
  74. template <typename Iterator>
  75. static bool
  76. parse_exp_n(Iterator& first, Iterator const& last, int& attr_)
  77. {
  78. return extract_int<int, 10, 1, -1>::call(first, last, attr_);
  79. }
  80. ///////////////////////////////////////////////////////////////////////
  81. // The parse_nan() and parse_inf() functions get called whenever:
  82. //
  83. // - a number to parse does not start with a digit (after having
  84. // successfully parsed an optional sign)
  85. //
  86. // or
  87. //
  88. // - after a floating point number of the value 1 (having no
  89. // exponential part and a fractional part value of 0) has been
  90. // parsed.
  91. //
  92. // The first call allows to recognize representations of NaN or Inf
  93. // starting with a non-digit character (such as NaN, Inf, QNaN etc.).
  94. //
  95. // The second call allows to recognize representation formats starting
  96. // with a 1.0 (such as 1.0#NAN or 1.0#INF etc.).
  97. //
  98. // The functions should return true if a Nan or Inf has been found. In
  99. // this case the attr should be set to the matched value (NaN or
  100. // Inf). The optional sign will be automatically applied afterwards.
  101. //
  102. // The default implementation below recognizes representations of NaN
  103. // and Inf as mandated by the C99 Standard and as proposed for
  104. // inclusion into the C++0x Standard: nan, nan(...), inf and infinity
  105. // (the matching is performed case-insensitively).
  106. ///////////////////////////////////////////////////////////////////////
  107. template <typename Iterator, typename Attribute>
  108. static bool
  109. parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
  110. {
  111. if (first == last)
  112. return false; // end of input reached
  113. if (*first != 'n' && *first != 'N')
  114. return false; // not "nan"
  115. // nan[(...)] ?
  116. if (detail::string_parse("nan", "NAN", first, last, unused))
  117. {
  118. if (first != last && *first == '(')
  119. {
  120. // skip trailing (...) part
  121. Iterator i = first;
  122. while (++i != last && *i != ')')
  123. ;
  124. if (i == last)
  125. return false; // no trailing ')' found, give up
  126. first = ++i;
  127. }
  128. attr_ = std::numeric_limits<T>::quiet_NaN();
  129. return true;
  130. }
  131. return false;
  132. }
  133. template <typename Iterator, typename Attribute>
  134. static bool
  135. parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
  136. {
  137. if (first == last)
  138. return false; // end of input reached
  139. if (*first != 'i' && *first != 'I')
  140. return false; // not "inf"
  141. // inf or infinity ?
  142. if (detail::string_parse("inf", "INF", first, last, unused))
  143. {
  144. // skip allowed 'inity' part of infinity
  145. detail::string_parse("inity", "INITY", first, last, unused);
  146. attr_ = std::numeric_limits<T>::infinity();
  147. return true;
  148. }
  149. return false;
  150. }
  151. };
  152. ///////////////////////////////////////////////////////////////////////////
  153. // Default (signed) real number policies
  154. ///////////////////////////////////////////////////////////////////////////
  155. template <typename T>
  156. struct real_policies : ureal_policies<T>
  157. {
  158. template <typename Iterator>
  159. static bool
  160. parse_sign(Iterator& first, Iterator const& last)
  161. {
  162. return extract_sign(first, last);
  163. }
  164. };
  165. template <typename T>
  166. struct strict_ureal_policies : ureal_policies<T>
  167. {
  168. static bool const expect_dot = true;
  169. };
  170. template <typename T>
  171. struct strict_real_policies : real_policies<T>
  172. {
  173. static bool const expect_dot = true;
  174. };
  175. }}}
  176. #endif