1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 1998-2002 |
---|
4 | * John Maddock |
---|
5 | * |
---|
6 | * Use, modification and distribution are subject to the |
---|
7 | * Boost Software License, Version 1.0. (See accompanying file |
---|
8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * LOCATION: see http://www.boost.org for most recent version. |
---|
14 | * FILE regex_grep.hpp |
---|
15 | * VERSION see <boost/version.hpp> |
---|
16 | * DESCRIPTION: Provides regex_grep implementation. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef BOOST_REGEX_V4_REGEX_GREP_HPP |
---|
20 | #define BOOST_REGEX_V4_REGEX_GREP_HPP |
---|
21 | |
---|
22 | |
---|
23 | namespace boost{ |
---|
24 | |
---|
25 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
26 | # include BOOST_ABI_PREFIX |
---|
27 | #endif |
---|
28 | |
---|
29 | // |
---|
30 | // regex_grep: |
---|
31 | // find all non-overlapping matches within the sequence first last: |
---|
32 | // |
---|
33 | template <class Predicate, class BidiIterator, class charT, class traits> |
---|
34 | inline unsigned int regex_grep(Predicate foo, |
---|
35 | BidiIterator first, |
---|
36 | BidiIterator last, |
---|
37 | const basic_regex<charT, traits>& e, |
---|
38 | match_flag_type flags = match_default) |
---|
39 | { |
---|
40 | if(e.flags() & regex_constants::failbit) |
---|
41 | return false; |
---|
42 | |
---|
43 | typedef typename match_results<BidiIterator>::allocator_type match_allocator_type; |
---|
44 | |
---|
45 | match_results<BidiIterator> m; |
---|
46 | re_detail::perl_matcher<BidiIterator, match_allocator_type, traits> matcher(first, last, m, e, flags, first); |
---|
47 | unsigned int count = 0; |
---|
48 | while(matcher.find()) |
---|
49 | { |
---|
50 | ++count; |
---|
51 | if(0 == foo(m)) |
---|
52 | return count; // caller doesn't want to go on |
---|
53 | if(m[0].second == last) |
---|
54 | return count; // we've reached the end, don't try and find an extra null match. |
---|
55 | if(m.length() == 0) |
---|
56 | { |
---|
57 | if(m[0].second == last) |
---|
58 | return count; |
---|
59 | // we found a NULL-match, now try to find |
---|
60 | // a non-NULL one at the same position: |
---|
61 | match_results<BidiIterator, match_allocator_type> m2(m); |
---|
62 | matcher.setf(match_not_null | match_continuous); |
---|
63 | if(matcher.find()) |
---|
64 | { |
---|
65 | ++count; |
---|
66 | if(0 == foo(m)) |
---|
67 | return count; |
---|
68 | } |
---|
69 | else |
---|
70 | { |
---|
71 | // reset match back to where it was: |
---|
72 | m = m2; |
---|
73 | } |
---|
74 | matcher.unsetf((match_not_null | match_continuous) & ~flags); |
---|
75 | } |
---|
76 | } |
---|
77 | return count; |
---|
78 | } |
---|
79 | |
---|
80 | // |
---|
81 | // regex_grep convenience interfaces: |
---|
82 | #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
---|
83 | // |
---|
84 | // this isn't really a partial specialisation, but template function |
---|
85 | // overloading - if the compiler doesn't support partial specialisation |
---|
86 | // then it really won't support this either: |
---|
87 | template <class Predicate, class charT, class traits> |
---|
88 | inline unsigned int regex_grep(Predicate foo, const charT* str, |
---|
89 | const basic_regex<charT, traits>& e, |
---|
90 | match_flag_type flags = match_default) |
---|
91 | { |
---|
92 | return regex_grep(foo, str, str + traits::length(str), e, flags); |
---|
93 | } |
---|
94 | |
---|
95 | template <class Predicate, class ST, class SA, class charT, class traits> |
---|
96 | inline unsigned int regex_grep(Predicate foo, const std::basic_string<charT, ST, SA>& s, |
---|
97 | const basic_regex<charT, traits>& e, |
---|
98 | match_flag_type flags = match_default) |
---|
99 | { |
---|
100 | return regex_grep(foo, s.begin(), s.end(), e, flags); |
---|
101 | } |
---|
102 | #else // partial specialisation |
---|
103 | inline unsigned int regex_grep(bool (*foo)(const cmatch&), const char* str, |
---|
104 | const regex& e, |
---|
105 | match_flag_type flags = match_default) |
---|
106 | { |
---|
107 | return regex_grep(foo, str, str + regex::traits_type::length(str), e, flags); |
---|
108 | } |
---|
109 | #ifndef BOOST_NO_WREGEX |
---|
110 | inline unsigned int regex_grep(bool (*foo)(const wcmatch&), const wchar_t* str, |
---|
111 | const wregex& e, |
---|
112 | match_flag_type flags = match_default) |
---|
113 | { |
---|
114 | return regex_grep(foo, str, str + wregex::traits_type::length(str), e, flags); |
---|
115 | } |
---|
116 | #endif |
---|
117 | inline unsigned int regex_grep(bool (*foo)(const match_results<std::string::const_iterator>&), const std::string& s, |
---|
118 | const regex& e, |
---|
119 | match_flag_type flags = match_default) |
---|
120 | { |
---|
121 | return regex_grep(foo, s.begin(), s.end(), e, flags); |
---|
122 | } |
---|
123 | #if !defined(BOOST_NO_WREGEX) |
---|
124 | inline unsigned int regex_grep(bool (*foo)(const match_results<std::basic_string<wchar_t>::const_iterator>&), |
---|
125 | const std::basic_string<wchar_t>& s, |
---|
126 | const wregex& e, |
---|
127 | match_flag_type flags = match_default) |
---|
128 | { |
---|
129 | return regex_grep(foo, s.begin(), s.end(), e, flags); |
---|
130 | } |
---|
131 | #endif |
---|
132 | #endif |
---|
133 | |
---|
134 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
135 | # include BOOST_ABI_SUFFIX |
---|
136 | #endif |
---|
137 | |
---|
138 | } // namespace boost |
---|
139 | |
---|
140 | #endif // BOOST_REGEX_V4_REGEX_GREP_HPP |
---|
141 | |
---|