finder_regex.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Boost string_algo library find_regex.hpp header file ---------------------------//
  2. // Copyright Pavol Droba 2002-2003.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/ for updates, documentation, and revision history.
  8. #ifndef BOOST_STRING_FINDER_REGEX_DETAIL_HPP
  9. #define BOOST_STRING_FINDER_REGEX_DETAIL_HPP
  10. #include <boost/algorithm/string/config.hpp>
  11. #include <boost/regex.hpp>
  12. #include <boost/range/iterator_range_core.hpp>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. namespace boost {
  16. namespace algorithm {
  17. namespace detail {
  18. // regex find functor -----------------------------------------------//
  19. // regex search result
  20. template<typename IteratorT>
  21. struct regex_search_result :
  22. public iterator_range<IteratorT>
  23. {
  24. typedef regex_search_result<IteratorT> type;
  25. typedef iterator_range<IteratorT> base_type;
  26. typedef BOOST_STRING_TYPENAME base_type::value_type value_type;
  27. typedef BOOST_STRING_TYPENAME base_type::difference_type difference_type;
  28. typedef BOOST_STRING_TYPENAME base_type::const_iterator const_iterator;
  29. typedef BOOST_STRING_TYPENAME base_type::iterator iterator;
  30. typedef boost::match_results<iterator> match_results_type;
  31. // Construction
  32. // Construction from the match result
  33. regex_search_result( const match_results_type& MatchResults ) :
  34. base_type( MatchResults[0].first, MatchResults[0].second ),
  35. m_MatchResults( MatchResults ) {}
  36. // Construction of empty match. End iterator has to be specified
  37. regex_search_result( IteratorT End ) :
  38. base_type( End, End ) {}
  39. regex_search_result( const regex_search_result& Other ) :
  40. base_type( Other.begin(), Other.end() ),
  41. m_MatchResults( Other.m_MatchResults ) {}
  42. // Assignment
  43. regex_search_result& operator=( const regex_search_result& Other )
  44. {
  45. base_type::operator=( Other );
  46. m_MatchResults=Other.m_MatchResults;
  47. return *this;
  48. }
  49. // Match result retrieval
  50. const match_results_type& match_results() const
  51. {
  52. return m_MatchResults;
  53. }
  54. private:
  55. // Saved match result
  56. match_results_type m_MatchResults;
  57. };
  58. // find_regex
  59. /*
  60. Regex based search functor
  61. */
  62. template<typename RegExT>
  63. struct find_regexF
  64. {
  65. typedef RegExT regex_type;
  66. typedef const RegExT& regex_reference_type;
  67. // Construction
  68. find_regexF( regex_reference_type Rx, match_flag_type MatchFlags = match_default ) :
  69. m_Rx(Rx), m_MatchFlags(MatchFlags) {}
  70. // Operation
  71. template< typename ForwardIteratorT >
  72. regex_search_result<ForwardIteratorT>
  73. operator()(
  74. ForwardIteratorT Begin,
  75. ForwardIteratorT End ) const
  76. {
  77. typedef ForwardIteratorT input_iterator_type;
  78. typedef regex_search_result<ForwardIteratorT> result_type;
  79. // instantiate match result
  80. match_results<input_iterator_type> result;
  81. // search for a match
  82. if ( ::boost::regex_search( Begin, End, result, m_Rx, m_MatchFlags ) )
  83. {
  84. // construct a result
  85. return result_type( result );
  86. }
  87. else
  88. {
  89. // empty result
  90. return result_type( End );
  91. }
  92. }
  93. private:
  94. regex_reference_type m_Rx; // Regexp
  95. match_flag_type m_MatchFlags; // match flags
  96. };
  97. } // namespace detail
  98. } // namespace algorithm
  99. } // namespace boost
  100. #endif // BOOST_STRING_FIND_DETAIL_HPP