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