3 * Copyright (c) 1998-2002
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)
13 * LOCATION: see http://www.boost.org for most recent version.
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares template class sub_match.
19 #ifndef BOOST_REGEX_V4_SUB_MATCH_HPP
20 #define BOOST_REGEX_V4_SUB_MATCH_HPP
22 #ifdef BOOST_HAS_ABI_HEADERS
23 # include BOOST_ABI_PREFIX
28 template <class BidiIterator>
29 struct sub_match : public std::pair<BidiIterator, BidiIterator>
31 typedef typename re_detail::regex_iterator_traits<BidiIterator>::value_type value_type;
32 #if defined(BOOST_NO_STD_ITERATOR_TRAITS) || defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
33 typedef std::ptrdiff_t difference_type;
35 typedef typename re_detail::regex_iterator_traits<BidiIterator>::difference_type difference_type;
37 typedef BidiIterator iterator_type;
38 typedef BidiIterator iterator;
39 typedef BidiIterator const_iterator;
43 sub_match() : std::pair<BidiIterator, BidiIterator>(), matched(false) {}
44 sub_match(BidiIterator i) : std::pair<BidiIterator, BidiIterator>(i, i), matched(false) {}
45 #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
46 && !BOOST_WORKAROUND(BOOST_MSVC, < 1310)\
47 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)\
48 && !BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
49 template <class T, class A>
50 operator std::basic_string<value_type, T, A> ()const
52 return std::basic_string<value_type, T, A>(this->first, this->second);
55 operator std::basic_string<value_type> ()const
60 difference_type BOOST_REGEX_CALL length()const
62 difference_type n = ::boost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second);
65 std::basic_string<value_type> str()const
67 std::basic_string<value_type> result;
68 std::size_t len = ::boost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second);
70 BidiIterator i = this->first;
71 while(i != this->second)
78 int compare(const sub_match& s)const
80 if(matched != s.matched)
81 return static_cast<int>(matched) - static_cast<int>(s.matched);
82 return str().compare(s.str());
84 int compare(const std::basic_string<value_type>& s)const
86 return str().compare(s);
88 int compare(const value_type* p)const
90 return str().compare(p);
93 bool operator==(const sub_match& that)const
94 { return compare(that) == 0; }
95 bool BOOST_REGEX_CALL operator !=(const sub_match& that)const
96 { return compare(that) != 0; }
97 bool operator<(const sub_match& that)const
98 { return compare(that) < 0; }
99 bool operator>(const sub_match& that)const
100 { return compare(that) > 0; }
101 bool operator<=(const sub_match& that)const
102 { return compare(that) <= 0; }
103 bool operator>=(const sub_match& that)const
104 { return compare(that) >= 0; }
106 #ifdef BOOST_REGEX_MATCH_EXTRA
107 typedef std::vector<sub_match<BidiIterator> > capture_sequence_type;
109 const capture_sequence_type& captures()const
112 m_captures.reset(new capture_sequence_type());
116 // Private implementation API: DO NOT USE!
118 capture_sequence_type& get_captures()const
121 m_captures.reset(new capture_sequence_type());
126 mutable boost::scoped_ptr<capture_sequence_type> m_captures;
130 sub_match(const sub_match& that, bool
131 #ifdef BOOST_REGEX_MATCH_EXTRA
136 : std::pair<BidiIterator, BidiIterator>(that),
137 matched(that.matched)
139 #ifdef BOOST_REGEX_MATCH_EXTRA
142 m_captures.reset(new capture_sequence_type(*(that.m_captures)));
145 sub_match& operator=(const sub_match& that)
147 this->first = that.first;
148 this->second = that.second;
149 matched = that.matched;
150 #ifdef BOOST_REGEX_MATCH_EXTRA
152 get_captures() = *(that.m_captures);
158 #ifdef BOOST_OLD_REGEX_H
160 // the following are deprecated, do not use!!
163 operator unsigned int()const;
164 operator short()const
166 return (short)(int)(*this);
168 operator unsigned short()const
170 return (unsigned short)(unsigned int)(*this);
175 typedef sub_match<const char*> csub_match;
176 typedef sub_match<std::string::const_iterator> ssub_match;
177 #ifndef BOOST_NO_WREGEX
178 typedef sub_match<const wchar_t*> wcsub_match;
179 typedef sub_match<std::wstring::const_iterator> wssub_match;
182 // comparison to std::basic_string<> part 1:
183 template <class RandomAccessIterator, class traits, class Allocator>
184 inline bool operator == (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
185 const sub_match<RandomAccessIterator>& m)
186 { return s.compare(m.str()) == 0; }
187 template <class RandomAccessIterator, class traits, class Allocator>
188 inline bool operator != (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
189 const sub_match<RandomAccessIterator>& m)
190 { return s.compare(m.str()) != 0; }
191 template <class RandomAccessIterator, class traits, class Allocator>
192 inline bool operator < (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
193 const sub_match<RandomAccessIterator>& m)
194 { return s.compare(m.str()) < 0; }
195 template <class RandomAccessIterator, class traits, class Allocator>
196 inline bool operator <= (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
197 const sub_match<RandomAccessIterator>& m)
198 { return s.compare(m.str()) <= 0; }
199 template <class RandomAccessIterator, class traits, class Allocator>
200 inline bool operator >= (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
201 const sub_match<RandomAccessIterator>& m)
202 { return s.compare(m.str()) >= 0; }
203 template <class RandomAccessIterator, class traits, class Allocator>
204 inline bool operator > (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
205 const sub_match<RandomAccessIterator>& m)
206 { return s.compare(m.str()) > 0; }
207 // comparison to std::basic_string<> part 2:
208 template <class RandomAccessIterator, class traits, class Allocator>
209 inline bool operator == (const sub_match<RandomAccessIterator>& m,
210 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
211 { return m.str().compare(s) == 0; }
212 template <class RandomAccessIterator, class traits, class Allocator>
213 inline bool operator != (const sub_match<RandomAccessIterator>& m,
214 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
215 { return m.str().compare(s) != 0; }
216 template <class RandomAccessIterator, class traits, class Allocator>
217 inline bool operator < (const sub_match<RandomAccessIterator>& m,
218 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
219 { return m.str().compare(s) < 0; }
220 template <class RandomAccessIterator, class traits, class Allocator>
221 inline bool operator > (const sub_match<RandomAccessIterator>& m,
222 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
223 { return m.str().compare(s) > 0; }
224 template <class RandomAccessIterator, class traits, class Allocator>
225 inline bool operator <= (const sub_match<RandomAccessIterator>& m,
226 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
227 { return m.str().compare(s) <= 0; }
228 template <class RandomAccessIterator, class traits, class Allocator>
229 inline bool operator >= (const sub_match<RandomAccessIterator>& m,
230 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
231 { return m.str().compare(s) >= 0; }
232 // comparison to const charT* part 1:
233 template <class RandomAccessIterator>
234 inline bool operator == (const sub_match<RandomAccessIterator>& m,
235 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
236 { return m.str().compare(s) == 0; }
237 template <class RandomAccessIterator>
238 inline bool operator != (const sub_match<RandomAccessIterator>& m,
239 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
240 { return m.str().compare(s) != 0; }
241 template <class RandomAccessIterator>
242 inline bool operator > (const sub_match<RandomAccessIterator>& m,
243 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
244 { return m.str().compare(s) > 0; }
245 template <class RandomAccessIterator>
246 inline bool operator < (const sub_match<RandomAccessIterator>& m,
247 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
248 { return m.str().compare(s) < 0; }
249 template <class RandomAccessIterator>
250 inline bool operator >= (const sub_match<RandomAccessIterator>& m,
251 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
252 { return m.str().compare(s) >= 0; }
253 template <class RandomAccessIterator>
254 inline bool operator <= (const sub_match<RandomAccessIterator>& m,
255 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
256 { return m.str().compare(s) <= 0; }
257 // comparison to const charT* part 2:
258 template <class RandomAccessIterator>
259 inline bool operator == (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
260 const sub_match<RandomAccessIterator>& m)
261 { return m.str().compare(s) == 0; }
262 template <class RandomAccessIterator>
263 inline bool operator != (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
264 const sub_match<RandomAccessIterator>& m)
265 { return m.str().compare(s) != 0; }
266 template <class RandomAccessIterator>
267 inline bool operator < (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
268 const sub_match<RandomAccessIterator>& m)
269 { return m.str().compare(s) > 0; }
270 template <class RandomAccessIterator>
271 inline bool operator > (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
272 const sub_match<RandomAccessIterator>& m)
273 { return m.str().compare(s) < 0; }
274 template <class RandomAccessIterator>
275 inline bool operator <= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
276 const sub_match<RandomAccessIterator>& m)
277 { return m.str().compare(s) >= 0; }
278 template <class RandomAccessIterator>
279 inline bool operator >= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
280 const sub_match<RandomAccessIterator>& m)
281 { return m.str().compare(s) <= 0; }
283 // comparison to const charT& part 1:
284 template <class RandomAccessIterator>
285 inline bool operator == (const sub_match<RandomAccessIterator>& m,
286 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
287 { return m.str().compare(0, m.length(), &s, 1) == 0; }
288 template <class RandomAccessIterator>
289 inline bool operator != (const sub_match<RandomAccessIterator>& m,
290 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
291 { return m.str().compare(0, m.length(), &s, 1) != 0; }
292 template <class RandomAccessIterator>
293 inline bool operator > (const sub_match<RandomAccessIterator>& m,
294 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
295 { return m.str().compare(0, m.length(), &s, 1) > 0; }
296 template <class RandomAccessIterator>
297 inline bool operator < (const sub_match<RandomAccessIterator>& m,
298 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
299 { return m.str().compare(0, m.length(), &s, 1) < 0; }
300 template <class RandomAccessIterator>
301 inline bool operator >= (const sub_match<RandomAccessIterator>& m,
302 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
303 { return m.str().compare(0, m.length(), &s, 1) >= 0; }
304 template <class RandomAccessIterator>
305 inline bool operator <= (const sub_match<RandomAccessIterator>& m,
306 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
307 { return m.str().compare(0, m.length(), &s, 1) <= 0; }
308 // comparison to const charT* part 2:
309 template <class RandomAccessIterator>
310 inline bool operator == (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
311 const sub_match<RandomAccessIterator>& m)
312 { return m.str().compare(0, m.length(), &s, 1) == 0; }
313 template <class RandomAccessIterator>
314 inline bool operator != (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
315 const sub_match<RandomAccessIterator>& m)
316 { return m.str().compare(0, m.length(), &s, 1) != 0; }
317 template <class RandomAccessIterator>
318 inline bool operator < (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
319 const sub_match<RandomAccessIterator>& m)
320 { return m.str().compare(0, m.length(), &s, 1) > 0; }
321 template <class RandomAccessIterator>
322 inline bool operator > (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
323 const sub_match<RandomAccessIterator>& m)
324 { return m.str().compare(0, m.length(), &s, 1) < 0; }
325 template <class RandomAccessIterator>
326 inline bool operator <= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
327 const sub_match<RandomAccessIterator>& m)
328 { return m.str().compare(0, m.length(), &s, 1) >= 0; }
329 template <class RandomAccessIterator>
330 inline bool operator >= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
331 const sub_match<RandomAccessIterator>& m)
332 { return m.str().compare(0, m.length(), &s, 1) <= 0; }
334 // addition operators:
335 template <class RandomAccessIterator, class traits, class Allocator>
336 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
337 operator + (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
338 const sub_match<RandomAccessIterator>& m)
340 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
341 result.reserve(s.size() + m.length() + 1);
342 return result.append(s).append(m.first, m.second);
344 template <class RandomAccessIterator, class traits, class Allocator>
345 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
346 operator + (const sub_match<RandomAccessIterator>& m,
347 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
349 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
350 result.reserve(s.size() + m.length() + 1);
351 return result.append(m.first, m.second).append(s);
353 #if !(defined(__GNUC__) && defined(BOOST_NO_STD_LOCALE))
354 template <class RandomAccessIterator>
355 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
356 operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
357 const sub_match<RandomAccessIterator>& m)
359 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
360 result.reserve(std::char_traits<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
361 return result.append(s).append(m.first, m.second);
363 template <class RandomAccessIterator>
364 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
365 operator + (const sub_match<RandomAccessIterator>& m,
366 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const * s)
368 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
369 result.reserve(std::char_traits<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
370 return result.append(m.first, m.second).append(s);
373 // worwaround versions:
374 template <class RandomAccessIterator>
375 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
376 operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
377 const sub_match<RandomAccessIterator>& m)
381 template <class RandomAccessIterator>
382 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
383 operator + (const sub_match<RandomAccessIterator>& m,
384 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const * s)
389 template <class RandomAccessIterator>
390 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
391 operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
392 const sub_match<RandomAccessIterator>& m)
394 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
395 result.reserve(m.length() + 2);
396 return result.append(1, s).append(m.first, m.second);
398 template <class RandomAccessIterator>
399 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
400 operator + (const sub_match<RandomAccessIterator>& m,
401 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
403 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
404 result.reserve(m.length() + 2);
405 return result.append(m.first, m.second).append(1, s);
407 template <class RandomAccessIterator>
408 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
409 operator + (const sub_match<RandomAccessIterator>& m1,
410 const sub_match<RandomAccessIterator>& m2)
412 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
413 result.reserve(m1.length() + m2.length() + 1);
414 return result.append(m1.first, m1.second).append(m2.first, m2.second);
416 #ifndef BOOST_NO_STD_LOCALE
417 template <class charT, class traits, class RandomAccessIterator>
418 std::basic_ostream<charT, traits>&
419 operator << (std::basic_ostream<charT, traits>& os,
420 const sub_match<RandomAccessIterator>& s)
422 return (os << s.str());
425 template <class RandomAccessIterator>
426 std::ostream& operator << (std::ostream& os,
427 const sub_match<RandomAccessIterator>& s)
429 return (os << s.str());
433 #ifdef BOOST_OLD_REGEX_H
435 template <class BidiIterator, class charT>
436 int do_toi(BidiIterator i, BidiIterator j, char c, int radix)
440 int result = std::strtol(s.c_str(), &p, radix);
441 if(*p)raise_regex_exception("Bad sub-expression");
447 template <class I, class charT>
448 int do_toi(I& i, I j, charT c)
451 while((i != j) && (isdigit(*i)))
453 result = result*10 + (*i - '0');
461 template <class BidiIterator>
462 sub_match<BidiIterator>::operator int()const
464 BidiIterator i = first;
465 BidiIterator j = second;
466 if(i == j)raise_regex_exception("Bad sub-expression");
468 if((i != j) && (*i == '-'))
473 neg *= re_detail::do_toi(i, j, *i);
474 if(i != j)raise_regex_exception("Bad sub-expression");
477 template <class BidiIterator>
478 sub_match<BidiIterator>::operator unsigned int()const
480 BidiIterator i = first;
481 BidiIterator j = second;
483 raise_regex_exception("Bad sub-expression");
484 return re_detail::do_toi(i, j, *first);
490 #ifdef BOOST_HAS_ABI_HEADERS
491 # include BOOST_ABI_SUFFIX