]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/v4/regex_iterator.hpp
update to boost 1.32.0
[lyx.git] / boost / boost / regex / v4 / regex_iterator.hpp
1 /*
2  *
3  * Copyright (c) 2003
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         regex_iterator.hpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Provides regex_iterator implementation.
17   */
18
19 #ifndef BOOST_REGEX_V4_REGEX_ITERATOR_HPP
20 #define BOOST_REGEX_V4_REGEX_ITERATOR_HPP
21
22 #include <boost/shared_ptr.hpp>
23
24 namespace boost{
25
26 #ifdef BOOST_HAS_ABI_HEADERS
27 #  include BOOST_ABI_PREFIX
28 #endif
29
30 template <class BidirectionalIterator, 
31           class charT,
32           class traits,
33           class Allocator>
34 class regex_iterator_implementation 
35 {
36    typedef basic_regex<charT, traits, Allocator> regex_type;
37
38    match_results<BidirectionalIterator> what;  // current match
39    BidirectionalIterator                base;  // start of sequence
40    BidirectionalIterator                end;   // end of sequence
41    const regex_type*                    pre;   // the expression
42    match_flag_type                      flags; // flags for matching
43
44 public:
45    regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f)
46       : base(), end(last), pre(p), flags(f){}
47    bool init(BidirectionalIterator first)
48    {
49       base = first;
50       return regex_search(first, end, what, *pre, flags);
51    }
52    bool compare(const regex_iterator_implementation& that)
53    {
54       if(this == &that) return true;
55       return (pre == that.pre) && (end == that.end) && (flags == that.flags) && (what[0].first == that.what[0].first) && (what[0].second == that.what[0].second);
56    }
57    const match_results<BidirectionalIterator>& get()
58    { return what; }
59    bool next()
60    {
61       if(what.prefix().first != what[0].second)
62          flags |= match_prev_avail;
63       BidirectionalIterator next_start = what[0].second;
64       match_flag_type f(flags);
65       if(!what.length())
66          f |= regex_constants::match_not_initial_null;
67       bool result = regex_search(next_start, end, what, *pre, f);
68       if(result)
69          what.set_base(base);
70       return result;
71    }
72 };
73
74 template <class BidirectionalIterator, 
75           class charT = BOOST_DEDUCED_TYPENAME re_detail::regex_iterator_traits<BidirectionalIterator>::value_type,
76           class traits = regex_traits<charT>,
77           class Allocator = BOOST_DEFAULT_ALLOCATOR(charT) >
78 class regex_iterator 
79 #ifndef BOOST_NO_STD_ITERATOR
80    : public std::iterator<
81          std::forward_iterator_tag, 
82          match_results<BidirectionalIterator>,
83          typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type,
84          const match_results<BidirectionalIterator>*,
85          const match_results<BidirectionalIterator>& >         
86 #endif
87 {
88 private:
89    typedef regex_iterator_implementation<BidirectionalIterator, charT, traits, Allocator> impl;
90    typedef shared_ptr<impl> pimpl;
91 public:
92    typedef          basic_regex<charT, traits, Allocator>                   regex_type;
93    typedef          match_results<BidirectionalIterator>                    value_type;
94    typedef typename re_detail::regex_iterator_traits<BidirectionalIterator>::difference_type 
95                                                                             difference_type;
96    typedef          const value_type*                                       pointer;
97    typedef          const value_type&                                       reference; 
98    typedef          std::forward_iterator_tag                               iterator_category;
99    
100    regex_iterator(){}
101    regex_iterator(BidirectionalIterator a, BidirectionalIterator b, 
102                   const regex_type& re, 
103                   match_flag_type m = match_default)
104                   : pdata(new impl(&re, b, m))
105    {
106       if(!pdata->init(a))
107       {
108          pdata.reset();
109       }
110    }
111    regex_iterator(const regex_iterator& that)
112       : pdata(that.pdata) {}
113    regex_iterator& operator=(const regex_iterator& that)
114    {
115       pdata = that.pdata;
116       return *this;
117    }
118    bool operator==(const regex_iterator& that)const
119    { 
120       if((pdata.get() == 0) || (that.pdata.get() == 0))
121          return pdata.get() == that.pdata.get();
122       return pdata->compare(*(that.pdata.get())); 
123    }
124    bool operator!=(const regex_iterator& that)const
125    { return !(*this == that); }
126    const value_type& operator*()const
127    { return pdata->get(); }
128    const value_type* operator->()const
129    { return &(pdata->get()); }
130    regex_iterator& operator++()
131    {
132       cow();
133       if(0 == pdata->next())
134       {
135          pdata.reset();
136       }
137       return *this;
138    }
139    regex_iterator operator++(int)
140    {
141       regex_iterator result(*this);
142       ++(*this);
143       return result;
144    }
145 private:
146
147    pimpl pdata;
148
149    void cow()
150    {
151       // copy-on-write
152       if(pdata.get() && !pdata.unique())
153       {
154          pdata.reset(new impl(*(pdata.get())));
155       }
156    }
157 };
158
159 typedef regex_iterator<const char*> cregex_iterator;
160 typedef regex_iterator<std::string::const_iterator> sregex_iterator;
161 #ifndef BOOST_NO_WREGEX
162 typedef regex_iterator<const wchar_t*> wcregex_iterator;
163 typedef regex_iterator<std::wstring::const_iterator> wsregex_iterator;
164 #endif
165
166 #ifdef BOOST_HAS_ABI_HEADERS
167 #  include BOOST_ABI_SUFFIX
168 #endif
169
170 } // namespace boost
171
172 #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP
173