1 | /* |
2 | * |
3 | * Copyright (c) 2003 |
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_token_iterator.hpp |
15 | * VERSION see <boost/version.hpp> |
16 | * DESCRIPTION: Provides regex_token_iterator implementation. |
17 | */ |
18 | |
19 | #ifndef BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP |
20 | #define BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP |
21 | |
22 | #include <boost/shared_ptr.hpp> |
23 | #include <boost/detail/workaround.hpp> |
24 | #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ |
25 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) |
26 | // |
27 | // Borland C++ Builder 6, and Visual C++ 6, |
28 | // can't cope with the array template constructor |
29 | // so we have a template member that will accept any type as |
30 | // argument, and then assert that is really is an array: |
31 | // |
32 | #include <boost/static_assert.hpp> |
33 | #include <boost/type_traits/is_array.hpp> |
34 | #endif |
35 | |
36 | namespace boost{ |
37 | |
38 | #ifdef BOOST_MSVC |
39 | #pragma warning(push) |
40 | #pragma warning(disable: 4103) |
41 | #endif |
42 | #ifdef BOOST_HAS_ABI_HEADERS |
43 | # include BOOST_ABI_PREFIX |
44 | #endif |
45 | #ifdef BOOST_MSVC |
46 | #pragma warning(pop) |
47 | #pragma warning(push) |
48 | #pragma warning(disable:4700) |
49 | #endif |
50 | |
51 | template <class BidirectionalIterator, |
52 | class charT, |
53 | class traits> |
54 | class regex_token_iterator_implementation |
55 | { |
56 | typedef basic_regex<charT, traits> regex_type; |
57 | typedef sub_match<BidirectionalIterator> value_type; |
58 | |
59 | match_results<BidirectionalIterator> what; // current match |
60 | BidirectionalIterator base; // start of search area |
61 | BidirectionalIterator end; // end of search area |
62 | const regex_type re; // the expression |
63 | match_flag_type flags; // match flags |
64 | value_type result; // the current string result |
65 | int N; // the current sub-expression being enumerated |
66 | std::vector<int> subs; // the sub-expressions to enumerate |
67 | |
68 | public: |
69 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f) |
70 | : end(last), re(*p), flags(f){ subs.push_back(sub); } |
71 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f) |
72 | : end(last), re(*p), flags(f), subs(v){} |
73 | #if !BOOST_WORKAROUND(__HP_aCC, < 60700) |
74 | #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ |
75 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ |
76 | || BOOST_WORKAROUND(__HP_aCC, < 60700) |
77 | template <class T> |
78 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f) |
79 | : end(last), re(*p), flags(f) |
80 | { |
81 | // assert that T really is an array: |
82 | BOOST_STATIC_ASSERT(::boost::is_array<T>::value); |
83 | const std::size_t array_size = sizeof(T) / sizeof(submatches[0]); |
84 | for(std::size_t i = 0; i < array_size; ++i) |
85 | { |
86 | subs.push_back(submatches[i]); |
87 | } |
88 | } |
89 | #else |
90 | template <std::size_t CN> |
91 | regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f) |
92 | : end(last), re(*p), flags(f) |
93 | { |
94 | for(std::size_t i = 0; i < CN; ++i) |
95 | { |
96 | subs.push_back(submatches[i]); |
97 | } |
98 | } |
99 | #endif |
100 | #endif |
101 | bool init(BidirectionalIterator first) |
102 | { |
103 | N = 0; |
104 | base = first; |
105 | if(regex_search(first, end, what, re, flags, base) == true) |
106 | { |
107 | N = 0; |
108 | result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]); |
109 | return true; |
110 | } |
111 | else if((subs[N] == -1) && (first != end)) |
112 | { |
113 | result.first = first; |
114 | result.second = end; |
115 | result.matched = (first != end); |
116 | N = -1; |
117 | return true; |
118 | } |
119 | return false; |
120 | } |
121 | bool compare(const regex_token_iterator_implementation& that) |
122 | { |
123 | if(this == &that) return true; |
124 | return (&re.get_data() == &that.re.get_data()) |
125 | && (end == that.end) |
126 | && (flags == that.flags) |
127 | && (N == that.N) |
128 | && (what[0].first == that.what[0].first) |
129 | && (what[0].second == that.what[0].second); |
130 | } |
131 | const value_type& get() |
132 | { return result; } |
133 | bool next() |
134 | { |
135 | if(N == -1) |
136 | return false; |
137 | if(N+1 < (int)subs.size()) |
138 | { |
139 | ++N; |
140 | result =((subs[N] == -1) ? what.prefix() : what[subs[N]]); |
141 | return true; |
142 | } |
143 | //if(what.prefix().first != what[0].second) |
144 | // flags |= /*match_prev_avail |*/ regex_constants::match_not_bob; |
145 | BidirectionalIterator last_end(what[0].second); |
146 | if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base)) |
147 | { |
148 | N =0; |
149 | result =((subs[N] == -1) ? what.prefix() : what[subs[N]]); |
150 | return true; |
151 | } |
152 | else if((last_end != end) && (subs[0] == -1)) |
153 | { |
154 | N =-1; |
155 | result.first = last_end; |
156 | result.second = end; |
157 | result.matched = (last_end != end); |
158 | return true; |
159 | } |
160 | return false; |
161 | } |
162 | private: |
163 | regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&); |
164 | }; |
165 | |
166 | template <class BidirectionalIterator, |
167 | class charT = BOOST_DEDUCED_TYPENAME BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::value_type, |
168 | class traits = regex_traits<charT> > |
169 | class regex_token_iterator |
170 | { |
171 | private: |
172 | typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl; |
173 | typedef shared_ptr<impl> pimpl; |
174 | public: |
175 | typedef basic_regex<charT, traits> regex_type; |
176 | typedef sub_match<BidirectionalIterator> value_type; |
177 | typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type |
178 | difference_type; |
179 | typedef const value_type* pointer; |
180 | typedef const value_type& reference; |
181 | typedef std::forward_iterator_tag iterator_category; |
182 | |
183 | regex_token_iterator(){} |
184 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
185 | int submatch = 0, match_flag_type m = match_default) |
186 | : pdata(new impl(&re, b, submatch, m)) |
187 | { |
188 | if(!pdata->init(a)) |
189 | pdata.reset(); |
190 | } |
191 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
192 | const std::vector<int>& submatches, match_flag_type m = match_default) |
193 | : pdata(new impl(&re, b, submatches, m)) |
194 | { |
195 | if(!pdata->init(a)) |
196 | pdata.reset(); |
197 | } |
198 | #if !BOOST_WORKAROUND(__HP_aCC, < 60700) |
199 | #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ |
200 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ |
201 | || BOOST_WORKAROUND(__HP_aCC, < 60700) |
202 | template <class T> |
203 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
204 | const T& submatches, match_flag_type m = match_default) |
205 | : pdata(new impl(&re, b, submatches, m)) |
206 | { |
207 | if(!pdata->init(a)) |
208 | pdata.reset(); |
209 | } |
210 | #else |
211 | template <std::size_t N> |
212 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
213 | const int (&submatches)[N], match_flag_type m = match_default) |
214 | : pdata(new impl(&re, b, submatches, m)) |
215 | { |
216 | if(!pdata->init(a)) |
217 | pdata.reset(); |
218 | } |
219 | #endif |
220 | #endif |
221 | regex_token_iterator(const regex_token_iterator& that) |
222 | : pdata(that.pdata) {} |
223 | regex_token_iterator& operator=(const regex_token_iterator& that) |
224 | { |
225 | pdata = that.pdata; |
226 | return *this; |
227 | } |
228 | bool operator==(const regex_token_iterator& that)const |
229 | { |
230 | if((pdata.get() == 0) || (that.pdata.get() == 0)) |
231 | return pdata.get() == that.pdata.get(); |
232 | return pdata->compare(*(that.pdata.get())); |
233 | } |
234 | bool operator!=(const regex_token_iterator& that)const |
235 | { return !(*this == that); } |
236 | const value_type& operator*()const |
237 | { return pdata->get(); } |
238 | const value_type* operator->()const |
239 | { return &(pdata->get()); } |
240 | regex_token_iterator& operator++() |
241 | { |
242 | cow(); |
243 | if(0 == pdata->next()) |
244 | { |
245 | pdata.reset(); |
246 | } |
247 | return *this; |
248 | } |
249 | regex_token_iterator operator++(int) |
250 | { |
251 | regex_token_iterator result(*this); |
252 | ++(*this); |
253 | return result; |
254 | } |
255 | private: |
256 | |
257 | pimpl pdata; |
258 | |
259 | void cow() |
260 | { |
261 | // copy-on-write |
262 | if(pdata.get() && !pdata.unique()) |
263 | { |
264 | pdata.reset(new impl(*(pdata.get()))); |
265 | } |
266 | } |
267 | }; |
268 | |
269 | typedef regex_token_iterator<const char*> cregex_token_iterator; |
270 | typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator; |
271 | #ifndef BOOST_NO_WREGEX |
272 | typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator; |
273 | typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator; |
274 | #endif |
275 | |
276 | template <class charT, class traits> |
277 | inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default) |
278 | { |
279 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); |
280 | } |
281 | template <class charT, class traits, class ST, class SA> |
282 | inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default) |
283 | { |
284 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); |
285 | } |
286 | template <class charT, class traits, std::size_t N> |
287 | inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default) |
288 | { |
289 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); |
290 | } |
291 | template <class charT, class traits, class ST, class SA, std::size_t N> |
292 | inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default) |
293 | { |
294 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); |
295 | } |
296 | template <class charT, class traits> |
297 | inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default) |
298 | { |
299 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); |
300 | } |
301 | template <class charT, class traits, class ST, class SA> |
302 | inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default) |
303 | { |
304 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); |
305 | } |
306 | |
307 | #ifdef BOOST_MSVC |
308 | #pragma warning(pop) |
309 | #pragma warning(push) |
310 | #pragma warning(disable: 4103) |
311 | #endif |
312 | #ifdef BOOST_HAS_ABI_HEADERS |
313 | # include BOOST_ABI_SUFFIX |
314 | #endif |
315 | #ifdef BOOST_MSVC |
316 | #pragma warning(pop) |
317 | #endif |
318 | |
319 | } // namespace boost |
320 | |
321 | #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP |
322 | |
323 | |
324 | |
325 | |
326 | |