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_token_iterator.hpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Provides regex_token_iterator implementation.
17   */
18 
19 #ifndef BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP
20 #define BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP
21 
22 #include <memory>
23 
24 namespace boost{
25 
26 template <class BidirectionalIterator,
27           class charT,
28           class traits>
29 class regex_token_iterator_implementation
30 {
31    typedef basic_regex<charT, traits> regex_type;
32    typedef sub_match<BidirectionalIterator>      value_type;
33 
34    match_results<BidirectionalIterator> what;   // current match
35    BidirectionalIterator                base;    // start of search area
36    BidirectionalIterator                end;    // end of search area
37    const regex_type                     re;    // the expression
38    match_flag_type                      flags;  // match flags
39    value_type                           result; // the current string result
40    int                                  N;      // the current sub-expression being enumerated
41    std::vector<int>                     subs;   // the sub-expressions to enumerate
42 
43 public:
regex_token_iterator_implementation(const regex_type * p,BidirectionalIterator last,int sub,match_flag_type f)44    regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, int sub, match_flag_type f)
45       : end(last), re(*p), flags(f), N(0){ subs.push_back(sub); }
regex_token_iterator_implementation(const regex_type * p,BidirectionalIterator last,const std::vector<int> & v,match_flag_type f)46    regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const std::vector<int>& v, match_flag_type f)
47       : end(last), re(*p), flags(f), N(0), subs(v){}
48    template <std::size_t CN>
regex_token_iterator_implementation(const regex_type * p,BidirectionalIterator last,const int (& submatches)[CN],match_flag_type f)49    regex_token_iterator_implementation(const regex_type* p, BidirectionalIterator last, const int (&submatches)[CN], match_flag_type f)
50       : end(last), re(*p), flags(f), N(0)
51    {
52       for(std::size_t i = 0; i < CN; ++i)
53       {
54          subs.push_back(submatches[i]);
55       }
56    }
57    regex_token_iterator_implementation(const regex_token_iterator_implementation& other) = default;
init(BidirectionalIterator first)58    bool init(BidirectionalIterator first)
59    {
60       N = 0;
61       base = first;
62       if(regex_search(first, end, what, re, flags, base) == true)
63       {
64          N = 0;
65          result = ((subs[N] == -1) ? what.prefix() : what[(int)subs[N]]);
66          return true;
67       }
68       else if((subs[N] == -1) && (first != end))
69       {
70          result.first = first;
71          result.second = end;
72          result.matched = (first != end);
73          N = -1;
74          return true;
75       }
76       return false;
77    }
compare(const regex_token_iterator_implementation & that)78    bool compare(const regex_token_iterator_implementation& that)
79    {
80       if(this == &that) return true;
81       return (&re.get_data() == &that.re.get_data())
82          && (end == that.end)
83          && (flags == that.flags)
84          && (N == that.N)
85          && (what[0].first == that.what[0].first)
86          && (what[0].second == that.what[0].second);
87    }
get()88    const value_type& get()
89    { return result; }
next()90    bool next()
91    {
92       if(N == -1)
93          return false;
94       if(N+1 < (int)subs.size())
95       {
96          ++N;
97          result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
98          return true;
99       }
100       //if(what.prefix().first != what[0].second)
101       //   flags |= /*match_prev_avail |*/ regex_constants::match_not_bob;
102       BidirectionalIterator last_end(what[0].second);
103       if(regex_search(last_end, end, what, re, ((what[0].first == what[0].second) ? flags | regex_constants::match_not_initial_null : flags), base))
104       {
105          N =0;
106          result =((subs[N] == -1) ? what.prefix() : what[subs[N]]);
107          return true;
108       }
109       else if((last_end != end) && (subs[0] == -1))
110       {
111          N =-1;
112          result.first = last_end;
113          result.second = end;
114          result.matched = (last_end != end);
115          return true;
116       }
117       return false;
118    }
119 private:
120    regex_token_iterator_implementation& operator=(const regex_token_iterator_implementation&);
121 };
122 
123 template <class BidirectionalIterator,
124           class charT = typename std::iterator_traits<BidirectionalIterator>::value_type,
125           class traits = regex_traits<charT> >
126 class regex_token_iterator
127 {
128 private:
129    typedef regex_token_iterator_implementation<BidirectionalIterator, charT, traits> impl;
130    typedef std::shared_ptr<impl> pimpl;
131 public:
132    typedef          basic_regex<charT, traits>                   regex_type;
133    typedef          sub_match<BidirectionalIterator>                        value_type;
134    typedef typename std::iterator_traits<BidirectionalIterator>::difference_type
135                                                                             difference_type;
136    typedef          const value_type*                                       pointer;
137    typedef          const value_type&                                       reference;
138    typedef          std::forward_iterator_tag                               iterator_category;
139 
regex_token_iterator()140    regex_token_iterator(){}
regex_token_iterator(BidirectionalIterator a,BidirectionalIterator b,const regex_type & re,int submatch=0,match_flag_type m=match_default)141    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
142                         int submatch = 0, match_flag_type m = match_default)
143                         : pdata(new impl(&re, b, submatch, m))
144    {
145       if(!pdata->init(a))
146          pdata.reset();
147    }
regex_token_iterator(BidirectionalIterator a,BidirectionalIterator b,const regex_type & re,const std::vector<int> & submatches,match_flag_type m=match_default)148    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
149                         const std::vector<int>& submatches, match_flag_type m = match_default)
150                         : pdata(new impl(&re, b, submatches, m))
151    {
152       if(!pdata->init(a))
153          pdata.reset();
154    }
155    template <std::size_t N>
regex_token_iterator(BidirectionalIterator a,BidirectionalIterator b,const regex_type & re,const int (& submatches)[N],match_flag_type m=match_default)156    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b, const regex_type& re,
157                         const int (&submatches)[N], match_flag_type m = match_default)
158                         : pdata(new impl(&re, b, submatches, m))
159    {
160       if(!pdata->init(a))
161          pdata.reset();
162    }
regex_token_iterator(const regex_token_iterator & that)163    regex_token_iterator(const regex_token_iterator& that)
164       : pdata(that.pdata) {}
operator =(const regex_token_iterator & that)165    regex_token_iterator& operator=(const regex_token_iterator& that)
166    {
167       pdata = that.pdata;
168       return *this;
169    }
operator ==(const regex_token_iterator & that) const170    bool operator==(const regex_token_iterator& that)const
171    {
172       if((pdata.get() == 0) || (that.pdata.get() == 0))
173          return pdata.get() == that.pdata.get();
174       return pdata->compare(*(that.pdata.get()));
175    }
operator !=(const regex_token_iterator & that) const176    bool operator!=(const regex_token_iterator& that)const
177    { return !(*this == that); }
operator *() const178    const value_type& operator*()const
179    { return pdata->get(); }
operator ->() const180    const value_type* operator->()const
181    { return &(pdata->get()); }
operator ++()182    regex_token_iterator& operator++()
183    {
184       cow();
185       if(0 == pdata->next())
186       {
187          pdata.reset();
188       }
189       return *this;
190    }
operator ++(int)191    regex_token_iterator operator++(int)
192    {
193       regex_token_iterator result(*this);
194       ++(*this);
195       return result;
196    }
197 private:
198 
199    pimpl pdata;
200 
cow()201    void cow()
202    {
203       // copy-on-write
204       if(pdata.get() && (pdata.use_count() > 1))
205       {
206          pdata.reset(new impl(*(pdata.get())));
207       }
208    }
209 };
210 
211 typedef regex_token_iterator<const char*> cregex_token_iterator;
212 typedef regex_token_iterator<std::string::const_iterator> sregex_token_iterator;
213 #ifndef BOOST_NO_WREGEX
214 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
215 typedef regex_token_iterator<std::wstring::const_iterator> wsregex_token_iterator;
216 #endif
217 
218 template <class charT, class traits>
make_regex_token_iterator(const charT * p,const basic_regex<charT,traits> & e,int submatch=0,regex_constants::match_flag_type m=regex_constants::match_default)219 inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
220 {
221    return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
222 }
223 template <class charT, class traits, class ST, class SA>
make_regex_token_iterator(const std::basic_string<charT,ST,SA> & p,const basic_regex<charT,traits> & e,int submatch=0,regex_constants::match_flag_type m=regex_constants::match_default)224 inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, int submatch = 0, regex_constants::match_flag_type m = regex_constants::match_default)
225 {
226    return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
227 }
228 template <class charT, class traits, std::size_t N>
make_regex_token_iterator(const charT * p,const basic_regex<charT,traits> & e,const int (& submatch)[N],regex_constants::match_flag_type m=regex_constants::match_default)229 inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
230 {
231    return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
232 }
233 template <class charT, class traits, class ST, class SA, std::size_t N>
make_regex_token_iterator(const std::basic_string<charT,ST,SA> & p,const basic_regex<charT,traits> & e,const int (& submatch)[N],regex_constants::match_flag_type m=regex_constants::match_default)234 inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const int (&submatch)[N], regex_constants::match_flag_type m = regex_constants::match_default)
235 {
236    return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
237 }
238 template <class charT, class traits>
make_regex_token_iterator(const charT * p,const basic_regex<charT,traits> & e,const std::vector<int> & submatch,regex_constants::match_flag_type m=regex_constants::match_default)239 inline regex_token_iterator<const charT*, charT, traits> make_regex_token_iterator(const charT* p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
240 {
241    return regex_token_iterator<const charT*, charT, traits>(p, p+traits::length(p), e, submatch, m);
242 }
243 template <class charT, class traits, class ST, class SA>
make_regex_token_iterator(const std::basic_string<charT,ST,SA> & p,const basic_regex<charT,traits> & e,const std::vector<int> & submatch,regex_constants::match_flag_type m=regex_constants::match_default)244 inline regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits> make_regex_token_iterator(const std::basic_string<charT, ST, SA>& p, const basic_regex<charT, traits>& e, const std::vector<int>& submatch, regex_constants::match_flag_type m = regex_constants::match_default)
245 {
246    return regex_token_iterator<typename std::basic_string<charT, ST, SA>::const_iterator, charT, traits>(p.begin(), p.end(), e, submatch, m);
247 }
248 
249 } // namespace boost
250 
251 #endif // BOOST_REGEX_V5_REGEX_TOKEN_ITERATOR_HPP
252 
253 
254 
255 
256