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 | #ifndef BOOST_NO_STD_ITERATOR |
171 | : public std::iterator< |
172 | std::forward_iterator_tag, |
173 | sub_match<BidirectionalIterator>, |
174 | typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type, |
175 | const sub_match<BidirectionalIterator>*, |
176 | const sub_match<BidirectionalIterator>& > |
177 | #endif |
178 | { |
179 | private: |
180 | typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl; |
181 | typedef shared_ptr<impl> pimpl; |
182 | public: |
183 | typedef basic_regex<charT, traits> regex_type; |
184 | typedef sub_match<BidirectionalIterator> value_type; |
185 | typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type |
186 | difference_type; |
187 | typedef const value_type* pointer; |
188 | typedef const value_type& reference; |
189 | typedef std::forward_iterator_tag iterator_category; |
190 | |
191 | regex_token_iterator(){} |
192 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
193 | int submatch = 0, match_flag_type m = match_default) |
194 | : pdata(new impl(&re, b, submatch, m)) |
195 | { |
196 | if(!pdata->init(a)) |
197 | pdata.reset(); |
198 | } |
199 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
200 | const std::vector<int>& submatches, match_flag_type m = match_default) |
201 | : pdata(new impl(&re, b, submatches, m)) |
202 | { |
203 | if(!pdata->init(a)) |
204 | pdata.reset(); |
205 | } |
206 | #if !BOOST_WORKAROUND(__HP_aCC, < 60700) |
207 | #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ |
208 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ |
209 | || BOOST_WORKAROUND(__HP_aCC, < 60700) |
210 | template <class T> |
211 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
212 | const T& submatches, match_flag_type m = match_default) |
213 | : pdata(new impl(&re, b, submatches, m)) |
214 | { |
215 | if(!pdata->init(a)) |
216 | pdata.reset(); |
217 | } |
218 | #else |
219 | template <std::size_t N> |
220 | regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, |
221 | const int (&submatches)[N], match_flag_type m = match_default) |
222 | : pdata(new impl(&re, b, submatches, m)) |
223 | { |
224 | if(!pdata->init(a)) |
225 | pdata.reset(); |
226 | } |
227 | #endif |
228 | #endif |
229 | regex_token_iterator(const regex_token_iterator& that) |
230 | : pdata(that.pdata) {} |
231 | regex_token_iterator& operator=(const regex_token_iterator& that) |
232 | { |
233 | pdata = that.pdata; |
234 | return *this; |
235 | } |
236 | bool operator==(const regex_token_iterator& that)const |
237 | { |
238 | if((pdata.get() == 0) || (that.pdata.get() == 0)) |
239 | return pdata.get() == that.pdata.get(); |
240 | return pdata->compare(*(that.pdata.get())); |
241 | } |
242 | bool operator!=(const regex_token_iterator& that)const |
243 | { return !(*this == that); } |
244 | const value_type& operator*()const |
245 | { return pdata->get(); } |
246 | const value_type* operator->()const |
247 | { return &(pdata->get()); } |
248 | regex_token_iterator& operator++() |
249 | { |
250 | cow(); |
251 | if(0 == pdata->next()) |
252 | { |
253 | pdata.reset(); |
254 | } |
255 | return *this; |
256 | } |
257 | regex_token_iterator operator++(int) |
258 | { |
259 | regex_token_iterator result(*this); |
260 | ++(*this); |
261 | return result; |
262 | } |
263 | private: |
264 | |
265 | pimpl pdata; |
266 | |
267 | void cow() |
268 | { |
269 | // copy-on-write |
270 | if(pdata.get() && !pdata.unique()) |
271 | { |
272 | pdata.reset(new impl(*(pdata.get()))); |
273 | } |
274 | } |
275 | }; |
276 | |
277 | typedef regex_token_iterator<const char*> cregex_token_iterator; |
278 | typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator; |
279 | #ifndef BOOST_NO_WREGEX |
280 | typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator; |
281 | typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator; |
282 | #endif |
283 | |
284 | template <class charT, class traits> |
285 | 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) |
286 | { |
287 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); |
288 | } |
289 | template <class charT, class traits, class ST, class SA> |
290 | 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) |
291 | { |
292 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); |
293 | } |
294 | template <class charT, class traits, std::size_t N> |
295 | 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) |
296 | { |
297 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); |
298 | } |
299 | template <class charT, class traits, class ST, class SA, std::size_t N> |
300 | 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) |
301 | { |
302 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); |
303 | } |
304 | template <class charT, class traits> |
305 | 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) |
306 | { |
307 | return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m); |
308 | } |
309 | template <class charT, class traits, class ST, class SA> |
310 | 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) |
311 | { |
312 | return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m); |
313 | } |
314 | |
315 | #ifdef BOOST_MSVC |
316 | #pragma warning(pop) |
317 | #pragma warning(push) |
318 | #pragma warning(disable: 4103) |
319 | #endif |
320 | #ifdef BOOST_HAS_ABI_HEADERS |
321 | # include BOOST_ABI_SUFFIX |
322 | #endif |
323 | #ifdef BOOST_MSVC |
324 | #pragma warning(pop) |
325 | #endif |
326 | |
327 | } // namespace boost |
328 | |
329 | #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP |
330 | |
331 | |
332 | |
333 | |
334 | |