1 | /* |
2 | * |
3 | * Copyright (c) 1998-2002 |
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 sub_match.cpp |
15 | * VERSION see <boost/version.hpp> |
16 | * DESCRIPTION: Declares template class sub_match. |
17 | */ |
18 | |
19 | #ifndef BOOST_REGEX_V5_SUB_MATCH_HPP |
20 | #define BOOST_REGEX_V5_SUB_MATCH_HPP |
21 | |
22 | namespace boost{ |
23 | |
24 | template <class BidiIterator> |
25 | struct sub_match : public std::pair<BidiIterator, BidiIterator> |
26 | { |
27 | typedef typename std::iterator_traits<BidiIterator>::value_type value_type; |
28 | typedef typename std::iterator_traits<BidiIterator>::difference_type difference_type; |
29 | typedef BidiIterator iterator_type; |
30 | typedef BidiIterator iterator; |
31 | typedef BidiIterator const_iterator; |
32 | |
33 | bool matched; |
34 | |
35 | sub_match() : std::pair<BidiIterator, BidiIterator>(), matched(false) {} |
36 | sub_match(BidiIterator i) : std::pair<BidiIterator, BidiIterator>(i, i), matched(false) {} |
37 | template <class T, class A> |
38 | operator std::basic_string<value_type, T, A> ()const |
39 | { |
40 | return matched ? std::basic_string<value_type, T, A>(this->first, this->second) : std::basic_string<value_type, T, A>(); |
41 | } |
42 | difference_type length()const |
43 | { |
44 | difference_type n = matched ? std::distance((BidiIterator)this->first, (BidiIterator)this->second) : 0; |
45 | return n; |
46 | } |
47 | std::basic_string<value_type> str()const |
48 | { |
49 | std::basic_string<value_type> result; |
50 | if(matched) |
51 | { |
52 | std::size_t len = std::distance((BidiIterator)this->first, (BidiIterator)this->second); |
53 | result.reserve(len); |
54 | BidiIterator i = this->first; |
55 | while(i != this->second) |
56 | { |
57 | result.append(1, *i); |
58 | ++i; |
59 | } |
60 | } |
61 | return result; |
62 | } |
63 | int compare(const sub_match& s)const |
64 | { |
65 | if(matched != s.matched) |
66 | return static_cast<int>(matched) - static_cast<int>(s.matched); |
67 | return str().compare(s.str()); |
68 | } |
69 | int compare(const std::basic_string<value_type>& s)const |
70 | { |
71 | return str().compare(s); |
72 | } |
73 | int compare(const value_type* p)const |
74 | { |
75 | return str().compare(p); |
76 | } |
77 | |
78 | bool operator==(const sub_match& that)const |
79 | { return compare(that) == 0; } |
80 | bool operator !=(const sub_match& that)const |
81 | { return compare(that) != 0; } |
82 | bool operator<(const sub_match& that)const |
83 | { return compare(that) < 0; } |
84 | bool operator>(const sub_match& that)const |
85 | { return compare(that) > 0; } |
86 | bool operator<=(const sub_match& that)const |
87 | { return compare(that) <= 0; } |
88 | bool operator>=(const sub_match& that)const |
89 | { return compare(that) >= 0; } |
90 | |
91 | #ifdef BOOST_REGEX_MATCH_EXTRA |
92 | typedef std::vector<sub_match<BidiIterator> > capture_sequence_type; |
93 | |
94 | const capture_sequence_type& captures()const |
95 | { |
96 | if(!m_captures) |
97 | m_captures.reset(new capture_sequence_type()); |
98 | return *m_captures; |
99 | } |
100 | // |
101 | // Private implementation API: DO NOT USE! |
102 | // |
103 | capture_sequence_type& get_captures()const |
104 | { |
105 | if(!m_captures) |
106 | m_captures.reset(new capture_sequence_type()); |
107 | return *m_captures; |
108 | } |
109 | |
110 | private: |
111 | mutable std::unique_ptr<capture_sequence_type> m_captures; |
112 | public: |
113 | |
114 | #endif |
115 | sub_match(const sub_match& that, bool |
116 | #ifdef BOOST_REGEX_MATCH_EXTRA |
117 | deep_copy |
118 | #endif |
119 | = true |
120 | ) |
121 | : std::pair<BidiIterator, BidiIterator>(that), |
122 | matched(that.matched) |
123 | { |
124 | #ifdef BOOST_REGEX_MATCH_EXTRA |
125 | if(that.m_captures) |
126 | if(deep_copy) |
127 | m_captures.reset(new capture_sequence_type(*(that.m_captures))); |
128 | #endif |
129 | } |
130 | sub_match& operator=(const sub_match& that) |
131 | { |
132 | this->first = that.first; |
133 | this->second = that.second; |
134 | matched = that.matched; |
135 | #ifdef BOOST_REGEX_MATCH_EXTRA |
136 | if(that.m_captures) |
137 | get_captures() = *(that.m_captures); |
138 | #endif |
139 | return *this; |
140 | } |
141 | // |
142 | // Make this type a range, for both Boost.Range, and C++11: |
143 | // |
144 | BidiIterator begin()const { return this->first; } |
145 | BidiIterator end()const { return this->second; } |
146 | }; |
147 | |
148 | typedef sub_match<const char*> csub_match; |
149 | typedef sub_match<std::string::const_iterator> ssub_match; |
150 | #ifndef BOOST_NO_WREGEX |
151 | typedef sub_match<const wchar_t*> wcsub_match; |
152 | typedef sub_match<std::wstring::const_iterator> wssub_match; |
153 | #endif |
154 | |
155 | // comparison to std::basic_string<> part 1: |
156 | template <class RandomAccessIterator, class traits, class Allocator> |
157 | inline bool operator == (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s, |
158 | const sub_match<RandomAccessIterator>& m) |
159 | { return s.compare(m.str()) == 0; } |
160 | template <class RandomAccessIterator, class traits, class Allocator> |
161 | inline bool operator != (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s, |
162 | const sub_match<RandomAccessIterator>& m) |
163 | { return s.compare(m.str()) != 0; } |
164 | template <class RandomAccessIterator, class traits, class Allocator> |
165 | inline bool operator < (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s, |
166 | const sub_match<RandomAccessIterator>& m) |
167 | { return s.compare(m.str()) < 0; } |
168 | template <class RandomAccessIterator, class traits, class Allocator> |
169 | inline bool operator <= (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s, |
170 | const sub_match<RandomAccessIterator>& m) |
171 | { return s.compare(m.str()) <= 0; } |
172 | template <class RandomAccessIterator, class traits, class Allocator> |
173 | inline bool operator >= (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s, |
174 | const sub_match<RandomAccessIterator>& m) |
175 | { return s.compare(m.str()) >= 0; } |
176 | template <class RandomAccessIterator, class traits, class Allocator> |
177 | inline bool operator > (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s, |
178 | const sub_match<RandomAccessIterator>& m) |
179 | { return s.compare(m.str()) > 0; } |
180 | // comparison to std::basic_string<> part 2: |
181 | template <class RandomAccessIterator, class traits, class Allocator> |
182 | inline bool operator == (const sub_match<RandomAccessIterator>& m, |
183 | const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s) |
184 | { return m.str().compare(s) == 0; } |
185 | template <class RandomAccessIterator, class traits, class Allocator> |
186 | inline bool operator != (const sub_match<RandomAccessIterator>& m, |
187 | const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s) |
188 | { return m.str().compare(s) != 0; } |
189 | template <class RandomAccessIterator, class traits, class Allocator> |
190 | inline bool operator < (const sub_match<RandomAccessIterator>& m, |
191 | const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s) |
192 | { return m.str().compare(s) < 0; } |
193 | template <class RandomAccessIterator, class traits, class Allocator> |
194 | inline bool operator > (const sub_match<RandomAccessIterator>& m, |
195 | const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s) |
196 | { return m.str().compare(s) > 0; } |
197 | template <class RandomAccessIterator, class traits, class Allocator> |
198 | inline bool operator <= (const sub_match<RandomAccessIterator>& m, |
199 | const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s) |
200 | { return m.str().compare(s) <= 0; } |
201 | template <class RandomAccessIterator, class traits, class Allocator> |
202 | inline bool operator >= (const sub_match<RandomAccessIterator>& m, |
203 | const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s) |
204 | { return m.str().compare(s) >= 0; } |
205 | // comparison to const charT* part 1: |
206 | template <class RandomAccessIterator> |
207 | inline bool operator == (const sub_match<RandomAccessIterator>& m, |
208 | typename std::iterator_traits<RandomAccessIterator>::value_type const* s) |
209 | { return m.str().compare(s) == 0; } |
210 | template <class RandomAccessIterator> |
211 | inline bool operator != (const sub_match<RandomAccessIterator>& m, |
212 | typename std::iterator_traits<RandomAccessIterator>::value_type const* s) |
213 | { return m.str().compare(s) != 0; } |
214 | template <class RandomAccessIterator> |
215 | inline bool operator > (const sub_match<RandomAccessIterator>& m, |
216 | typename std::iterator_traits<RandomAccessIterator>::value_type const* s) |
217 | { return m.str().compare(s) > 0; } |
218 | template <class RandomAccessIterator> |
219 | inline bool operator < (const sub_match<RandomAccessIterator>& m, |
220 | typename std::iterator_traits<RandomAccessIterator>::value_type const* s) |
221 | { return m.str().compare(s) < 0; } |
222 | template <class RandomAccessIterator> |
223 | inline bool operator >= (const sub_match<RandomAccessIterator>& m, |
224 | typename std::iterator_traits<RandomAccessIterator>::value_type const* s) |
225 | { return m.str().compare(s) >= 0; } |
226 | template <class RandomAccessIterator> |
227 | inline bool operator <= (const sub_match<RandomAccessIterator>& m, |
228 | typename std::iterator_traits<RandomAccessIterator>::value_type const* s) |
229 | { return m.str().compare(s) <= 0; } |
230 | // comparison to const charT* part 2: |
231 | template <class RandomAccessIterator> |
232 | inline bool operator == (typename std::iterator_traits<RandomAccessIterator>::value_type const* s, |
233 | const sub_match<RandomAccessIterator>& m) |
234 | { return m.str().compare(s) == 0; } |
235 | template <class RandomAccessIterator> |
236 | inline bool operator != (typename std::iterator_traits<RandomAccessIterator>::value_type const* s, |
237 | const sub_match<RandomAccessIterator>& m) |
238 | { return m.str().compare(s) != 0; } |
239 | template <class RandomAccessIterator> |
240 | inline bool operator < (typename std::iterator_traits<RandomAccessIterator>::value_type const* s, |
241 | const sub_match<RandomAccessIterator>& m) |
242 | { return m.str().compare(s) > 0; } |
243 | template <class RandomAccessIterator> |
244 | inline bool operator > (typename std::iterator_traits<RandomAccessIterator>::value_type const* s, |
245 | const sub_match<RandomAccessIterator>& m) |
246 | { return m.str().compare(s) < 0; } |
247 | template <class RandomAccessIterator> |
248 | inline bool operator <= (typename std::iterator_traits<RandomAccessIterator>::value_type const* s, |
249 | const sub_match<RandomAccessIterator>& m) |
250 | { return m.str().compare(s) >= 0; } |
251 | template <class RandomAccessIterator> |
252 | inline bool operator >= (typename std::iterator_traits<RandomAccessIterator>::value_type const* s, |
253 | const sub_match<RandomAccessIterator>& m) |
254 | { return m.str().compare(s) <= 0; } |
255 | |
256 | // comparison to const charT& part 1: |
257 | template <class RandomAccessIterator> |
258 | inline bool operator == (const sub_match<RandomAccessIterator>& m, |
259 | typename std::iterator_traits<RandomAccessIterator>::value_type const& s) |
260 | { return m.str().compare(0, m.length(), &s, 1) == 0; } |
261 | template <class RandomAccessIterator> |
262 | inline bool operator != (const sub_match<RandomAccessIterator>& m, |
263 | typename std::iterator_traits<RandomAccessIterator>::value_type const& s) |
264 | { return m.str().compare(0, m.length(), &s, 1) != 0; } |
265 | template <class RandomAccessIterator> |
266 | inline bool operator > (const sub_match<RandomAccessIterator>& m, |
267 | typename std::iterator_traits<RandomAccessIterator>::value_type const& s) |
268 | { return m.str().compare(0, m.length(), &s, 1) > 0; } |
269 | template <class RandomAccessIterator> |
270 | inline bool operator < (const sub_match<RandomAccessIterator>& m, |
271 | typename std::iterator_traits<RandomAccessIterator>::value_type const& s) |
272 | { return m.str().compare(0, m.length(), &s, 1) < 0; } |
273 | template <class RandomAccessIterator> |
274 | inline bool operator >= (const sub_match<RandomAccessIterator>& m, |
275 | typename std::iterator_traits<RandomAccessIterator>::value_type const& s) |
276 | { return m.str().compare(0, m.length(), &s, 1) >= 0; } |
277 | template <class RandomAccessIterator> |
278 | inline bool operator <= (const sub_match<RandomAccessIterator>& m, |
279 | typename std::iterator_traits<RandomAccessIterator>::value_type const& s) |
280 | { return m.str().compare(0, m.length(), &s, 1) <= 0; } |
281 | // comparison to const charT* part 2: |
282 | template <class RandomAccessIterator> |
283 | inline bool operator == (typename std::iterator_traits<RandomAccessIterator>::value_type const& s, |
284 | const sub_match<RandomAccessIterator>& m) |
285 | { return m.str().compare(0, m.length(), &s, 1) == 0; } |
286 | template <class RandomAccessIterator> |
287 | inline bool operator != (typename std::iterator_traits<RandomAccessIterator>::value_type const& s, |
288 | const sub_match<RandomAccessIterator>& m) |
289 | { return m.str().compare(0, m.length(), &s, 1) != 0; } |
290 | template <class RandomAccessIterator> |
291 | inline bool operator < (typename std::iterator_traits<RandomAccessIterator>::value_type const& s, |
292 | const sub_match<RandomAccessIterator>& m) |
293 | { return m.str().compare(0, m.length(), &s, 1) > 0; } |
294 | template <class RandomAccessIterator> |
295 | inline bool operator > (typename std::iterator_traits<RandomAccessIterator>::value_type const& s, |
296 | const sub_match<RandomAccessIterator>& m) |
297 | { return m.str().compare(0, m.length(), &s, 1) < 0; } |
298 | template <class RandomAccessIterator> |
299 | inline bool operator <= (typename std::iterator_traits<RandomAccessIterator>::value_type const& s, |
300 | const sub_match<RandomAccessIterator>& m) |
301 | { return m.str().compare(0, m.length(), &s, 1) >= 0; } |
302 | template <class RandomAccessIterator> |
303 | inline bool operator >= (typename std::iterator_traits<RandomAccessIterator>::value_type const& s, |
304 | const sub_match<RandomAccessIterator>& m) |
305 | { return m.str().compare(0, m.length(), &s, 1) <= 0; } |
306 | |
307 | // addition operators: |
308 | template <class RandomAccessIterator, class traits, class Allocator> |
309 | inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> |
310 | operator + (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s, |
311 | const sub_match<RandomAccessIterator>& m) |
312 | { |
313 | std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result; |
314 | result.reserve(s.size() + m.length() + 1); |
315 | return result.append(s).append(m.first, m.second); |
316 | } |
317 | template <class RandomAccessIterator, class traits, class Allocator> |
318 | inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> |
319 | operator + (const sub_match<RandomAccessIterator>& m, |
320 | const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s) |
321 | { |
322 | std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result; |
323 | result.reserve(s.size() + m.length() + 1); |
324 | return result.append(m.first, m.second).append(s); |
325 | } |
326 | template <class RandomAccessIterator> |
327 | inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> |
328 | operator + (typename std::iterator_traits<RandomAccessIterator>::value_type const* s, |
329 | const sub_match<RandomAccessIterator>& m) |
330 | { |
331 | std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result; |
332 | result.reserve(std::char_traits<typename std::iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1); |
333 | return result.append(s).append(m.first, m.second); |
334 | } |
335 | template <class RandomAccessIterator> |
336 | inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> |
337 | operator + (const sub_match<RandomAccessIterator>& m, |
338 | typename std::iterator_traits<RandomAccessIterator>::value_type const * s) |
339 | { |
340 | std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result; |
341 | result.reserve(std::char_traits<typename std::iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1); |
342 | return result.append(m.first, m.second).append(s); |
343 | } |
344 | template <class RandomAccessIterator> |
345 | inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> |
346 | operator + (typename std::iterator_traits<RandomAccessIterator>::value_type const& s, |
347 | const sub_match<RandomAccessIterator>& m) |
348 | { |
349 | std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result; |
350 | result.reserve(m.length() + 2); |
351 | return result.append(1, s).append(m.first, m.second); |
352 | } |
353 | template <class RandomAccessIterator> |
354 | inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> |
355 | operator + (const sub_match<RandomAccessIterator>& m, |
356 | typename std::iterator_traits<RandomAccessIterator>::value_type const& s) |
357 | { |
358 | std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result; |
359 | result.reserve(m.length() + 2); |
360 | return result.append(m.first, m.second).append(1, s); |
361 | } |
362 | template <class RandomAccessIterator> |
363 | inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> |
364 | operator + (const sub_match<RandomAccessIterator>& m1, |
365 | const sub_match<RandomAccessIterator>& m2) |
366 | { |
367 | std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result; |
368 | result.reserve(m1.length() + m2.length() + 1); |
369 | return result.append(m1.first, m1.second).append(m2.first, m2.second); |
370 | } |
371 | template <class charT, class traits, class RandomAccessIterator> |
372 | std::basic_ostream<charT, traits>& |
373 | operator << (std::basic_ostream<charT, traits>& os, |
374 | const sub_match<RandomAccessIterator>& s) |
375 | { |
376 | return (os << s.str()); |
377 | } |
378 | |
379 | } // namespace boost |
380 | |
381 | #endif |
382 | |
383 | |