range_matcher.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // range_matcher.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_RANGE_MATCHER_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_RANGE_MATCHER_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. # pragma warning(push)
  13. # pragma warning(disable : 4100) // unreferenced formal parameter
  14. #endif
  15. #include <boost/mpl/bool.hpp>
  16. #include <boost/xpressive/detail/detail_fwd.hpp>
  17. #include <boost/xpressive/detail/core/quant_style.hpp>
  18. #include <boost/xpressive/detail/core/state.hpp>
  19. namespace boost { namespace xpressive { namespace detail
  20. {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // range_matcher
  23. //
  24. template<typename Traits, typename ICase>
  25. struct range_matcher
  26. : quant_style_fixed_width<1>
  27. {
  28. typedef typename Traits::char_type char_type;
  29. typedef ICase icase_type;
  30. char_type ch_min_;
  31. char_type ch_max_;
  32. bool not_;
  33. range_matcher(char_type ch_min, char_type ch_max, bool no, Traits const &)
  34. : ch_min_(ch_min)
  35. , ch_max_(ch_max)
  36. , not_(no)
  37. {
  38. }
  39. void inverse()
  40. {
  41. this->not_ = !this->not_;
  42. }
  43. bool in_range(Traits const &tr, char_type ch, mpl::false_) const // case-sensitive
  44. {
  45. return tr.in_range(this->ch_min_, this->ch_max_, ch);
  46. }
  47. bool in_range(Traits const &tr, char_type ch, mpl::true_) const // case-insensitive
  48. {
  49. return tr.in_range_nocase(this->ch_min_, this->ch_max_, ch);
  50. }
  51. template<typename BidiIter, typename Next>
  52. bool match(match_state<BidiIter> &state, Next const &next) const
  53. {
  54. if(state.eos() || this->not_ ==
  55. this->in_range(traits_cast<Traits>(state), *state.cur_, icase_type()))
  56. {
  57. return false;
  58. }
  59. ++state.cur_;
  60. if(next.match(state))
  61. {
  62. return true;
  63. }
  64. --state.cur_;
  65. return false;
  66. }
  67. };
  68. }}}
  69. #if defined(_MSC_VER)
  70. # pragma warning(pop)
  71. #endif
  72. #endif