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)
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.
19 #ifndef BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP
20 #define BOOST_REGEX_V4_U32REGEX_TOKEN_ITERATOR_HPP
22 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
23 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
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:
30 #include <boost/static_assert.hpp>
31 #include <boost/type_traits/is_array.hpp>
36 #ifdef BOOST_HAS_ABI_HEADERS
37 # include BOOST_ABI_PREFIX
40 # pragma warning(push)
41 # pragma warning(disable:4700)
44 template <class BidirectionalIterator>
45 class u32regex_token_iterator_implementation
47 typedef u32regex regex_type;
48 typedef sub_match<BidirectionalIterator> value_type;
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
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)
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)
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)
76 subs.push_back(submatches[i]);
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)
84 for(std::size_t i = 0; i < CN; ++i)
86 subs.push_back(submatches[i]);
91 bool init(BidirectionalIterator first)
95 if(u32regex_search(first, end, what, re, flags, base) == true)
98 result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
101 else if((subs[N] == -1) && (first != end))
103 result.first = first;
105 result.matched = (first != end);
111 bool compare(const u32regex_token_iterator_implementation& that)
113 if(this == &that) return true;
114 return (&re.get_data() == &that.re.get_data())
116 && (flags == that.flags)
118 && (what[0].first == that.what[0].first)
119 && (what[0].second == that.what[0].second);
121 const value_type& get()
127 if(N+1 < (int)subs.size())
130 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
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))
139 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
142 else if((last_end != end) && (subs[0] == -1))
145 result.first = last_end;
147 result.matched = (last_end != end);
153 u32regex_token_iterator_implementation& operator=(const u32regex_token_iterator_implementation&);
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>& >
168 typedef u32regex_token_iterator_implementation<BidirectionalIterator> impl;
169 typedef shared_ptr<impl> pimpl;
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
175 typedef const value_type* pointer;
176 typedef const value_type& reference;
177 typedef std::forward_iterator_tag iterator_category;
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))
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))
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)
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))
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))
215 u32regex_token_iterator(const u32regex_token_iterator& that)
216 : pdata(that.pdata) {}
217 u32regex_token_iterator& operator=(const u32regex_token_iterator& that)
222 bool operator==(const u32regex_token_iterator& that)const
224 if((pdata.get() == 0) || (that.pdata.get() == 0))
225 return pdata.get() == that.pdata.get();
226 return pdata->compare(*(that.pdata.get()));
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++()
237 if(0 == pdata->next())
243 u32regex_token_iterator operator++(int)
245 u32regex_token_iterator result(*this);
256 if(pdata.get() && !pdata.unique())
258 pdata.reset(new impl(*(pdata.get())));
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;
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)
270 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
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)
275 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
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)
281 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
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)
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);
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)
292 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
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)
299 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
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)
305 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
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)
312 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
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)
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);
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)
324 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
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)
330 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
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)
335 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
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)
341 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
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)
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);
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)
352 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
356 # pragma warning(pop)
358 #ifdef BOOST_HAS_ABI_HEADERS
359 # include BOOST_ABI_SUFFIX
364 #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP