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 regex_token_iterator.hpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Provides regex_token_iterator implementation.
19 #ifndef BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
20 #define BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP
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(__MWERKS__, BOOST_TESTED_AT(0x3003))
27 // Borland C++ Builder 6, and Visual C++ 6,
28 // can't cope with the array template constructor
29 // so we have a template member that will accept any type as
30 // argument, and then assert that is really is an array:
32 #include <boost/static_assert.hpp>
33 #include <boost/type_traits/is_array.hpp>
40 #pragma warning(disable: 4103)
42 #ifdef BOOST_HAS_ABI_HEADERS
43 # include BOOST_ABI_PREFIX
48 #pragma warning(disable:4700)
51 template <class BidirectionalIterator,
54 class regex_token_iterator_implementation
56 typedef basic_regex<charT, traits> regex_type;
57 typedef sub_match<BidirectionalIterator> value_type;
59 match_results<BidirectionalIterator> what; // current match
60 BidirectionalIterator base; // start of search area
61 BidirectionalIterator end; // end of search area
62 const regex_type re; // the expression
63 match_flag_type flags; // match flags
64 value_type result; // the current string result
65 int N; // the current sub-expression being enumerated
66 std::vector<int> subs; // the sub-expressions to enumerate
69 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
70 : end(last), re(*p), flags(f){ subs.push_back(sub); }
71 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
72 : end(last), re(*p), flags(f), subs(v){}
73 #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
74 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
75 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
76 || BOOST_WORKAROUND(__HP_aCC, < 60700)
78 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const T& submatches, match_flag_type f)
79 : end(last), re(*p), flags(f)
81 // assert that T really is an array:
82 BOOST_STATIC_ASSERT(::boost::is_array<T>::value);
83 const std::size_t array_size = sizeof(T) / sizeof(submatches[0]);
84 for(std::size_t i = 0; i < array_size; ++i)
86 subs.push_back(submatches[i]);
90 template <std::size_t CN>
91 regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
92 : end(last), re(*p), flags(f)
94 for(std::size_t i = 0; i < CN; ++i)
96 subs.push_back(submatches[i]);
101 bool init(BidirectionalIterator first)
105 if(regex_search(first, end, what, re, flags, base) == true)
108 result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
111 else if((subs[N] == -1) && (first != end))
113 result.first = first;
115 result.matched = (first != end);
121 bool compare(const regex_token_iterator_implementation& that)
123 if(this == &that) return true;
124 return (&re.get_data() == &that.re.get_data())
126 && (flags == that.flags)
128 && (what[0].first == that.what[0].first)
129 && (what[0].second == that.what[0].second);
131 const value_type& get()
137 if(N+1 < (int)subs.size())
140 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
143 //if(what.prefix().first != what[0].second)
144 // flags |= /*match_prev_avail |*/ regex_constants::match_not_bob;
145 BidirectionalIterator last_end(what[0].second);
146 if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
149 result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
152 else if((last_end != end) && (subs[0] == -1))
155 result.first = last_end;
157 result.matched = (last_end != end);
163 regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&);
166 template <class BidirectionalIterator,
167 class charT = BOOST_DEDUCED_TYPENAME BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::value_type,
168 class traits = regex_traits<charT> >
169 class regex_token_iterator
170 #ifndef BOOST_NO_STD_ITERATOR
171 : public std::iterator<
172 std::forward_iterator_tag,
173 sub_match<BidirectionalIterator>,
174 typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type,
175 const sub_match<BidirectionalIterator>*,
176 const sub_match<BidirectionalIterator>& >
180 typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl;
181 typedef shared_ptr<impl> pimpl;
183 typedef basic_regex<charT, traits> regex_type;
184 typedef sub_match<BidirectionalIterator> value_type;
185 typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type
187 typedef const value_type* pointer;
188 typedef const value_type& reference;
189 typedef std::forward_iterator_tag iterator_category;
191 regex_token_iterator(){}
192 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
193 int submatch = 0, match_flag_type m = match_default)
194 : pdata(new impl(&re, b, submatch, m))
199 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
200 const std::vector<int>& submatches, match_flag_type m = match_default)
201 : pdata(new impl(&re, b, submatches, m))
206 #if !BOOST_WORKAROUND(__HP_aCC, < 60700)
207 #if (BOOST_WORKAROUND(__BORLANDC__, >= 0x560) && BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570)))\
208 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) \
209 || BOOST_WORKAROUND(__HP_aCC, < 60700)
211 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
212 const T& submatches, match_flag_type m = match_default)
213 : pdata(new impl(&re, b, submatches, m))
219 template <std::size_t N>
220 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
221 const int (&submatches)[N], match_flag_type m = match_default)
222 : pdata(new impl(&re, b, submatches, m))
229 regex_token_iterator(const regex_token_iterator& that)
230 : pdata(that.pdata) {}
231 regex_token_iterator& operator=(const regex_token_iterator& that)
236 bool operator==(const regex_token_iterator& that)const
238 if((pdata.get() == 0) || (that.pdata.get() == 0))
239 return pdata.get() == that.pdata.get();
240 return pdata->compare(*(that.pdata.get()));
242 bool operator!=(const regex_token_iterator& that)const
243 { return !(*this == that); }
244 const value_type& operator*()const
245 { return pdata->get(); }
246 const value_type* operator->()const
247 { return &(pdata->get()); }
248 regex_token_iterator& operator++()
251 if(0 == pdata->next())
257 regex_token_iterator operator++(int)
259 regex_token_iterator result(*this);
270 if(pdata.get() && !pdata.unique())
272 pdata.reset(new impl(*(pdata.get())));
277 typedef regex_token_iterator<const char*> cregex_token_iterator;
278 typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator;
279 #ifndef BOOST_NO_WREGEX
280 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
281 typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator;
284 template <class charT, class traits>
285 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)
287 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
289 template <class charT, class traits, class ST, class SA>
290 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)
292 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
294 template <class charT, class traits, std::size_t N>
295 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)
297 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
299 template <class charT, class traits, class ST, class SA, std::size_t N>
300 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)
302 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
304 template <class charT, class traits>
305 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)
307 return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
309 template <class charT, class traits, class ST, class SA>
310 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)
312 return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
317 #pragma warning(push)
318 #pragma warning(disable: 4103)
320 #ifdef BOOST_HAS_ABI_HEADERS
321 # include BOOST_ABI_SUFFIX
329 #endif // BOOST_REGEX_V4_REGEX_TOKEN_ITERATOR_HPP