1 /*
2  *
3  * Copyright (c) 2003
4  * 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_MSVC
27 #pragma warning(push)
28 #pragma warning(disable: 4103)
29 #endif
30 #ifdef BOOST_HAS_ABI_HEADERS
31 #  include BOOST_ABI_PREFIX
32 #endif
33 #ifdef BOOST_MSVC
34 #pragma warning(pop)
35 #endif
36 
37 template <class BidirectionalIterator,
38           class charT,
39           class traits>
40 class regex_iterator_implementation
41 {
42    typedef basic_regex<charT, traits> regex_type;
43 
44    match_results<BidirectionalIterator> what;  // current match
45    BidirectionalIterator                base;  // start of sequence
46    BidirectionalIterator                end;   // end of sequence
47    const regex_type                     re;   // the expression
48    match_flag_type                      flags; // flags for matching
49 
50 public:
regex_iterator_implementation(const regex_type * p,BidirectionalIterator last,match_flag_type f)51    regex_iterator_implementation(const regex_type* p, BidirectionalIterator last, match_flag_type f)
52       : base(), end(last), re(*p), flags(f){}
regex_iterator_implementation(const regex_iterator_implementation & other)53    regex_iterator_implementation(const regex_iterator_implementation& other)
54       :what(other.what), base(other.base), end(other.end), re(other.re), flags(other.flags){}
init(BidirectionalIterator first)55    bool init(BidirectionalIterator first)
56    {
57       base = first;
58       return regex_search(first, end, what, re, flags);
59    }
compare(const regex_iterator_implementation & that)60    bool compare(const regex_iterator_implementation& that)
61    {
62       if(this == &that) return true;
63       return (&re.get_data() == &that.re.get_data()) && (end == that.end) && (flags == that.flags) && (what[0].first == that.what[0].first) && (what[0].second == that.what[0].second);
64    }
get()65    const match_results<BidirectionalIterator>& get()
66    { return what; }
next()67    bool next()
68    {
69       //if(what.prefix().first != what[0].second)
70       //   flags |= match_prev_avail;
71       BidirectionalIterator next_start = what[0].second;
72       match_flag_type f(flags);
73       if(!what.length() || (f & regex_constants::match_posix))
74          f |= regex_constants::match_not_initial_null;
75       //if(base != next_start)
76       //   f |= regex_constants::match_not_bob;
77       bool result = regex_search(next_start, end, what, re, f, base);
78       if(result)
79          what.set_base(base);
80       return result;
81    }
82 private:
83    regex_iterator_implementation& operator=(const regex_iterator_implementation&);
84 };
85 
86 template <class BidirectionalIterator,
87           class charT = BOOST_DEDUCED_TYPENAME BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::value_type,
88           class traits = regex_traits<charT> >
89 class regex_iterator
90 {
91 private:
92    typedef regex_iterator_implementation<BidirectionalIterator, charT, traits> impl;
93    typedef shared_ptr<impl> pimpl;
94 public:
95    typedef          basic_regex<charT, traits>                   regex_type;
96    typedef          match_results<BidirectionalIterator>                    value_type;
97    typedef typename BOOST_REGEX_DETAIL_NS::regex_iterator_traits<BidirectionalIterator>::difference_type
98                                                                             difference_type;
99    typedef          const value_type*                                       pointer;
100    typedef          const value_type&                                       reference;
101    typedef          std::forward_iterator_tag                               iterator_category;
102 
regex_iterator()103    regex_iterator(){}
regex_iterator(BidirectionalIterator a,BidirectionalIterator b,const regex_type & re,match_flag_type m=match_default)104    regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
105                   const regex_type& re,
106                   match_flag_type m = match_default)
107                   : pdata(new impl(&re, b, m))
108    {
109       if(!pdata->init(a))
110       {
111          pdata.reset();
112       }
113    }
regex_iterator(const regex_iterator & that)114    regex_iterator(const regex_iterator& that)
115       : pdata(that.pdata) {}
operator =(const regex_iterator & that)116    regex_iterator& operator=(const regex_iterator& that)
117    {
118       pdata = that.pdata;
119       return *this;
120    }
operator ==(const regex_iterator & that) const121    bool operator==(const regex_iterator& that)const
122    {
123       if((pdata.get() == 0) || (that.pdata.get() == 0))
124          return pdata.get() == that.pdata.get();
125       return pdata->compare(*(that.pdata.get()));
126    }
operator !=(const regex_iterator & that) const127    bool operator!=(const regex_iterator& that)const
128    { return !(*this == that); }
operator *() const129    const value_type& operator*()const
130    { return pdata->get(); }
operator ->() const131    const value_type* operator->()const
132    { return &(pdata->get()); }
operator ++()133    regex_iterator& operator++()
134    {
135       cow();
136       if(0 == pdata->next())
137       {
138          pdata.reset();
139       }
140       return *this;
141    }
operator ++(int)142    regex_iterator operator++(int)
143    {
144       regex_iterator result(*this);
145       ++(*this);
146       return result;
147    }
148 private:
149 
150    pimpl pdata;
151 
cow()152    void cow()
153    {
154       // copy-on-write
155       if(pdata.get() && !pdata.unique())
156       {
157          pdata.reset(new impl(*(pdata.get())));
158       }
159    }
160 };
161 
162 typedef regex_iterator<const char*> cregex_iterator;
163 typedef regex_iterator<std::string::const_iterator> sregex_iterator;
164 #ifndef BOOST_NO_WREGEX
165 typedef regex_iterator<const wchar_t*> wcregex_iterator;
166 typedef regex_iterator<std::wstring::const_iterator> wsregex_iterator;
167 #endif
168 
169 // make_regex_iterator:
170 template <class charT, class traits>
make_regex_iterator(const charT * p,const basic_regex<charT,traits> & e,regex_constants::match_flag_type m=regex_constants::match_default)171 inline regex_iterator<const charT*, charT, traits> make_regex_iterator(const charT* p, const basic_regex<charT, traits>& e, regex_constants::match_flag_type m = regex_constants::match_default)
172 {
173    return regex_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, m);
174 }
175 template <class charT, class traits, class ST, class SA>
make_regex_iterator(const std::basic_string<charT,ST,SA> & p,const basic_regex<charT,traits> & e,regex_constants::match_flag_type m=regex_constants::match_default)176 inline regex_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, regex_constants::match_flag_type m = regex_constants::match_default)
177 {
178    return regex_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, m);
179 }
180 
181 #ifdef BOOST_MSVC
182 #pragma warning(push)
183 #pragma warning(disable: 4103)
184 #endif
185 #ifdef BOOST_HAS_ABI_HEADERS
186 #  include BOOST_ABI_SUFFIX
187 #endif
188 #ifdef BOOST_MSVC
189 #pragma warning(pop)
190 #endif
191 
192 } // namespace boost
193 
194 #endif // BOOST_REGEX_V4_REGEX_ITERATOR_HPP
195 
196