]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/v4/match_results.hpp
3139a857b220676daa33c9daddd157590be0f371
[lyx.git] / boost / boost / regex / v4 / match_results.hpp
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * Dr John Maddock
5  *
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)
9  *
10  */
11
12  /*
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.
17   */
18
19 #ifndef BOOST_REGEX_V4_MATCH_RESULTS_HPP
20 #define BOOST_REGEX_V4_MATCH_RESULTS_HPP
21
22 #ifdef BOOST_HAS_ABI_HEADERS
23 #  include BOOST_ABI_PREFIX
24 #endif
25
26 namespace boost{
27
28 template <class BidiIterator
29          , class Allocator = BOOST_DEFAULT_ALLOCATOR(sub_match<BidiIterator> )
30          >
31 class match_results
32
33 private:
34 #ifndef BOOST_NO_STD_ALLOCATOR
35    typedef          std::vector<sub_match<BidiIterator>, Allocator> vector_type;
36 #else
37    typedef          std::vector<sub_match<BidiIterator> >           vector_type;
38 #endif
39 public: 
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;
43 #else
44    typedef          const value_type&                                       const_reference;
45 #endif
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;
56
57    // construct/copy/destroy:
58    explicit match_results(const Allocator& a = Allocator())
59 #ifndef BOOST_NO_STD_ALLOCATOR
60       : m_subs(a), m_base() {}
61 #else
62       : m_subs(), m_base() { (void)a; }
63 #endif
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)
67    {
68       m_subs = m.m_subs;
69       m_base = m.m_base;
70       return *this;
71    }
72    ~match_results(){}
73
74    // size:
75    size_type size() const
76    { return m_subs.size() - 2; }
77    size_type max_size() const
78    { return m_subs.max_size(); }
79    bool empty() const
80    { return m_subs.size() < 2; }
81    // element access:
82    difference_type length(int sub = 0) const
83    {
84       sub += 2;
85       if((sub < (int)m_subs.size()) && (sub > 0))
86          return m_subs[sub].length();
87       return 0;
88    }
89    difference_type position(unsigned int sub = 0) const
90    {
91       sub += 2;
92       if(sub < m_subs.size())
93       {
94          const sub_match<BidiIterator>& s = m_subs[sub];
95          if(s.matched)
96          {
97             return boost::re_detail::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
98          }
99       }
100       return ~static_cast<difference_type>(0);
101    }
102    string_type str(int sub = 0) const
103    {
104       sub += 2;
105       string_type result;
106       if(sub < (int)m_subs.size() && (sub > 0))
107       {
108          const sub_match<BidiIterator>& s = m_subs[sub];
109          if(s.matched)
110          {
111             result = s;
112          }
113       }
114       return result;
115    }
116    const_reference operator[](int sub) const
117    {
118       sub += 2;
119       if(sub < (int)m_subs.size() && (sub >= 0))
120       {
121          return m_subs[sub];
122       }
123       return m_null;
124    }
125
126    const_reference prefix() const
127    {
128       return (*this)[-1];
129    }
130
131    const_reference suffix() const
132    {
133       return (*this)[-2];
134    }
135    const_iterator begin() const
136    {
137       return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
138    }
139    const_iterator end() const
140    {
141       return m_subs.end();
142    }
143    // format:
144    template <class OutputIterator>
145    OutputIterator format(OutputIterator out,
146                          const string_type& fmt,
147                          match_flag_type flags = format_default) const
148    {
149       return regex_format(out, *this, fmt, flags);
150    }
151    string_type format(const string_type& fmt,
152                       match_flag_type flags = format_default) const
153    {
154       return regex_format(*this, fmt, flags);
155    }
156
157    allocator_type get_allocator() const
158    {
159 #ifndef BOOST_NO_STD_ALLOCATOR
160       return m_subs.get_allocator();
161 #else
162      return allocator_type();
163 #endif
164    }
165    void swap(match_results& that)
166    {
167       std::swap(m_subs, that.m_subs);
168       std::swap(m_base, that.m_base);
169    }
170    bool operator==(const match_results& that)const
171    {
172       return (m_subs == that.m_subs) && (m_base == that.m_base);
173    }
174    bool operator!=(const match_results& that)const
175    { return !(*this == that); }
176
177 #ifdef BOOST_REGEX_MATCH_EXTRA
178    typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
179
180    const capture_sequence_type& captures(int i)const
181    {
182       return (*this)[i].captures();
183    }
184 #endif
185
186    //
187    // private access functions:
188    void BOOST_REGEX_CALL set_second(BidiIterator i)
189    {
190       assert(m_subs.size() > 2);
191       m_subs[2].second = i;
192       m_subs[2].matched = true;
193       m_subs[0].first = i;
194       m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
195       m_null.first = i;
196       m_null.second = i;
197       m_null.matched = false;
198    }
199
200    void BOOST_REGEX_CALL set_second(BidiIterator i, size_type pos, bool m = true)
201    {
202       pos += 2;
203       assert(m_subs.size() > pos);
204       m_subs[pos].second = i;
205       m_subs[pos].matched = m;
206       if(pos == 2)
207       {
208          m_subs[0].first = i;
209          m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
210          m_null.first = i;
211          m_null.second = i;
212          m_null.matched = false;
213       }
214    }
215    void BOOST_REGEX_CALL set_size(size_type n, BidiIterator i, BidiIterator j)
216    {
217       value_type v(j);
218       size_type len = m_subs.size();
219       if(len > n + 2)
220       {
221          m_subs.erase(m_subs.begin()+n+2);
222          std::fill(m_subs.begin(), m_subs.end(), v);
223       }
224       else
225       {
226          std::fill(m_subs.begin(), m_subs.end(), v);
227          if(n+2 != len)
228             m_subs.insert(m_subs.end(), n+2-len, v);
229       }
230       m_subs[1].first = i;
231    }
232    void BOOST_REGEX_CALL set_base(BidiIterator pos)
233    {
234       m_base = pos;
235    }
236    void BOOST_REGEX_CALL set_first(BidiIterator i)
237    {
238       // set up prefix:
239       m_subs[1].second = i;
240       m_subs[1].matched = (m_subs[1].first != i);
241       // set up $0:
242       m_subs[2].first = i;
243       // zero out everything else:
244       for(size_type n = 3; n < m_subs.size(); ++n)
245       {
246          m_subs[n].first = m_subs[n].second = m_subs[0].second;
247          m_subs[n].matched = false;
248       }
249    }
250    void BOOST_REGEX_CALL set_first(BidiIterator i, size_type pos)
251    {
252       assert(pos+2 < m_subs.size());
253       if(pos)
254          m_subs[pos+2].first = i;
255       else
256          set_first(i);
257    }
258    void BOOST_REGEX_CALL maybe_assign(const match_results<BidiIterator, Allocator>& m);
259
260
261 private:
262    vector_type            m_subs; // subexpressions
263    BidiIterator   m_base; // where the search started from
264    sub_match<BidiIterator> m_null; // a null match
265 };
266
267 template <class BidiIterator, class Allocator>
268 void BOOST_REGEX_CALL match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
269 {
270    const_iterator p1, p2;
271    p1 = begin();
272    p2 = m.begin();
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;
278    std::size_t i;
279    for(i = 0; i < size(); ++i)
280    {
281       //
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;
287
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)))
291          break;
292       if((p1->matched == true) && (p2->matched == false))
293          return;
294       ++p1;
295       ++p2;
296    }
297    if(i == size())
298       return;
299    if(base2 < base1)
300       *this = m;
301    else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
302       *this = m;
303 }
304
305 template <class BidiIterator, class Allocator>
306 void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
307 {
308    a.swap(b);
309 }
310
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)
316 {
317    return (os << s.str());
318 }
319 #else
320 template <class BidiIterator, class Allocator>
321 std::ostream& operator << (std::ostream& os,
322                            const match_results<BidiIterator, Allocator>& s)
323 {
324    return (os << s.str());
325 }
326 #endif
327
328 } // namespace boost
329
330 #ifdef BOOST_HAS_ABI_HEADERS
331 #  include BOOST_ABI_SUFFIX
332 #endif
333
334 #endif
335