3 * Copyright (c) 1998-2002
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 match_results.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares template class match_results.
19 #ifndef BOOST_REGEX_V4_MATCH_RESULTS_HPP
20 #define BOOST_REGEX_V4_MATCH_RESULTS_HPP
22 #ifdef BOOST_HAS_ABI_HEADERS
23 # include BOOST_ABI_PREFIX
28 template <class BidiIterator
29 , class Allocator = BOOST_DEFAULT_ALLOCATOR(sub_match<BidiIterator> )
34 #ifndef BOOST_NO_STD_ALLOCATOR
35 typedef std::vector<sub_match<BidiIterator>, Allocator> vector_type;
37 typedef std::vector<sub_match<BidiIterator> > vector_type;
40 typedef sub_match<BidiIterator> value_type;
41 #if !defined(BOOST_NO_STD_ALLOCATOR) && !(defined(BOOST_MSVC) && defined(_STLPORT_VERSION))
42 typedef typename Allocator::const_reference const_reference;
44 typedef const value_type& const_reference;
46 typedef const_reference reference;
47 typedef typename vector_type::const_iterator const_iterator;
48 typedef const_iterator iterator;
49 typedef typename re_detail::regex_iterator_traits<
50 BidiIterator>::difference_type difference_type;
51 typedef typename Allocator::size_type size_type;
52 typedef Allocator allocator_type;
53 typedef typename re_detail::regex_iterator_traits<
54 BidiIterator>::value_type char_type;
55 typedef std::basic_string<char_type> string_type;
57 // construct/copy/destroy:
58 explicit match_results(const Allocator& a = Allocator())
59 #ifndef BOOST_NO_STD_ALLOCATOR
60 : m_subs(a), m_base() {}
62 : m_subs(), m_base() { (void)a; }
64 match_results(const match_results& m)
65 : m_subs(m.m_subs), m_base(m.m_base) {}
66 match_results& operator=(const match_results& m)
75 size_type size() const
76 { return m_subs.size() - 2; }
77 size_type max_size() const
78 { return m_subs.max_size(); }
80 { return m_subs.size() < 2; }
82 difference_type length(int sub = 0) const
85 if((sub < (int)m_subs.size()) && (sub > 0))
86 return m_subs[sub].length();
89 difference_type position(unsigned int sub = 0) const
92 if(sub < m_subs.size())
94 const sub_match<BidiIterator>& s = m_subs[sub];
97 return boost::re_detail::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
100 return ~static_cast<difference_type>(0);
102 string_type str(int sub = 0) const
106 if(sub < (int)m_subs.size() && (sub > 0))
108 const sub_match<BidiIterator>& s = m_subs[sub];
116 const_reference operator[](int sub) const
119 if(sub < (int)m_subs.size() && (sub >= 0))
126 const_reference prefix() const
131 const_reference suffix() const
135 const_iterator begin() const
137 return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
139 const_iterator end() const
144 template <class OutputIterator>
145 OutputIterator format(OutputIterator out,
146 const string_type& fmt,
147 match_flag_type flags = format_default) const
149 return regex_format(out, *this, fmt, flags);
151 string_type format(const string_type& fmt,
152 match_flag_type flags = format_default) const
154 return regex_format(*this, fmt, flags);
157 allocator_type get_allocator() const
159 #ifndef BOOST_NO_STD_ALLOCATOR
160 return m_subs.get_allocator();
162 return allocator_type();
165 void swap(match_results& that)
167 std::swap(m_subs, that.m_subs);
168 std::swap(m_base, that.m_base);
170 bool operator==(const match_results& that)const
172 return (m_subs == that.m_subs) && (m_base == that.m_base);
174 bool operator!=(const match_results& that)const
175 { return !(*this == that); }
177 #ifdef BOOST_REGEX_MATCH_EXTRA
178 typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
180 const capture_sequence_type& captures(int i)const
182 return (*this)[i].captures();
187 // private access functions:
188 void BOOST_REGEX_CALL set_second(BidiIterator i)
190 assert(m_subs.size() > 2);
191 m_subs[2].second = i;
192 m_subs[2].matched = true;
194 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
197 m_null.matched = false;
200 void BOOST_REGEX_CALL set_second(BidiIterator i, size_type pos, bool m = true)
203 assert(m_subs.size() > pos);
204 m_subs[pos].second = i;
205 m_subs[pos].matched = m;
209 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
212 m_null.matched = false;
215 void BOOST_REGEX_CALL set_size(size_type n, BidiIterator i, BidiIterator j)
218 size_type len = m_subs.size();
221 m_subs.erase(m_subs.begin()+n+2);
222 std::fill(m_subs.begin(), m_subs.end(), v);
226 std::fill(m_subs.begin(), m_subs.end(), v);
228 m_subs.insert(m_subs.end(), n+2-len, v);
232 void BOOST_REGEX_CALL set_base(BidiIterator pos)
236 void BOOST_REGEX_CALL set_first(BidiIterator i)
239 m_subs[1].second = i;
240 m_subs[1].matched = (m_subs[1].first != i);
243 // zero out everything else:
244 for(size_type n = 3; n < m_subs.size(); ++n)
246 m_subs[n].first = m_subs[n].second = m_subs[0].second;
247 m_subs[n].matched = false;
250 void BOOST_REGEX_CALL set_first(BidiIterator i, size_type pos)
252 assert(pos+2 < m_subs.size());
254 m_subs[pos+2].first = i;
258 void BOOST_REGEX_CALL maybe_assign(const match_results<BidiIterator, Allocator>& m);
262 vector_type m_subs; // subexpressions
263 BidiIterator m_base; // where the search started from
264 sub_match<BidiIterator> m_null; // a null match
267 template <class BidiIterator, class Allocator>
268 void BOOST_REGEX_CALL match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
270 const_iterator p1, p2;
273 BidiIterator base = (*this)[-1].first;
274 std::size_t len1 = 0;
275 std::size_t len2 = 0;
276 std::size_t base1 = 0;
277 std::size_t base2 = 0;
279 for(i = 0; i < size(); ++i)
282 // leftmost takes priority over longest:
283 base1 = boost::re_detail::distance(base, p1->first);
284 base2 = boost::re_detail::distance(base, p2->first);
285 if(base1 < base2) return;
286 if(base2 < base1) break;
288 len1 = boost::re_detail::distance((BidiIterator)p1->first, (BidiIterator)p1->second);
289 len2 = boost::re_detail::distance((BidiIterator)p2->first, (BidiIterator)p2->second);
290 if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
292 if((p1->matched == true) && (p2->matched == false))
301 else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
305 template <class BidiIterator, class Allocator>
306 void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
311 #ifndef BOOST_NO_STD_LOCALE
312 template <class charT, class traits, class BidiIterator, class Allocator>
313 std::basic_ostream<charT, traits>&
314 operator << (std::basic_ostream<charT, traits>& os,
315 const match_results<BidiIterator, Allocator>& s)
317 return (os << s.str());
320 template <class BidiIterator, class Allocator>
321 std::ostream& operator << (std::ostream& os,
322 const match_results<BidiIterator, Allocator>& s)
324 return (os << s.str());
330 #ifdef BOOST_HAS_ABI_HEADERS
331 # include BOOST_ABI_SUFFIX