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), __fmt + __builtin_strlen(__fmt),
1769 * @name 10.5 Allocator
1774 * @brief Gets a copy of the allocator.
1777 get_allocator() const
1778 { return _Base_type::get_allocator(); }
1788 * @brief Swaps the contents of two match_results.
1791 swap(match_results& __that)
1792 { _Base_type::swap(__that); }
1796 friend class __regex::_SpecializedResults<_Bi_iter, _Allocator>;
1799 typedef match_results<const char*> cmatch;
1800 typedef match_results<string::const_iterator> smatch;
1801 #ifdef _GLIBCXX_USE_WCHAR_T
1802 typedef match_results<const wchar_t*> wcmatch;
1803 typedef match_results<wstring::const_iterator> wsmatch;
1806 // match_results comparisons
1808 * @brief Compares two match_results for equality.
1809 * @returns true if the two objects refer to the same match,
1812 template<typename _Bi_iter, typename _Allocator>
1814 operator==(const match_results<_Bi_iter, _Allocator>& __m1,
1815 const match_results<_Bi_iter, _Allocator>& __m2)
1817 if (__m1.ready() != __m2.ready())
1819 if (!__m1.ready()) // both are not ready
1821 if (__m1.empty() != __m2.empty())
1823 if (__m1.empty()) // both are empty
1825 return __m1.prefix() == __m2.prefix()
1826 && __m1.size() == __m2.size()
1827 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1828 && __m1.suffix() == __m2.suffix();
1832 * @brief Compares two match_results for inequality.
1833 * @returns true if the two objects do not refer to the same match,
1836 template<typename _Bi_iter, class _Allocator>
1838 operator!=(const match_results<_Bi_iter, _Allocator>& __m1,
1839 const match_results<_Bi_iter, _Allocator>& __m2)
1840 { return !(__m1 == __m2); }
1842 // [7.10.6] match_results swap
1844 * @brief Swaps two match results.
1845 * @param lhs A match result.
1846 * @param rhs A match result.
1848 * The contents of the two match_results objects are swapped.
1850 template<typename _Bi_iter, typename _Allocator>
1852 swap(match_results<_Bi_iter, _Allocator>& __lhs,
1853 match_results<_Bi_iter, _Allocator>& __rhs)
1854 { __lhs.swap(__rhs); }
1856 // [7.11.2] Function template regex_match
1858 * @name Matching, Searching, and Replacing
1863 * @brief Determines if there is a match between the regular expression @p e
1864 * and all of the character sequence [first, last).
1866 * @param s Start of the character sequence to match.
1867 * @param e One-past-the-end of the character sequence to match.
1868 * @param m The match results.
1869 * @param re The regular expression.
1870 * @param flags Controls how the regular expression is matched.
1872 * @retval true A match exists.
1873 * @retval false Otherwise.
1875 * @throws an exception of type regex_error.
1877 * @todo Implement this function.
1879 template<typename _Bi_iter, typename _Allocator,
1880 typename _Ch_type, typename _Rx_traits>
1882 regex_match(_Bi_iter __s,
1884 match_results<_Bi_iter, _Allocator>& __m,
1885 const basic_regex<_Ch_type, _Rx_traits>& __re,
1886 regex_constants::match_flag_type __flags
1887 = regex_constants::match_default)
1889 __regex::_AutomatonPtr __a = __re._M_get_automaton();
1890 __regex::_Automaton::_SizeT __sz = __a->_M_sub_count();
1891 __regex::_SpecializedCursor<_Bi_iter> __cs(__s, __e);
1892 __regex::_SpecializedResults<_Bi_iter, _Allocator> __r(__sz, __cs, __m);
1893 __regex::_Grep_matcher __matcher(__cs, __r, __a, __flags);
1894 return __m[0].matched;
1898 * @brief Indicates if there is a match between the regular expression @p e
1899 * and all of the character sequence [first, last).
1901 * @param first Beginning of the character sequence to match.
1902 * @param last One-past-the-end of the character sequence to match.
1903 * @param re The regular expression.
1904 * @param flags Controls how the regular expression is matched.
1906 * @retval true A match exists.
1907 * @retval false Otherwise.
1909 * @throws an exception of type regex_error.
1911 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1913 regex_match(_Bi_iter __first, _Bi_iter __last,
1914 const basic_regex<_Ch_type, _Rx_traits>& __re,
1915 regex_constants::match_flag_type __flags
1916 = regex_constants::match_default)
1918 match_results<_Bi_iter> __what;
1919 return regex_match(__first, __last, __what, __re, __flags);
1923 * @brief Determines if there is a match between the regular expression @p e
1924 * and a C-style null-terminated string.
1926 * @param s The C-style null-terminated string to match.
1927 * @param m The match results.
1928 * @param re The regular expression.
1929 * @param f Controls how the regular expression is matched.
1931 * @retval true A match exists.
1932 * @retval false Otherwise.
1934 * @throws an exception of type regex_error.
1936 template<typename _Ch_type, typename _Allocator, typename _Rx_traits>
1938 regex_match(const _Ch_type* __s,
1939 match_results<const _Ch_type*, _Allocator>& __m,
1940 const basic_regex<_Ch_type, _Rx_traits>& __re,
1941 regex_constants::match_flag_type __f
1942 = regex_constants::match_default)
1943 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
1946 * @brief Determines if there is a match between the regular expression @p e
1949 * @param s The string to match.
1950 * @param m The match results.
1951 * @param re The regular expression.
1952 * @param flags Controls how the regular expression is matched.
1954 * @retval true A match exists.
1955 * @retval false Otherwise.
1957 * @throws an exception of type regex_error.
1959 template<typename _Ch_traits, typename _Ch_alloc,
1960 typename _Allocator, typename _Ch_type, typename _Rx_traits>
1962 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
1963 match_results<typename basic_string<_Ch_type,
1964 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
1965 const basic_regex<_Ch_type, _Rx_traits>& __re,
1966 regex_constants::match_flag_type __flags
1967 = regex_constants::match_default)
1968 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
1971 * @brief Indicates if there is a match between the regular expression @p e
1972 * and a C-style null-terminated string.
1974 * @param s The C-style null-terminated string to match.
1975 * @param re The regular expression.
1976 * @param f Controls how the regular expression is matched.
1978 * @retval true A match exists.
1979 * @retval false Otherwise.
1981 * @throws an exception of type regex_error.
1983 template<typename _Ch_type, class _Rx_traits>
1985 regex_match(const _Ch_type* __s,
1986 const basic_regex<_Ch_type, _Rx_traits>& __re,
1987 regex_constants::match_flag_type __f
1988 = regex_constants::match_default)
1989 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
1992 * @brief Indicates if there is a match between the regular expression @p e
1995 * @param s [IN] The string to match.
1996 * @param re [IN] The regular expression.
1997 * @param flags [IN] Controls how the regular expression is matched.
1999 * @retval true A match exists.
2000 * @retval false Otherwise.
2002 * @throws an exception of type regex_error.
2004 template<typename _Ch_traits, typename _Str_allocator,
2005 typename _Ch_type, typename _Rx_traits>
2007 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2008 const basic_regex<_Ch_type, _Rx_traits>& __re,
2009 regex_constants::match_flag_type __flags
2010 = regex_constants::match_default)
2011 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2013 // [7.11.3] Function template regex_search
2015 * Searches for a regular expression within a range.
2016 * @param first [IN] The start of the string to search.
2017 * @param last [IN] One-past-the-end of the string to search.
2018 * @param m [OUT] The match results.
2019 * @param re [IN] The regular expression to search for.
2020 * @param flags [IN] Search policy flags.
2021 * @retval true A match was found within the string.
2022 * @retval false No match was found within the string, the content of %m is
2025 * @throws an exception of type regex_error.
2027 * @todo Implement this function.
2029 template<typename _Bi_iter, typename _Allocator,
2030 typename _Ch_type, typename _Rx_traits>
2032 regex_search(_Bi_iter __first, _Bi_iter __last,
2033 match_results<_Bi_iter, _Allocator>& __m,
2034 const basic_regex<_Ch_type, _Rx_traits>& __re,
2035 regex_constants::match_flag_type __flags
2036 = regex_constants::match_default)
2040 * Searches for a regular expression within a range.
2041 * @param first [IN] The start of the string to search.
2042 * @param last [IN] One-past-the-end of the string to search.
2043 * @param re [IN] The regular expression to search for.
2044 * @param flags [IN] Search policy flags.
2045 * @retval true A match was found within the string.
2046 * @retval false No match was found within the string.
2049 * @throws an exception of type regex_error.
2051 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2053 regex_search(_Bi_iter __first, _Bi_iter __last,
2054 const basic_regex<_Ch_type, _Rx_traits>& __re,
2055 regex_constants::match_flag_type __flags
2056 = regex_constants::match_default)
2058 match_results<_Bi_iter> __what;
2059 return regex_search(__first, __last, __what, __re, __flags);
2063 * @brief Searches for a regular expression within a C-string.
2064 * @param s [IN] A C-string to search for the regex.
2065 * @param m [OUT] The set of regex matches.
2066 * @param e [IN] The regex to search for in @p s.
2067 * @param f [IN] The search flags.
2068 * @retval true A match was found within the string.
2069 * @retval false No match was found within the string, the content of %m is
2073 * @throws an exception of type regex_error.
2075 template<typename _Ch_type, class _Allocator, class _Rx_traits>
2077 regex_search(const _Ch_type* __s,
2078 match_results<const _Ch_type*, _Allocator>& __m,
2079 const basic_regex<_Ch_type, _Rx_traits>& __e,
2080 regex_constants::match_flag_type __f
2081 = regex_constants::match_default)
2082 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2085 * @brief Searches for a regular expression within a C-string.
2086 * @param s [IN] The C-string to search.
2087 * @param e [IN] The regular expression to search for.
2088 * @param f [IN] Search policy flags.
2089 * @retval true A match was found within the string.
2090 * @retval false No match was found within the string.
2093 * @throws an exception of type regex_error.
2095 template<typename _Ch_type, typename _Rx_traits>
2097 regex_search(const _Ch_type* __s,
2098 const basic_regex<_Ch_type, _Rx_traits>& __e,
2099 regex_constants::match_flag_type __f
2100 = regex_constants::match_default)
2101 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2104 * @brief Searches for a regular expression within a string.
2105 * @param s [IN] The string to search.
2106 * @param e [IN] The regular expression to search for.
2107 * @param flags [IN] Search policy flags.
2108 * @retval true A match was found within the string.
2109 * @retval false No match was found within the string.
2112 * @throws an exception of type regex_error.
2114 template<typename _Ch_traits, typename _String_allocator,
2115 typename _Ch_type, typename _Rx_traits>
2117 regex_search(const basic_string<_Ch_type, _Ch_traits,
2118 _String_allocator>& __s,
2119 const basic_regex<_Ch_type, _Rx_traits>& __e,
2120 regex_constants::match_flag_type __flags
2121 = regex_constants::match_default)
2122 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2125 * @brief Searches for a regular expression within a string.
2126 * @param s [IN] A C++ string to search for the regex.
2127 * @param m [OUT] The set of regex matches.
2128 * @param e [IN] The regex to search for in @p s.
2129 * @param f [IN] The search flags.
2130 * @retval true A match was found within the string.
2131 * @retval false No match was found within the string, the content of %m is
2134 * @throws an exception of type regex_error.
2136 template<typename _Ch_traits, typename _Ch_alloc,
2137 typename _Allocator, typename _Ch_type,
2138 typename _Rx_traits>
2140 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2141 match_results<typename basic_string<_Ch_type,
2142 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
2143 const basic_regex<_Ch_type, _Rx_traits>& __e,
2144 regex_constants::match_flag_type __f
2145 = regex_constants::match_default)
2146 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2148 // std [28.11.4] Function template regex_replace
2159 * @throws an exception of type regex_error.
2161 * @todo Implement this function.
2163 template<typename _Out_iter, typename _Bi_iter,
2164 typename _Rx_traits, typename _Ch_type>
2166 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2167 const basic_regex<_Ch_type, _Rx_traits>& __e,
2168 const basic_string<_Ch_type>& __fmt,
2169 regex_constants::match_flag_type __flags
2170 = regex_constants::match_default)
2180 * @returns a copy of string @p s with replacements.
2182 * @throws an exception of type regex_error.
2184 template<typename _Rx_traits, typename _Ch_type>
2185 inline basic_string<_Ch_type>
2186 regex_replace(const basic_string<_Ch_type>& __s,
2187 const basic_regex<_Ch_type, _Rx_traits>& __e,
2188 const basic_string<_Ch_type>& __fmt,
2189 regex_constants::match_flag_type __flags
2190 = regex_constants::match_default)
2192 std::string __result;
2193 regex_replace(std::back_inserter(__result),
2194 __s.begin(), __s.end(), __e, __fmt, __flags);
2200 // std [28.12] Class template regex_iterator
2202 * An iterator adaptor that will provide repeated calls of regex_search over
2203 * a range until no more matches remain.
2205 template<typename _Bi_iter,
2206 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2207 typename _Rx_traits = regex_traits<_Ch_type> >
2208 class regex_iterator
2211 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2212 typedef match_results<_Bi_iter> value_type;
2213 typedef std::ptrdiff_t difference_type;
2214 typedef const value_type* pointer;
2215 typedef const value_type& reference;
2216 typedef std::forward_iterator_tag iterator_category;
2220 * @brief Provides a singular iterator, useful for indicating
2221 * one-past-the-end of a range.
2222 * @todo Implement this function.
2228 * Constructs a %regex_iterator...
2229 * @param a [IN] The start of a text range to search.
2230 * @param b [IN] One-past-the-end of the text range to search.
2231 * @param re [IN] The regular expression to match.
2232 * @param m [IN] Policy flags for match rules.
2233 * @todo Implement this function.
2236 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2237 regex_constants::match_flag_type __m
2238 = regex_constants::match_default);
2241 * Copy constructs a %regex_iterator.
2242 * @todo Implement this function.
2245 regex_iterator(const regex_iterator& __rhs);
2248 * @todo Implement this function.
2252 operator=(const regex_iterator& __rhs);
2255 * @todo Implement this function.
2259 operator==(const regex_iterator& __rhs);
2262 * @todo Implement this function.
2266 operator!=(const regex_iterator& __rhs);
2269 * @todo Implement this function.
2276 * @todo Implement this function.
2283 * @todo Implement this function.
2290 * @todo Implement this function.
2297 // these members are shown for exposition only:
2300 const regex_type* pregex;
2301 regex_constants::match_flag_type flags;
2302 match_results<_Bi_iter> match;
2305 typedef regex_iterator<const char*> cregex_iterator;
2306 typedef regex_iterator<string::const_iterator> sregex_iterator;
2307 #ifdef _GLIBCXX_USE_WCHAR_T
2308 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2309 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2312 // [7.12.2] Class template regex_token_iterator
2314 * Iterates over submatches in a range (or @a splits a text string).
2316 * The purpose of this iterator is to enumerate all, or all specified,
2317 * matches of a regular expression within a text range. The dereferenced
2318 * value of an iterator of this class is a std::sub_match object.
2320 template<typename _Bi_iter,
2321 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2322 typename _Rx_traits = regex_traits<_Ch_type> >
2323 class regex_token_iterator
2326 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2327 typedef sub_match<_Bi_iter> value_type;
2328 typedef std::ptrdiff_t difference_type;
2329 typedef const value_type* pointer;
2330 typedef const value_type& reference;
2331 typedef std::forward_iterator_tag iterator_category;
2335 * @brief Default constructs a %regex_token_iterator.
2336 * @todo Implement this function.
2338 * A default-constructed %regex_token_iterator is a singular iterator
2339 * that will compare equal to the one-past-the-end value for any
2340 * iterator of the same type.
2342 regex_token_iterator();
2345 * Constructs a %regex_token_iterator...
2346 * @param a [IN] The start of the text to search.
2347 * @param b [IN] One-past-the-end of the text to search.
2348 * @param re [IN] The regular expression to search for.
2349 * @param submatch [IN] Which submatch to return. There are some
2350 * special values for this parameter:
2351 * - -1 each enumerated subexpression does NOT
2352 * match the regular expression (aka field
2354 * - 0 the entire string matching the
2355 * subexpression is returned for each match
2357 * - >0 enumerates only the indicated
2358 * subexpression from a match within the text.
2359 * @param m [IN] Policy flags for match rules.
2361 * @todo Implement this function.
2364 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2366 regex_constants::match_flag_type __m
2367 = regex_constants::match_default);
2370 * Constructs a %regex_token_iterator...
2371 * @param a [IN] The start of the text to search.
2372 * @param b [IN] One-past-the-end of the text to search.
2373 * @param re [IN] The regular expression to search for.
2374 * @param submatches [IN] A list of subexpressions to return for each
2375 * regular expression match within the text.
2376 * @param m [IN] Policy flags for match rules.
2378 * @todo Implement this function.
2381 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2382 const regex_type& __re,
2383 const std::vector<int>& __submatches,
2384 regex_constants::match_flag_type __m
2385 = regex_constants::match_default);
2388 * Constructs a %regex_token_iterator...
2389 * @param a [IN] The start of the text to search.
2390 * @param b [IN] One-past-the-end of the text to search.
2391 * @param re [IN] The regular expression to search for.
2392 * @param submatches [IN] A list of subexpressions to return for each
2393 * regular expression match within the text.
2394 * @param m [IN] Policy flags for match rules.
2396 * @todo Implement this function.
2399 template<std::size_t _Nm>
2400 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2401 const regex_type& __re,
2402 const int (&__submatches)[_Nm],
2403 regex_constants::match_flag_type __m
2404 = regex_constants::match_default);
2407 * @brief Copy constructs a %regex_token_iterator.
2408 * @param rhs [IN] A %regex_token_iterator to copy.
2409 * @todo Implement this function.
2411 regex_token_iterator(const regex_token_iterator& __rhs);
2414 * @brief Assigns a %regex_token_iterator to another.
2415 * @param rhs [IN] A %regex_token_iterator to copy.
2416 * @todo Implement this function.
2418 regex_token_iterator&
2419 operator=(const regex_token_iterator& __rhs);
2422 * @brief Compares a %regex_token_iterator to another for equality.
2423 * @todo Implement this function.
2426 operator==(const regex_token_iterator& __rhs);
2429 * @brief Compares a %regex_token_iterator to another for inequality.
2430 * @todo Implement this function.
2433 operator!=(const regex_token_iterator& __rhs);
2436 * @brief Dereferences a %regex_token_iterator.
2437 * @todo Implement this function.
2443 * @brief Selects a %regex_token_iterator member.
2444 * @todo Implement this function.
2450 * @brief Increments a %regex_token_iterator.
2451 * @todo Implement this function.
2453 regex_token_iterator&
2457 * @brief Postincrements a %regex_token_iterator.
2458 * @todo Implement this function.
2460 regex_token_iterator
2463 private: // data members for exposition only:
2464 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> position_iterator;
2466 position_iterator __position;
2467 const value_type* __result;
2468 value_type __suffix;
2470 std::vector<int> __subs;
2473 /** @brief Token iterator for C-style NULL-terminated strings. */
2474 typedef regex_token_iterator<const char*> cregex_token_iterator;
2475 /** @brief Token iterator for standard strings. */
2476 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2477 #ifdef _GLIBCXX_USE_WCHAR_T
2478 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2479 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2480 /** @brief Token iterator for standard wide-character strings. */
2481 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2485 _GLIBCXX_END_NAMESPACE_VERSION