source: NonGTP/Boost/boost/algorithm/string/detail/finder_regex.hpp @ 857

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