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
29 #pragma warning(disable : 4251 4231 4660)
32 template <class BidiIterator, class Allocator>
36 #ifndef BOOST_NO_STD_ALLOCATOR
37 typedef std::vector<sub_match<BidiIterator>, Allocator> vector_type;
39 typedef std::vector<sub_match<BidiIterator> > vector_type;
42 typedef sub_match<BidiIterator> value_type;
43 #if !defined(BOOST_NO_STD_ALLOCATOR) && !(defined(BOOST_MSVC) && defined(_STLPORT_VERSION))
44 typedef typename Allocator::const_reference const_reference;
46 typedef const value_type& const_reference;
48 typedef const_reference reference;
49 typedef typename vector_type::const_iterator const_iterator;
50 typedef const_iterator iterator;
51 typedef typename re_detail::regex_iterator_traits<
52 BidiIterator>::difference_type difference_type;
53 typedef typename Allocator::size_type size_type;
54 typedef Allocator allocator_type;
55 typedef typename re_detail::regex_iterator_traits<
56 BidiIterator>::value_type char_type;
57 typedef std::basic_string<char_type> string_type;
59 // construct/copy/destroy:
60 explicit match_results(const Allocator& a = Allocator())
61 #ifndef BOOST_NO_STD_ALLOCATOR
62 : m_subs(a), m_base() {}
64 : m_subs(), m_base() { (void)a; }
66 match_results(const match_results& m)
67 : m_subs(m.m_subs), m_base(m.m_base) {}
68 match_results& operator=(const match_results& m)
77 size_type size() const
78 { return empty() ? 0 : m_subs.size() - 2; }
79 size_type max_size() const
80 { return m_subs.max_size(); }
82 { return m_subs.size() < 2; }
84 difference_type length(int sub = 0) const
87 if((sub < (int)m_subs.size()) && (sub > 0))
88 return m_subs[sub].length();
91 difference_type position(size_type sub = 0) const
94 if(sub < m_subs.size())
96 const sub_match<BidiIterator>& s = m_subs[sub];
97 if(s.matched || (sub == 2))
99 return ::boost::re_detail::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
102 return ~static_cast<difference_type>(0);
104 string_type str(int sub = 0) const
108 if(sub < (int)m_subs.size() && (sub > 0))
110 const sub_match<BidiIterator>& s = m_subs[sub];
118 const_reference operator[](int sub) const
121 if(sub < (int)m_subs.size() && (sub >= 0))
128 const_reference prefix() const
133 const_reference suffix() const
137 const_iterator begin() const
139 return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
141 const_iterator end() const
146 template <class OutputIterator>
147 OutputIterator format(OutputIterator out,
148 const string_type& fmt,
149 match_flag_type flags = format_default) const
151 re_detail::trivial_format_traits<char_type> traits;
152 return re_detail::regex_format_imp(out, *this, fmt.data(), fmt.data() + fmt.size(), flags, traits);
154 string_type format(const string_type& fmt,
155 match_flag_type flags = format_default) const
158 re_detail::string_out_iterator<string_type> i(result);
159 re_detail::trivial_format_traits<char_type> traits;
160 re_detail::regex_format_imp(i, *this, fmt.data(), fmt.data() + fmt.size(), flags, traits);
163 // format with locale:
164 template <class OutputIterator, class RegexT>
165 OutputIterator format(OutputIterator out,
166 const string_type& fmt,
167 match_flag_type flags,
168 const RegexT& re) const
170 return ::boost::re_detail::regex_format_imp(out, *this, fmt.data(), fmt.data() + fmt.size(), flags, re.get_traits());
172 template <class RegexT>
173 string_type format(const string_type& fmt,
174 match_flag_type flags,
175 const RegexT& re) const
178 re_detail::string_out_iterator<string_type> i(result);
179 ::boost::re_detail::regex_format_imp(i, *this, fmt.data(), fmt.data() + fmt.size(), flags, re.get_traits());
183 allocator_type get_allocator() const
185 #ifndef BOOST_NO_STD_ALLOCATOR
186 return m_subs.get_allocator();
188 return allocator_type();
191 void swap(match_results& that)
193 std::swap(m_subs, that.m_subs);
194 std::swap(m_base, that.m_base);
196 bool operator==(const match_results& that)const
198 return (m_subs == that.m_subs) && (m_base == that.m_base);
200 bool operator!=(const match_results& that)const
201 { return !(*this == that); }
203 #ifdef BOOST_REGEX_MATCH_EXTRA
204 typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
206 const capture_sequence_type& captures(int i)const
208 return (*this)[i].captures();
213 // private access functions:
214 void BOOST_REGEX_CALL set_second(BidiIterator i)
216 BOOST_ASSERT(m_subs.size() > 2);
217 m_subs[2].second = i;
218 m_subs[2].matched = true;
220 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
223 m_null.matched = false;
226 void BOOST_REGEX_CALL set_second(BidiIterator i, size_type pos, bool m = true)
229 BOOST_ASSERT(m_subs.size() > pos);
230 m_subs[pos].second = i;
231 m_subs[pos].matched = m;
235 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
238 m_null.matched = false;
241 void BOOST_REGEX_CALL set_size(size_type n, BidiIterator i, BidiIterator j)
244 size_type len = m_subs.size();
247 m_subs.erase(m_subs.begin()+n+2, m_subs.end());
248 std::fill(m_subs.begin(), m_subs.end(), v);
252 std::fill(m_subs.begin(), m_subs.end(), v);
254 m_subs.insert(m_subs.end(), n+2-len, v);
258 void BOOST_REGEX_CALL set_base(BidiIterator pos)
262 BidiIterator base()const
266 void BOOST_REGEX_CALL set_first(BidiIterator i)
269 m_subs[1].second = i;
270 m_subs[1].matched = (m_subs[1].first != i);
273 // zero out everything else:
274 for(size_type n = 3; n < m_subs.size(); ++n)
276 m_subs[n].first = m_subs[n].second = m_subs[0].second;
277 m_subs[n].matched = false;
280 void BOOST_REGEX_CALL set_first(BidiIterator i, size_type pos)
282 BOOST_ASSERT(pos+2 < m_subs.size());
284 m_subs[pos+2].first = i;
288 void BOOST_REGEX_CALL maybe_assign(const match_results<BidiIterator, Allocator>& m);
292 vector_type m_subs; // subexpressions
293 BidiIterator m_base; // where the search started from
294 sub_match<BidiIterator> m_null; // a null match
297 template <class BidiIterator, class Allocator>
298 void BOOST_REGEX_CALL match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
300 const_iterator p1, p2;
304 // Distances are measured from the start of *this* match, unless this isn't
305 // a valid match in which case we use the start of the whole sequence. Note that
306 // no subsequent match-candidate can ever be to the left of the first match found.
307 // This ensures that when we are using bidirectional iterators, that distances
308 // measured are as short as possible, and therefore as efficient as possible
309 // to compute. Finally note that we don't use the "matched" data member to test
310 // whether a sub-expression is a valid match, because partial matches set this
311 // to false for sub-expression 0.
313 BidiIterator l_end = this->suffix().second;
314 BidiIterator l_base = (p1->first == l_end) ? this->prefix().first : (*this)[0].first;
315 difference_type len1 = 0;
316 difference_type len2 = 0;
317 difference_type base1 = 0;
318 difference_type base2 = 0;
320 for(i = 0; i < size(); ++i, ++p1, ++p2)
323 // Leftmost takes priority over longest; handle special cases
324 // where distances need not be computed first (an optimisation
325 // for bidirectional iterators: ensure that we don't accidently
326 // compute the length of the whole sequence, as this can be really
329 if(p1->first == l_end)
331 if(p2->first != l_end)
333 // p2 must be better than p1, and no need to calculate
341 // *p1 and *p2 are either unmatched or match end-of sequence,
342 // either way no need to calculate distances:
343 if((p1->matched == false) && (p2->matched == true))
345 if((p1->matched == true) && (p2->matched == false))
350 else if(p2->first == l_end)
352 // p1 better than p2, and no need to calculate distances:
355 base1 = ::boost::re_detail::distance(l_base, p1->first);
356 base2 = ::boost::re_detail::distance(l_base, p2->first);
357 BOOST_ASSERT(base1 >= 0);
358 BOOST_ASSERT(base2 >= 0);
359 if(base1 < base2) return;
360 if(base2 < base1) break;
362 len1 = ::boost::re_detail::distance((BidiIterator)p1->first, (BidiIterator)p1->second);
363 len2 = ::boost::re_detail::distance((BidiIterator)p2->first, (BidiIterator)p2->second);
364 BOOST_ASSERT(len1 >= 0);
365 BOOST_ASSERT(len2 >= 0);
366 if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
368 if((p1->matched == true) && (p2->matched == false))
375 else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
379 template <class BidiIterator, class Allocator>
380 void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
385 #ifndef BOOST_NO_STD_LOCALE
386 template <class charT, class traits, class BidiIterator, class Allocator>
387 std::basic_ostream<charT, traits>&
388 operator << (std::basic_ostream<charT, traits>& os,
389 const match_results<BidiIterator, Allocator>& s)
391 return (os << s.str());
394 template <class BidiIterator, class Allocator>
395 std::ostream& operator << (std::ostream& os,
396 const match_results<BidiIterator, Allocator>& s)
398 return (os << s.str());
407 #ifdef BOOST_HAS_ABI_HEADERS
408 # include BOOST_ABI_SUFFIX