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
160 typedef u32regex_token_iterator_implementation<BidirectionalIterator> impl;
161 typedef shared_ptr<impl> pimpl;
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
167 typedef const value_type* pointer;
168 typedef const value_type& reference;
169 typedef std::forward_iterator_tag iterator_category;
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))
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))
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)
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))
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))
207 u32regex_token_iterator(const u32regex_token_iterator& that)
208 : pdata(that.pdata) {}
209 u32regex_token_iterator& operator=(const u32regex_token_iterator& that)
214 bool operator==(const u32regex_token_iterator& that)const
216 if((pdata.get() == 0) || (that.pdata.get() == 0))
217 return pdata.get() == that.pdata.get();
218 return pdata->compare(*(that.pdata.get()));
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++()
229 if(0 == pdata->next())
235 u32regex_token_iterator operator++(int)
237 u32regex_token_iterator result(*this);
248 if(pdata.get() && !pdata.unique())
250 pdata.reset(new impl(*(pdata.get())));
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;
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)
262 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
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)
267 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
270 #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
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)
273 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
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)
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);
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)
284 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
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)
291 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
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)
297 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
300 #if !defined(BOOST_REGEX_UCHAR_IS_WCHAR_T)
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)
304 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
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)
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);
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)
316 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
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)
322 return u32regex_token_iterator<const char*>(p, p+std::strlen(p), e, submatch, m);
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)
327 return u32regex_token_iterator<const wchar_t*>(p, p+std::wcslen(p), e, submatch, m);
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)
333 return u32regex_token_iterator<const UChar*>(p, p+u_strlen(p), e, submatch, m);
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)
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);
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)
344 return u32regex_token_iterator<const UChar*>(s.getBuffer(), s.getBuffer() + s.length(), e, submatch, m);
348 # pragma warning(pop)
350 #ifdef BOOST_HAS_ABI_HEADERS
351 # include BOOST_ABI_SUFFIX
356 #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP