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