*: Use headername alias to associate private includes to public includes.
[gcc.git] / libstdc++-v3 / include / bits / regex.h
1 // class template regex -*- C++ -*-
2
3 // Copyright (C) 2010 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /**
26 * @file bits/regex.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
29 */
30
31 _GLIBCXX_BEGIN_NAMESPACE(std)
32
33 /**
34 * @defgroup regex Regular Expressions
35 * A facility for performing regular expression pattern matching.
36 */
37 //@{
38
39 // [7.7] Class regex_traits
40 /**
41 * @brief Describes aspects of a regular expression.
42 *
43 * A regular expression traits class that satisfies the requirements of
44 * section [28.7].
45 *
46 * The class %regex is parameterized around a set of related types and
47 * functions used to complete the definition of its semantics. This class
48 * satisfies the requirements of such a traits class.
49 */
50 template<typename _Ch_type>
51 struct regex_traits
52 {
53 public:
54 typedef _Ch_type char_type;
55 typedef std::basic_string<char_type> string_type;
56 typedef std::locale locale_type;
57 typedef std::ctype_base::mask char_class_type;
58
59 public:
60 /**
61 * @brief Constructs a default traits object.
62 */
63 regex_traits()
64 { }
65
66 /**
67 * @brief Gives the length of a C-style string starting at @p __p.
68 *
69 * @param __p a pointer to the start of a character sequence.
70 *
71 * @returns the number of characters between @p *__p and the first
72 * default-initialized value of type @p char_type. In other words, uses
73 * the C-string algorithm for determining the length of a sequence of
74 * characters.
75 */
76 static std::size_t
77 length(const char_type* __p)
78 { return string_type::traits_type::length(__p); }
79
80 /**
81 * @brief Performs the identity translation.
82 *
83 * @param c A character to the locale-specific character set.
84 *
85 * @returns c.
86 */
87 char_type
88 translate(char_type __c) const
89 { return __c; }
90
91 /**
92 * @brief Translates a character into a case-insensitive equivalent.
93 *
94 * @param c A character to the locale-specific character set.
95 *
96 * @returns the locale-specific lower-case equivalent of c.
97 * @throws std::bad_cast if the imbued locale does not support the ctype
98 * facet.
99 */
100 char_type
101 translate_nocase(char_type __c) const
102 {
103 using std::ctype;
104 using std::use_facet;
105 return use_facet<ctype<char_type> >(_M_locale).tolower(__c);
106 }
107
108 /**
109 * @brief Gets a sort key for a character sequence.
110 *
111 * @param first beginning of the character sequence.
112 * @param last one-past-the-end of the character sequence.
113 *
114 * Returns a sort key for the character sequence designated by the
115 * iterator range [F1, F2) such that if the character sequence [G1, G2)
116 * sorts before the character sequence [H1, H2) then
117 * v.transform(G1, G2) < v.transform(H1, H2).
118 *
119 * What this really does is provide a more efficient way to compare a
120 * string to multiple other strings in locales with fancy collation
121 * rules and equivalence classes.
122 *
123 * @returns a locale-specific sort key equivalent to the input range.
124 *
125 * @throws std::bad_cast if the current locale does not have a collate
126 * facet.
127 */
128 template<typename _Fwd_iter>
129 string_type
130 transform(_Fwd_iter __first, _Fwd_iter __last) const
131 {
132 using std::collate;
133 using std::use_facet;
134 const collate<_Ch_type>& __c(use_facet<
135 collate<_Ch_type> >(_M_locale));
136 string_type __s(__first, __last);
137 return __c.transform(__s.data(), __s.data() + __s.size());
138 }
139
140 /**
141 * @brief Gets a sort key for a character sequence, independant of case.
142 *
143 * @param first beginning of the character sequence.
144 * @param last one-past-the-end of the character sequence.
145 *
146 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
147 * typeid(collate_byname<_Ch_type>) and the form of the sort key
148 * returned by collate_byname<_Ch_type>::transform(first, last) is known
149 * and can be converted into a primary sort key then returns that key,
150 * otherwise returns an empty string.
151 *
152 * @todo Implement this function.
153 */
154 template<typename _Fwd_iter>
155 string_type
156 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
157 { return string_type(); }
158
159 /**
160 * @brief Gets a collation element by name.
161 *
162 * @param first beginning of the collation element name.
163 * @param last one-past-the-end of the collation element name.
164 *
165 * @returns a sequence of one or more characters that represents the
166 * collating element consisting of the character sequence designated by
167 * the iterator range [first, last). Returns an empty string if the
168 * character sequence is not a valid collating element.
169 *
170 * @todo Implement this function.
171 */
172 template<typename _Fwd_iter>
173 string_type
174 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
175 { return string_type(); }
176
177 /**
178 * @brief Maps one or more characters to a named character
179 * classification.
180 *
181 * @param first beginning of the character sequence.
182 * @param last one-past-the-end of the character sequence.
183 * @param icase ignores the case of the classification name.
184 *
185 * @returns an unspecified value that represents the character
186 * classification named by the character sequence designated by the
187 * iterator range [first, last). If @p icase is true, the returned mask
188 * identifies the classification regardless of the case of the characters
189 * to be matched (for example, [[:lower:]] is the same as [[:alpha:]]),
190 * otherwise a case-dependant classification is returned. The value
191 * returned shall be independent of the case of the characters in the
192 * character sequence. If the name is not recognized then returns a value
193 * that compares equal to 0.
194 *
195 * At least the following names (or their wide-character equivalent) are
196 * supported.
197 * - d
198 * - w
199 * - s
200 * - alnum
201 * - alpha
202 * - blank
203 * - cntrl
204 * - digit
205 * - graph
206 * - lower
207 * - print
208 * - punct
209 * - space
210 * - upper
211 * - xdigit
212 *
213 * @todo Implement this function.
214 */
215 template<typename _Fwd_iter>
216 char_class_type
217 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
218 bool __icase = false) const
219 { return 0; }
220
221 /**
222 * @brief Determines if @p c is a member of an identified class.
223 *
224 * @param c a character.
225 * @param f a class type (as returned from lookup_classname).
226 *
227 * @returns true if the character @p c is a member of the classification
228 * represented by @p f, false otherwise.
229 *
230 * @throws std::bad_cast if the current locale does not have a ctype
231 * facet.
232 */
233 bool
234 isctype(_Ch_type __c, char_class_type __f) const;
235
236 /**
237 * @brief Converts a digit to an int.
238 *
239 * @param ch a character representing a digit.
240 * @param radix the radix if the numeric conversion (limited to 8, 10,
241 * or 16).
242 *
243 * @returns the value represented by the digit ch in base radix if the
244 * character ch is a valid digit in base radix; otherwise returns -1.
245 */
246 int
247 value(_Ch_type __ch, int __radix) const;
248
249 /**
250 * @brief Imbues the regex_traits object with a copy of a new locale.
251 *
252 * @param loc A locale.
253 *
254 * @returns a copy of the previous locale in use by the regex_traits
255 * object.
256 *
257 * @note Calling imbue with a different locale than the one currently in
258 * use invalidates all cached data held by *this.
259 */
260 locale_type
261 imbue(locale_type __loc)
262 {
263 std::swap(_M_locale, __loc);
264 return __loc;
265 }
266
267 /**
268 * @brief Gets a copy of the current locale in use by the regex_traits
269 * object.
270 */
271 locale_type
272 getloc() const
273 { return _M_locale; }
274
275 protected:
276 locale_type _M_locale;
277 };
278
279 template<typename _Ch_type>
280 bool
281 regex_traits<_Ch_type>::
282 isctype(_Ch_type __c, char_class_type __f) const
283 {
284 using std::ctype;
285 using std::use_facet;
286 const ctype<_Ch_type>& __ctype(use_facet<
287 ctype<_Ch_type> >(_M_locale));
288
289 if (__ctype.is(__f, __c))
290 return true;
291
292 // special case of underscore in [[:w:]]
293 if (__c == __ctype.widen('_'))
294 {
295 const char __wb[] = "w";
296 char_class_type __wt = this->lookup_classname(__wb,
297 __wb + sizeof(__wb));
298 if (__f | __wt)
299 return true;
300 }
301
302 // special case of [[:space:]] in [[:blank:]]
303 if (__ctype.is(std::ctype_base::space, __c))
304 {
305 const char __bb[] = "blank";
306 char_class_type __bt = this->lookup_classname(__bb,
307 __bb + sizeof(__bb));
308 if (__f | __bt)
309 return true;
310 }
311
312 return false;
313 }
314
315 template<typename _Ch_type>
316 int
317 regex_traits<_Ch_type>::
318 value(_Ch_type __ch, int __radix) const
319 {
320 std::basic_istringstream<_Ch_type> __is(string_type(1, __ch));
321 int __v;
322 if (__radix == 8)
323 __is >> std::oct;
324 else if (__radix == 16)
325 __is >> std::hex;
326 __is >> __v;
327 return __is.fail() ? -1 : __v;
328 }
329
330 // [7.8] Class basic_regex
331 /**
332 * Objects of specializations of this class represent regular expressions
333 * constructed from sequences of character type @p _Ch_type.
334 *
335 * Storage for the regular expression is allocated and deallocated as
336 * necessary by the member functions of this class.
337 */
338 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type> >
339 class basic_regex
340 {
341 public:
342 // types:
343 typedef _Ch_type value_type;
344 typedef regex_constants::syntax_option_type flag_type;
345 typedef typename _Rx_traits::locale_type locale_type;
346 typedef typename _Rx_traits::string_type string_type;
347
348 /**
349 * @name Constants
350 * std [28.8.1](1)
351 */
352 //@{
353 static constexpr regex_constants::syntax_option_type icase
354 = regex_constants::icase;
355 static constexpr regex_constants::syntax_option_type nosubs
356 = regex_constants::nosubs;
357 static constexpr regex_constants::syntax_option_type optimize
358 = regex_constants::optimize;
359 static constexpr regex_constants::syntax_option_type collate
360 = regex_constants::collate;
361 static constexpr regex_constants::syntax_option_type ECMAScript
362 = regex_constants::ECMAScript;
363 static constexpr regex_constants::syntax_option_type basic
364 = regex_constants::basic;
365 static constexpr regex_constants::syntax_option_type extended
366 = regex_constants::extended;
367 static constexpr regex_constants::syntax_option_type awk
368 = regex_constants::awk;
369 static constexpr regex_constants::syntax_option_type grep
370 = regex_constants::grep;
371 static constexpr regex_constants::syntax_option_type egrep
372 = regex_constants::egrep;
373 //@}
374
375 // [7.8.2] construct/copy/destroy
376 /**
377 * Constructs a basic regular expression that does not match any
378 * character sequence.
379 */
380 basic_regex()
381 : _M_flags(regex_constants::ECMAScript),
382 _M_automaton(__regex::__compile<const _Ch_type*, _Rx_traits>(0, 0,
383 _M_traits, _M_flags))
384 { }
385
386 /**
387 * @brief Constructs a basic regular expression from the sequence
388 * [p, p + char_traits<_Ch_type>::length(p)) interpreted according to the
389 * flags in @p f.
390 *
391 * @param p A pointer to the start of a C-style null-terminated string
392 * containing a regular expression.
393 * @param f Flags indicating the syntax rules and options.
394 *
395 * @throws regex_error if @p p is not a valid regular expression.
396 */
397 explicit
398 basic_regex(const _Ch_type* __p,
399 flag_type __f = regex_constants::ECMAScript)
400 : _M_flags(__f),
401 _M_automaton(__regex::__compile(__p, __p + _Rx_traits::length(__p),
402 _M_traits, _M_flags))
403 { }
404
405 /**
406 * @brief Constructs a basic regular expression from the sequence
407 * [p, p + len) interpreted according to the flags in @p f.
408 *
409 * @param p A pointer to the start of a string containing a regular
410 * expression.
411 * @param len The length of the string containing the regular expression.
412 * @param f Flags indicating the syntax rules and options.
413 *
414 * @throws regex_error if @p p is not a valid regular expression.
415 */
416 basic_regex(const _Ch_type* __p, std::size_t __len, flag_type __f)
417 : _M_flags(__f),
418 _M_automaton(__regex::__compile(__p, __p + __len, _M_traits, _M_flags))
419 { }
420
421 /**
422 * @brief Copy-constructs a basic regular expression.
423 *
424 * @param rhs A @p regex object.
425 */
426 basic_regex(const basic_regex& __rhs)
427 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
428 _M_automaton(__rhs._M_automaton)
429 { }
430
431 /**
432 * @brief Move-constructs a basic regular expression.
433 *
434 * @param rhs A @p regex object.
435 */
436 basic_regex(const basic_regex&& __rhs)
437 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
438 _M_automaton(std::move(__rhs._M_automaton))
439 { }
440
441 /**
442 * @brief Constructs a basic regular expression from the string
443 * @p s interpreted according to the flags in @p f.
444 *
445 * @param s A string containing a regular expression.
446 * @param f Flags indicating the syntax rules and options.
447 *
448 * @throws regex_error if @p s is not a valid regular expression.
449 */
450 template<typename _Ch_traits, typename _Ch_alloc>
451 explicit
452 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
453 _Ch_alloc>& __s,
454 flag_type __f = regex_constants::ECMAScript)
455 : _M_flags(__f),
456 _M_automaton(__regex::__compile(__s.begin(), __s.end(),
457 _M_traits, _M_flags))
458 { }
459
460 /**
461 * @brief Constructs a basic regular expression from the range
462 * [first, last) interpreted according to the flags in @p f.
463 *
464 * @param first The start of a range containing a valid regular
465 * expression.
466 * @param last The end of a range containing a valid regular
467 * expression.
468 * @param f The format flags of the regular expression.
469 *
470 * @throws regex_error if @p [first, last) is not a valid regular
471 * expression.
472 */
473 template<typename _InputIterator>
474 basic_regex(_InputIterator __first, _InputIterator __last,
475 flag_type __f = regex_constants::ECMAScript)
476 : _M_flags(__f),
477 _M_automaton(__regex::__compile(__first, __last, _M_traits, _M_flags))
478 { }
479
480 /**
481 * @brief Constructs a basic regular expression from an initializer list.
482 *
483 * @param l The initializer list.
484 * @param f The format flags of the regular expression.
485 *
486 * @throws regex_error if @p l is not a valid regular expression.
487 */
488 basic_regex(initializer_list<_Ch_type> __l,
489 flag_type __f = regex_constants::ECMAScript)
490 : _M_flags(__f),
491 _M_automaton(__regex::__compile(__l.begin(), __l.end(),
492 _M_traits, _M_flags))
493 { }
494
495 /**
496 * @brief Destroys a basic regular expression.
497 */
498 ~basic_regex()
499 { }
500
501 /**
502 * @brief Assigns one regular expression to another.
503 */
504 basic_regex&
505 operator=(const basic_regex& __rhs)
506 { return this->assign(__rhs); }
507
508 /**
509 * @brief Move-assigns one regular expression to another.
510 */
511 basic_regex&
512 operator=(basic_regex&& __rhs)
513 { return this->assign(std::move(__rhs)); }
514
515 /**
516 * @brief Replaces a regular expression with a new one constructed from
517 * a C-style null-terminated string.
518 *
519 * @param A pointer to the start of a null-terminated C-style string
520 * containing a regular expression.
521 */
522 basic_regex&
523 operator=(const _Ch_type* __p)
524 { return this->assign(__p, flags()); }
525
526 /**
527 * @brief Replaces a regular expression with a new one constructed from
528 * a string.
529 *
530 * @param A pointer to a string containing a regular expression.
531 */
532 template<typename _Ch_typeraits, typename _Allocator>
533 basic_regex&
534 operator=(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s)
535 { return this->assign(__s, flags()); }
536
537 // [7.8.3] assign
538 /**
539 * @brief the real assignment operator.
540 *
541 * @param rhs Another regular expression object.
542 */
543 basic_regex&
544 assign(const basic_regex& __rhs)
545 {
546 basic_regex __tmp(__rhs);
547 this->swap(__tmp);
548 return *this;
549 }
550
551 /**
552 * @brief The move-assignment operator.
553 *
554 * @param rhs Another regular expression object.
555 */
556 basic_regex&
557 assign(basic_regex&& __rhs)
558 {
559 basic_regex __tmp(std::move(__rhs));
560 this->swap(__tmp);
561 return *this;
562 }
563
564 /**
565 * @brief Assigns a new regular expression to a regex object from a
566 * C-style null-terminated string containing a regular expression
567 * pattern.
568 *
569 * @param p A pointer to a C-style null-terminated string containing
570 * a regular expression pattern.
571 * @param flags Syntax option flags.
572 *
573 * @throws regex_error if p does not contain a valid regular expression
574 * pattern interpreted according to @p flags. If regex_error is thrown,
575 * *this remains unchanged.
576 */
577 basic_regex&
578 assign(const _Ch_type* __p,
579 flag_type __flags = regex_constants::ECMAScript)
580 { return this->assign(string_type(__p), __flags); }
581
582 /**
583 * @brief Assigns a new regular expression to a regex object from a
584 * C-style string containing a regular expression pattern.
585 *
586 * @param p A pointer to a C-style string containing a
587 * regular expression pattern.
588 * @param len The length of the regular expression pattern string.
589 * @param flags Syntax option flags.
590 *
591 * @throws regex_error if p does not contain a valid regular expression
592 * pattern interpreted according to @p flags. If regex_error is thrown,
593 * *this remains unchanged.
594 */
595 basic_regex&
596 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
597 { return this->assign(string_type(__p, __len), __flags); }
598
599 /**
600 * @brief Assigns a new regular expression to a regex object from a
601 * string containing a regular expression pattern.
602 *
603 * @param s A string containing a regular expression pattern.
604 * @param flags Syntax option flags.
605 *
606 * @throws regex_error if p does not contain a valid regular expression
607 * pattern interpreted according to @p flags. If regex_error is thrown,
608 * *this remains unchanged.
609 */
610 template<typename _Ch_typeraits, typename _Allocator>
611 basic_regex&
612 assign(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s,
613 flag_type __f = regex_constants::ECMAScript)
614 {
615 basic_regex __tmp(__s, __f);
616 this->swap(__tmp);
617 return *this;
618 }
619
620 /**
621 * @brief Assigns a new regular expression to a regex object.
622 *
623 * @param first The start of a range containing a valid regular
624 * expression.
625 * @param last The end of a range containing a valid regular
626 * expression.
627 * @param flags Syntax option flags.
628 *
629 * @throws regex_error if p does not contain a valid regular expression
630 * pattern interpreted according to @p flags. If regex_error is thrown,
631 * the object remains unchanged.
632 */
633 template<typename _InputIterator>
634 basic_regex&
635 assign(_InputIterator __first, _InputIterator __last,
636 flag_type __flags = regex_constants::ECMAScript)
637 { return this->assign(string_type(__first, __last), __flags); }
638
639 /**
640 * @brief Assigns a new regular expression to a regex object.
641 *
642 * @param l An initializer list representing a regular expression.
643 * @param flags Syntax option flags.
644 *
645 * @throws regex_error if @p l does not contain a valid regular
646 * expression pattern interpreted according to @p flags. If regex_error
647 * is thrown, the object remains unchanged.
648 */
649 basic_regex&
650 assign(initializer_list<_Ch_type> __l,
651 flag_type __f = regex_constants::ECMAScript)
652 { return this->assign(__l.begin(), __l.end(), __f); }
653
654 // [7.8.4] const operations
655 /**
656 * @brief Gets the number of marked subexpressions within the regular
657 * expression.
658 */
659 unsigned int
660 mark_count() const
661 { return _M_automaton->_M_sub_count() - 1; }
662
663 /**
664 * @brief Gets the flags used to construct the regular expression
665 * or in the last call to assign().
666 */
667 flag_type
668 flags() const
669 { return _M_flags; }
670
671 // [7.8.5] locale
672 /**
673 * @brief Imbues the regular expression object with the given locale.
674 *
675 * @param loc A locale.
676 */
677 locale_type
678 imbue(locale_type __loc)
679 { return _M_traits.imbue(__loc); }
680
681 /**
682 * @brief Gets the locale currently imbued in the regular expression
683 * object.
684 */
685 locale_type
686 getloc() const
687 { return _M_traits.getloc(); }
688
689 // [7.8.6] swap
690 /**
691 * @brief Swaps the contents of two regular expression objects.
692 *
693 * @param rhs Another regular expression object.
694 */
695 void
696 swap(basic_regex& __rhs)
697 {
698 std::swap(_M_flags, __rhs._M_flags);
699 std::swap(_M_traits, __rhs._M_traits);
700 std::swap(_M_automaton, __rhs._M_automaton);
701 }
702
703 #ifdef _GLIBCXX_DEBUG
704 void
705 _M_dot(std::ostream& __ostr)
706 { _M_automaton->_M_dot(__ostr); }
707 #endif
708
709 const __regex::_AutomatonPtr&
710 _M_get_automaton() const
711 { return _M_automaton; }
712
713 protected:
714 flag_type _M_flags;
715 _Rx_traits _M_traits;
716 __regex::_AutomatonPtr _M_automaton;
717 };
718
719 /** @brief Standard regular expressions. */
720 typedef basic_regex<char> regex;
721 #ifdef _GLIBCXX_USE_WCHAR_T
722 /** @brief Standard wide-character regular expressions. */
723 typedef basic_regex<wchar_t> wregex;
724 #endif
725
726
727 // [7.8.6] basic_regex swap
728 /**
729 * @brief Swaps the contents of two regular expression objects.
730 * @param lhs First regular expression.
731 * @param rhs Second regular expression.
732 */
733 template<typename _Ch_type, typename _Rx_traits>
734 inline void
735 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
736 basic_regex<_Ch_type, _Rx_traits>& __rhs)
737 { __lhs.swap(__rhs); }
738
739
740 // [7.9] Class template sub_match
741 /**
742 * A sequence of characters matched by a particular marked sub-expression.
743 *
744 * An object of this class is essentially a pair of iterators marking a
745 * matched subexpression within a regular expression pattern match. Such
746 * objects can be converted to and compared with std::basic_string objects
747 * of a similar base character type as the pattern matched by the regular
748 * expression.
749 *
750 * The iterators that make up the pair are the usual half-open interval
751 * referencing the actual original pattern matched.
752 */
753 template<typename _BiIter>
754 class sub_match : public std::pair<_BiIter, _BiIter>
755 {
756 public:
757 typedef typename iterator_traits<_BiIter>::value_type value_type;
758 typedef typename iterator_traits<_BiIter>::difference_type
759 difference_type;
760 typedef _BiIter iterator;
761 typedef std::basic_string<value_type> string_type;
762
763 public:
764 bool matched;
765
766 /**
767 * Gets the length of the matching sequence.
768 */
769 difference_type
770 length() const
771 { return this->matched ? std::distance(this->first, this->second) : 0; }
772
773 /**
774 * @brief Gets the matching sequence as a string.
775 *
776 * @returns the matching sequence as a string.
777 *
778 * This is the implicit conversion operator. It is identical to the
779 * str() member function except that it will want to pop up in
780 * unexpected places and cause a great deal of confusion and cursing
781 * from the unwary.
782 */
783 operator string_type() const
784 {
785 return this->matched
786 ? string_type(this->first, this->second)
787 : string_type();
788 }
789
790 /**
791 * @brief Gets the matching sequence as a string.
792 *
793 * @returns the matching sequence as a string.
794 */
795 string_type
796 str() const
797 {
798 return this->matched
799 ? string_type(this->first, this->second)
800 : string_type();
801 }
802
803 /**
804 * @brief Compares this and another matched sequence.
805 *
806 * @param s Another matched sequence to compare to this one.
807 *
808 * @retval <0 this matched sequence will collate before @p s.
809 * @retval =0 this matched sequence is equivalent to @p s.
810 * @retval <0 this matched sequence will collate after @p s.
811 */
812 int
813 compare(const sub_match& __s) const
814 { return this->str().compare(__s.str()); }
815
816 /**
817 * @brief Compares this sub_match to a string.
818 *
819 * @param s A string to compare to this sub_match.
820 *
821 * @retval <0 this matched sequence will collate before @p s.
822 * @retval =0 this matched sequence is equivalent to @p s.
823 * @retval <0 this matched sequence will collate after @p s.
824 */
825 int
826 compare(const string_type& __s) const
827 { return this->str().compare(__s); }
828
829 /**
830 * @brief Compares this sub_match to a C-style string.
831 *
832 * @param s A C-style string to compare to this sub_match.
833 *
834 * @retval <0 this matched sequence will collate before @p s.
835 * @retval =0 this matched sequence is equivalent to @p s.
836 * @retval <0 this matched sequence will collate after @p s.
837 */
838 int
839 compare(const value_type* __s) const
840 { return this->str().compare(__s); }
841 };
842
843
844 /** @brief Standard regex submatch over a C-style null-terminated string. */
845 typedef sub_match<const char*> csub_match;
846 /** @brief Standard regex submatch over a standard string. */
847 typedef sub_match<string::const_iterator> ssub_match;
848 #ifdef _GLIBCXX_USE_WCHAR_T
849 /** @brief Regex submatch over a C-style null-terminated wide string. */
850 typedef sub_match<const wchar_t*> wcsub_match;
851 /** @brief Regex submatch over a standard wide string. */
852 typedef sub_match<wstring::const_iterator> wssub_match;
853 #endif
854
855 // [7.9.2] sub_match non-member operators
856
857 /**
858 * @brief Tests the equivalence of two regular expression submatches.
859 * @param lhs First regular expression submatch.
860 * @param rhs Second regular expression submatch.
861 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
862 */
863 template<typename _BiIter>
864 inline bool
865 operator==(const sub_match<_BiIter>& __lhs,
866 const sub_match<_BiIter>& __rhs)
867 { return __lhs.compare(__rhs) == 0; }
868
869 /**
870 * @brief Tests the inequivalence of two regular expression submatches.
871 * @param lhs First regular expression submatch.
872 * @param rhs Second regular expression submatch.
873 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
874 */
875 template<typename _BiIter>
876 inline bool
877 operator!=(const sub_match<_BiIter>& __lhs,
878 const sub_match<_BiIter>& __rhs)
879 { return __lhs.compare(__rhs) != 0; }
880
881 /**
882 * @brief Tests the ordering of two regular expression submatches.
883 * @param lhs First regular expression submatch.
884 * @param rhs Second regular expression submatch.
885 * @returns true if @a lhs precedes @a rhs, false otherwise.
886 */
887 template<typename _BiIter>
888 inline bool
889 operator<(const sub_match<_BiIter>& __lhs,
890 const sub_match<_BiIter>& __rhs)
891 { return __lhs.compare(__rhs) < 0; }
892
893 /**
894 * @brief Tests the ordering of two regular expression submatches.
895 * @param lhs First regular expression submatch.
896 * @param rhs Second regular expression submatch.
897 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
898 */
899 template<typename _BiIter>
900 inline bool
901 operator<=(const sub_match<_BiIter>& __lhs,
902 const sub_match<_BiIter>& __rhs)
903 { return __lhs.compare(__rhs) <= 0; }
904
905 /**
906 * @brief Tests the ordering of two regular expression submatches.
907 * @param lhs First regular expression submatch.
908 * @param rhs Second regular expression submatch.
909 * @returns true if @a lhs does not precede @a rhs, false otherwise.
910 */
911 template<typename _BiIter>
912 inline bool
913 operator>=(const sub_match<_BiIter>& __lhs,
914 const sub_match<_BiIter>& __rhs)
915 { return __lhs.compare(__rhs) >= 0; }
916
917 /**
918 * @brief Tests the ordering of two regular expression submatches.
919 * @param lhs First regular expression submatch.
920 * @param rhs Second regular expression submatch.
921 * @returns true if @a lhs succeeds @a rhs, false otherwise.
922 */
923 template<typename _BiIter>
924 inline bool
925 operator>(const sub_match<_BiIter>& __lhs,
926 const sub_match<_BiIter>& __rhs)
927 { return __lhs.compare(__rhs) > 0; }
928
929 /**
930 * @brief Tests the equivalence of a string and a regular expression
931 * submatch.
932 * @param lhs A string.
933 * @param rhs A regular expression submatch.
934 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
935 */
936 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
937 inline bool
938 operator==(const basic_string<
939 typename iterator_traits<_Bi_iter>::value_type,
940 _Ch_traits, _Ch_alloc>& __lhs,
941 const sub_match<_Bi_iter>& __rhs)
942 { return __lhs == __rhs.str(); }
943
944 /**
945 * @brief Tests the inequivalence of a string and a regular expression
946 * submatch.
947 * @param lhs A string.
948 * @param rhs A regular expression submatch.
949 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
950 */
951 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
952 inline bool
953 operator!=(const basic_string<
954 typename iterator_traits<_Bi_iter>::value_type,
955 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
956 { return __lhs != __rhs.str(); }
957
958 /**
959 * @brief Tests the ordering of a string and a regular expression submatch.
960 * @param lhs A string.
961 * @param rhs A regular expression submatch.
962 * @returns true if @a lhs precedes @a rhs, false otherwise.
963 */
964 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
965 inline bool
966 operator<(const basic_string<
967 typename iterator_traits<_Bi_iter>::value_type,
968 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
969 { return __lhs < __rhs.str(); }
970
971 /**
972 * @brief Tests the ordering of a string and a regular expression submatch.
973 * @param lhs A string.
974 * @param rhs A regular expression submatch.
975 * @returns true if @a lhs succeeds @a rhs, false otherwise.
976 */
977 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
978 inline bool
979 operator>(const basic_string<
980 typename iterator_traits<_Bi_iter>::value_type,
981 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
982 { return __lhs > __rhs.str(); }
983
984 /**
985 * @brief Tests the ordering of a string and a regular expression submatch.
986 * @param lhs A string.
987 * @param rhs A regular expression submatch.
988 * @returns true if @a lhs does not precede @a rhs, false otherwise.
989 */
990 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
991 inline bool
992 operator>=(const basic_string<
993 typename iterator_traits<_Bi_iter>::value_type,
994 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
995 { return __lhs >= __rhs.str(); }
996
997 /**
998 * @brief Tests the ordering of a string and a regular expression submatch.
999 * @param lhs A string.
1000 * @param rhs A regular expression submatch.
1001 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1002 */
1003 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1004 inline bool
1005 operator<=(const basic_string<
1006 typename iterator_traits<_Bi_iter>::value_type,
1007 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1008 { return __lhs <= __rhs.str(); }
1009
1010 /**
1011 * @brief Tests the equivalence of a regular expression submatch and a
1012 * string.
1013 * @param lhs A regular expression submatch.
1014 * @param rhs A string.
1015 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1016 */
1017 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1018 inline bool
1019 operator==(const sub_match<_Bi_iter>& __lhs,
1020 const basic_string<
1021 typename iterator_traits<_Bi_iter>::value_type,
1022 _Ch_traits, _Ch_alloc>& __rhs)
1023 { return __lhs.str() == __rhs; }
1024
1025 /**
1026 * @brief Tests the inequivalence of a regular expression submatch and a
1027 * string.
1028 * @param lhs A regular expression submatch.
1029 * @param rhs A string.
1030 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1031 */
1032 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1033 inline bool
1034 operator!=(const sub_match<_Bi_iter>& __lhs,
1035 const basic_string<
1036 typename iterator_traits<_Bi_iter>::value_type,
1037 _Ch_traits, _Ch_alloc>& __rhs)
1038 { return __lhs.str() != __rhs; }
1039
1040 /**
1041 * @brief Tests the ordering of a regular expression submatch and a string.
1042 * @param lhs A regular expression submatch.
1043 * @param rhs A string.
1044 * @returns true if @a lhs precedes @a rhs, false otherwise.
1045 */
1046 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1047 inline bool
1048 operator<(const sub_match<_Bi_iter>& __lhs,
1049 const basic_string<
1050 typename iterator_traits<_Bi_iter>::value_type,
1051 _Ch_traits, _Ch_alloc>& __rhs)
1052 { return __lhs.str() < __rhs; }
1053
1054 /**
1055 * @brief Tests the ordering of a regular expression submatch and a string.
1056 * @param lhs A regular expression submatch.
1057 * @param rhs A string.
1058 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1059 */
1060 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1061 inline bool
1062 operator>(const sub_match<_Bi_iter>& __lhs,
1063 const basic_string<
1064 typename iterator_traits<_Bi_iter>::value_type,
1065 _Ch_traits, _Ch_alloc>& __rhs)
1066 { return __lhs.str() > __rhs; }
1067
1068 /**
1069 * @brief Tests the ordering of a regular expression submatch and a string.
1070 * @param lhs A regular expression submatch.
1071 * @param rhs A string.
1072 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1073 */
1074 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1075 inline bool
1076 operator>=(const sub_match<_Bi_iter>& __lhs,
1077 const basic_string<
1078 typename iterator_traits<_Bi_iter>::value_type,
1079 _Ch_traits, _Ch_alloc>& __rhs)
1080 { return __lhs.str() >= __rhs; }
1081
1082 /**
1083 * @brief Tests the ordering of a regular expression submatch and a string.
1084 * @param lhs A regular expression submatch.
1085 * @param rhs A string.
1086 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1087 */
1088 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1089 inline bool
1090 operator<=(const sub_match<_Bi_iter>& __lhs,
1091 const basic_string<
1092 typename iterator_traits<_Bi_iter>::value_type,
1093 _Ch_traits, _Ch_alloc>& __rhs)
1094 { return __lhs.str() <= __rhs; }
1095
1096 /**
1097 * @brief Tests the equivalence of a C string and a regular expression
1098 * submatch.
1099 * @param lhs A C string.
1100 * @param rhs A regular expression submatch.
1101 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1102 */
1103 template<typename _Bi_iter>
1104 inline bool
1105 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1106 const sub_match<_Bi_iter>& __rhs)
1107 { return __lhs == __rhs.str(); }
1108
1109 /**
1110 * @brief Tests the inequivalence of an iterator value and a regular
1111 * expression submatch.
1112 * @param lhs A regular expression submatch.
1113 * @param rhs A string.
1114 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1115 */
1116 template<typename _Bi_iter>
1117 inline bool
1118 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1119 const sub_match<_Bi_iter>& __rhs)
1120 { return __lhs != __rhs.str(); }
1121
1122 /**
1123 * @brief Tests the ordering of a string and a regular expression submatch.
1124 * @param lhs A string.
1125 * @param rhs A regular expression submatch.
1126 * @returns true if @a lhs precedes @a rhs, false otherwise.
1127 */
1128 template<typename _Bi_iter>
1129 inline bool
1130 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1131 const sub_match<_Bi_iter>& __rhs)
1132 { return __lhs < __rhs.str(); }
1133
1134 /**
1135 * @brief Tests the ordering of a string and a regular expression submatch.
1136 * @param lhs A string.
1137 * @param rhs A regular expression submatch.
1138 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1139 */
1140 template<typename _Bi_iter>
1141 inline bool
1142 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1143 const sub_match<_Bi_iter>& __rhs)
1144 { return __lhs > __rhs.str(); }
1145
1146 /**
1147 * @brief Tests the ordering of a string and a regular expression submatch.
1148 * @param lhs A string.
1149 * @param rhs A regular expression submatch.
1150 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1151 */
1152 template<typename _Bi_iter>
1153 inline bool
1154 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1155 const sub_match<_Bi_iter>& __rhs)
1156 { return __lhs >= __rhs.str(); }
1157
1158 /**
1159 * @brief Tests the ordering of a string and a regular expression submatch.
1160 * @param lhs A string.
1161 * @param rhs A regular expression submatch.
1162 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1163 */
1164 template<typename _Bi_iter>
1165 inline bool
1166 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1167 const sub_match<_Bi_iter>& __rhs)
1168 { return __lhs <= __rhs.str(); }
1169
1170 /**
1171 * @brief Tests the equivalence of a regular expression submatch and a
1172 * string.
1173 * @param lhs A regular expression submatch.
1174 * @param rhs A pointer to a string?
1175 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1176 */
1177 template<typename _Bi_iter>
1178 inline bool
1179 operator==(const sub_match<_Bi_iter>& __lhs,
1180 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1181 { return __lhs.str() == __rhs; }
1182
1183 /**
1184 * @brief Tests the inequivalence of a regular expression submatch and a
1185 * string.
1186 * @param lhs A regular expression submatch.
1187 * @param rhs A pointer to a string.
1188 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1189 */
1190 template<typename _Bi_iter>
1191 inline bool
1192 operator!=(const sub_match<_Bi_iter>& __lhs,
1193 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1194 { return __lhs.str() != __rhs; }
1195
1196 /**
1197 * @brief Tests the ordering of a regular expression submatch and a string.
1198 * @param lhs A regular expression submatch.
1199 * @param rhs A string.
1200 * @returns true if @a lhs precedes @a rhs, false otherwise.
1201 */
1202 template<typename _Bi_iter>
1203 inline bool
1204 operator<(const sub_match<_Bi_iter>& __lhs,
1205 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1206 { return __lhs.str() < __rhs; }
1207
1208 /**
1209 * @brief Tests the ordering of a regular expression submatch and a string.
1210 * @param lhs A regular expression submatch.
1211 * @param rhs A string.
1212 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1213 */
1214 template<typename _Bi_iter>
1215 inline bool
1216 operator>(const sub_match<_Bi_iter>& __lhs,
1217 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1218 { return __lhs.str() > __rhs; }
1219
1220 /**
1221 * @brief Tests the ordering of a regular expression submatch and a string.
1222 * @param lhs A regular expression submatch.
1223 * @param rhs A string.
1224 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1225 */
1226 template<typename _Bi_iter>
1227 inline bool
1228 operator>=(const sub_match<_Bi_iter>& __lhs,
1229 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1230 { return __lhs.str() >= __rhs; }
1231
1232 /**
1233 * @brief Tests the ordering of a regular expression submatch and a string.
1234 * @param lhs A regular expression submatch.
1235 * @param rhs A string.
1236 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1237 */
1238 template<typename _Bi_iter>
1239 inline bool
1240 operator<=(const sub_match<_Bi_iter>& __lhs,
1241 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1242 { return __lhs.str() <= __rhs; }
1243
1244 /**
1245 * @brief Tests the equivalence of a string and a regular expression
1246 * submatch.
1247 * @param lhs A string.
1248 * @param rhs A regular expression submatch.
1249 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1250 */
1251 template<typename _Bi_iter>
1252 inline bool
1253 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1254 const sub_match<_Bi_iter>& __rhs)
1255 { return __lhs == __rhs.str(); }
1256
1257 /**
1258 * @brief Tests the inequivalence of a string and a regular expression
1259 * submatch.
1260 * @param lhs A string.
1261 * @param rhs A regular expression submatch.
1262 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1263 */
1264 template<typename _Bi_iter>
1265 inline bool
1266 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1267 const sub_match<_Bi_iter>& __rhs)
1268 { return __lhs != __rhs.str(); }
1269
1270 /**
1271 * @brief Tests the ordering of a string and a regular expression submatch.
1272 * @param lhs A string.
1273 * @param rhs A regular expression submatch.
1274 * @returns true if @a lhs precedes @a rhs, false otherwise.
1275 */
1276 template<typename _Bi_iter>
1277 inline bool
1278 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1279 const sub_match<_Bi_iter>& __rhs)
1280 { return __lhs < __rhs.str(); }
1281
1282 /**
1283 * @brief Tests the ordering of a string and a regular expression submatch.
1284 * @param lhs A string.
1285 * @param rhs A regular expression submatch.
1286 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1287 */
1288 template<typename _Bi_iter>
1289 inline bool
1290 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1291 const sub_match<_Bi_iter>& __rhs)
1292 { return __lhs > __rhs.str(); }
1293
1294 /**
1295 * @brief Tests the ordering of a string and a regular expression submatch.
1296 * @param lhs A string.
1297 * @param rhs A regular expression submatch.
1298 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1299 */
1300 template<typename _Bi_iter>
1301 inline bool
1302 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1303 const sub_match<_Bi_iter>& __rhs)
1304 { return __lhs >= __rhs.str(); }
1305
1306 /**
1307 * @brief Tests the ordering of a string and a regular expression submatch.
1308 * @param lhs A string.
1309 * @param rhs A regular expression submatch.
1310 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1311 */
1312 template<typename _Bi_iter>
1313 inline bool
1314 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1315 const sub_match<_Bi_iter>& __rhs)
1316 { return __lhs <= __rhs.str(); }
1317
1318 /**
1319 * @brief Tests the equivalence of a regular expression submatch and a
1320 * string.
1321 * @param lhs A regular expression submatch.
1322 * @param rhs A const string reference.
1323 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1324 */
1325 template<typename _Bi_iter>
1326 inline bool
1327 operator==(const sub_match<_Bi_iter>& __lhs,
1328 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1329 { return __lhs.str() == __rhs; }
1330
1331 /**
1332 * @brief Tests the inequivalence of a regular expression submatch and a
1333 * string.
1334 * @param lhs A regular expression submatch.
1335 * @param rhs A const string reference.
1336 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1337 */
1338 template<typename _Bi_iter>
1339 inline bool
1340 operator!=(const sub_match<_Bi_iter>& __lhs,
1341 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1342 { return __lhs.str() != __rhs; }
1343
1344 /**
1345 * @brief Tests the ordering of a regular expression submatch and a string.
1346 * @param lhs A regular expression submatch.
1347 * @param rhs A const string reference.
1348 * @returns true if @a lhs precedes @a rhs, false otherwise.
1349 */
1350 template<typename _Bi_iter>
1351 inline bool
1352 operator<(const sub_match<_Bi_iter>& __lhs,
1353 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1354 { return __lhs.str() < __rhs; }
1355
1356 /**
1357 * @brief Tests the ordering of a regular expression submatch and a string.
1358 * @param lhs A regular expression submatch.
1359 * @param rhs A const string reference.
1360 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1361 */
1362 template<typename _Bi_iter>
1363 inline bool
1364 operator>(const sub_match<_Bi_iter>& __lhs,
1365 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1366 { return __lhs.str() > __rhs; }
1367
1368 /**
1369 * @brief Tests the ordering of a regular expression submatch and a string.
1370 * @param lhs A regular expression submatch.
1371 * @param rhs A const string reference.
1372 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1373 */
1374 template<typename _Bi_iter>
1375 inline bool
1376 operator>=(const sub_match<_Bi_iter>& __lhs,
1377 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1378 { return __lhs.str() >= __rhs; }
1379
1380 /**
1381 * @brief Tests the ordering of a regular expression submatch and a string.
1382 * @param lhs A regular expression submatch.
1383 * @param rhs A const string reference.
1384 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1385 */
1386 template<typename _Bi_iter>
1387 inline bool
1388 operator<=(const sub_match<_Bi_iter>& __lhs,
1389 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1390 { return __lhs.str() <= __rhs; }
1391
1392 /**
1393 * @brief Inserts a matched string into an output stream.
1394 *
1395 * @param os The output stream.
1396 * @param m A submatch string.
1397 *
1398 * @returns the output stream with the submatch string inserted.
1399 */
1400 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1401 inline
1402 basic_ostream<_Ch_type, _Ch_traits>&
1403 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1404 const sub_match<_Bi_iter>& __m)
1405 { return __os << __m.str(); }
1406
1407 // [7.10] Class template match_results
1408
1409 /*
1410 * Special sub_match object representing an unmatched sub-expression.
1411 */
1412 template<typename _Bi_iter>
1413 inline const sub_match<_Bi_iter>&
1414 __unmatched_sub()
1415 {
1416 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1417 return __unmatched;
1418 }
1419
1420 /**
1421 * @brief The results of a match or search operation.
1422 *
1423 * A collection of character sequences representing the result of a regular
1424 * expression match. Storage for the collection is allocated and freed as
1425 * necessary by the member functions of class template match_results.
1426 *
1427 * This class satisfies the Sequence requirements, with the exception that
1428 * only the operations defined for a const-qualified Sequence are supported.
1429 *
1430 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1431 * the whole match. In this case the %sub_match member matched is always true.
1432 * The sub_match object stored at index n denotes what matched the marked
1433 * sub-expression n within the matched expression. If the sub-expression n
1434 * participated in a regular expression match then the %sub_match member
1435 * matched evaluates to true, and members first and second denote the range
1436 * of characters [first, second) which formed that match. Otherwise matched
1437 * is false, and members first and second point to the end of the sequence
1438 * that was searched.
1439 *
1440 * @nosubgrouping
1441 */
1442 template<typename _Bi_iter,
1443 typename _Allocator = allocator<sub_match<_Bi_iter> > >
1444 class match_results
1445 : private std::vector<std::sub_match<_Bi_iter>, _Allocator>
1446 {
1447 private:
1448 /*
1449 * The vector base is empty if this does not represent a successful match.
1450 * Otherwise it contains n+3 elements where n is the number of marked
1451 * sub-expressions:
1452 * [0] entire match
1453 * [1] 1st marked subexpression
1454 * ...
1455 * [n] nth marked subexpression
1456 * [n+1] prefix
1457 * [n+2] suffix
1458 */
1459 typedef std::vector<std::sub_match<_Bi_iter>, _Allocator>
1460 _Base_type;
1461
1462 public:
1463 /**
1464 * @name 10.? Public Types
1465 */
1466 //@{
1467 typedef sub_match<_Bi_iter> value_type;
1468 typedef const value_type& const_reference;
1469 typedef const_reference reference;
1470 typedef typename _Base_type::const_iterator const_iterator;
1471 typedef const_iterator iterator;
1472 typedef typename std::iterator_traits<_Bi_iter>::difference_type
1473 difference_type;
1474 /* TODO: needs allocator_traits */
1475 typedef typename _Allocator::size_type size_type;
1476 typedef _Allocator allocator_type;
1477 typedef typename std::iterator_traits<_Bi_iter>::value_type
1478 char_type;
1479 typedef std::basic_string<char_type> string_type;
1480 //@}
1481
1482 public:
1483 /**
1484 * @name 10.1 Construction, Copying, and Destruction
1485 */
1486 //@{
1487
1488 /**
1489 * @brief Constructs a default %match_results container.
1490 * @post size() returns 0 and str() returns an empty string.
1491 */
1492 explicit
1493 match_results(const _Allocator& __a = _Allocator())
1494 : _Base_type(__a)
1495 { }
1496
1497 /**
1498 * @brief Copy constructs a %match_results.
1499 */
1500 match_results(const match_results& __rhs)
1501 : _Base_type(__rhs)
1502 { }
1503
1504 /**
1505 * @brief Assigns rhs to *this.
1506 */
1507 match_results&
1508 operator=(const match_results __rhs)
1509 {
1510 match_results(__rhs).swap(*this);
1511 return *this;
1512 }
1513
1514 /**
1515 * @brief Destroys a %match_results object.
1516 */
1517 ~match_results()
1518 { }
1519
1520 //@}
1521
1522 /**
1523 * @name 10.2 Size
1524 */
1525 //@{
1526
1527 /**
1528 * @brief Gets the number of matches and submatches.
1529 *
1530 * The number of matches for a given regular expression will be either 0
1531 * if there was no match or mark_count() + 1 if a match was successful.
1532 * Some matches may be empty.
1533 *
1534 * @returns the number of matches found.
1535 */
1536 size_type
1537 size() const
1538 {
1539 size_type __size = _Base_type::size();
1540 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1541 }
1542
1543 size_type
1544 max_size() const
1545 { return _Base_type::max_size(); }
1546
1547 /**
1548 * @brief Indicates if the %match_results contains no results.
1549 * @retval true The %match_results object is empty.
1550 * @retval false The %match_results object is not empty.
1551 */
1552 bool
1553 empty() const
1554 { return _Base_type::empty(); }
1555
1556 //@}
1557
1558 /**
1559 * @name 10.3 Element Access
1560 */
1561 //@{
1562
1563 /**
1564 * @brief Gets the length of the indicated submatch.
1565 * @param sub indicates the submatch.
1566 *
1567 * This function returns the length of the indicated submatch, or the
1568 * length of the entire match if @p sub is zero (the default).
1569 */
1570 difference_type
1571 length(size_type __sub = 0) const
1572 { return this[__sub].length(); }
1573
1574 /**
1575 * @brief Gets the offset of the beginning of the indicated submatch.
1576 * @param sub indicates the submatch.
1577 *
1578 * This function returns the offset from the beginning of the target
1579 * sequence to the beginning of the submatch, unless the value of @p sub
1580 * is zero (the default), in which case this function returns the offset
1581 * from the beginning of the target sequence to the beginning of the
1582 * match.
1583 *
1584 * Returns -1 if @p sub is out of range.
1585 */
1586 difference_type
1587 position(size_type __sub = 0) const
1588 {
1589 return __sub < size() ? std::distance(this->prefix().first,
1590 (*this)[__sub].first) : -1;
1591 }
1592
1593 /**
1594 * @brief Gets the match or submatch converted to a string type.
1595 * @param sub indicates the submatch.
1596 *
1597 * This function gets the submatch (or match, if @p sub is zero) extracted
1598 * from the target range and converted to the associated string type.
1599 */
1600 string_type
1601 str(size_type __sub = 0) const
1602 { return (*this)[__sub].str(); }
1603
1604 /**
1605 * @brief Gets a %sub_match reference for the match or submatch.
1606 * @param sub indicates the submatch.
1607 *
1608 * This function gets a reference to the indicated submatch, or the entire
1609 * match if @p sub is zero.
1610 *
1611 * If @p sub >= size() then this function returns a %sub_match with a
1612 * special value indicating no submatch.
1613 */
1614 const_reference
1615 operator[](size_type __sub) const
1616 {
1617 return __sub < size()
1618 ? _Base_type::operator[](__sub)
1619 : __unmatched_sub<_Bi_iter>();
1620 }
1621
1622 /**
1623 * @brief Gets a %sub_match representing the match prefix.
1624 *
1625 * This function gets a reference to a %sub_match object representing the
1626 * part of the target range between the start of the target range and the
1627 * start of the match.
1628 */
1629 const_reference
1630 prefix() const
1631 {
1632 return !empty()
1633 ? _Base_type::operator[](_Base_type::size() - 2)
1634 : __unmatched_sub<_Bi_iter>();
1635 }
1636
1637 /**
1638 * @brief Gets a %sub_match representing the match suffix.
1639 *
1640 * This function gets a reference to a %sub_match object representing the
1641 * part of the target range between the end of the match and the end of
1642 * the target range.
1643 */
1644 const_reference
1645 suffix() const
1646 {
1647 return !empty()
1648 ? _Base_type::operator[](_Base_type::size() - 1)
1649 : __unmatched_sub<_Bi_iter>();
1650 }
1651
1652 /**
1653 * @brief Gets an iterator to the start of the %sub_match collection.
1654 */
1655 const_iterator
1656 begin() const
1657 { return _Base_type::begin(); }
1658
1659 /**
1660 * @brief Gets an iterator to the start of the %sub_match collection.
1661 */
1662 const_iterator
1663 cbegin() const
1664 { return _Base_type::cbegin(); }
1665
1666 /**
1667 * @brief Gets an iterator to one-past-the-end of the collection.
1668 */
1669 const_iterator
1670 end() const
1671 {
1672 return !empty()
1673 ? _Base_type::end() - 2
1674 : _Base_type::end();
1675 }
1676
1677 /**
1678 * @brief Gets an iterator to one-past-the-end of the collection.
1679 */
1680 const_iterator
1681 cend() const
1682 {
1683 return !empty()
1684 ? _Base_type::cend() - 2
1685 : _Base_type::cend();
1686 }
1687
1688 //@}
1689
1690 /**
1691 * @name 10.4 Formatting
1692 *
1693 * These functions perform formatted substitution of the matched character
1694 * sequences into their target. The format specifiers and escape sequences
1695 * accepted by these functions are determined by their @p flags parameter
1696 * as documented above.
1697 */
1698 //@{
1699
1700 /**
1701 * @todo Implement this function.
1702 */
1703 template<typename _Out_iter>
1704 _Out_iter
1705 format(_Out_iter __out, const string_type& __fmt,
1706 regex_constants::match_flag_type __flags
1707 = regex_constants::format_default) const
1708 { return __out; }
1709
1710 /**
1711 * @todo Implement this function.
1712 */
1713 string_type
1714 format(const string_type& __fmt,
1715 regex_constants::match_flag_type __flags
1716 = regex_constants::format_default) const;
1717
1718 //@}
1719
1720 /**
1721 * @name 10.5 Allocator
1722 */
1723 //@{
1724
1725 /**
1726 * @brief Gets a copy of the allocator.
1727 */
1728 allocator_type
1729 get_allocator() const
1730 { return _Base_type::get_allocator(); }
1731
1732 //@}
1733
1734 /**
1735 * @name 10.6 Swap
1736 */
1737 //@{
1738
1739 /**
1740 * @brief Swaps the contents of two match_results.
1741 */
1742 void
1743 swap(match_results& __that)
1744 { _Base_type::swap(__that); }
1745 //@}
1746
1747 private:
1748 friend class __regex::_SpecializedResults<_Bi_iter, _Allocator>;
1749 };
1750
1751 typedef match_results<const char*> cmatch;
1752 typedef match_results<string::const_iterator> smatch;
1753 #ifdef _GLIBCXX_USE_WCHAR_T
1754 typedef match_results<const wchar_t*> wcmatch;
1755 typedef match_results<wstring::const_iterator> wsmatch;
1756 #endif
1757
1758 // match_results comparisons
1759 /**
1760 * @brief Compares two match_results for equality.
1761 * @returns true if the two objects refer to the same match,
1762 * false otherwise.
1763 * @todo Implement this function.
1764 */
1765 template<typename _Bi_iter, typename _Allocator>
1766 inline bool
1767 operator==(const match_results<_Bi_iter, _Allocator>& __m1,
1768 const match_results<_Bi_iter, _Allocator>& __m2);
1769
1770 /**
1771 * @brief Compares two match_results for inequality.
1772 * @returns true if the two objects do not refer to the same match,
1773 * false otherwise.
1774 */
1775 template<typename _Bi_iter, class _Allocator>
1776 inline bool
1777 operator!=(const match_results<_Bi_iter, _Allocator>& __m1,
1778 const match_results<_Bi_iter, _Allocator>& __m2)
1779 { return !(__m1 == __m2); }
1780
1781 // [7.10.6] match_results swap
1782 /**
1783 * @brief Swaps two match results.
1784 * @param lhs A match result.
1785 * @param rhs A match result.
1786 *
1787 * The contents of the two match_results objects are swapped.
1788 */
1789 template<typename _Bi_iter, typename _Allocator>
1790 inline void
1791 swap(match_results<_Bi_iter, _Allocator>& __lhs,
1792 match_results<_Bi_iter, _Allocator>& __rhs)
1793 { __lhs.swap(__rhs); }
1794
1795 // [7.11.2] Function template regex_match
1796 /**
1797 * @name Matching, Searching, and Replacing
1798 */
1799 //@{
1800
1801 /**
1802 * @brief Determines if there is a match between the regular expression @p e
1803 * and all of the character sequence [first, last).
1804 *
1805 * @param s Start of the character sequence to match.
1806 * @param e One-past-the-end of the character sequence to match.
1807 * @param m The match results.
1808 * @param re The regular expression.
1809 * @param flags Controls how the regular expression is matched.
1810 *
1811 * @retval true A match exists.
1812 * @retval false Otherwise.
1813 *
1814 * @throws an exception of type regex_error.
1815 *
1816 * @todo Implement this function.
1817 */
1818 template<typename _Bi_iter, typename _Allocator,
1819 typename _Ch_type, typename _Rx_traits>
1820 bool
1821 regex_match(_Bi_iter __s,
1822 _Bi_iter __e,
1823 match_results<_Bi_iter, _Allocator>& __m,
1824 const basic_regex<_Ch_type, _Rx_traits>& __re,
1825 regex_constants::match_flag_type __flags
1826 = regex_constants::match_default)
1827 {
1828 __regex::_AutomatonPtr __a = __re._M_get_automaton();
1829 __regex::_Automaton::_SizeT __sz = __a->_M_sub_count();
1830 __regex::_SpecializedCursor<_Bi_iter> __cs(__s, __e);
1831 __regex::_SpecializedResults<_Bi_iter, _Allocator> __r(__sz, __cs, __m);
1832 __regex::_Grep_matcher __matcher(__cs, __r, __a, __flags);
1833 return __m[0].matched;
1834 }
1835
1836 /**
1837 * @brief Indicates if there is a match between the regular expression @p e
1838 * and all of the character sequence [first, last).
1839 *
1840 * @param first Beginning of the character sequence to match.
1841 * @param last One-past-the-end of the character sequence to match.
1842 * @param re The regular expression.
1843 * @param flags Controls how the regular expression is matched.
1844 *
1845 * @retval true A match exists.
1846 * @retval false Otherwise.
1847 *
1848 * @throws an exception of type regex_error.
1849 */
1850 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1851 bool
1852 regex_match(_Bi_iter __first, _Bi_iter __last,
1853 const basic_regex<_Ch_type, _Rx_traits>& __re,
1854 regex_constants::match_flag_type __flags
1855 = regex_constants::match_default)
1856 {
1857 match_results<_Bi_iter> __what;
1858 return regex_match(__first, __last, __what, __re, __flags);
1859 }
1860
1861 /**
1862 * @brief Determines if there is a match between the regular expression @p e
1863 * and a C-style null-terminated string.
1864 *
1865 * @param s The C-style null-terminated string to match.
1866 * @param m The match results.
1867 * @param re The regular expression.
1868 * @param f Controls how the regular expression is matched.
1869 *
1870 * @retval true A match exists.
1871 * @retval false Otherwise.
1872 *
1873 * @throws an exception of type regex_error.
1874 */
1875 template<typename _Ch_type, typename _Allocator, typename _Rx_traits>
1876 inline bool
1877 regex_match(const _Ch_type* __s,
1878 match_results<const _Ch_type*, _Allocator>& __m,
1879 const basic_regex<_Ch_type, _Rx_traits>& __re,
1880 regex_constants::match_flag_type __f
1881 = regex_constants::match_default)
1882 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
1883
1884 /**
1885 * @brief Determines if there is a match between the regular expression @p e
1886 * and a string.
1887 *
1888 * @param s The string to match.
1889 * @param m The match results.
1890 * @param re The regular expression.
1891 * @param flags Controls how the regular expression is matched.
1892 *
1893 * @retval true A match exists.
1894 * @retval false Otherwise.
1895 *
1896 * @throws an exception of type regex_error.
1897 */
1898 template<typename _Ch_traits, typename _Ch_alloc,
1899 typename _Allocator, typename _Ch_type, typename _Rx_traits>
1900 inline bool
1901 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
1902 match_results<typename basic_string<_Ch_type,
1903 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
1904 const basic_regex<_Ch_type, _Rx_traits>& __re,
1905 regex_constants::match_flag_type __flags
1906 = regex_constants::match_default)
1907 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
1908
1909 /**
1910 * @brief Indicates if there is a match between the regular expression @p e
1911 * and a C-style null-terminated string.
1912 *
1913 * @param s The C-style null-terminated string to match.
1914 * @param re The regular expression.
1915 * @param f Controls how the regular expression is matched.
1916 *
1917 * @retval true A match exists.
1918 * @retval false Otherwise.
1919 *
1920 * @throws an exception of type regex_error.
1921 */
1922 template<typename _Ch_type, class _Rx_traits>
1923 inline bool
1924 regex_match(const _Ch_type* __s,
1925 const basic_regex<_Ch_type, _Rx_traits>& __re,
1926 regex_constants::match_flag_type __f
1927 = regex_constants::match_default)
1928 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
1929
1930 /**
1931 * @brief Indicates if there is a match between the regular expression @p e
1932 * and a string.
1933 *
1934 * @param s [IN] The string to match.
1935 * @param re [IN] The regular expression.
1936 * @param flags [IN] Controls how the regular expression is matched.
1937 *
1938 * @retval true A match exists.
1939 * @retval false Otherwise.
1940 *
1941 * @throws an exception of type regex_error.
1942 */
1943 template<typename _Ch_traits, typename _Str_allocator,
1944 typename _Ch_type, typename _Rx_traits>
1945 inline bool
1946 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
1947 const basic_regex<_Ch_type, _Rx_traits>& __re,
1948 regex_constants::match_flag_type __flags
1949 = regex_constants::match_default)
1950 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
1951
1952 // [7.11.3] Function template regex_search
1953 /**
1954 * Searches for a regular expression within a range.
1955 * @param first [IN] The start of the string to search.
1956 * @param last [IN] One-past-the-end of the string to search.
1957 * @param m [OUT] The match results.
1958 * @param re [IN] The regular expression to search for.
1959 * @param flags [IN] Search policy flags.
1960 * @retval true A match was found within the string.
1961 * @retval false No match was found within the string, the content of %m is
1962 * undefined.
1963 *
1964 * @throws an exception of type regex_error.
1965 *
1966 * @todo Implement this function.
1967 */
1968 template<typename _Bi_iter, typename _Allocator,
1969 typename _Ch_type, typename _Rx_traits>
1970 inline bool
1971 regex_search(_Bi_iter __first, _Bi_iter __last,
1972 match_results<_Bi_iter, _Allocator>& __m,
1973 const basic_regex<_Ch_type, _Rx_traits>& __re,
1974 regex_constants::match_flag_type __flags
1975 = regex_constants::match_default)
1976 { return false; }
1977
1978 /**
1979 * Searches for a regular expression within a range.
1980 * @param first [IN] The start of the string to search.
1981 * @param last [IN] One-past-the-end of the string to search.
1982 * @param re [IN] The regular expression to search for.
1983 * @param flags [IN] Search policy flags.
1984 * @retval true A match was found within the string.
1985 * @retval false No match was found within the string.
1986 * @doctodo
1987 *
1988 * @throws an exception of type regex_error.
1989 */
1990 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1991 inline bool
1992 regex_search(_Bi_iter __first, _Bi_iter __last,
1993 const basic_regex<_Ch_type, _Rx_traits>& __re,
1994 regex_constants::match_flag_type __flags
1995 = regex_constants::match_default)
1996 {
1997 match_results<_Bi_iter> __what;
1998 return regex_search(__first, __last, __what, __re, __flags);
1999 }
2000
2001 /**
2002 * @brief Searches for a regular expression within a C-string.
2003 * @param s [IN] A C-string to search for the regex.
2004 * @param m [OUT] The set of regex matches.
2005 * @param e [IN] The regex to search for in @p s.
2006 * @param f [IN] The search flags.
2007 * @retval true A match was found within the string.
2008 * @retval false No match was found within the string, the content of %m is
2009 * undefined.
2010 * @doctodo
2011 *
2012 * @throws an exception of type regex_error.
2013 */
2014 template<typename _Ch_type, class _Allocator, class _Rx_traits>
2015 inline bool
2016 regex_search(const _Ch_type* __s,
2017 match_results<const _Ch_type*, _Allocator>& __m,
2018 const basic_regex<_Ch_type, _Rx_traits>& __e,
2019 regex_constants::match_flag_type __f
2020 = regex_constants::match_default)
2021 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2022
2023 /**
2024 * @brief Searches for a regular expression within a C-string.
2025 * @param s [IN] The C-string to search.
2026 * @param e [IN] The regular expression to search for.
2027 * @param f [IN] Search policy flags.
2028 * @retval true A match was found within the string.
2029 * @retval false No match was found within the string.
2030 * @doctodo
2031 *
2032 * @throws an exception of type regex_error.
2033 */
2034 template<typename _Ch_type, typename _Rx_traits>
2035 inline bool
2036 regex_search(const _Ch_type* __s,
2037 const basic_regex<_Ch_type, _Rx_traits>& __e,
2038 regex_constants::match_flag_type __f
2039 = regex_constants::match_default)
2040 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2041
2042 /**
2043 * @brief Searches for a regular expression within a string.
2044 * @param s [IN] The string to search.
2045 * @param e [IN] The regular expression to search for.
2046 * @param flags [IN] Search policy flags.
2047 * @retval true A match was found within the string.
2048 * @retval false No match was found within the string.
2049 * @doctodo
2050 *
2051 * @throws an exception of type regex_error.
2052 */
2053 template<typename _Ch_traits, typename _String_allocator,
2054 typename _Ch_type, typename _Rx_traits>
2055 inline bool
2056 regex_search(const basic_string<_Ch_type, _Ch_traits,
2057 _String_allocator>& __s,
2058 const basic_regex<_Ch_type, _Rx_traits>& __e,
2059 regex_constants::match_flag_type __flags
2060 = regex_constants::match_default)
2061 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2062
2063 /**
2064 * @brief Searches for a regular expression within a string.
2065 * @param s [IN] A C++ string to search for the regex.
2066 * @param m [OUT] The set of regex matches.
2067 * @param e [IN] The regex to search for in @p s.
2068 * @param f [IN] The search flags.
2069 * @retval true A match was found within the string.
2070 * @retval false No match was found within the string, the content of %m is
2071 * undefined.
2072 *
2073 * @throws an exception of type regex_error.
2074 */
2075 template<typename _Ch_traits, typename _Ch_alloc,
2076 typename _Allocator, typename _Ch_type,
2077 typename _Rx_traits>
2078 inline bool
2079 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2080 match_results<typename basic_string<_Ch_type,
2081 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
2082 const basic_regex<_Ch_type, _Rx_traits>& __e,
2083 regex_constants::match_flag_type __f
2084 = regex_constants::match_default)
2085 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2086
2087 // std [28.11.4] Function template regex_replace
2088 /**
2089 * @doctodo
2090 * @param out
2091 * @param first
2092 * @param last
2093 * @param e
2094 * @param fmt
2095 * @param flags
2096 *
2097 * @returns out
2098 * @throws an exception of type regex_error.
2099 *
2100 * @todo Implement this function.
2101 */
2102 template<typename _Out_iter, typename _Bi_iter,
2103 typename _Rx_traits, typename _Ch_type>
2104 inline _Out_iter
2105 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2106 const basic_regex<_Ch_type, _Rx_traits>& __e,
2107 const basic_string<_Ch_type>& __fmt,
2108 regex_constants::match_flag_type __flags
2109 = regex_constants::match_default)
2110 { return __out; }
2111
2112 /**
2113 * @doctodo
2114 * @param s
2115 * @param e
2116 * @param fmt
2117 * @param flags
2118 *
2119 * @returns a copy of string @p s with replacements.
2120 *
2121 * @throws an exception of type regex_error.
2122 */
2123 template<typename _Rx_traits, typename _Ch_type>
2124 inline basic_string<_Ch_type>
2125 regex_replace(const basic_string<_Ch_type>& __s,
2126 const basic_regex<_Ch_type, _Rx_traits>& __e,
2127 const basic_string<_Ch_type>& __fmt,
2128 regex_constants::match_flag_type __flags
2129 = regex_constants::match_default)
2130 {
2131 std::string __result;
2132 regex_replace(std::back_inserter(__result),
2133 __s.begin(), __s.end(), __e, __fmt, __flags);
2134 return __result;
2135 }
2136
2137 //@}
2138
2139 // std [28.12] Class template regex_iterator
2140 /**
2141 * An iterator adaptor that will provide repeated calls of regex_search over
2142 * a range until no more matches remain.
2143 */
2144 template<typename _Bi_iter,
2145 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2146 typename _Rx_traits = regex_traits<_Ch_type> >
2147 class regex_iterator
2148 {
2149 public:
2150 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2151 typedef match_results<_Bi_iter> value_type;
2152 typedef std::ptrdiff_t difference_type;
2153 typedef const value_type* pointer;
2154 typedef const value_type& reference;
2155 typedef std::forward_iterator_tag iterator_category;
2156
2157 public:
2158 /**
2159 * @brief Provides a singular iterator, useful for indicating
2160 * one-past-the-end of a range.
2161 * @todo Implement this function.
2162 * @doctodo
2163 */
2164 regex_iterator();
2165
2166 /**
2167 * Constructs a %regex_iterator...
2168 * @param a [IN] The start of a text range to search.
2169 * @param b [IN] One-past-the-end of the text range to search.
2170 * @param re [IN] The regular expression to match.
2171 * @param m [IN] Policy flags for match rules.
2172 * @todo Implement this function.
2173 * @doctodo
2174 */
2175 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2176 regex_constants::match_flag_type __m
2177 = regex_constants::match_default);
2178
2179 /**
2180 * Copy constructs a %regex_iterator.
2181 * @todo Implement this function.
2182 * @doctodo
2183 */
2184 regex_iterator(const regex_iterator& __rhs);
2185
2186 /**
2187 * @todo Implement this function.
2188 * @doctodo
2189 */
2190 regex_iterator&
2191 operator=(const regex_iterator& __rhs);
2192
2193 /**
2194 * @todo Implement this function.
2195 * @doctodo
2196 */
2197 bool
2198 operator==(const regex_iterator& __rhs);
2199
2200 /**
2201 * @todo Implement this function.
2202 * @doctodo
2203 */
2204 bool
2205 operator!=(const regex_iterator& __rhs);
2206
2207 /**
2208 * @todo Implement this function.
2209 * @doctodo
2210 */
2211 const value_type&
2212 operator*();
2213
2214 /**
2215 * @todo Implement this function.
2216 * @doctodo
2217 */
2218 const value_type*
2219 operator->();
2220
2221 /**
2222 * @todo Implement this function.
2223 * @doctodo
2224 */
2225 regex_iterator&
2226 operator++();
2227
2228 /**
2229 * @todo Implement this function.
2230 * @doctodo
2231 */
2232 regex_iterator
2233 operator++(int);
2234
2235 private:
2236 // these members are shown for exposition only:
2237 _Bi_iter begin;
2238 _Bi_iter end;
2239 const regex_type* pregex;
2240 regex_constants::match_flag_type flags;
2241 match_results<_Bi_iter> match;
2242 };
2243
2244 typedef regex_iterator<const char*> cregex_iterator;
2245 typedef regex_iterator<string::const_iterator> sregex_iterator;
2246 #ifdef _GLIBCXX_USE_WCHAR_T
2247 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2248 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2249 #endif
2250
2251 // [7.12.2] Class template regex_token_iterator
2252 /**
2253 * Iterates over submatches in a range (or @a splits a text string).
2254 *
2255 * The purpose of this iterator is to enumerate all, or all specified,
2256 * matches of a regular expression within a text range. The dereferenced
2257 * value of an iterator of this class is a std::sub_match object.
2258 */
2259 template<typename _Bi_iter,
2260 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2261 typename _Rx_traits = regex_traits<_Ch_type> >
2262 class regex_token_iterator
2263 {
2264 public:
2265 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2266 typedef sub_match<_Bi_iter> value_type;
2267 typedef std::ptrdiff_t difference_type;
2268 typedef const value_type* pointer;
2269 typedef const value_type& reference;
2270 typedef std::forward_iterator_tag iterator_category;
2271
2272 public:
2273 /**
2274 * @brief Default constructs a %regex_token_iterator.
2275 * @todo Implement this function.
2276 *
2277 * A default-constructed %regex_token_iterator is a singular iterator
2278 * that will compare equal to the one-past-the-end value for any
2279 * iterator of the same type.
2280 */
2281 regex_token_iterator();
2282
2283 /**
2284 * Constructs a %regex_token_iterator...
2285 * @param a [IN] The start of the text to search.
2286 * @param b [IN] One-past-the-end of the text to search.
2287 * @param re [IN] The regular expression to search for.
2288 * @param submatch [IN] Which submatch to return. There are some
2289 * special values for this parameter:
2290 * - -1 each enumerated subexpression does NOT
2291 * match the regular expression (aka field
2292 * splitting)
2293 * - 0 the entire string matching the
2294 * subexpression is returned for each match
2295 * within the text.
2296 * - >0 enumerates only the indicated
2297 * subexpression from a match within the text.
2298 * @param m [IN] Policy flags for match rules.
2299 *
2300 * @todo Implement this function.
2301 * @doctodo
2302 */
2303 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2304 int __submatch = 0,
2305 regex_constants::match_flag_type __m
2306 = regex_constants::match_default);
2307
2308 /**
2309 * Constructs a %regex_token_iterator...
2310 * @param a [IN] The start of the text to search.
2311 * @param b [IN] One-past-the-end of the text to search.
2312 * @param re [IN] The regular expression to search for.
2313 * @param submatches [IN] A list of subexpressions to return for each
2314 * regular expression match within the text.
2315 * @param m [IN] Policy flags for match rules.
2316 *
2317 * @todo Implement this function.
2318 * @doctodo
2319 */
2320 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2321 const regex_type& __re,
2322 const std::vector<int>& __submatches,
2323 regex_constants::match_flag_type __m
2324 = regex_constants::match_default);
2325
2326 /**
2327 * Constructs a %regex_token_iterator...
2328 * @param a [IN] The start of the text to search.
2329 * @param b [IN] One-past-the-end of the text to search.
2330 * @param re [IN] The regular expression to search for.
2331 * @param submatches [IN] A list of subexpressions to return for each
2332 * regular expression match within the text.
2333 * @param m [IN] Policy flags for match rules.
2334
2335 * @todo Implement this function.
2336 * @doctodo
2337 */
2338 template<std::size_t _Nm>
2339 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2340 const regex_type& __re,
2341 const int (&__submatches)[_Nm],
2342 regex_constants::match_flag_type __m
2343 = regex_constants::match_default);
2344
2345 /**
2346 * @brief Copy constructs a %regex_token_iterator.
2347 * @param rhs [IN] A %regex_token_iterator to copy.
2348 * @todo Implement this function.
2349 */
2350 regex_token_iterator(const regex_token_iterator& __rhs);
2351
2352 /**
2353 * @brief Assigns a %regex_token_iterator to another.
2354 * @param rhs [IN] A %regex_token_iterator to copy.
2355 * @todo Implement this function.
2356 */
2357 regex_token_iterator&
2358 operator=(const regex_token_iterator& __rhs);
2359
2360 /**
2361 * @brief Compares a %regex_token_iterator to another for equality.
2362 * @todo Implement this function.
2363 */
2364 bool
2365 operator==(const regex_token_iterator& __rhs);
2366
2367 /**
2368 * @brief Compares a %regex_token_iterator to another for inequality.
2369 * @todo Implement this function.
2370 */
2371 bool
2372 operator!=(const regex_token_iterator& __rhs);
2373
2374 /**
2375 * @brief Dereferences a %regex_token_iterator.
2376 * @todo Implement this function.
2377 */
2378 const value_type&
2379 operator*();
2380
2381 /**
2382 * @brief Selects a %regex_token_iterator member.
2383 * @todo Implement this function.
2384 */
2385 const value_type*
2386 operator->();
2387
2388 /**
2389 * @brief Increments a %regex_token_iterator.
2390 * @todo Implement this function.
2391 */
2392 regex_token_iterator&
2393 operator++();
2394
2395 /**
2396 * @brief Postincrements a %regex_token_iterator.
2397 * @todo Implement this function.
2398 */
2399 regex_token_iterator
2400 operator++(int);
2401
2402 private: // data members for exposition only:
2403 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> position_iterator;
2404
2405 position_iterator __position;
2406 const value_type* __result;
2407 value_type __suffix;
2408 std::size_t __n;
2409 std::vector<int> __subs;
2410 };
2411
2412 /** @brief Token iterator for C-style NULL-terminated strings. */
2413 typedef regex_token_iterator<const char*> cregex_token_iterator;
2414 /** @brief Token iterator for standard strings. */
2415 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2416 #ifdef _GLIBCXX_USE_WCHAR_T
2417 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2418 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2419 /** @brief Token iterator for standard wide-character strings. */
2420 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2421 #endif
2422
2423 //@} // group regex
2424 _GLIBCXX_END_NAMESPACE
2425