1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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)
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.
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.
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/>.
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
31 namespace std _GLIBCXX_VISIBILITY(default)
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
36 * @defgroup regex Regular Expressions
37 * A facility for performing regular expression pattern matching.
41 // [7.7] Class regex_traits
43 * @brief Describes aspects of a regular expression.
45 * A regular expression traits class that satisfies the requirements of
48 * The class %regex is parameterized around a set of related types and
49 * functions used to complete the definition of its semantics. This class
50 * satisfies the requirements of such a traits class.
52 template<typename _Ch_type>
56 typedef _Ch_type char_type;
57 typedef std::basic_string<char_type> string_type;
58 typedef std::locale locale_type;
59 typedef std::ctype_base::mask char_class_type;
63 * @brief Constructs a default traits object.
69 * @brief Gives the length of a C-style string starting at @p __p.
71 * @param __p a pointer to the start of a character sequence.
73 * @returns the number of characters between @p *__p and the first
74 * default-initialized value of type @p char_type. In other words, uses
75 * the C-string algorithm for determining the length of a sequence of
79 length(const char_type* __p)
80 { return string_type::traits_type::length(__p); }
83 * @brief Performs the identity translation.
85 * @param c A character to the locale-specific character set.
90 translate(char_type __c) const
94 * @brief Translates a character into a case-insensitive equivalent.
96 * @param c A character to the locale-specific character set.
98 * @returns the locale-specific lower-case equivalent of c.
99 * @throws std::bad_cast if the imbued locale does not support the ctype
103 translate_nocase(char_type __c) const
106 using std::use_facet;
107 return use_facet<ctype<char_type> >(_M_locale).tolower(__c);
111 * @brief Gets a sort key for a character sequence.
113 * @param first beginning of the character sequence.
114 * @param last one-past-the-end of the character sequence.
116 * Returns a sort key for the character sequence designated by the
117 * iterator range [F1, F2) such that if the character sequence [G1, G2)
118 * sorts before the character sequence [H1, H2) then
119 * v.transform(G1, G2) < v.transform(H1, H2).
121 * What this really does is provide a more efficient way to compare a
122 * string to multiple other strings in locales with fancy collation
123 * rules and equivalence classes.
125 * @returns a locale-specific sort key equivalent to the input range.
127 * @throws std::bad_cast if the current locale does not have a collate
130 template<typename _Fwd_iter>
132 transform(_Fwd_iter __first, _Fwd_iter __last) const
135 using std::use_facet;
136 const collate<_Ch_type>& __c(use_facet<
137 collate<_Ch_type> >(_M_locale));
138 string_type __s(__first, __last);
139 return __c.transform(__s.data(), __s.data() + __s.size());
143 * @brief Gets a sort key for a character sequence, independant of case.
145 * @param first beginning of the character sequence.
146 * @param last one-past-the-end of the character sequence.
148 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
149 * typeid(collate_byname<_Ch_type>) and the form of the sort key
150 * returned by collate_byname<_Ch_type>::transform(first, last) is known
151 * and can be converted into a primary sort key then returns that key,
152 * otherwise returns an empty string.
154 * @todo Implement this function.
156 template<typename _Fwd_iter>
158 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
159 { return string_type(); }
162 * @brief Gets a collation element by name.
164 * @param first beginning of the collation element name.
165 * @param last one-past-the-end of the collation element name.
167 * @returns a sequence of one or more characters that represents the
168 * collating element consisting of the character sequence designated by
169 * the iterator range [first, last). Returns an empty string if the
170 * character sequence is not a valid collating element.
172 * @todo Implement this function.
174 template<typename _Fwd_iter>
176 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
177 { return string_type(); }
180 * @brief Maps one or more characters to a named character
183 * @param first beginning of the character sequence.
184 * @param last one-past-the-end of the character sequence.
185 * @param icase ignores the case of the classification name.
187 * @returns an unspecified value that represents the character
188 * classification named by the character sequence designated by the
189 * iterator range [first, last). If @p icase is true, the returned mask
190 * identifies the classification regardless of the case of the characters
191 * to be matched (for example, [[:lower:]] is the same as [[:alpha:]]),
192 * otherwise a case-dependant classification is returned. The value
193 * returned shall be independent of the case of the characters in the
194 * character sequence. If the name is not recognized then returns a value
195 * that compares equal to 0.
197 * At least the following names (or their wide-character equivalent) are
215 * @todo Implement this function.
217 template<typename _Fwd_iter>
219 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
220 bool __icase = false) const
224 * @brief Determines if @p c is a member of an identified class.
226 * @param c a character.
227 * @param f a class type (as returned from lookup_classname).
229 * @returns true if the character @p c is a member of the classification
230 * represented by @p f, false otherwise.
232 * @throws std::bad_cast if the current locale does not have a ctype
236 isctype(_Ch_type __c, char_class_type __f) const;
239 * @brief Converts a digit to an int.
241 * @param ch a character representing a digit.
242 * @param radix the radix if the numeric conversion (limited to 8, 10,
245 * @returns the value represented by the digit ch in base radix if the
246 * character ch is a valid digit in base radix; otherwise returns -1.
249 value(_Ch_type __ch, int __radix) const;
252 * @brief Imbues the regex_traits object with a copy of a new locale.
254 * @param loc A locale.
256 * @returns a copy of the previous locale in use by the regex_traits
259 * @note Calling imbue with a different locale than the one currently in
260 * use invalidates all cached data held by *this.
263 imbue(locale_type __loc)
265 std::swap(_M_locale, __loc);
270 * @brief Gets a copy of the current locale in use by the regex_traits
275 { return _M_locale; }
278 locale_type _M_locale;
281 template<typename _Ch_type>
283 regex_traits<_Ch_type>::
284 isctype(_Ch_type __c, char_class_type __f) const
287 using std::use_facet;
288 const ctype<_Ch_type>& __ctype(use_facet<
289 ctype<_Ch_type> >(_M_locale));
291 if (__ctype.is(__f, __c))
294 // special case of underscore in [[:w:]]
295 if (__c == __ctype.widen('_'))
297 const char __wb[] = "w";
298 char_class_type __wt = this->lookup_classname(__wb,
299 __wb + sizeof(__wb));
304 // special case of [[:space:]] in [[:blank:]]
305 if (__ctype.is(std::ctype_base::space, __c))
307 const char __bb[] = "blank";
308 char_class_type __bt = this->lookup_classname(__bb,
309 __bb + sizeof(__bb));
317 template<typename _Ch_type>
319 regex_traits<_Ch_type>::
320 value(_Ch_type __ch, int __radix) const
322 std::basic_istringstream<_Ch_type> __is(string_type(1, __ch));
326 else if (__radix == 16)
329 return __is.fail() ? -1 : __v;
332 // [7.8] Class basic_regex
334 * Objects of specializations of this class represent regular expressions
335 * constructed from sequences of character type @p _Ch_type.
337 * Storage for the regular expression is allocated and deallocated as
338 * necessary by the member functions of this class.
340 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type> >
345 typedef _Ch_type value_type;
346 typedef regex_constants::syntax_option_type flag_type;
347 typedef typename _Rx_traits::locale_type locale_type;
348 typedef typename _Rx_traits::string_type string_type;
355 static constexpr regex_constants::syntax_option_type icase
356 = regex_constants::icase;
357 static constexpr regex_constants::syntax_option_type nosubs
358 = regex_constants::nosubs;
359 static constexpr regex_constants::syntax_option_type optimize
360 = regex_constants::optimize;
361 static constexpr regex_constants::syntax_option_type collate
362 = regex_constants::collate;
363 static constexpr regex_constants::syntax_option_type ECMAScript
364 = regex_constants::ECMAScript;
365 static constexpr regex_constants::syntax_option_type basic
366 = regex_constants::basic;
367 static constexpr regex_constants::syntax_option_type extended
368 = regex_constants::extended;
369 static constexpr regex_constants::syntax_option_type awk
370 = regex_constants::awk;
371 static constexpr regex_constants::syntax_option_type grep
372 = regex_constants::grep;
373 static constexpr regex_constants::syntax_option_type egrep
374 = regex_constants::egrep;
377 // [7.8.2] construct/copy/destroy
379 * Constructs a basic regular expression that does not match any
380 * character sequence.
383 : _M_flags(regex_constants::ECMAScript),
384 _M_automaton(__regex::__compile<const _Ch_type*, _Rx_traits>(0, 0,
385 _M_traits, _M_flags))
389 * @brief Constructs a basic regular expression from the sequence
390 * [p, p + char_traits<_Ch_type>::length(p)) interpreted according to the
393 * @param p A pointer to the start of a C-style null-terminated string
394 * containing a regular expression.
395 * @param f Flags indicating the syntax rules and options.
397 * @throws regex_error if @p p is not a valid regular expression.
400 basic_regex(const _Ch_type* __p,
401 flag_type __f = regex_constants::ECMAScript)
403 _M_automaton(__regex::__compile(__p, __p + _Rx_traits::length(__p),
404 _M_traits, _M_flags))
408 * @brief Constructs a basic regular expression from the sequence
409 * [p, p + len) interpreted according to the flags in @p f.
411 * @param p A pointer to the start of a string containing a regular
413 * @param len The length of the string containing the regular expression.
414 * @param f Flags indicating the syntax rules and options.
416 * @throws regex_error if @p p is not a valid regular expression.
418 basic_regex(const _Ch_type* __p, std::size_t __len, flag_type __f)
420 _M_automaton(__regex::__compile(__p, __p + __len, _M_traits, _M_flags))
424 * @brief Copy-constructs a basic regular expression.
426 * @param rhs A @p regex object.
428 basic_regex(const basic_regex& __rhs)
429 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
430 _M_automaton(__rhs._M_automaton)
434 * @brief Move-constructs a basic regular expression.
436 * @param rhs A @p regex object.
438 basic_regex(const basic_regex&& __rhs)
439 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
440 _M_automaton(std::move(__rhs._M_automaton))
444 * @brief Constructs a basic regular expression from the string
445 * @p s interpreted according to the flags in @p f.
447 * @param s A string containing a regular expression.
448 * @param f Flags indicating the syntax rules and options.
450 * @throws regex_error if @p s is not a valid regular expression.
452 template<typename _Ch_traits, typename _Ch_alloc>
454 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
456 flag_type __f = regex_constants::ECMAScript)
458 _M_automaton(__regex::__compile(__s.begin(), __s.end(),
459 _M_traits, _M_flags))
463 * @brief Constructs a basic regular expression from the range
464 * [first, last) interpreted according to the flags in @p f.
466 * @param first The start of a range containing a valid regular
468 * @param last The end of a range containing a valid regular
470 * @param f The format flags of the regular expression.
472 * @throws regex_error if @p [first, last) is not a valid regular
475 template<typename _InputIterator>
476 basic_regex(_InputIterator __first, _InputIterator __last,
477 flag_type __f = regex_constants::ECMAScript)
479 _M_automaton(__regex::__compile(__first, __last, _M_traits, _M_flags))
483 * @brief Constructs a basic regular expression from an initializer list.
485 * @param l The initializer list.
486 * @param f The format flags of the regular expression.
488 * @throws regex_error if @p l is not a valid regular expression.
490 basic_regex(initializer_list<_Ch_type> __l,
491 flag_type __f = regex_constants::ECMAScript)
493 _M_automaton(__regex::__compile(__l.begin(), __l.end(),
494 _M_traits, _M_flags))
498 * @brief Destroys a basic regular expression.
504 * @brief Assigns one regular expression to another.
507 operator=(const basic_regex& __rhs)
508 { return this->assign(__rhs); }
511 * @brief Move-assigns one regular expression to another.
514 operator=(basic_regex&& __rhs)
515 { return this->assign(std::move(__rhs)); }
518 * @brief Replaces a regular expression with a new one constructed from
519 * a C-style null-terminated string.
521 * @param A pointer to the start of a null-terminated C-style string
522 * containing a regular expression.
525 operator=(const _Ch_type* __p)
526 { return this->assign(__p, flags()); }
529 * @brief Replaces a regular expression with a new one constructed from
532 * @param A pointer to a string containing a regular expression.
534 template<typename _Ch_typeraits, typename _Allocator>
536 operator=(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s)
537 { return this->assign(__s, flags()); }
541 * @brief the real assignment operator.
543 * @param rhs Another regular expression object.
546 assign(const basic_regex& __rhs)
548 basic_regex __tmp(__rhs);
554 * @brief The move-assignment operator.
556 * @param rhs Another regular expression object.
559 assign(basic_regex&& __rhs)
561 basic_regex __tmp(std::move(__rhs));
567 * @brief Assigns a new regular expression to a regex object from a
568 * C-style null-terminated string containing a regular expression
571 * @param p A pointer to a C-style null-terminated string containing
572 * a regular expression pattern.
573 * @param flags Syntax option flags.
575 * @throws regex_error if p does not contain a valid regular expression
576 * pattern interpreted according to @p flags. If regex_error is thrown,
577 * *this remains unchanged.
580 assign(const _Ch_type* __p,
581 flag_type __flags = regex_constants::ECMAScript)
582 { return this->assign(string_type(__p), __flags); }
585 * @brief Assigns a new regular expression to a regex object from a
586 * C-style string containing a regular expression pattern.
588 * @param p A pointer to a C-style string containing a
589 * regular expression pattern.
590 * @param len The length of the regular expression pattern string.
591 * @param flags Syntax option flags.
593 * @throws regex_error if p does not contain a valid regular expression
594 * pattern interpreted according to @p flags. If regex_error is thrown,
595 * *this remains unchanged.
598 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
599 { return this->assign(string_type(__p, __len), __flags); }
602 * @brief Assigns a new regular expression to a regex object from a
603 * string containing a regular expression pattern.
605 * @param s A string containing a regular expression pattern.
606 * @param flags Syntax option flags.
608 * @throws regex_error if p does not contain a valid regular expression
609 * pattern interpreted according to @p flags. If regex_error is thrown,
610 * *this remains unchanged.
612 template<typename _Ch_typeraits, typename _Allocator>
614 assign(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s,
615 flag_type __f = regex_constants::ECMAScript)
617 basic_regex __tmp(__s, __f);
623 * @brief Assigns a new regular expression to a regex object.
625 * @param first The start of a range containing a valid regular
627 * @param last The end of a range containing a valid regular
629 * @param flags Syntax option flags.
631 * @throws regex_error if p does not contain a valid regular expression
632 * pattern interpreted according to @p flags. If regex_error is thrown,
633 * the object remains unchanged.
635 template<typename _InputIterator>
637 assign(_InputIterator __first, _InputIterator __last,
638 flag_type __flags = regex_constants::ECMAScript)
639 { return this->assign(string_type(__first, __last), __flags); }
642 * @brief Assigns a new regular expression to a regex object.
644 * @param l An initializer list representing a regular expression.
645 * @param flags Syntax option flags.
647 * @throws regex_error if @p l does not contain a valid regular
648 * expression pattern interpreted according to @p flags. If regex_error
649 * is thrown, the object remains unchanged.
652 assign(initializer_list<_Ch_type> __l,
653 flag_type __f = regex_constants::ECMAScript)
654 { return this->assign(__l.begin(), __l.end(), __f); }
656 // [7.8.4] const operations
658 * @brief Gets the number of marked subexpressions within the regular
663 { return _M_automaton->_M_sub_count() - 1; }
666 * @brief Gets the flags used to construct the regular expression
667 * or in the last call to assign().
675 * @brief Imbues the regular expression object with the given locale.
677 * @param loc A locale.
680 imbue(locale_type __loc)
681 { return _M_traits.imbue(__loc); }
684 * @brief Gets the locale currently imbued in the regular expression
689 { return _M_traits.getloc(); }
693 * @brief Swaps the contents of two regular expression objects.
695 * @param rhs Another regular expression object.
698 swap(basic_regex& __rhs)
700 std::swap(_M_flags, __rhs._M_flags);
701 std::swap(_M_traits, __rhs._M_traits);
702 std::swap(_M_automaton, __rhs._M_automaton);
705 #ifdef _GLIBCXX_DEBUG
707 _M_dot(std::ostream& __ostr)
708 { _M_automaton->_M_dot(__ostr); }
711 const __regex::_AutomatonPtr&
712 _M_get_automaton() const
713 { return _M_automaton; }
717 _Rx_traits _M_traits;
718 __regex::_AutomatonPtr _M_automaton;
721 /** @brief Standard regular expressions. */
722 typedef basic_regex<char> regex;
723 #ifdef _GLIBCXX_USE_WCHAR_T
724 /** @brief Standard wide-character regular expressions. */
725 typedef basic_regex<wchar_t> wregex;
729 // [7.8.6] basic_regex swap
731 * @brief Swaps the contents of two regular expression objects.
732 * @param lhs First regular expression.
733 * @param rhs Second regular expression.
735 template<typename _Ch_type, typename _Rx_traits>
737 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
738 basic_regex<_Ch_type, _Rx_traits>& __rhs)
739 { __lhs.swap(__rhs); }
742 // [7.9] Class template sub_match
744 * A sequence of characters matched by a particular marked sub-expression.
746 * An object of this class is essentially a pair of iterators marking a
747 * matched subexpression within a regular expression pattern match. Such
748 * objects can be converted to and compared with std::basic_string objects
749 * of a similar base character type as the pattern matched by the regular
752 * The iterators that make up the pair are the usual half-open interval
753 * referencing the actual original pattern matched.
755 template<typename _BiIter>
756 class sub_match : public std::pair<_BiIter, _BiIter>
759 typedef typename iterator_traits<_BiIter>::value_type value_type;
760 typedef typename iterator_traits<_BiIter>::difference_type
762 typedef _BiIter iterator;
763 typedef std::basic_string<value_type> string_type;
768 constexpr sub_match() : matched() { }
771 * Gets the length of the matching sequence.
775 { return this->matched ? std::distance(this->first, this->second) : 0; }
778 * @brief Gets the matching sequence as a string.
780 * @returns the matching sequence as a string.
782 * This is the implicit conversion operator. It is identical to the
783 * str() member function except that it will want to pop up in
784 * unexpected places and cause a great deal of confusion and cursing
787 operator string_type() const
790 ? string_type(this->first, this->second)
795 * @brief Gets the matching sequence as a string.
797 * @returns the matching sequence as a string.
803 ? string_type(this->first, this->second)
808 * @brief Compares this and another matched sequence.
810 * @param s Another matched sequence to compare to this one.
812 * @retval <0 this matched sequence will collate before @p s.
813 * @retval =0 this matched sequence is equivalent to @p s.
814 * @retval <0 this matched sequence will collate after @p s.
817 compare(const sub_match& __s) const
818 { return this->str().compare(__s.str()); }
821 * @brief Compares this sub_match to a string.
823 * @param s A string to compare to this sub_match.
825 * @retval <0 this matched sequence will collate before @p s.
826 * @retval =0 this matched sequence is equivalent to @p s.
827 * @retval <0 this matched sequence will collate after @p s.
830 compare(const string_type& __s) const
831 { return this->str().compare(__s); }
834 * @brief Compares this sub_match to a C-style string.
836 * @param s A C-style string to compare to this sub_match.
838 * @retval <0 this matched sequence will collate before @p s.
839 * @retval =0 this matched sequence is equivalent to @p s.
840 * @retval <0 this matched sequence will collate after @p s.
843 compare(const value_type* __s) const
844 { return this->str().compare(__s); }
848 /** @brief Standard regex submatch over a C-style null-terminated string. */
849 typedef sub_match<const char*> csub_match;
850 /** @brief Standard regex submatch over a standard string. */
851 typedef sub_match<string::const_iterator> ssub_match;
852 #ifdef _GLIBCXX_USE_WCHAR_T
853 /** @brief Regex submatch over a C-style null-terminated wide string. */
854 typedef sub_match<const wchar_t*> wcsub_match;
855 /** @brief Regex submatch over a standard wide string. */
856 typedef sub_match<wstring::const_iterator> wssub_match;
859 // [7.9.2] sub_match non-member operators
862 * @brief Tests the equivalence of two regular expression submatches.
863 * @param lhs First regular expression submatch.
864 * @param rhs Second regular expression submatch.
865 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
867 template<typename _BiIter>
869 operator==(const sub_match<_BiIter>& __lhs,
870 const sub_match<_BiIter>& __rhs)
871 { return __lhs.compare(__rhs) == 0; }
874 * @brief Tests the inequivalence of two regular expression submatches.
875 * @param lhs First regular expression submatch.
876 * @param rhs Second regular expression submatch.
877 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
879 template<typename _BiIter>
881 operator!=(const sub_match<_BiIter>& __lhs,
882 const sub_match<_BiIter>& __rhs)
883 { return __lhs.compare(__rhs) != 0; }
886 * @brief Tests the ordering of two regular expression submatches.
887 * @param lhs First regular expression submatch.
888 * @param rhs Second regular expression submatch.
889 * @returns true if @a lhs precedes @a rhs, false otherwise.
891 template<typename _BiIter>
893 operator<(const sub_match<_BiIter>& __lhs,
894 const sub_match<_BiIter>& __rhs)
895 { return __lhs.compare(__rhs) < 0; }
898 * @brief Tests the ordering of two regular expression submatches.
899 * @param lhs First regular expression submatch.
900 * @param rhs Second regular expression submatch.
901 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
903 template<typename _BiIter>
905 operator<=(const sub_match<_BiIter>& __lhs,
906 const sub_match<_BiIter>& __rhs)
907 { return __lhs.compare(__rhs) <= 0; }
910 * @brief Tests the ordering of two regular expression submatches.
911 * @param lhs First regular expression submatch.
912 * @param rhs Second regular expression submatch.
913 * @returns true if @a lhs does not precede @a rhs, false otherwise.
915 template<typename _BiIter>
917 operator>=(const sub_match<_BiIter>& __lhs,
918 const sub_match<_BiIter>& __rhs)
919 { return __lhs.compare(__rhs) >= 0; }
922 * @brief Tests the ordering of two regular expression submatches.
923 * @param lhs First regular expression submatch.
924 * @param rhs Second regular expression submatch.
925 * @returns true if @a lhs succeeds @a rhs, false otherwise.
927 template<typename _BiIter>
929 operator>(const sub_match<_BiIter>& __lhs,
930 const sub_match<_BiIter>& __rhs)
931 { return __lhs.compare(__rhs) > 0; }
934 * @brief Tests the equivalence of a string and a regular expression
936 * @param lhs A string.
937 * @param rhs A regular expression submatch.
938 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
940 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
942 operator==(const basic_string<
943 typename iterator_traits<_Bi_iter>::value_type,
944 _Ch_traits, _Ch_alloc>& __lhs,
945 const sub_match<_Bi_iter>& __rhs)
946 { return __lhs == __rhs.str(); }
949 * @brief Tests the inequivalence of a string and a regular expression
951 * @param lhs A string.
952 * @param rhs A regular expression submatch.
953 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
955 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
957 operator!=(const basic_string<
958 typename iterator_traits<_Bi_iter>::value_type,
959 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
960 { return __lhs != __rhs.str(); }
963 * @brief Tests the ordering of a string and a regular expression submatch.
964 * @param lhs A string.
965 * @param rhs A regular expression submatch.
966 * @returns true if @a lhs precedes @a rhs, false otherwise.
968 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
970 operator<(const basic_string<
971 typename iterator_traits<_Bi_iter>::value_type,
972 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
973 { return __lhs < __rhs.str(); }
976 * @brief Tests the ordering of a string and a regular expression submatch.
977 * @param lhs A string.
978 * @param rhs A regular expression submatch.
979 * @returns true if @a lhs succeeds @a rhs, false otherwise.
981 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
983 operator>(const basic_string<
984 typename iterator_traits<_Bi_iter>::value_type,
985 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
986 { return __lhs > __rhs.str(); }
989 * @brief Tests the ordering of a string and a regular expression submatch.
990 * @param lhs A string.
991 * @param rhs A regular expression submatch.
992 * @returns true if @a lhs does not precede @a rhs, false otherwise.
994 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
996 operator>=(const basic_string<
997 typename iterator_traits<_Bi_iter>::value_type,
998 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
999 { return __lhs >= __rhs.str(); }
1002 * @brief Tests the ordering of a string and a regular expression submatch.
1003 * @param lhs A string.
1004 * @param rhs A regular expression submatch.
1005 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1007 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1009 operator<=(const basic_string<
1010 typename iterator_traits<_Bi_iter>::value_type,
1011 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1012 { return __lhs <= __rhs.str(); }
1015 * @brief Tests the equivalence of a regular expression submatch and a
1017 * @param lhs A regular expression submatch.
1018 * @param rhs A string.
1019 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1021 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1023 operator==(const sub_match<_Bi_iter>& __lhs,
1025 typename iterator_traits<_Bi_iter>::value_type,
1026 _Ch_traits, _Ch_alloc>& __rhs)
1027 { return __lhs.str() == __rhs; }
1030 * @brief Tests the inequivalence of a regular expression submatch and a
1032 * @param lhs A regular expression submatch.
1033 * @param rhs A string.
1034 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1036 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1038 operator!=(const sub_match<_Bi_iter>& __lhs,
1040 typename iterator_traits<_Bi_iter>::value_type,
1041 _Ch_traits, _Ch_alloc>& __rhs)
1042 { return __lhs.str() != __rhs; }
1045 * @brief Tests the ordering of a regular expression submatch and a string.
1046 * @param lhs A regular expression submatch.
1047 * @param rhs A string.
1048 * @returns true if @a lhs precedes @a rhs, false otherwise.
1050 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1052 operator<(const sub_match<_Bi_iter>& __lhs,
1054 typename iterator_traits<_Bi_iter>::value_type,
1055 _Ch_traits, _Ch_alloc>& __rhs)
1056 { return __lhs.str() < __rhs; }
1059 * @brief Tests the ordering of a regular expression submatch and a string.
1060 * @param lhs A regular expression submatch.
1061 * @param rhs A string.
1062 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1064 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1066 operator>(const sub_match<_Bi_iter>& __lhs,
1068 typename iterator_traits<_Bi_iter>::value_type,
1069 _Ch_traits, _Ch_alloc>& __rhs)
1070 { return __lhs.str() > __rhs; }
1073 * @brief Tests the ordering of a regular expression submatch and a string.
1074 * @param lhs A regular expression submatch.
1075 * @param rhs A string.
1076 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1078 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1080 operator>=(const sub_match<_Bi_iter>& __lhs,
1082 typename iterator_traits<_Bi_iter>::value_type,
1083 _Ch_traits, _Ch_alloc>& __rhs)
1084 { return __lhs.str() >= __rhs; }
1087 * @brief Tests the ordering of a regular expression submatch and a string.
1088 * @param lhs A regular expression submatch.
1089 * @param rhs A string.
1090 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1092 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1094 operator<=(const sub_match<_Bi_iter>& __lhs,
1096 typename iterator_traits<_Bi_iter>::value_type,
1097 _Ch_traits, _Ch_alloc>& __rhs)
1098 { return __lhs.str() <= __rhs; }
1101 * @brief Tests the equivalence of a C string and a regular expression
1103 * @param lhs A C string.
1104 * @param rhs A regular expression submatch.
1105 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1107 template<typename _Bi_iter>
1109 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1110 const sub_match<_Bi_iter>& __rhs)
1111 { return __lhs == __rhs.str(); }
1114 * @brief Tests the inequivalence of an iterator value and a regular
1115 * expression submatch.
1116 * @param lhs A regular expression submatch.
1117 * @param rhs A string.
1118 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1120 template<typename _Bi_iter>
1122 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1123 const sub_match<_Bi_iter>& __rhs)
1124 { return __lhs != __rhs.str(); }
1127 * @brief Tests the ordering of a string and a regular expression submatch.
1128 * @param lhs A string.
1129 * @param rhs A regular expression submatch.
1130 * @returns true if @a lhs precedes @a rhs, false otherwise.
1132 template<typename _Bi_iter>
1134 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1135 const sub_match<_Bi_iter>& __rhs)
1136 { return __lhs < __rhs.str(); }
1139 * @brief Tests the ordering of a string and a regular expression submatch.
1140 * @param lhs A string.
1141 * @param rhs A regular expression submatch.
1142 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1144 template<typename _Bi_iter>
1146 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1147 const sub_match<_Bi_iter>& __rhs)
1148 { return __lhs > __rhs.str(); }
1151 * @brief Tests the ordering of a string and a regular expression submatch.
1152 * @param lhs A string.
1153 * @param rhs A regular expression submatch.
1154 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1156 template<typename _Bi_iter>
1158 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1159 const sub_match<_Bi_iter>& __rhs)
1160 { return __lhs >= __rhs.str(); }
1163 * @brief Tests the ordering of a string and a regular expression submatch.
1164 * @param lhs A string.
1165 * @param rhs A regular expression submatch.
1166 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1168 template<typename _Bi_iter>
1170 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1171 const sub_match<_Bi_iter>& __rhs)
1172 { return __lhs <= __rhs.str(); }
1175 * @brief Tests the equivalence of a regular expression submatch and a
1177 * @param lhs A regular expression submatch.
1178 * @param rhs A pointer to a string?
1179 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1181 template<typename _Bi_iter>
1183 operator==(const sub_match<_Bi_iter>& __lhs,
1184 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1185 { return __lhs.str() == __rhs; }
1188 * @brief Tests the inequivalence of a regular expression submatch and a
1190 * @param lhs A regular expression submatch.
1191 * @param rhs A pointer to a string.
1192 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1194 template<typename _Bi_iter>
1196 operator!=(const sub_match<_Bi_iter>& __lhs,
1197 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1198 { return __lhs.str() != __rhs; }
1201 * @brief Tests the ordering of a regular expression submatch and a string.
1202 * @param lhs A regular expression submatch.
1203 * @param rhs A string.
1204 * @returns true if @a lhs precedes @a rhs, false otherwise.
1206 template<typename _Bi_iter>
1208 operator<(const sub_match<_Bi_iter>& __lhs,
1209 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1210 { return __lhs.str() < __rhs; }
1213 * @brief Tests the ordering of a regular expression submatch and a string.
1214 * @param lhs A regular expression submatch.
1215 * @param rhs A string.
1216 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1218 template<typename _Bi_iter>
1220 operator>(const sub_match<_Bi_iter>& __lhs,
1221 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1222 { return __lhs.str() > __rhs; }
1225 * @brief Tests the ordering of a regular expression submatch and a string.
1226 * @param lhs A regular expression submatch.
1227 * @param rhs A string.
1228 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1230 template<typename _Bi_iter>
1232 operator>=(const sub_match<_Bi_iter>& __lhs,
1233 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1234 { return __lhs.str() >= __rhs; }
1237 * @brief Tests the ordering of a regular expression submatch and a string.
1238 * @param lhs A regular expression submatch.
1239 * @param rhs A string.
1240 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1242 template<typename _Bi_iter>
1244 operator<=(const sub_match<_Bi_iter>& __lhs,
1245 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1246 { return __lhs.str() <= __rhs; }
1249 * @brief Tests the equivalence of a string and a regular expression
1251 * @param lhs A string.
1252 * @param rhs A regular expression submatch.
1253 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1255 template<typename _Bi_iter>
1257 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1258 const sub_match<_Bi_iter>& __rhs)
1259 { return __lhs == __rhs.str(); }
1262 * @brief Tests the inequivalence of a string and a regular expression
1264 * @param lhs A string.
1265 * @param rhs A regular expression submatch.
1266 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1268 template<typename _Bi_iter>
1270 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1271 const sub_match<_Bi_iter>& __rhs)
1272 { return __lhs != __rhs.str(); }
1275 * @brief Tests the ordering of a string and a regular expression submatch.
1276 * @param lhs A string.
1277 * @param rhs A regular expression submatch.
1278 * @returns true if @a lhs precedes @a rhs, false otherwise.
1280 template<typename _Bi_iter>
1282 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1283 const sub_match<_Bi_iter>& __rhs)
1284 { return __lhs < __rhs.str(); }
1287 * @brief Tests the ordering of a string and a regular expression submatch.
1288 * @param lhs A string.
1289 * @param rhs A regular expression submatch.
1290 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1292 template<typename _Bi_iter>
1294 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1295 const sub_match<_Bi_iter>& __rhs)
1296 { return __lhs > __rhs.str(); }
1299 * @brief Tests the ordering of a string and a regular expression submatch.
1300 * @param lhs A string.
1301 * @param rhs A regular expression submatch.
1302 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1304 template<typename _Bi_iter>
1306 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1307 const sub_match<_Bi_iter>& __rhs)
1308 { return __lhs >= __rhs.str(); }
1311 * @brief Tests the ordering of a string and a regular expression submatch.
1312 * @param lhs A string.
1313 * @param rhs A regular expression submatch.
1314 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1316 template<typename _Bi_iter>
1318 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1319 const sub_match<_Bi_iter>& __rhs)
1320 { return __lhs <= __rhs.str(); }
1323 * @brief Tests the equivalence of a regular expression submatch and a
1325 * @param lhs A regular expression submatch.
1326 * @param rhs A const string reference.
1327 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1329 template<typename _Bi_iter>
1331 operator==(const sub_match<_Bi_iter>& __lhs,
1332 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1333 { return __lhs.str() == __rhs; }
1336 * @brief Tests the inequivalence of a regular expression submatch and a
1338 * @param lhs A regular expression submatch.
1339 * @param rhs A const string reference.
1340 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1342 template<typename _Bi_iter>
1344 operator!=(const sub_match<_Bi_iter>& __lhs,
1345 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1346 { return __lhs.str() != __rhs; }
1349 * @brief Tests the ordering of a regular expression submatch and a string.
1350 * @param lhs A regular expression submatch.
1351 * @param rhs A const string reference.
1352 * @returns true if @a lhs precedes @a rhs, false otherwise.
1354 template<typename _Bi_iter>
1356 operator<(const sub_match<_Bi_iter>& __lhs,
1357 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1358 { return __lhs.str() < __rhs; }
1361 * @brief Tests the ordering of a regular expression submatch and a string.
1362 * @param lhs A regular expression submatch.
1363 * @param rhs A const string reference.
1364 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1366 template<typename _Bi_iter>
1368 operator>(const sub_match<_Bi_iter>& __lhs,
1369 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1370 { return __lhs.str() > __rhs; }
1373 * @brief Tests the ordering of a regular expression submatch and a string.
1374 * @param lhs A regular expression submatch.
1375 * @param rhs A const string reference.
1376 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1378 template<typename _Bi_iter>
1380 operator>=(const sub_match<_Bi_iter>& __lhs,
1381 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1382 { return __lhs.str() >= __rhs; }
1385 * @brief Tests the ordering of a regular expression submatch and a string.
1386 * @param lhs A regular expression submatch.
1387 * @param rhs A const string reference.
1388 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1390 template<typename _Bi_iter>
1392 operator<=(const sub_match<_Bi_iter>& __lhs,
1393 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1394 { return __lhs.str() <= __rhs; }
1397 * @brief Inserts a matched string into an output stream.
1399 * @param os The output stream.
1400 * @param m A submatch string.
1402 * @returns the output stream with the submatch string inserted.
1404 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1406 basic_ostream<_Ch_type, _Ch_traits>&
1407 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1408 const sub_match<_Bi_iter>& __m)
1409 { return __os << __m.str(); }
1411 // [7.10] Class template match_results
1414 * Special sub_match object representing an unmatched sub-expression.
1416 template<typename _Bi_iter>
1417 inline const sub_match<_Bi_iter>&
1420 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1425 * @brief The results of a match or search operation.
1427 * A collection of character sequences representing the result of a regular
1428 * expression match. Storage for the collection is allocated and freed as
1429 * necessary by the member functions of class template match_results.
1431 * This class satisfies the Sequence requirements, with the exception that
1432 * only the operations defined for a const-qualified Sequence are supported.
1434 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1435 * the whole match. In this case the %sub_match member matched is always true.
1436 * The sub_match object stored at index n denotes what matched the marked
1437 * sub-expression n within the matched expression. If the sub-expression n
1438 * participated in a regular expression match then the %sub_match member
1439 * matched evaluates to true, and members first and second denote the range
1440 * of characters [first, second) which formed that match. Otherwise matched
1441 * is false, and members first and second point to the end of the sequence
1442 * that was searched.
1446 template<typename _Bi_iter,
1447 typename _Allocator = allocator<sub_match<_Bi_iter> > >
1449 : private std::vector<std::sub_match<_Bi_iter>, _Allocator>
1453 * The vector base is empty if this does not represent a successful match.
1454 * Otherwise it contains n+3 elements where n is the number of marked
1457 * [1] 1st marked subexpression
1459 * [n] nth marked subexpression
1463 typedef std::vector<std::sub_match<_Bi_iter>, _Allocator>
1468 * @name 10.? Public Types
1471 typedef sub_match<_Bi_iter> value_type;
1472 typedef const value_type& const_reference;
1473 typedef const_reference reference;
1474 typedef typename _Base_type::const_iterator const_iterator;
1475 typedef const_iterator iterator;
1476 typedef typename std::iterator_traits<_Bi_iter>::difference_type
1478 /* TODO: needs allocator_traits */
1479 typedef typename _Allocator::size_type size_type;
1480 typedef _Allocator allocator_type;
1481 typedef typename std::iterator_traits<_Bi_iter>::value_type
1483 typedef std::basic_string<char_type> string_type;
1488 * @name 10.1 Construction, Copying, and Destruction
1493 * @brief Constructs a default %match_results container.
1494 * @post size() returns 0 and str() returns an empty string.
1497 match_results(const _Allocator& __a = _Allocator())
1502 * @brief Copy constructs a %match_results.
1504 match_results(const match_results& __rhs)
1509 * @brief Assigns rhs to *this.
1512 operator=(const match_results __rhs)
1514 match_results(__rhs).swap(*this);
1519 * @brief Destroys a %match_results object.
1528 * @brief Indicates if the %match_results is ready.
1529 * @retval true The object has a fully-established result state.
1530 * @retval false The object is not ready.
1532 bool ready() const { return !_Base_type::empty(); }
1540 * @brief Gets the number of matches and submatches.
1542 * The number of matches for a given regular expression will be either 0
1543 * if there was no match or mark_count() + 1 if a match was successful.
1544 * Some matches may be empty.
1546 * @returns the number of matches found.
1551 size_type __size = _Base_type::size();
1552 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1557 { return _Base_type::max_size(); }
1560 * @brief Indicates if the %match_results contains no results.
1561 * @retval true The %match_results object is empty.
1562 * @retval false The %match_results object is not empty.
1566 { return size() == 0; }
1571 * @name 10.3 Element Access
1576 * @brief Gets the length of the indicated submatch.
1577 * @param sub indicates the submatch.
1578 * @pre ready() == true
1580 * This function returns the length of the indicated submatch, or the
1581 * length of the entire match if @p sub is zero (the default).
1584 length(size_type __sub = 0) const
1585 { return (*this)[__sub].length(); }
1588 * @brief Gets the offset of the beginning of the indicated submatch.
1589 * @param sub indicates the submatch.
1590 * @pre ready() == true
1592 * This function returns the offset from the beginning of the target
1593 * sequence to the beginning of the submatch, unless the value of @p sub
1594 * is zero (the default), in which case this function returns the offset
1595 * from the beginning of the target sequence to the beginning of the
1598 * Returns -1 if @p sub is out of range.
1601 position(size_type __sub = 0) const
1603 return __sub < size() ? std::distance(this->prefix().first,
1604 (*this)[__sub].first) : -1;
1608 * @brief Gets the match or submatch converted to a string type.
1609 * @param sub indicates the submatch.
1610 * @pre ready() == true
1612 * This function gets the submatch (or match, if @p sub is zero) extracted
1613 * from the target range and converted to the associated string type.
1616 str(size_type __sub = 0) const
1617 { return (*this)[__sub].str(); }
1620 * @brief Gets a %sub_match reference for the match or submatch.
1621 * @param sub indicates the submatch.
1622 * @pre ready() == true
1624 * This function gets a reference to the indicated submatch, or the entire
1625 * match if @p sub is zero.
1627 * If @p sub >= size() then this function returns a %sub_match with a
1628 * special value indicating no submatch.
1631 operator[](size_type __sub) const
1633 _GLIBCXX_DEBUG_ASSERT( ready() );
1634 return __sub < size()
1635 ? _Base_type::operator[](__sub)
1636 : __unmatched_sub<_Bi_iter>();
1640 * @brief Gets a %sub_match representing the match prefix.
1641 * @pre ready() == true
1643 * This function gets a reference to a %sub_match object representing the
1644 * part of the target range between the start of the target range and the
1645 * start of the match.
1650 _GLIBCXX_DEBUG_ASSERT( ready() );
1652 ? _Base_type::operator[](_Base_type::size() - 2)
1653 : __unmatched_sub<_Bi_iter>();
1657 * @brief Gets a %sub_match representing the match suffix.
1658 * @pre ready() == true
1660 * This function gets a reference to a %sub_match object representing the
1661 * part of the target range between the end of the match and the end of
1667 _GLIBCXX_DEBUG_ASSERT( ready() );
1669 ? _Base_type::operator[](_Base_type::size() - 1)
1670 : __unmatched_sub<_Bi_iter>();
1674 * @brief Gets an iterator to the start of the %sub_match collection.
1678 { return _Base_type::begin(); }
1681 * @brief Gets an iterator to the start of the %sub_match collection.
1685 { return _Base_type::cbegin(); }
1688 * @brief Gets an iterator to one-past-the-end of the collection.
1692 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
1695 * @brief Gets an iterator to one-past-the-end of the collection.
1704 * @name 10.4 Formatting
1706 * These functions perform formatted substitution of the matched
1707 * character sequences into their target. The format specifiers and
1708 * escape sequences accepted by these functions are determined by
1709 * their @p flags parameter as documented above.
1714 * @pre ready() == true
1715 * @todo Implement this function.
1717 template<typename _Out_iter>
1719 format(_Out_iter __out, const char_type* __fmt_first,
1720 const char_type* __fmt_last,
1721 regex_constants::match_flag_type __flags
1722 = regex_constants::format_default) const
1726 * @pre ready() == true
1728 template<typename _Out_iter, typename _St, typename _Sa>
1730 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1731 regex_constants::match_flag_type __flags
1732 = regex_constants::format_default) const
1734 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1739 * @pre ready() == true
1741 template<typename _Out_iter, typename _St, typename _Sa>
1742 basic_string<char_type, _St, _Sa>
1743 format(const basic_string<char_type, _St, _Sa>& __fmt,
1744 regex_constants::match_flag_type __flags
1745 = regex_constants::format_default) const
1747 basic_string<char_type, _St, _Sa> __result;
1748 format(std::back_inserter(__result), __fmt, __flags);
1753 * @pre ready() == true
1756 format(const char_type* __fmt,
1757 regex_constants::match_flag_type __flags
1758 = regex_constants::format_default) const
1760 string_type __result;
1761 format(std::back_inserter(__result),
1762 __fmt + char_traits<char_type>::length(__fmt),
1770 * @name 10.5 Allocator
1775 * @brief Gets a copy of the allocator.
1778 get_allocator() const
1779 { return _Base_type::get_allocator(); }
1789 * @brief Swaps the contents of two match_results.
1792 swap(match_results& __that)
1793 { _Base_type::swap(__that); }
1797 friend class __regex::_SpecializedResults<_Bi_iter, _Allocator>;
1800 typedef match_results<const char*> cmatch;
1801 typedef match_results<string::const_iterator> smatch;
1802 #ifdef _GLIBCXX_USE_WCHAR_T
1803 typedef match_results<const wchar_t*> wcmatch;
1804 typedef match_results<wstring::const_iterator> wsmatch;
1807 // match_results comparisons
1809 * @brief Compares two match_results for equality.
1810 * @returns true if the two objects refer to the same match,
1813 template<typename _Bi_iter, typename _Allocator>
1815 operator==(const match_results<_Bi_iter, _Allocator>& __m1,
1816 const match_results<_Bi_iter, _Allocator>& __m2)
1818 if (__m1.ready() != __m2.ready())
1820 if (!__m1.ready()) // both are not ready
1822 if (__m1.empty() != __m2.empty())
1824 if (__m1.empty()) // both are empty
1826 return __m1.prefix() == __m2.prefix()
1827 && __m1.size() == __m2.size()
1828 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1829 && __m1.suffix() == __m2.suffix();
1833 * @brief Compares two match_results for inequality.
1834 * @returns true if the two objects do not refer to the same match,
1837 template<typename _Bi_iter, class _Allocator>
1839 operator!=(const match_results<_Bi_iter, _Allocator>& __m1,
1840 const match_results<_Bi_iter, _Allocator>& __m2)
1841 { return !(__m1 == __m2); }
1843 // [7.10.6] match_results swap
1845 * @brief Swaps two match results.
1846 * @param lhs A match result.
1847 * @param rhs A match result.
1849 * The contents of the two match_results objects are swapped.
1851 template<typename _Bi_iter, typename _Allocator>
1853 swap(match_results<_Bi_iter, _Allocator>& __lhs,
1854 match_results<_Bi_iter, _Allocator>& __rhs)
1855 { __lhs.swap(__rhs); }
1857 // [7.11.2] Function template regex_match
1859 * @name Matching, Searching, and Replacing
1864 * @brief Determines if there is a match between the regular expression @p e
1865 * and all of the character sequence [first, last).
1867 * @param s Start of the character sequence to match.
1868 * @param e One-past-the-end of the character sequence to match.
1869 * @param m The match results.
1870 * @param re The regular expression.
1871 * @param flags Controls how the regular expression is matched.
1873 * @retval true A match exists.
1874 * @retval false Otherwise.
1876 * @throws an exception of type regex_error.
1878 * @todo Implement this function.
1880 template<typename _Bi_iter, typename _Allocator,
1881 typename _Ch_type, typename _Rx_traits>
1883 regex_match(_Bi_iter __s,
1885 match_results<_Bi_iter, _Allocator>& __m,
1886 const basic_regex<_Ch_type, _Rx_traits>& __re,
1887 regex_constants::match_flag_type __flags
1888 = regex_constants::match_default)
1890 __regex::_AutomatonPtr __a = __re._M_get_automaton();
1891 __regex::_Automaton::_SizeT __sz = __a->_M_sub_count();
1892 __regex::_SpecializedCursor<_Bi_iter> __cs(__s, __e);
1893 __regex::_SpecializedResults<_Bi_iter, _Allocator> __r(__sz, __cs, __m);
1894 __regex::_Grep_matcher __matcher(__cs, __r, __a, __flags);
1895 return __m[0].matched;
1899 * @brief Indicates if there is a match between the regular expression @p e
1900 * and all of the character sequence [first, last).
1902 * @param first Beginning of the character sequence to match.
1903 * @param last One-past-the-end of the character sequence to match.
1904 * @param re The regular expression.
1905 * @param flags Controls how the regular expression is matched.
1907 * @retval true A match exists.
1908 * @retval false Otherwise.
1910 * @throws an exception of type regex_error.
1912 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1914 regex_match(_Bi_iter __first, _Bi_iter __last,
1915 const basic_regex<_Ch_type, _Rx_traits>& __re,
1916 regex_constants::match_flag_type __flags
1917 = regex_constants::match_default)
1919 match_results<_Bi_iter> __what;
1920 return regex_match(__first, __last, __what, __re, __flags);
1924 * @brief Determines if there is a match between the regular expression @p e
1925 * and a C-style null-terminated string.
1927 * @param s The C-style null-terminated string to match.
1928 * @param m The match results.
1929 * @param re The regular expression.
1930 * @param f Controls how the regular expression is matched.
1932 * @retval true A match exists.
1933 * @retval false Otherwise.
1935 * @throws an exception of type regex_error.
1937 template<typename _Ch_type, typename _Allocator, typename _Rx_traits>
1939 regex_match(const _Ch_type* __s,
1940 match_results<const _Ch_type*, _Allocator>& __m,
1941 const basic_regex<_Ch_type, _Rx_traits>& __re,
1942 regex_constants::match_flag_type __f
1943 = regex_constants::match_default)
1944 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
1947 * @brief Determines if there is a match between the regular expression @p e
1950 * @param s The string to match.
1951 * @param m The match results.
1952 * @param re The regular expression.
1953 * @param flags Controls how the regular expression is matched.
1955 * @retval true A match exists.
1956 * @retval false Otherwise.
1958 * @throws an exception of type regex_error.
1960 template<typename _Ch_traits, typename _Ch_alloc,
1961 typename _Allocator, typename _Ch_type, typename _Rx_traits>
1963 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
1964 match_results<typename basic_string<_Ch_type,
1965 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
1966 const basic_regex<_Ch_type, _Rx_traits>& __re,
1967 regex_constants::match_flag_type __flags
1968 = regex_constants::match_default)
1969 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
1972 * @brief Indicates if there is a match between the regular expression @p e
1973 * and a C-style null-terminated string.
1975 * @param s The C-style null-terminated string to match.
1976 * @param re The regular expression.
1977 * @param f Controls how the regular expression is matched.
1979 * @retval true A match exists.
1980 * @retval false Otherwise.
1982 * @throws an exception of type regex_error.
1984 template<typename _Ch_type, class _Rx_traits>
1986 regex_match(const _Ch_type* __s,
1987 const basic_regex<_Ch_type, _Rx_traits>& __re,
1988 regex_constants::match_flag_type __f
1989 = regex_constants::match_default)
1990 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
1993 * @brief Indicates if there is a match between the regular expression @p e
1996 * @param s [IN] The string to match.
1997 * @param re [IN] The regular expression.
1998 * @param flags [IN] Controls how the regular expression is matched.
2000 * @retval true A match exists.
2001 * @retval false Otherwise.
2003 * @throws an exception of type regex_error.
2005 template<typename _Ch_traits, typename _Str_allocator,
2006 typename _Ch_type, typename _Rx_traits>
2008 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2009 const basic_regex<_Ch_type, _Rx_traits>& __re,
2010 regex_constants::match_flag_type __flags
2011 = regex_constants::match_default)
2012 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2014 // [7.11.3] Function template regex_search
2016 * Searches for a regular expression within a range.
2017 * @param first [IN] The start of the string to search.
2018 * @param last [IN] One-past-the-end of the string to search.
2019 * @param m [OUT] The match results.
2020 * @param re [IN] The regular expression to search for.
2021 * @param flags [IN] Search policy flags.
2022 * @retval true A match was found within the string.
2023 * @retval false No match was found within the string, the content of %m is
2026 * @throws an exception of type regex_error.
2028 * @todo Implement this function.
2030 template<typename _Bi_iter, typename _Allocator,
2031 typename _Ch_type, typename _Rx_traits>
2033 regex_search(_Bi_iter __first, _Bi_iter __last,
2034 match_results<_Bi_iter, _Allocator>& __m,
2035 const basic_regex<_Ch_type, _Rx_traits>& __re,
2036 regex_constants::match_flag_type __flags
2037 = regex_constants::match_default)
2041 * Searches for a regular expression within a range.
2042 * @param first [IN] The start of the string to search.
2043 * @param last [IN] One-past-the-end of the string to search.
2044 * @param re [IN] The regular expression to search for.
2045 * @param flags [IN] Search policy flags.
2046 * @retval true A match was found within the string.
2047 * @retval false No match was found within the string.
2050 * @throws an exception of type regex_error.
2052 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2054 regex_search(_Bi_iter __first, _Bi_iter __last,
2055 const basic_regex<_Ch_type, _Rx_traits>& __re,
2056 regex_constants::match_flag_type __flags
2057 = regex_constants::match_default)
2059 match_results<_Bi_iter> __what;
2060 return regex_search(__first, __last, __what, __re, __flags);
2064 * @brief Searches for a regular expression within a C-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
2074 * @throws an exception of type regex_error.
2076 template<typename _Ch_type, class _Allocator, class _Rx_traits>
2078 regex_search(const _Ch_type* __s,
2079 match_results<const _Ch_type*, _Allocator>& __m,
2080 const basic_regex<_Ch_type, _Rx_traits>& __e,
2081 regex_constants::match_flag_type __f
2082 = regex_constants::match_default)
2083 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2086 * @brief Searches for a regular expression within a C-string.
2087 * @param s [IN] The C-string to search.
2088 * @param e [IN] The regular expression to search for.
2089 * @param f [IN] Search policy flags.
2090 * @retval true A match was found within the string.
2091 * @retval false No match was found within the string.
2094 * @throws an exception of type regex_error.
2096 template<typename _Ch_type, typename _Rx_traits>
2098 regex_search(const _Ch_type* __s,
2099 const basic_regex<_Ch_type, _Rx_traits>& __e,
2100 regex_constants::match_flag_type __f
2101 = regex_constants::match_default)
2102 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2105 * @brief Searches for a regular expression within a string.
2106 * @param s [IN] The string to search.
2107 * @param e [IN] The regular expression to search for.
2108 * @param flags [IN] Search policy flags.
2109 * @retval true A match was found within the string.
2110 * @retval false No match was found within the string.
2113 * @throws an exception of type regex_error.
2115 template<typename _Ch_traits, typename _String_allocator,
2116 typename _Ch_type, typename _Rx_traits>
2118 regex_search(const basic_string<_Ch_type, _Ch_traits,
2119 _String_allocator>& __s,
2120 const basic_regex<_Ch_type, _Rx_traits>& __e,
2121 regex_constants::match_flag_type __flags
2122 = regex_constants::match_default)
2123 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2126 * @brief Searches for a regular expression within a string.
2127 * @param s [IN] A C++ string to search for the regex.
2128 * @param m [OUT] The set of regex matches.
2129 * @param e [IN] The regex to search for in @p s.
2130 * @param f [IN] The search flags.
2131 * @retval true A match was found within the string.
2132 * @retval false No match was found within the string, the content of %m is
2135 * @throws an exception of type regex_error.
2137 template<typename _Ch_traits, typename _Ch_alloc,
2138 typename _Allocator, typename _Ch_type,
2139 typename _Rx_traits>
2141 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2142 match_results<typename basic_string<_Ch_type,
2143 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
2144 const basic_regex<_Ch_type, _Rx_traits>& __e,
2145 regex_constants::match_flag_type __f
2146 = regex_constants::match_default)
2147 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2149 // std [28.11.4] Function template regex_replace
2160 * @throws an exception of type regex_error.
2162 * @todo Implement this function.
2164 template<typename _Out_iter, typename _Bi_iter,
2165 typename _Rx_traits, typename _Ch_type>
2167 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2168 const basic_regex<_Ch_type, _Rx_traits>& __e,
2169 const basic_string<_Ch_type>& __fmt,
2170 regex_constants::match_flag_type __flags
2171 = regex_constants::match_default)
2181 * @returns a copy of string @p s with replacements.
2183 * @throws an exception of type regex_error.
2185 template<typename _Rx_traits, typename _Ch_type>
2186 inline basic_string<_Ch_type>
2187 regex_replace(const basic_string<_Ch_type>& __s,
2188 const basic_regex<_Ch_type, _Rx_traits>& __e,
2189 const basic_string<_Ch_type>& __fmt,
2190 regex_constants::match_flag_type __flags
2191 = regex_constants::match_default)
2193 std::string __result;
2194 regex_replace(std::back_inserter(__result),
2195 __s.begin(), __s.end(), __e, __fmt, __flags);
2201 // std [28.12] Class template regex_iterator
2203 * An iterator adaptor that will provide repeated calls of regex_search over
2204 * a range until no more matches remain.
2206 template<typename _Bi_iter,
2207 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2208 typename _Rx_traits = regex_traits<_Ch_type> >
2209 class regex_iterator
2212 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2213 typedef match_results<_Bi_iter> value_type;
2214 typedef std::ptrdiff_t difference_type;
2215 typedef const value_type* pointer;
2216 typedef const value_type& reference;
2217 typedef std::forward_iterator_tag iterator_category;
2221 * @brief Provides a singular iterator, useful for indicating
2222 * one-past-the-end of a range.
2223 * @todo Implement this function.
2229 * Constructs a %regex_iterator...
2230 * @param a [IN] The start of a text range to search.
2231 * @param b [IN] One-past-the-end of the text range to search.
2232 * @param re [IN] The regular expression to match.
2233 * @param m [IN] Policy flags for match rules.
2234 * @todo Implement this function.
2237 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2238 regex_constants::match_flag_type __m
2239 = regex_constants::match_default);
2242 * Copy constructs a %regex_iterator.
2243 * @todo Implement this function.
2246 regex_iterator(const regex_iterator& __rhs);
2249 * @todo Implement this function.
2253 operator=(const regex_iterator& __rhs);
2256 * @todo Implement this function.
2260 operator==(const regex_iterator& __rhs);
2263 * @todo Implement this function.
2267 operator!=(const regex_iterator& __rhs);
2270 * @todo Implement this function.
2277 * @todo Implement this function.
2284 * @todo Implement this function.
2291 * @todo Implement this function.
2298 // these members are shown for exposition only:
2301 const regex_type* pregex;
2302 regex_constants::match_flag_type flags;
2303 match_results<_Bi_iter> match;
2306 typedef regex_iterator<const char*> cregex_iterator;
2307 typedef regex_iterator<string::const_iterator> sregex_iterator;
2308 #ifdef _GLIBCXX_USE_WCHAR_T
2309 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2310 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2313 // [7.12.2] Class template regex_token_iterator
2315 * Iterates over submatches in a range (or @a splits a text string).
2317 * The purpose of this iterator is to enumerate all, or all specified,
2318 * matches of a regular expression within a text range. The dereferenced
2319 * value of an iterator of this class is a std::sub_match object.
2321 template<typename _Bi_iter,
2322 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2323 typename _Rx_traits = regex_traits<_Ch_type> >
2324 class regex_token_iterator
2327 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2328 typedef sub_match<_Bi_iter> value_type;
2329 typedef std::ptrdiff_t difference_type;
2330 typedef const value_type* pointer;
2331 typedef const value_type& reference;
2332 typedef std::forward_iterator_tag iterator_category;
2336 * @brief Default constructs a %regex_token_iterator.
2337 * @todo Implement this function.
2339 * A default-constructed %regex_token_iterator is a singular iterator
2340 * that will compare equal to the one-past-the-end value for any
2341 * iterator of the same type.
2343 regex_token_iterator();
2346 * Constructs a %regex_token_iterator...
2347 * @param a [IN] The start of the text to search.
2348 * @param b [IN] One-past-the-end of the text to search.
2349 * @param re [IN] The regular expression to search for.
2350 * @param submatch [IN] Which submatch to return. There are some
2351 * special values for this parameter:
2352 * - -1 each enumerated subexpression does NOT
2353 * match the regular expression (aka field
2355 * - 0 the entire string matching the
2356 * subexpression is returned for each match
2358 * - >0 enumerates only the indicated
2359 * subexpression from a match within the text.
2360 * @param m [IN] Policy flags for match rules.
2362 * @todo Implement this function.
2365 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2367 regex_constants::match_flag_type __m
2368 = regex_constants::match_default);
2371 * Constructs a %regex_token_iterator...
2372 * @param a [IN] The start of the text to search.
2373 * @param b [IN] One-past-the-end of the text to search.
2374 * @param re [IN] The regular expression to search for.
2375 * @param submatches [IN] A list of subexpressions to return for each
2376 * regular expression match within the text.
2377 * @param m [IN] Policy flags for match rules.
2379 * @todo Implement this function.
2382 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2383 const regex_type& __re,
2384 const std::vector<int>& __submatches,
2385 regex_constants::match_flag_type __m
2386 = regex_constants::match_default);
2389 * Constructs a %regex_token_iterator...
2390 * @param a [IN] The start of the text to search.
2391 * @param b [IN] One-past-the-end of the text to search.
2392 * @param re [IN] The regular expression to search for.
2393 * @param submatches [IN] A list of subexpressions to return for each
2394 * regular expression match within the text.
2395 * @param m [IN] Policy flags for match rules.
2397 * @todo Implement this function.
2400 template<std::size_t _Nm>
2401 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2402 const regex_type& __re,
2403 const int (&__submatches)[_Nm],
2404 regex_constants::match_flag_type __m
2405 = regex_constants::match_default);
2408 * @brief Copy constructs a %regex_token_iterator.
2409 * @param rhs [IN] A %regex_token_iterator to copy.
2410 * @todo Implement this function.
2412 regex_token_iterator(const regex_token_iterator& __rhs);
2415 * @brief Assigns a %regex_token_iterator to another.
2416 * @param rhs [IN] A %regex_token_iterator to copy.
2417 * @todo Implement this function.
2419 regex_token_iterator&
2420 operator=(const regex_token_iterator& __rhs);
2423 * @brief Compares a %regex_token_iterator to another for equality.
2424 * @todo Implement this function.
2427 operator==(const regex_token_iterator& __rhs);
2430 * @brief Compares a %regex_token_iterator to another for inequality.
2431 * @todo Implement this function.
2434 operator!=(const regex_token_iterator& __rhs);
2437 * @brief Dereferences a %regex_token_iterator.
2438 * @todo Implement this function.
2444 * @brief Selects a %regex_token_iterator member.
2445 * @todo Implement this function.
2451 * @brief Increments a %regex_token_iterator.
2452 * @todo Implement this function.
2454 regex_token_iterator&
2458 * @brief Postincrements a %regex_token_iterator.
2459 * @todo Implement this function.
2461 regex_token_iterator
2464 private: // data members for exposition only:
2465 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> position_iterator;
2467 position_iterator __position;
2468 const value_type* __result;
2469 value_type __suffix;
2471 std::vector<int> __subs;
2474 /** @brief Token iterator for C-style NULL-terminated strings. */
2475 typedef regex_token_iterator<const char*> cregex_token_iterator;
2476 /** @brief Token iterator for standard strings. */
2477 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2478 #ifdef _GLIBCXX_USE_WCHAR_T
2479 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2480 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2481 /** @brief Token iterator for standard wide-character strings. */
2482 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2486 _GLIBCXX_END_NAMESPACE_VERSION