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