finder.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /// Contains the definition of the basic_regex\<\> class template and its associated helper functions.
  2. //
  3. // Copyright 2008 Eric Niebler. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_XPRESSIVE_DETAIL_CORE_FINDER_HPP_EAN_10_04_2005
  7. #define BOOST_XPRESSIVE_DETAIL_CORE_FINDER_HPP_EAN_10_04_2005
  8. // MS compatible compilers support #pragma once
  9. #if defined(_MSC_VER)
  10. # pragma once
  11. # pragma warning(push)
  12. # pragma warning(disable : 4189) // local variable is initialized but not referenced
  13. #endif
  14. #include <boost/xpressive/detail/detail_fwd.hpp>
  15. #include <boost/xpressive/detail/core/regex_impl.hpp>
  16. #include <boost/xpressive/detail/utility/boyer_moore.hpp>
  17. #include <boost/xpressive/detail/utility/hash_peek_bitset.hpp>
  18. namespace boost { namespace xpressive { namespace detail
  19. {
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // boyer_moore_finder
  22. //
  23. template<typename BidiIter, typename Traits>
  24. struct boyer_moore_finder
  25. : finder<BidiIter>
  26. {
  27. typedef typename iterator_value<BidiIter>::type char_type;
  28. boyer_moore_finder(char_type const *begin, char_type const *end, Traits const &tr, bool icase)
  29. : bm_(begin, end, tr, icase)
  30. {
  31. }
  32. bool ok_for_partial_matches() const
  33. {
  34. return false;
  35. }
  36. bool operator ()(match_state<BidiIter> &state) const
  37. {
  38. Traits const &tr = traits_cast<Traits>(state);
  39. state.cur_ = this->bm_.find(state.cur_, state.end_, tr);
  40. return state.cur_ != state.end_;
  41. }
  42. private:
  43. boyer_moore_finder(boyer_moore_finder const &);
  44. boyer_moore_finder &operator =(boyer_moore_finder const &);
  45. boyer_moore<BidiIter, Traits> bm_;
  46. };
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // hash_peek_finder
  49. //
  50. template<typename BidiIter, typename Traits>
  51. struct hash_peek_finder
  52. : finder<BidiIter>
  53. {
  54. typedef typename iterator_value<BidiIter>::type char_type;
  55. hash_peek_finder(hash_peek_bitset<char_type> const &bset)
  56. : bset_(bset)
  57. {
  58. }
  59. bool operator ()(match_state<BidiIter> &state) const
  60. {
  61. Traits const &tr = traits_cast<Traits>(state);
  62. state.cur_ = (this->bset_.icase()
  63. ? this->find_(state.cur_, state.end_, tr, mpl::true_())
  64. : this->find_(state.cur_, state.end_, tr, mpl::false_()));
  65. return state.cur_ != state.end_;
  66. }
  67. private:
  68. hash_peek_finder(hash_peek_finder const &);
  69. hash_peek_finder &operator =(hash_peek_finder const &);
  70. template<typename ICase>
  71. BidiIter find_(BidiIter begin, BidiIter end, Traits const &tr, ICase) const
  72. {
  73. for(; begin != end && !this->bset_.test(*begin, tr, ICase()); ++begin)
  74. ;
  75. return begin;
  76. }
  77. hash_peek_bitset<char_type> bset_;
  78. };
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // line_start_finder
  81. //
  82. template<typename BidiIter, typename Traits, std::size_t Size = sizeof(typename iterator_value<BidiIter>::type)>
  83. struct line_start_finder
  84. : finder<BidiIter>
  85. {
  86. typedef typename iterator_value<BidiIter>::type char_type;
  87. typedef typename iterator_difference<BidiIter>::type diff_type;
  88. typedef typename Traits::char_class_type char_class_type;
  89. line_start_finder(Traits const &tr)
  90. : newline_(lookup_classname(tr, "newline"))
  91. {
  92. }
  93. bool operator ()(match_state<BidiIter> &state) const
  94. {
  95. if(state.bos() && state.flags_.match_bol_)
  96. {
  97. return true;
  98. }
  99. Traits const &tr = traits_cast<Traits>(state);
  100. BidiIter cur = state.cur_;
  101. BidiIter const end = state.end_;
  102. std::advance(cur, static_cast<diff_type>(-!state.bos()));
  103. for(; cur != end; ++cur)
  104. {
  105. if(tr.isctype(*cur, this->newline_))
  106. {
  107. state.cur_ = ++cur;
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. private:
  114. line_start_finder(line_start_finder const &);
  115. line_start_finder &operator =(line_start_finder const &);
  116. char_class_type newline_;
  117. };
  118. ///////////////////////////////////////////////////////////////////////////////
  119. // line_start_finder
  120. //
  121. template<typename BidiIter, typename Traits>
  122. struct line_start_finder<BidiIter, Traits, 1u>
  123. : finder<BidiIter>
  124. {
  125. typedef typename iterator_value<BidiIter>::type char_type;
  126. typedef typename iterator_difference<BidiIter>::type diff_type;
  127. typedef typename Traits::char_class_type char_class_type;
  128. line_start_finder(Traits const &tr)
  129. {
  130. char_class_type newline = lookup_classname(tr, "newline");
  131. for(int j = 0; j < 256; ++j)
  132. {
  133. this->bits_[j] = tr.isctype(static_cast<char_type>(static_cast<unsigned char>(j)), newline);
  134. }
  135. }
  136. bool operator ()(match_state<BidiIter> &state) const
  137. {
  138. if(state.bos() && state.flags_.match_bol_)
  139. {
  140. return true;
  141. }
  142. BidiIter cur = state.cur_;
  143. BidiIter const end = state.end_;
  144. std::advance(cur, static_cast<diff_type>(-!state.bos()));
  145. for(; cur != end; ++cur)
  146. {
  147. if(this->bits_[static_cast<unsigned char>(*cur)])
  148. {
  149. state.cur_ = ++cur;
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. private:
  156. line_start_finder(line_start_finder const &);
  157. line_start_finder &operator =(line_start_finder const &);
  158. bool bits_[256];
  159. };
  160. ///////////////////////////////////////////////////////////////////////////////
  161. // leading_simple_repeat_finder
  162. //
  163. template<typename BidiIter>
  164. struct leading_simple_repeat_finder
  165. : finder<BidiIter>
  166. {
  167. leading_simple_repeat_finder()
  168. : finder<BidiIter>()
  169. {}
  170. bool operator ()(match_state<BidiIter> &state) const
  171. {
  172. state.cur_ = state.next_search_;
  173. return true;
  174. }
  175. private:
  176. leading_simple_repeat_finder(leading_simple_repeat_finder const &);
  177. leading_simple_repeat_finder &operator =(leading_simple_repeat_finder const &);
  178. };
  179. }}}
  180. #if defined(_MSC_VER)
  181. # pragma warning(pop)
  182. #endif
  183. #endif