3 * Copyright (c) 1998-2009
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
24 #pragma warning(disable: 4103)
26 #ifdef BOOST_HAS_ABI_HEADERS
27 # include BOOST_ABI_PREFIX
36 #pragma warning(disable : 4251 4231)
37 # if BOOST_MSVC < 1600
38 # pragma warning(disable : 4660)
42 namespace BOOST_REGEX_DETAIL_NS{
44 class named_subexpressions;
48 template <class BidiIterator, class Allocator>
52 #ifndef BOOST_NO_STD_ALLOCATOR
53 typedef std::vector<sub_match<BidiIterator>, Allocator> vector_type;
55 typedef std::vector<sub_match<BidiIterator> > vector_type;
58 typedef sub_match<BidiIterator> value_type;
59 #if !defined(BOOST_NO_STD_ALLOCATOR) && !(defined(BOOST_MSVC) && defined(_STLPORT_VERSION))
60 typedef typename Allocator::const_reference const_reference;
62 typedef const value_type& const_reference;
64 typedef const_reference reference;
65 typedef typename vector_type::const_iterator const_iterator;
66 typedef const_iterator iterator;
67 typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<
68 BidiIterator>::difference_type difference_type;
69 typedef typename Allocator::size_type size_type;
70 typedef Allocator allocator_type;
71 typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<
72 BidiIterator>::value_type char_type;
73 typedef std::basic_string<char_type> string_type;
74 typedef BOOST_REGEX_DETAIL_NS::named_subexpressions named_sub_type;
76 // construct/copy/destroy:
77 explicit match_results(const Allocator& a = Allocator())
78 #ifndef BOOST_NO_STD_ALLOCATOR
79 : m_subs(a), m_base(), m_null(), m_last_closed_paren(0), m_is_singular(true) {}
81 : m_subs(), m_base(), m_null(), m_last_closed_paren(0), m_is_singular(true) { (void)a; }
84 // IMPORTANT: in the code below, the crazy looking checks around m_is_singular are
85 // all required because it is illegal to copy a singular iterator.
86 // See https://svn.boost.org/trac/boost/ticket/3632.
88 match_results(const match_results& m)
89 : m_subs(m.m_subs), m_named_subs(m.m_named_subs), m_last_closed_paren(m.m_last_closed_paren), m_is_singular(m.m_is_singular)
97 match_results& operator=(const match_results& m)
100 m_named_subs = m.m_named_subs;
101 m_last_closed_paren = m.m_last_closed_paren;
102 m_is_singular = m.m_is_singular;
113 size_type size() const
114 { return empty() ? 0 : m_subs.size() - 2; }
115 size_type max_size() const
116 { return m_subs.max_size(); }
118 { return m_subs.size() < 2; }
120 difference_type length(int sub = 0) const
125 if((sub < (int)m_subs.size()) && (sub > 0))
126 return m_subs[sub].length();
129 difference_type length(const char_type* sub) const
133 const char_type* sub_end = sub;
134 while(*sub_end) ++sub_end;
135 return length(named_subexpression_index(sub, sub_end));
137 template <class charT>
138 difference_type length(const charT* sub) const
142 const charT* sub_end = sub;
143 while(*sub_end) ++sub_end;
144 return length(named_subexpression_index(sub, sub_end));
146 template <class charT, class Traits, class A>
147 difference_type length(const std::basic_string<charT, Traits, A>& sub) const
149 return length(sub.c_str());
151 difference_type position(size_type sub = 0) const
156 if(sub < m_subs.size())
158 const sub_match<BidiIterator>& s = m_subs[sub];
159 if(s.matched || (sub == 2))
161 return ::boost::BOOST_REGEX_DETAIL_NS::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
164 return ~static_cast<difference_type>(0);
166 difference_type position(const char_type* sub) const
168 const char_type* sub_end = sub;
169 while(*sub_end) ++sub_end;
170 return position(named_subexpression_index(sub, sub_end));
172 template <class charT>
173 difference_type position(const charT* sub) const
175 const charT* sub_end = sub;
176 while(*sub_end) ++sub_end;
177 return position(named_subexpression_index(sub, sub_end));
179 template <class charT, class Traits, class A>
180 difference_type position(const std::basic_string<charT, Traits, A>& sub) const
182 return position(sub.c_str());
184 string_type str(int sub = 0) const
190 if(sub < (int)m_subs.size() && (sub > 0))
192 const sub_match<BidiIterator>& s = m_subs[sub];
200 string_type str(const char_type* sub) const
202 return (*this)[sub].str();
204 template <class Traits, class A>
205 string_type str(const std::basic_string<char_type, Traits, A>& sub) const
207 return (*this)[sub].str();
209 template <class charT>
210 string_type str(const charT* sub) const
212 return (*this)[sub].str();
214 template <class charT, class Traits, class A>
215 string_type str(const std::basic_string<charT, Traits, A>& sub) const
217 return (*this)[sub].str();
219 const_reference operator[](int sub) const
221 if(m_is_singular && m_subs.empty())
224 if(sub < (int)m_subs.size() && (sub >= 0))
231 // Named sub-expressions:
233 const_reference named_subexpression(const char_type* i, const char_type* j) const
236 // Scan for the leftmost *matched* subexpression with the specified named:
240 BOOST_REGEX_DETAIL_NS::named_subexpressions::range_type r = m_named_subs->equal_range(i, j);
241 while((r.first != r.second) && ((*this)[r.first->index].matched == false))
243 return r.first != r.second ? (*this)[r.first->index] : m_null;
245 template <class charT>
246 const_reference named_subexpression(const charT* i, const charT* j) const
248 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
251 std::vector<char_type> s;
253 s.insert(s.end(), *i++);
254 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
256 int named_subexpression_index(const char_type* i, const char_type* j) const
259 // Scan for the leftmost *matched* subexpression with the specified named.
260 // If none found then return the leftmost expression with that name,
261 // otherwise an invalid index:
265 BOOST_REGEX_DETAIL_NS::named_subexpressions::range_type s, r;
266 s = r = m_named_subs->equal_range(i, j);
267 while((r.first != r.second) && ((*this)[r.first->index].matched == false))
269 if(r.first == r.second)
271 return r.first != r.second ? r.first->index : -20;
273 template <class charT>
274 int named_subexpression_index(const charT* i, const charT* j) const
276 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
279 std::vector<char_type> s;
281 s.insert(s.end(), *i++);
282 return named_subexpression_index(&*s.begin(), &*s.begin() + s.size());
284 template <class Traits, class A>
285 const_reference operator[](const std::basic_string<char_type, Traits, A>& s) const
287 return named_subexpression(s.c_str(), s.c_str() + s.size());
289 const_reference operator[](const char_type* p) const
291 const char_type* e = p;
293 return named_subexpression(p, e);
296 template <class charT>
297 const_reference operator[](const charT* p) const
299 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
302 std::vector<char_type> s;
304 s.insert(s.end(), *p++);
305 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
307 template <class charT, class Traits, class A>
308 const_reference operator[](const std::basic_string<charT, Traits, A>& ns) const
310 BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
313 std::vector<char_type> s;
314 for(unsigned i = 0; i < ns.size(); ++i)
315 s.insert(s.end(), ns[i]);
316 return named_subexpression(&*s.begin(), &*s.begin() + s.size());
319 const_reference prefix() const
326 const_reference suffix() const
332 const_iterator begin() const
334 return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
336 const_iterator end() const
341 template <class OutputIterator, class Functor>
342 OutputIterator format(OutputIterator out,
344 match_flag_type flags = format_default) const
348 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator>::type F;
350 return func(*this, out, flags);
352 template <class Functor>
353 string_type format(Functor fmt, match_flag_type flags = format_default) const
357 std::basic_string<char_type> result;
358 BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > i(result);
360 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > >::type F;
363 func(*this, i, flags);
366 // format with locale:
367 template <class OutputIterator, class Functor, class RegexT>
368 OutputIterator format(OutputIterator out,
370 match_flag_type flags,
371 const RegexT& re) const
375 typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
376 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator, traits_type>::type F;
378 return func(*this, out, flags, re.get_traits());
380 template <class RegexT, class Functor>
381 string_type format(Functor fmt,
382 match_flag_type flags,
383 const RegexT& re) const
387 typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
388 std::basic_string<char_type> result;
389 BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> > i(result);
391 typedef typename BOOST_REGEX_DETAIL_NS::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<char_type> >, traits_type >::type F;
394 func(*this, i, flags, re.get_traits());
398 const_reference get_last_closed_paren()const
402 return m_last_closed_paren == 0 ? m_null : (*this)[m_last_closed_paren];
405 allocator_type get_allocator() const
407 #ifndef BOOST_NO_STD_ALLOCATOR
408 return m_subs.get_allocator();
410 return allocator_type();
413 void swap(match_results& that)
415 std::swap(m_subs, that.m_subs);
416 std::swap(m_named_subs, that.m_named_subs);
417 std::swap(m_last_closed_paren, that.m_last_closed_paren);
420 if(!that.m_is_singular)
422 m_base = that.m_base;
423 m_null = that.m_null;
426 else if(that.m_is_singular)
428 that.m_base = m_base;
429 that.m_null = m_null;
433 std::swap(m_base, that.m_base);
434 std::swap(m_null, that.m_null);
436 std::swap(m_is_singular, that.m_is_singular);
438 bool operator==(const match_results& that)const
442 return that.m_is_singular;
444 else if(that.m_is_singular)
448 return (m_subs == that.m_subs) && (m_base == that.m_base) && (m_last_closed_paren == that.m_last_closed_paren);
450 bool operator!=(const match_results& that)const
451 { return !(*this == that); }
453 #ifdef BOOST_REGEX_MATCH_EXTRA
454 typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
456 const capture_sequence_type& captures(int i)const
460 return (*this)[i].captures();
465 // private access functions:
466 void BOOST_REGEX_CALL set_second(BidiIterator i)
468 BOOST_ASSERT(m_subs.size() > 2);
469 m_subs[2].second = i;
470 m_subs[2].matched = true;
472 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
475 m_null.matched = false;
476 m_is_singular = false;
479 void BOOST_REGEX_CALL set_second(BidiIterator i, size_type pos, bool m = true, bool escape_k = false)
482 m_last_closed_paren = static_cast<int>(pos);
484 BOOST_ASSERT(m_subs.size() > pos);
485 m_subs[pos].second = i;
486 m_subs[pos].matched = m;
487 if((pos == 2) && !escape_k)
490 m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
493 m_null.matched = false;
494 m_is_singular = false;
497 void BOOST_REGEX_CALL set_size(size_type n, BidiIterator i, BidiIterator j)
500 size_type len = m_subs.size();
503 m_subs.erase(m_subs.begin()+n+2, m_subs.end());
504 std::fill(m_subs.begin(), m_subs.end(), v);
508 std::fill(m_subs.begin(), m_subs.end(), v);
510 m_subs.insert(m_subs.end(), n+2-len, v);
513 m_last_closed_paren = 0;
515 void BOOST_REGEX_CALL set_base(BidiIterator pos)
519 BidiIterator base()const
523 void BOOST_REGEX_CALL set_first(BidiIterator i)
525 BOOST_ASSERT(m_subs.size() > 2);
527 m_subs[1].second = i;
528 m_subs[1].matched = (m_subs[1].first != i);
531 // zero out everything else:
532 for(size_type n = 3; n < m_subs.size(); ++n)
534 m_subs[n].first = m_subs[n].second = m_subs[0].second;
535 m_subs[n].matched = false;
538 void BOOST_REGEX_CALL set_first(BidiIterator i, size_type pos, bool escape_k = false)
540 BOOST_ASSERT(pos+2 < m_subs.size());
543 m_subs[pos+2].first = i;
546 m_subs[1].second = i;
547 m_subs[1].matched = (m_subs[1].first != m_subs[1].second);
553 void BOOST_REGEX_CALL maybe_assign(const match_results<BidiIterator, Allocator>& m);
555 void BOOST_REGEX_CALL set_named_subs(boost::shared_ptr<named_sub_type> subs)
562 // Error handler called when an uninitialized match_results is accessed:
564 static void raise_logic_error()
566 std::logic_error e("Attempt to access an uninitialzed boost::match_results<> class.");
567 boost::throw_exception(e);
571 vector_type m_subs; // subexpressions
572 BidiIterator m_base; // where the search started from
573 sub_match<BidiIterator> m_null; // a null match
574 boost::shared_ptr<named_sub_type> m_named_subs; // Shared copy of named subs in the regex object
575 int m_last_closed_paren; // Last ) to be seen - used for formatting
576 bool m_is_singular; // True if our stored iterators are singular
579 template <class BidiIterator, class Allocator>
580 void BOOST_REGEX_CALL match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
587 const_iterator p1, p2;
591 // Distances are measured from the start of *this* match, unless this isn't
592 // a valid match in which case we use the start of the whole sequence. Note that
593 // no subsequent match-candidate can ever be to the left of the first match found.
594 // This ensures that when we are using bidirectional iterators, that distances
595 // measured are as short as possible, and therefore as efficient as possible
596 // to compute. Finally note that we don't use the "matched" data member to test
597 // whether a sub-expression is a valid match, because partial matches set this
598 // to false for sub-expression 0.
600 BidiIterator l_end = this->suffix().second;
601 BidiIterator l_base = (p1->first == l_end) ? this->prefix().first : (*this)[0].first;
602 difference_type len1 = 0;
603 difference_type len2 = 0;
604 difference_type base1 = 0;
605 difference_type base2 = 0;
607 for(i = 0; i < size(); ++i, ++p1, ++p2)
610 // Leftmost takes priority over longest; handle special cases
611 // where distances need not be computed first (an optimisation
612 // for bidirectional iterators: ensure that we don't accidently
613 // compute the length of the whole sequence, as this can be really
616 if(p1->first == l_end)
618 if(p2->first != l_end)
620 // p2 must be better than p1, and no need to calculate
628 // *p1 and *p2 are either unmatched or match end-of sequence,
629 // either way no need to calculate distances:
630 if((p1->matched == false) && (p2->matched == true))
632 if((p1->matched == true) && (p2->matched == false))
637 else if(p2->first == l_end)
639 // p1 better than p2, and no need to calculate distances:
642 base1 = ::boost::BOOST_REGEX_DETAIL_NS::distance(l_base, p1->first);
643 base2 = ::boost::BOOST_REGEX_DETAIL_NS::distance(l_base, p2->first);
644 BOOST_ASSERT(base1 >= 0);
645 BOOST_ASSERT(base2 >= 0);
646 if(base1 < base2) return;
647 if(base2 < base1) break;
649 len1 = ::boost::BOOST_REGEX_DETAIL_NS::distance((BidiIterator)p1->first, (BidiIterator)p1->second);
650 len2 = ::boost::BOOST_REGEX_DETAIL_NS::distance((BidiIterator)p2->first, (BidiIterator)p2->second);
651 BOOST_ASSERT(len1 >= 0);
652 BOOST_ASSERT(len2 >= 0);
653 if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
655 if((p1->matched == true) && (p2->matched == false))
662 else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
666 template <class BidiIterator, class Allocator>
667 void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
672 #ifndef BOOST_NO_STD_LOCALE
673 template <class charT, class traits, class BidiIterator, class Allocator>
674 std::basic_ostream<charT, traits>&
675 operator << (std::basic_ostream<charT, traits>& os,
676 const match_results<BidiIterator, Allocator>& s)
678 return (os << s.str());
681 template <class BidiIterator, class Allocator>
682 std::ostream& operator << (std::ostream& os,
683 const match_results<BidiIterator, Allocator>& s)
685 return (os << s.str());
695 #pragma warning(push)
696 #pragma warning(disable: 4103)
698 #ifdef BOOST_HAS_ABI_HEADERS
699 # include BOOST_ABI_SUFFIX