]>
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 | |
7c673cae FG |
158 | { |
159 | private: | |
160 | typedef u32regex_token_iterator_implementation<BidirectionalIterator> impl; | |
161 | typedef shared_ptr<impl> pimpl; | |
162 | public: | |
163 | typedef u32regex regex_type; | |
164 | typedef sub_match<BidirectionalIterator> value_type; | |
165 | typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type | |
166 | difference_type; | |
167 | typedef const value_type* pointer; | |
168 | typedef const value_type& reference; | |
169 | typedef std::forward_iterator_tag iterator_category; | |
170 | ||
171 | u32regex_token_iterator(){} | |
172 | u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
173 | int submatch = 0, match_flag_type m = match_default) | |
174 | : pdata(new impl(&re, b, submatch, m)) | |
175 | { | |
176 | if(!pdata->init(a)) | |
177 | pdata.reset(); | |
178 | } | |
179 | u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
180 | const std::vector<int>& submatches, match_flag_type m = match_default) | |
181 | : pdata(new impl(&re, b, submatches, m)) | |
182 | { | |
183 | if(!pdata->init(a)) | |
184 | pdata.reset(); | |
185 | } | |
186 | #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\ | |
187 | || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \ | |
188 | || BOOST_WORKAROUND(__HP_aCC, < 60700) | |
189 | template <class T> | |
190 | u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
191 | const T& submatches, match_flag_type m = match_default) | |
192 | : pdata(new impl(&re, b, submatches, m)) | |
193 | { | |
194 | if(!pdata->init(a)) | |
195 | pdata.reset(); | |
196 | } | |
197 | #else | |
198 | template <std::size_t N> | |
199 | u32regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re, | |
200 | const int (&submatches)[N], 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 | #endif | |
207 | u32regex_token_iterator(const u32regex_token_iterator& that) | |
208 | : pdata(that.pdata) {} | |
209 | u32regex_token_iterator& operator=(const u32regex_token_iterator& that) | |
210 | { | |
211 | pdata = that.pdata; | |
212 | return *this; | |
213 | } | |
214 | bool operator==(const u32regex_token_iterator& that)const | |
215 | { | |
216 | if((pdata.get() == 0) || (that.pdata.get() == 0)) | |
217 | return pdata.get() == that.pdata.get(); | |
218 | return pdata->compare(*(that.pdata.get())); | |
219 | } | |
220 | bool operator!=(const u32regex_token_iterator& that)const | |
221 | { return !(*this == that); } | |
222 | const value_type& operator*()const | |
223 | { return pdata->get(); } | |
224 | const value_type* operator->()const | |
225 | { return &(pdata->get()); } | |
226 | u32regex_token_iterator& operator++() | |
227 | { | |
228 | cow(); | |
229 | if(0 == pdata->next()) | |
230 | { | |
231 | pdata.reset(); | |
232 | } | |
233 | return *this; | |
234 | } | |
235 | u32regex_token_iterator operator++(int) | |
236 | { | |
237 | u32regex_token_iterator result(*this); | |
238 | ++(*this); | |
239 | return result; | |
240 | } | |
241 | private: | |
242 | ||
243 | pimpl pdata; | |
244 | ||
245 | void cow() | |
246 | { | |
247 | // copy-on-write | |
248 | if(pdata.get() && !pdata.unique()) | |
249 | { | |
250 | pdata.reset(new impl(*(pdata.get()))); | |
251 | } | |
252 | } | |
253 | }; | |
254 | ||
255 | typedef u32regex_token_iterator<const char*> utf8regex_token_iterator; | |
256 | typedef u32regex_token_iterator<const UChar*> utf16regex_token_iterator; | |
257 | typedef u32regex_token_iterator<const UChar32*> utf32regex_token_iterator; | |
258 | ||
259 | // construction from an integral sub_match state_id: | |
260 | 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) | |
261 | { | |
262 | return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m); | |
263 | } | |
264 | #ifndef BOOST_NO_WREGEX | |
265 | 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) | |
266 | { | |
267 | return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m); | |
268 | } | |
269 | #endif | |
92f5a8d4 | 270 | #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T) |
7c673cae FG |
271 | 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) |
272 | { | |
273 | return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m); | |
274 | } | |
275 | #endif | |
276 | template <class charT, class Traits, class Alloc> | |
277 | 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) | |
278 | { | |
279 | typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type; | |
280 | return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m); | |
281 | } | |
282 | 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) | |
283 | { | |
284 | return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m); | |
285 | } | |
286 | ||
287 | // construction from a reference to an array: | |
288 | template <std::size_t N> | |
289 | 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) | |
290 | { | |
291 | return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m); | |
292 | } | |
293 | #ifndef BOOST_NO_WREGEX | |
294 | template <std::size_t N> | |
295 | 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) | |
296 | { | |
297 | return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m); | |
298 | } | |
299 | #endif | |
92f5a8d4 | 300 | #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T) |
7c673cae FG |
301 | template <std::size_t N> |
302 | 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) | |
303 | { | |
304 | return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m); | |
305 | } | |
306 | #endif | |
307 | template <class charT, class Traits, class Alloc, std::size_t N> | |
308 | 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) | |
309 | { | |
310 | typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type; | |
311 | return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m); | |
312 | } | |
313 | template <std::size_t N> | |
314 | 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) | |
315 | { | |
316 | return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m); | |
317 | } | |
318 | ||
319 | // construction from a vector of sub_match state_id's: | |
320 | 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) | |
321 | { | |
322 | return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m); | |
323 | } | |
324 | #ifndef BOOST_NO_WREGEX | |
325 | 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) | |
326 | { | |
327 | return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m); | |
328 | } | |
329 | #endif | |
330 | #if !defined(U_WCHAR_IS_UTF16) && (U_SIZEOF_WCHAR_T != 2) | |
331 | 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) | |
332 | { | |
333 | return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m); | |
334 | } | |
335 | #endif | |
336 | template <class charT, class Traits, class Alloc> | |
337 | 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) | |
338 | { | |
339 | typedef typename std::basic_string<charT, Traits, Alloc>::const_iterator iter_type; | |
340 | return u32regex_token_iterator<iter_type>(p.begin(), p.end(), e, submatch, m); | |
341 | } | |
342 | 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) | |
343 | { | |
344 | return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m); | |
345 | } | |
346 | ||
347 | #ifdef BOOST_MSVC | |
348 | # pragma warning(pop) | |
349 | #endif | |
350 | #ifdef BOOST_HAS_ABI_HEADERS | |
351 | # include BOOST_ABI_SUFFIX | |
352 | #endif | |
353 | ||
354 | } // namespace boost | |
355 | ||
356 | #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP | |
357 | ||
358 | ||
359 | ||
360 |