real_policies.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // a number to parse does not start with a digit (after having
  83. // successfully parsed an optional sign).
  84. //
  85. // The functions should return true if a Nan or Inf has been found. In
  86. // this case the attr should be set to the matched value (NaN or
  87. // Inf). The optional sign will be automatically applied afterwards.
  88. //
  89. // The default implementation below recognizes representations of NaN
  90. // and Inf as mandated by the C99 Standard and as proposed for
  91. // inclusion into the C++0x Standard: nan, nan(...), inf and infinity
  92. // (the matching is performed case-insensitively).
  93. ///////////////////////////////////////////////////////////////////////
  94. template <typename Iterator, typename Attribute>
  95. static bool
  96. parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
  97. {
  98. if (first == last)
  99. return false; // end of input reached
  100. if (*first != 'n' && *first != 'N')
  101. return false; // not "nan"
  102. // nan[(...)] ?
  103. if (detail::string_parse("nan", "NAN", first, last, unused))
  104. {
  105. if (first != last && *first == '(')
  106. {
  107. // skip trailing (...) part
  108. Iterator i = first;
  109. while (++i != last && *i != ')')
  110. ;
  111. if (i == last)
  112. return false; // no trailing ')' found, give up
  113. first = ++i;
  114. }
  115. attr_ = std::numeric_limits<T>::quiet_NaN();
  116. return true;
  117. }
  118. return false;
  119. }
  120. template <typename Iterator, typename Attribute>
  121. static bool
  122. parse_inf(Iterator& first, Iterator const& last, Attribute& attr_)
  123. {
  124. if (first == last)
  125. return false; // end of input reached
  126. if (*first != 'i' && *first != 'I')
  127. return false; // not "inf"
  128. // inf or infinity ?
  129. if (detail::string_parse("inf", "INF", first, last, unused))
  130. {
  131. // skip allowed 'inity' part of infinity
  132. detail::string_parse("inity", "INITY", first, last, unused);
  133. attr_ = std::numeric_limits<T>::infinity();
  134. return true;
  135. }
  136. return false;
  137. }
  138. };
  139. ///////////////////////////////////////////////////////////////////////////
  140. // Default (signed) real number policies
  141. ///////////////////////////////////////////////////////////////////////////
  142. template <typename T>
  143. struct real_policies : ureal_policies<T>
  144. {
  145. template <typename Iterator>
  146. static bool
  147. parse_sign(Iterator& first, Iterator const& last)
  148. {
  149. return extract_sign(first, last);
  150. }
  151. };
  152. template <typename T>
  153. struct strict_ureal_policies : ureal_policies<T>
  154. {
  155. static bool const expect_dot = true;
  156. };
  157. template <typename T>
  158. struct strict_real_policies : real_policies<T>
  159. {
  160. static bool const expect_dot = true;
  161. };
  162. }}}
  163. #endif