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 _Rx_traits traits_type;
347 typedef typename traits_type::string_type string_type;
348 typedef regex_constants::syntax_option_type flag_type;
349 typedef typename traits_type::locale_type locale_type;
356 static constexpr regex_constants::syntax_option_type icase
357 = regex_constants::icase;
358 static constexpr regex_constants::syntax_option_type nosubs
359 = regex_constants::nosubs;
360 static constexpr regex_constants::syntax_option_type optimize
361 = regex_constants::optimize;
362 static constexpr regex_constants::syntax_option_type collate
363 = regex_constants::collate;
364 static constexpr regex_constants::syntax_option_type ECMAScript
365 = regex_constants::ECMAScript;
366 static constexpr regex_constants::syntax_option_type basic
367 = regex_constants::basic;
368 static constexpr regex_constants::syntax_option_type extended
369 = regex_constants::extended;
370 static constexpr regex_constants::syntax_option_type awk
371 = regex_constants::awk;
372 static constexpr regex_constants::syntax_option_type grep
373 = regex_constants::grep;
374 static constexpr regex_constants::syntax_option_type egrep
375 = regex_constants::egrep;
378 // [7.8.2] construct/copy/destroy
380 * Constructs a basic regular expression that does not match any
381 * character sequence.
384 : _M_flags(regex_constants::ECMAScript),
385 _M_automaton(__regex::__compile<const _Ch_type*, _Rx_traits>(0, 0,
386 _M_traits, _M_flags))
390 * @brief Constructs a basic regular expression from the sequence
391 * [p, p + char_traits<_Ch_type>::length(p)) interpreted according to the
394 * @param p A pointer to the start of a C-style null-terminated string
395 * containing a regular expression.
396 * @param f Flags indicating the syntax rules and options.
398 * @throws regex_error if @p p is not a valid regular expression.
401 basic_regex(const _Ch_type* __p,
402 flag_type __f = regex_constants::ECMAScript)
404 _M_automaton(__regex::__compile(__p, __p + _Rx_traits::length(__p),
405 _M_traits, _M_flags))
409 * @brief Constructs a basic regular expression from the sequence
410 * [p, p + len) interpreted according to the flags in @p f.
412 * @param p A pointer to the start of a string containing a regular
414 * @param len The length of the string containing the regular expression.
415 * @param f Flags indicating the syntax rules and options.
417 * @throws regex_error if @p p is not a valid regular expression.
419 basic_regex(const _Ch_type* __p, std::size_t __len, flag_type __f)
421 _M_automaton(__regex::__compile(__p, __p + __len, _M_traits, _M_flags))
425 * @brief Copy-constructs a basic regular expression.
427 * @param rhs A @p regex object.
429 basic_regex(const basic_regex& __rhs)
430 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
431 _M_automaton(__rhs._M_automaton)
435 * @brief Move-constructs a basic regular expression.
437 * @param rhs A @p regex object.
439 basic_regex(const basic_regex&& __rhs) noexcept
440 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
441 _M_automaton(std::move(__rhs._M_automaton))
445 * @brief Constructs a basic regular expression from the string
446 * @p s interpreted according to the flags in @p f.
448 * @param s A string containing a regular expression.
449 * @param f Flags indicating the syntax rules and options.
451 * @throws regex_error if @p s is not a valid regular expression.
453 template<typename _Ch_traits, typename _Ch_alloc>
455 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
457 flag_type __f = regex_constants::ECMAScript)
459 _M_automaton(__regex::__compile(__s.begin(), __s.end(),
460 _M_traits, _M_flags))
464 * @brief Constructs a basic regular expression from the range
465 * [first, last) interpreted according to the flags in @p f.
467 * @param first The start of a range containing a valid regular
469 * @param last The end of a range containing a valid regular
471 * @param f The format flags of the regular expression.
473 * @throws regex_error if @p [first, last) is not a valid regular
476 template<typename _InputIterator>
477 basic_regex(_InputIterator __first, _InputIterator __last,
478 flag_type __f = regex_constants::ECMAScript)
480 _M_automaton(__regex::__compile(__first, __last, _M_traits, _M_flags))
484 * @brief Constructs a basic regular expression from an initializer list.
486 * @param l The initializer list.
487 * @param f The format flags of the regular expression.
489 * @throws regex_error if @p l is not a valid regular expression.
491 basic_regex(initializer_list<_Ch_type> __l,
492 flag_type __f = regex_constants::ECMAScript)
494 _M_automaton(__regex::__compile(__l.begin(), __l.end(),
495 _M_traits, _M_flags))
499 * @brief Destroys a basic regular expression.
505 * @brief Assigns one regular expression to another.
508 operator=(const basic_regex& __rhs)
509 { return this->assign(__rhs); }
512 * @brief Move-assigns one regular expression to another.
515 operator=(basic_regex&& __rhs) noexcept
516 { return this->assign(std::move(__rhs)); }
519 * @brief Replaces a regular expression with a new one constructed from
520 * a C-style null-terminated string.
522 * @param A pointer to the start of a null-terminated C-style string
523 * containing a regular expression.
526 operator=(const _Ch_type* __p)
527 { return this->assign(__p, flags()); }
530 * @brief Replaces a regular expression with a new one constructed from
533 * @param A pointer to a string containing a regular expression.
535 template<typename _Ch_typeraits, typename _Allocator>
537 operator=(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s)
538 { return this->assign(__s, flags()); }
542 * @brief the real assignment operator.
544 * @param rhs Another regular expression object.
547 assign(const basic_regex& __rhs)
549 basic_regex __tmp(__rhs);
555 * @brief The move-assignment operator.
557 * @param rhs Another regular expression object.
560 assign(basic_regex&& __rhs) noexcept
562 basic_regex __tmp(std::move(__rhs));
568 * @brief Assigns a new regular expression to a regex object from a
569 * C-style null-terminated string containing a regular expression
572 * @param p A pointer to a C-style null-terminated string containing
573 * a regular expression pattern.
574 * @param flags Syntax option flags.
576 * @throws regex_error if p does not contain a valid regular expression
577 * pattern interpreted according to @p flags. If regex_error is thrown,
578 * *this remains unchanged.
581 assign(const _Ch_type* __p,
582 flag_type __flags = regex_constants::ECMAScript)
583 { return this->assign(string_type(__p), __flags); }
586 * @brief Assigns a new regular expression to a regex object from a
587 * C-style string containing a regular expression pattern.
589 * @param p A pointer to a C-style string containing a
590 * regular expression pattern.
591 * @param len The length of the regular expression pattern string.
592 * @param flags Syntax option flags.
594 * @throws regex_error if p does not contain a valid regular expression
595 * pattern interpreted according to @p flags. If regex_error is thrown,
596 * *this remains unchanged.
599 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
600 { return this->assign(string_type(__p, __len), __flags); }
603 * @brief Assigns a new regular expression to a regex object from a
604 * string containing a regular expression pattern.
606 * @param s A string containing a regular expression pattern.
607 * @param flags Syntax option flags.
609 * @throws regex_error if p does not contain a valid regular expression
610 * pattern interpreted according to @p flags. If regex_error is thrown,
611 * *this remains unchanged.
613 template<typename _Ch_typeraits, typename _Allocator>
615 assign(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s,
616 flag_type __f = regex_constants::ECMAScript)
618 basic_regex __tmp(__s, __f);
624 * @brief Assigns a new regular expression to a regex object.
626 * @param first The start of a range containing a valid regular
628 * @param last The end of a range containing a valid regular
630 * @param flags Syntax option flags.
632 * @throws regex_error if p does not contain a valid regular expression
633 * pattern interpreted according to @p flags. If regex_error is thrown,
634 * the object remains unchanged.
636 template<typename _InputIterator>
638 assign(_InputIterator __first, _InputIterator __last,
639 flag_type __flags = regex_constants::ECMAScript)
640 { return this->assign(string_type(__first, __last), __flags); }
643 * @brief Assigns a new regular expression to a regex object.
645 * @param l An initializer list representing a regular expression.
646 * @param flags Syntax option flags.
648 * @throws regex_error if @p l does not contain a valid regular
649 * expression pattern interpreted according to @p flags. If regex_error
650 * is thrown, the object remains unchanged.
653 assign(initializer_list<_Ch_type> __l,
654 flag_type __f = regex_constants::ECMAScript)
655 { return this->assign(__l.begin(), __l.end(), __f); }
657 // [7.8.4] const operations
659 * @brief Gets the number of marked subexpressions within the regular
664 { return _M_automaton->_M_sub_count() - 1; }
667 * @brief Gets the flags used to construct the regular expression
668 * or in the last call to assign().
676 * @brief Imbues the regular expression object with the given locale.
678 * @param loc A locale.
681 imbue(locale_type __loc)
682 { return _M_traits.imbue(__loc); }
685 * @brief Gets the locale currently imbued in the regular expression
690 { return _M_traits.getloc(); }
694 * @brief Swaps the contents of two regular expression objects.
696 * @param rhs Another regular expression object.
699 swap(basic_regex& __rhs)
701 std::swap(_M_flags, __rhs._M_flags);
702 std::swap(_M_traits, __rhs._M_traits);
703 std::swap(_M_automaton, __rhs._M_automaton);
706 #ifdef _GLIBCXX_DEBUG
708 _M_dot(std::ostream& __ostr)
709 { _M_automaton->_M_dot(__ostr); }
712 const __regex::_AutomatonPtr&
713 _M_get_automaton() const
714 { return _M_automaton; }
718 _Rx_traits _M_traits;
719 __regex::_AutomatonPtr _M_automaton;
722 /** @brief Standard regular expressions. */
723 typedef basic_regex<char> regex;
724 #ifdef _GLIBCXX_USE_WCHAR_T
725 /** @brief Standard wide-character regular expressions. */
726 typedef basic_regex<wchar_t> wregex;
730 // [7.8.6] basic_regex swap
732 * @brief Swaps the contents of two regular expression objects.
733 * @param lhs First regular expression.
734 * @param rhs Second regular expression.
736 template<typename _Ch_type, typename _Rx_traits>
738 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
739 basic_regex<_Ch_type, _Rx_traits>& __rhs)
740 { __lhs.swap(__rhs); }
743 // [7.9] Class template sub_match
745 * A sequence of characters matched by a particular marked sub-expression.
747 * An object of this class is essentially a pair of iterators marking a
748 * matched subexpression within a regular expression pattern match. Such
749 * objects can be converted to and compared with std::basic_string objects
750 * of a similar base character type as the pattern matched by the regular
753 * The iterators that make up the pair are the usual half-open interval
754 * referencing the actual original pattern matched.
756 template<typename _BiIter>
757 class sub_match : public std::pair<_BiIter, _BiIter>
760 typedef typename iterator_traits<_BiIter>::value_type value_type;
761 typedef typename iterator_traits<_BiIter>::difference_type
763 typedef _BiIter iterator;
764 typedef std::basic_string<value_type> string_type;
769 constexpr sub_match() : matched() { }
772 * Gets the length of the matching sequence.
776 { return this->matched ? std::distance(this->first, this->second) : 0; }
779 * @brief Gets the matching sequence as a string.
781 * @returns the matching sequence as a string.
783 * This is the implicit conversion operator. It is identical to the
784 * str() member function except that it will want to pop up in
785 * unexpected places and cause a great deal of confusion and cursing
788 operator string_type() const
791 ? string_type(this->first, this->second)
796 * @brief Gets the matching sequence as a string.
798 * @returns the matching sequence as a string.
804 ? string_type(this->first, this->second)
809 * @brief Compares this and another matched sequence.
811 * @param s Another matched sequence to compare to this one.
813 * @retval <0 this matched sequence will collate before @p s.
814 * @retval =0 this matched sequence is equivalent to @p s.
815 * @retval <0 this matched sequence will collate after @p s.
818 compare(const sub_match& __s) const
819 { return this->str().compare(__s.str()); }
822 * @brief Compares this sub_match to a string.
824 * @param s A string to compare to this sub_match.
826 * @retval <0 this matched sequence will collate before @p s.
827 * @retval =0 this matched sequence is equivalent to @p s.
828 * @retval <0 this matched sequence will collate after @p s.
831 compare(const string_type& __s) const
832 { return this->str().compare(__s); }
835 * @brief Compares this sub_match to a C-style string.
837 * @param s A C-style string to compare to this sub_match.
839 * @retval <0 this matched sequence will collate before @p s.
840 * @retval =0 this matched sequence is equivalent to @p s.
841 * @retval <0 this matched sequence will collate after @p s.
844 compare(const value_type* __s) const
845 { return this->str().compare(__s); }
849 /** @brief Standard regex submatch over a C-style null-terminated string. */
850 typedef sub_match<const char*> csub_match;
851 /** @brief Standard regex submatch over a standard string. */
852 typedef sub_match<string::const_iterator> ssub_match;
853 #ifdef _GLIBCXX_USE_WCHAR_T
854 /** @brief Regex submatch over a C-style null-terminated wide string. */
855 typedef sub_match<const wchar_t*> wcsub_match;
856 /** @brief Regex submatch over a standard wide string. */
857 typedef sub_match<wstring::const_iterator> wssub_match;
860 // [7.9.2] sub_match non-member operators
863 * @brief Tests the equivalence of two regular expression submatches.
864 * @param lhs First regular expression submatch.
865 * @param rhs Second regular expression submatch.
866 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
868 template<typename _BiIter>
870 operator==(const sub_match<_BiIter>& __lhs,
871 const sub_match<_BiIter>& __rhs)
872 { return __lhs.compare(__rhs) == 0; }
875 * @brief Tests the inequivalence of two regular expression submatches.
876 * @param lhs First regular expression submatch.
877 * @param rhs Second regular expression submatch.
878 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
880 template<typename _BiIter>
882 operator!=(const sub_match<_BiIter>& __lhs,
883 const sub_match<_BiIter>& __rhs)
884 { return __lhs.compare(__rhs) != 0; }
887 * @brief Tests the ordering of two regular expression submatches.
888 * @param lhs First regular expression submatch.
889 * @param rhs Second regular expression submatch.
890 * @returns true if @a lhs precedes @a rhs, false otherwise.
892 template<typename _BiIter>
894 operator<(const sub_match<_BiIter>& __lhs,
895 const sub_match<_BiIter>& __rhs)
896 { return __lhs.compare(__rhs) < 0; }
899 * @brief Tests the ordering of two regular expression submatches.
900 * @param lhs First regular expression submatch.
901 * @param rhs Second regular expression submatch.
902 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
904 template<typename _BiIter>
906 operator<=(const sub_match<_BiIter>& __lhs,
907 const sub_match<_BiIter>& __rhs)
908 { return __lhs.compare(__rhs) <= 0; }
911 * @brief Tests the ordering of two regular expression submatches.
912 * @param lhs First regular expression submatch.
913 * @param rhs Second regular expression submatch.
914 * @returns true if @a lhs does not precede @a rhs, false otherwise.
916 template<typename _BiIter>
918 operator>=(const sub_match<_BiIter>& __lhs,
919 const sub_match<_BiIter>& __rhs)
920 { return __lhs.compare(__rhs) >= 0; }
923 * @brief Tests the ordering of two regular expression submatches.
924 * @param lhs First regular expression submatch.
925 * @param rhs Second regular expression submatch.
926 * @returns true if @a lhs succeeds @a rhs, false otherwise.
928 template<typename _BiIter>
930 operator>(const sub_match<_BiIter>& __lhs,
931 const sub_match<_BiIter>& __rhs)
932 { return __lhs.compare(__rhs) > 0; }
935 * @brief Tests the equivalence of a string and a regular expression
937 * @param lhs A string.
938 * @param rhs A regular expression submatch.
939 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
941 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
943 operator==(const basic_string<
944 typename iterator_traits<_Bi_iter>::value_type,
945 _Ch_traits, _Ch_alloc>& __lhs,
946 const sub_match<_Bi_iter>& __rhs)
947 { return __rhs.compare(__lhs.c_str()) == 0; }
950 * @brief Tests the inequivalence of a string and a regular expression
952 * @param lhs A string.
953 * @param rhs A regular expression submatch.
954 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
956 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
958 operator!=(const basic_string<
959 typename iterator_traits<_Bi_iter>::value_type,
960 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
961 { return !(__lhs == __rhs); }
964 * @brief Tests the ordering of a string and a regular expression submatch.
965 * @param lhs A string.
966 * @param rhs A regular expression submatch.
967 * @returns true if @a lhs precedes @a rhs, false otherwise.
969 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
971 operator<(const basic_string<
972 typename iterator_traits<_Bi_iter>::value_type,
973 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
974 { return __rhs.compare(__lhs.c_str()) > 0; }
977 * @brief Tests the ordering of a string and a regular expression submatch.
978 * @param lhs A string.
979 * @param rhs A regular expression submatch.
980 * @returns true if @a lhs succeeds @a rhs, false otherwise.
982 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
984 operator>(const basic_string<
985 typename iterator_traits<_Bi_iter>::value_type,
986 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
987 { return __rhs < __lhs; }
990 * @brief Tests the ordering of a string and a regular expression submatch.
991 * @param lhs A string.
992 * @param rhs A regular expression submatch.
993 * @returns true if @a lhs does not precede @a rhs, false otherwise.
995 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
997 operator>=(const basic_string<
998 typename iterator_traits<_Bi_iter>::value_type,
999 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1000 { return !(__lhs < __rhs); }
1003 * @brief Tests the ordering of a string and a regular expression submatch.
1004 * @param lhs A string.
1005 * @param rhs A regular expression submatch.
1006 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1008 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1010 operator<=(const basic_string<
1011 typename iterator_traits<_Bi_iter>::value_type,
1012 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1013 { return !(__rhs < __lhs); }
1016 * @brief Tests the equivalence of a regular expression submatch and a
1018 * @param lhs A regular expression submatch.
1019 * @param rhs A string.
1020 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1022 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1024 operator==(const sub_match<_Bi_iter>& __lhs,
1026 typename iterator_traits<_Bi_iter>::value_type,
1027 _Ch_traits, _Ch_alloc>& __rhs)
1028 { return __lhs.compare(__rhs.c_str()) == 0; }
1031 * @brief Tests the inequivalence of a regular expression submatch and a
1033 * @param lhs A regular expression submatch.
1034 * @param rhs A string.
1035 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1037 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1039 operator!=(const sub_match<_Bi_iter>& __lhs,
1041 typename iterator_traits<_Bi_iter>::value_type,
1042 _Ch_traits, _Ch_alloc>& __rhs)
1043 { return !(__lhs == __rhs); }
1046 * @brief Tests the ordering of a regular expression submatch and a string.
1047 * @param lhs A regular expression submatch.
1048 * @param rhs A string.
1049 * @returns true if @a lhs precedes @a rhs, false otherwise.
1051 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1053 operator<(const sub_match<_Bi_iter>& __lhs,
1055 typename iterator_traits<_Bi_iter>::value_type,
1056 _Ch_traits, _Ch_alloc>& __rhs)
1057 { return __lhs.compare(__rhs.c_str()) < 0; }
1060 * @brief Tests the ordering of a regular expression submatch and a string.
1061 * @param lhs A regular expression submatch.
1062 * @param rhs A string.
1063 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1065 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1067 operator>(const sub_match<_Bi_iter>& __lhs,
1069 typename iterator_traits<_Bi_iter>::value_type,
1070 _Ch_traits, _Ch_alloc>& __rhs)
1071 { return __rhs < __lhs; }
1074 * @brief Tests the ordering of a regular expression submatch and a string.
1075 * @param lhs A regular expression submatch.
1076 * @param rhs A string.
1077 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1079 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1081 operator>=(const sub_match<_Bi_iter>& __lhs,
1083 typename iterator_traits<_Bi_iter>::value_type,
1084 _Ch_traits, _Ch_alloc>& __rhs)
1085 { return !(__lhs < __rhs); }
1088 * @brief Tests the ordering of a regular expression submatch and a string.
1089 * @param lhs A regular expression submatch.
1090 * @param rhs A string.
1091 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1093 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1095 operator<=(const sub_match<_Bi_iter>& __lhs,
1097 typename iterator_traits<_Bi_iter>::value_type,
1098 _Ch_traits, _Ch_alloc>& __rhs)
1099 { return !(__rhs < __lhs); }
1102 * @brief Tests the equivalence of a C string and a regular expression
1104 * @param lhs A C string.
1105 * @param rhs A regular expression submatch.
1106 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1108 template<typename _Bi_iter>
1110 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1111 const sub_match<_Bi_iter>& __rhs)
1112 { return __rhs.compare(__lhs) == 0; }
1115 * @brief Tests the inequivalence of an iterator value and a regular
1116 * expression submatch.
1117 * @param lhs A regular expression submatch.
1118 * @param rhs A string.
1119 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1121 template<typename _Bi_iter>
1123 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1124 const sub_match<_Bi_iter>& __rhs)
1125 { return !(__lhs == __rhs); }
1128 * @brief Tests the ordering of a string and a regular expression submatch.
1129 * @param lhs A string.
1130 * @param rhs A regular expression submatch.
1131 * @returns true if @a lhs precedes @a rhs, false otherwise.
1133 template<typename _Bi_iter>
1135 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1136 const sub_match<_Bi_iter>& __rhs)
1137 { return __rhs.compare(__lhs) > 0; }
1140 * @brief Tests the ordering of a string and a regular expression submatch.
1141 * @param lhs A string.
1142 * @param rhs A regular expression submatch.
1143 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1145 template<typename _Bi_iter>
1147 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1148 const sub_match<_Bi_iter>& __rhs)
1149 { return __rhs < __lhs; }
1152 * @brief Tests the ordering of a string and a regular expression submatch.
1153 * @param lhs A string.
1154 * @param rhs A regular expression submatch.
1155 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1157 template<typename _Bi_iter>
1159 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1160 const sub_match<_Bi_iter>& __rhs)
1161 { return !(__lhs < __rhs); }
1164 * @brief Tests the ordering of a string and a regular expression submatch.
1165 * @param lhs A string.
1166 * @param rhs A regular expression submatch.
1167 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1169 template<typename _Bi_iter>
1171 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1172 const sub_match<_Bi_iter>& __rhs)
1173 { return !(__rhs < __lhs); }
1176 * @brief Tests the equivalence of a regular expression submatch and a
1178 * @param lhs A regular expression submatch.
1179 * @param rhs A pointer to a string?
1180 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1182 template<typename _Bi_iter>
1184 operator==(const sub_match<_Bi_iter>& __lhs,
1185 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1186 { return __lhs.compare(__rhs) == 0; }
1189 * @brief Tests the inequivalence of a regular expression submatch and a
1191 * @param lhs A regular expression submatch.
1192 * @param rhs A pointer to a string.
1193 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1195 template<typename _Bi_iter>
1197 operator!=(const sub_match<_Bi_iter>& __lhs,
1198 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1199 { return !(__lhs == __rhs); }
1202 * @brief Tests the ordering of a regular expression submatch and a string.
1203 * @param lhs A regular expression submatch.
1204 * @param rhs A string.
1205 * @returns true if @a lhs precedes @a rhs, false otherwise.
1207 template<typename _Bi_iter>
1209 operator<(const sub_match<_Bi_iter>& __lhs,
1210 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1211 { return __lhs.compare(__rhs) < 0; }
1214 * @brief Tests the ordering of a regular expression submatch and a string.
1215 * @param lhs A regular expression submatch.
1216 * @param rhs A string.
1217 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1219 template<typename _Bi_iter>
1221 operator>(const sub_match<_Bi_iter>& __lhs,
1222 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1223 { return __rhs < __lhs; }
1226 * @brief Tests the ordering of a regular expression submatch and a string.
1227 * @param lhs A regular expression submatch.
1228 * @param rhs A string.
1229 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1231 template<typename _Bi_iter>
1233 operator>=(const sub_match<_Bi_iter>& __lhs,
1234 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1235 { return !(__lhs < __rhs); }
1238 * @brief Tests the ordering of a regular expression submatch and a string.
1239 * @param lhs A regular expression submatch.
1240 * @param rhs A string.
1241 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1243 template<typename _Bi_iter>
1245 operator<=(const sub_match<_Bi_iter>& __lhs,
1246 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1247 { return !(__rhs < __lhs); }
1250 * @brief Tests the equivalence of a string and a regular expression
1252 * @param lhs A string.
1253 * @param rhs A regular expression submatch.
1254 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1256 template<typename _Bi_iter>
1258 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1259 const sub_match<_Bi_iter>& __rhs)
1261 return __rhs.compare(typename sub_match<_Bi_iter>::string_type(1, __lhs))
1266 * @brief Tests the inequivalence of a string and a regular expression
1268 * @param lhs A string.
1269 * @param rhs A regular expression submatch.
1270 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1272 template<typename _Bi_iter>
1274 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1275 const sub_match<_Bi_iter>& __rhs)
1276 { return !(__lhs == __rhs); }
1279 * @brief Tests the ordering of a string and a regular expression submatch.
1280 * @param lhs A string.
1281 * @param rhs A regular expression submatch.
1282 * @returns true if @a lhs precedes @a rhs, false otherwise.
1284 template<typename _Bi_iter>
1286 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1287 const sub_match<_Bi_iter>& __rhs)
1289 return __rhs.compare(typename sub_match<_Bi_iter>::string_type(1, __lhs))
1294 * @brief Tests the ordering of a string and a regular expression submatch.
1295 * @param lhs A string.
1296 * @param rhs A regular expression submatch.
1297 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1299 template<typename _Bi_iter>
1301 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1302 const sub_match<_Bi_iter>& __rhs)
1303 { return __rhs < __lhs; }
1306 * @brief Tests the ordering of a string and a regular expression submatch.
1307 * @param lhs A string.
1308 * @param rhs A regular expression submatch.
1309 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1311 template<typename _Bi_iter>
1313 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1314 const sub_match<_Bi_iter>& __rhs)
1315 { return !(__lhs < __rhs); }
1318 * @brief Tests the ordering of a string and a regular expression submatch.
1319 * @param lhs A string.
1320 * @param rhs A regular expression submatch.
1321 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1323 template<typename _Bi_iter>
1325 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1326 const sub_match<_Bi_iter>& __rhs)
1327 { return !(__rhs < __lhs); }
1330 * @brief Tests the equivalence of a regular expression submatch and a
1332 * @param lhs A regular expression submatch.
1333 * @param rhs A const string reference.
1334 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1336 template<typename _Bi_iter>
1338 operator==(const sub_match<_Bi_iter>& __lhs,
1339 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1341 return __lhs.compare(typename sub_match<_Bi_iter>::string_type(1, __rhs))
1346 * @brief Tests the inequivalence of a regular expression submatch and a
1348 * @param lhs A regular expression submatch.
1349 * @param rhs A const string reference.
1350 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1352 template<typename _Bi_iter>
1354 operator!=(const sub_match<_Bi_iter>& __lhs,
1355 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1356 { return !(__lhs == __rhs); }
1359 * @brief Tests the ordering of a regular expression submatch and a string.
1360 * @param lhs A regular expression submatch.
1361 * @param rhs A const string reference.
1362 * @returns true if @a lhs precedes @a rhs, false otherwise.
1364 template<typename _Bi_iter>
1366 operator<(const sub_match<_Bi_iter>& __lhs,
1367 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1369 return __lhs.compare(typename sub_match<_Bi_iter>::string_type(1, __rhs))
1374 * @brief Tests the ordering of a regular expression submatch and a string.
1375 * @param lhs A regular expression submatch.
1376 * @param rhs A const string reference.
1377 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1379 template<typename _Bi_iter>
1381 operator>(const sub_match<_Bi_iter>& __lhs,
1382 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1383 { return __rhs < __lhs; }
1386 * @brief Tests the ordering of a regular expression submatch and a string.
1387 * @param lhs A regular expression submatch.
1388 * @param rhs A const string reference.
1389 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1391 template<typename _Bi_iter>
1393 operator>=(const sub_match<_Bi_iter>& __lhs,
1394 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1395 { return !(__lhs < __rhs); }
1398 * @brief Tests the ordering of a regular expression submatch and a string.
1399 * @param lhs A regular expression submatch.
1400 * @param rhs A const string reference.
1401 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1403 template<typename _Bi_iter>
1405 operator<=(const sub_match<_Bi_iter>& __lhs,
1406 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1407 { return !(__rhs < __lhs); }
1410 * @brief Inserts a matched string into an output stream.
1412 * @param os The output stream.
1413 * @param m A submatch string.
1415 * @returns the output stream with the submatch string inserted.
1417 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1419 basic_ostream<_Ch_type, _Ch_traits>&
1420 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1421 const sub_match<_Bi_iter>& __m)
1422 { return __os << __m.str(); }
1424 // [7.10] Class template match_results
1427 * Special sub_match object representing an unmatched sub-expression.
1429 template<typename _Bi_iter>
1430 inline const sub_match<_Bi_iter>&
1433 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1438 * @brief The results of a match or search operation.
1440 * A collection of character sequences representing the result of a regular
1441 * expression match. Storage for the collection is allocated and freed as
1442 * necessary by the member functions of class template match_results.
1444 * This class satisfies the Sequence requirements, with the exception that
1445 * only the operations defined for a const-qualified Sequence are supported.
1447 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1448 * the whole match. In this case the %sub_match member matched is always true.
1449 * The sub_match object stored at index n denotes what matched the marked
1450 * sub-expression n within the matched expression. If the sub-expression n
1451 * participated in a regular expression match then the %sub_match member
1452 * matched evaluates to true, and members first and second denote the range
1453 * of characters [first, second) which formed that match. Otherwise matched
1454 * is false, and members first and second point to the end of the sequence
1455 * that was searched.
1459 template<typename _Bi_iter,
1460 typename _Allocator = allocator<sub_match<_Bi_iter> > >
1462 : private std::vector<sub_match<_Bi_iter>, _Allocator>
1466 * The vector base is empty if this does not represent a successful match.
1467 * Otherwise it contains n+3 elements where n is the number of marked
1470 * [1] 1st marked subexpression
1472 * [n] nth marked subexpression
1476 typedef std::vector<sub_match<_Bi_iter>, _Allocator> _Base_type;
1480 * @name 10.? Public Types
1483 typedef sub_match<_Bi_iter> value_type;
1484 typedef const value_type& const_reference;
1485 typedef const_reference reference;
1486 typedef typename _Base_type::const_iterator const_iterator;
1487 typedef const_iterator iterator;
1488 typedef typename std::iterator_traits<_Bi_iter>::difference_type
1490 /* TODO: needs allocator_traits */
1491 typedef typename _Allocator::size_type size_type;
1492 typedef _Allocator allocator_type;
1493 typedef typename std::iterator_traits<_Bi_iter>::value_type
1495 typedef std::basic_string<char_type> string_type;
1500 * @name 28.10.1 Construction, Copying, and Destruction
1505 * @brief Constructs a default %match_results container.
1506 * @post size() returns 0 and str() returns an empty string.
1509 match_results(const _Allocator& __a = _Allocator())
1514 * @brief Copy constructs a %match_results.
1516 match_results(const match_results& __rhs)
1521 * @brief Move constructs a %match_results.
1523 match_results(match_results&& __rhs) noexcept
1524 : _Base_type(std::move(__rhs))
1528 * @brief Assigns rhs to *this.
1531 operator=(const match_results& __rhs)
1533 match_results(__rhs).swap(*this);
1538 * @brief Move-assigns rhs to *this.
1541 operator=(match_results&& __rhs)
1543 match_results(std::move(__rhs)).swap(*this);
1548 * @brief Destroys a %match_results object.
1557 * @brief Indicates if the %match_results is ready.
1558 * @retval true The object has a fully-established result state.
1559 * @retval false The object is not ready.
1561 bool ready() const { return !_Base_type::empty(); }
1564 * @name 28.10.2 Size
1569 * @brief Gets the number of matches and submatches.
1571 * The number of matches for a given regular expression will be either 0
1572 * if there was no match or mark_count() + 1 if a match was successful.
1573 * Some matches may be empty.
1575 * @returns the number of matches found.
1580 size_type __size = _Base_type::size();
1581 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1586 { return _Base_type::max_size(); }
1589 * @brief Indicates if the %match_results contains no results.
1590 * @retval true The %match_results object is empty.
1591 * @retval false The %match_results object is not empty.
1595 { return size() == 0; }
1600 * @name 10.3 Element Access
1605 * @brief Gets the length of the indicated submatch.
1606 * @param sub indicates the submatch.
1607 * @pre ready() == true
1609 * This function returns the length of the indicated submatch, or the
1610 * length of the entire match if @p sub is zero (the default).
1613 length(size_type __sub = 0) const
1614 { return (*this)[__sub].length(); }
1617 * @brief Gets the offset of the beginning of the indicated submatch.
1618 * @param sub indicates the submatch.
1619 * @pre ready() == true
1621 * This function returns the offset from the beginning of the target
1622 * sequence to the beginning of the submatch, unless the value of @p sub
1623 * is zero (the default), in which case this function returns the offset
1624 * from the beginning of the target sequence to the beginning of the
1627 * Returns -1 if @p sub is out of range.
1630 position(size_type __sub = 0) const
1632 return __sub < size() ? std::distance(this->prefix().first,
1633 (*this)[__sub].first) : -1;
1637 * @brief Gets the match or submatch converted to a string type.
1638 * @param sub indicates the submatch.
1639 * @pre ready() == true
1641 * This function gets the submatch (or match, if @p sub is zero) extracted
1642 * from the target range and converted to the associated string type.
1645 str(size_type __sub = 0) const
1646 { return (*this)[__sub].str(); }
1649 * @brief Gets a %sub_match reference for the match or submatch.
1650 * @param sub indicates the submatch.
1651 * @pre ready() == true
1653 * This function gets a reference to the indicated submatch, or the entire
1654 * match if @p sub is zero.
1656 * If @p sub >= size() then this function returns a %sub_match with a
1657 * special value indicating no submatch.
1660 operator[](size_type __sub) const
1662 _GLIBCXX_DEBUG_ASSERT( ready() );
1663 return __sub < size()
1664 ? _Base_type::operator[](__sub)
1665 : __unmatched_sub<_Bi_iter>();
1669 * @brief Gets a %sub_match representing the match prefix.
1670 * @pre ready() == true
1672 * This function gets a reference to a %sub_match object representing the
1673 * part of the target range between the start of the target range and the
1674 * start of the match.
1679 _GLIBCXX_DEBUG_ASSERT( ready() );
1681 ? _Base_type::operator[](_Base_type::size() - 2)
1682 : __unmatched_sub<_Bi_iter>();
1686 * @brief Gets a %sub_match representing the match suffix.
1687 * @pre ready() == true
1689 * This function gets a reference to a %sub_match object representing the
1690 * part of the target range between the end of the match and the end of
1696 _GLIBCXX_DEBUG_ASSERT( ready() );
1698 ? _Base_type::operator[](_Base_type::size() - 1)
1699 : __unmatched_sub<_Bi_iter>();
1703 * @brief Gets an iterator to the start of the %sub_match collection.
1707 { return _Base_type::begin(); }
1710 * @brief Gets an iterator to the start of the %sub_match collection.
1714 { return _Base_type::cbegin(); }
1717 * @brief Gets an iterator to one-past-the-end of the collection.
1721 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
1724 * @brief Gets an iterator to one-past-the-end of the collection.
1733 * @name 10.4 Formatting
1735 * These functions perform formatted substitution of the matched
1736 * character sequences into their target. The format specifiers and
1737 * escape sequences accepted by these functions are determined by
1738 * their @p flags parameter as documented above.
1743 * @pre ready() == true
1744 * @todo Implement this function.
1746 template<typename _Out_iter>
1748 format(_Out_iter __out, const char_type* __fmt_first,
1749 const char_type* __fmt_last,
1750 regex_constants::match_flag_type __flags
1751 = regex_constants::format_default) const
1755 * @pre ready() == true
1757 template<typename _Out_iter, typename _St, typename _Sa>
1759 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1760 regex_constants::match_flag_type __flags
1761 = regex_constants::format_default) const
1763 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1768 * @pre ready() == true
1770 template<typename _Out_iter, typename _St, typename _Sa>
1771 basic_string<char_type, _St, _Sa>
1772 format(const basic_string<char_type, _St, _Sa>& __fmt,
1773 regex_constants::match_flag_type __flags
1774 = regex_constants::format_default) const
1776 basic_string<char_type, _St, _Sa> __result;
1777 format(std::back_inserter(__result), __fmt, __flags);
1782 * @pre ready() == true
1785 format(const char_type* __fmt,
1786 regex_constants::match_flag_type __flags
1787 = regex_constants::format_default) const
1789 string_type __result;
1790 format(std::back_inserter(__result),
1791 __fmt + char_traits<char_type>::length(__fmt),
1799 * @name 10.5 Allocator
1804 * @brief Gets a copy of the allocator.
1807 get_allocator() const
1808 { return _Base_type::get_allocator(); }
1818 * @brief Swaps the contents of two match_results.
1821 swap(match_results& __that)
1822 { _Base_type::swap(__that); }
1826 friend class __regex::_SpecializedResults<_Bi_iter, _Allocator>;
1829 typedef match_results<const char*> cmatch;
1830 typedef match_results<string::const_iterator> smatch;
1831 #ifdef _GLIBCXX_USE_WCHAR_T
1832 typedef match_results<const wchar_t*> wcmatch;
1833 typedef match_results<wstring::const_iterator> wsmatch;
1836 // match_results comparisons
1838 * @brief Compares two match_results for equality.
1839 * @returns true if the two objects refer to the same match,
1842 template<typename _Bi_iter, typename _Allocator>
1844 operator==(const match_results<_Bi_iter, _Allocator>& __m1,
1845 const match_results<_Bi_iter, _Allocator>& __m2)
1847 if (__m1.ready() != __m2.ready())
1849 if (!__m1.ready()) // both are not ready
1851 if (__m1.empty() != __m2.empty())
1853 if (__m1.empty()) // both are empty
1855 return __m1.prefix() == __m2.prefix()
1856 && __m1.size() == __m2.size()
1857 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1858 && __m1.suffix() == __m2.suffix();
1862 * @brief Compares two match_results for inequality.
1863 * @returns true if the two objects do not refer to the same match,
1866 template<typename _Bi_iter, class _Allocator>
1868 operator!=(const match_results<_Bi_iter, _Allocator>& __m1,
1869 const match_results<_Bi_iter, _Allocator>& __m2)
1870 { return !(__m1 == __m2); }
1872 // [7.10.6] match_results swap
1874 * @brief Swaps two match results.
1875 * @param lhs A match result.
1876 * @param rhs A match result.
1878 * The contents of the two match_results objects are swapped.
1880 template<typename _Bi_iter, typename _Allocator>
1882 swap(match_results<_Bi_iter, _Allocator>& __lhs,
1883 match_results<_Bi_iter, _Allocator>& __rhs)
1884 { __lhs.swap(__rhs); }
1886 // [7.11.2] Function template regex_match
1888 * @name Matching, Searching, and Replacing
1893 * @brief Determines if there is a match between the regular expression @p e
1894 * and all of the character sequence [first, last).
1896 * @param s Start of the character sequence to match.
1897 * @param e One-past-the-end of the character sequence to match.
1898 * @param m The match results.
1899 * @param re The regular expression.
1900 * @param flags Controls how the regular expression is matched.
1902 * @retval true A match exists.
1903 * @retval false Otherwise.
1905 * @throws an exception of type regex_error.
1907 * @todo Implement this function.
1909 template<typename _Bi_iter, typename _Allocator,
1910 typename _Ch_type, typename _Rx_traits>
1912 regex_match(_Bi_iter __s,
1914 match_results<_Bi_iter, _Allocator>& __m,
1915 const basic_regex<_Ch_type, _Rx_traits>& __re,
1916 regex_constants::match_flag_type __flags
1917 = regex_constants::match_default)
1919 __regex::_AutomatonPtr __a = __re._M_get_automaton();
1920 __regex::_Automaton::_SizeT __sz = __a->_M_sub_count();
1921 __regex::_SpecializedCursor<_Bi_iter> __cs(__s, __e);
1922 __regex::_SpecializedResults<_Bi_iter, _Allocator> __r(__sz, __cs, __m);
1923 __regex::_Grep_matcher __matcher(__cs, __r, __a, __flags);
1924 return __m[0].matched;
1928 * @brief Indicates if there is a match between the regular expression @p e
1929 * and all of the character sequence [first, last).
1931 * @param first Beginning of the character sequence to match.
1932 * @param last One-past-the-end of the character sequence to match.
1933 * @param re The regular expression.
1934 * @param flags Controls how the regular expression is matched.
1936 * @retval true A match exists.
1937 * @retval false Otherwise.
1939 * @throws an exception of type regex_error.
1941 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1943 regex_match(_Bi_iter __first, _Bi_iter __last,
1944 const basic_regex<_Ch_type, _Rx_traits>& __re,
1945 regex_constants::match_flag_type __flags
1946 = regex_constants::match_default)
1948 match_results<_Bi_iter> __what;
1949 return regex_match(__first, __last, __what, __re, __flags);
1953 * @brief Determines if there is a match between the regular expression @p e
1954 * and a C-style null-terminated string.
1956 * @param s The C-style null-terminated string to match.
1957 * @param m The match results.
1958 * @param re The regular expression.
1959 * @param f Controls how the regular expression is matched.
1961 * @retval true A match exists.
1962 * @retval false Otherwise.
1964 * @throws an exception of type regex_error.
1966 template<typename _Ch_type, typename _Allocator, typename _Rx_traits>
1968 regex_match(const _Ch_type* __s,
1969 match_results<const _Ch_type*, _Allocator>& __m,
1970 const basic_regex<_Ch_type, _Rx_traits>& __re,
1971 regex_constants::match_flag_type __f
1972 = regex_constants::match_default)
1973 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
1976 * @brief Determines if there is a match between the regular expression @p e
1979 * @param s The string to match.
1980 * @param m The match results.
1981 * @param re The regular expression.
1982 * @param flags Controls how the regular expression is matched.
1984 * @retval true A match exists.
1985 * @retval false Otherwise.
1987 * @throws an exception of type regex_error.
1989 template<typename _Ch_traits, typename _Ch_alloc,
1990 typename _Allocator, typename _Ch_type, typename _Rx_traits>
1992 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
1993 match_results<typename basic_string<_Ch_type,
1994 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
1995 const basic_regex<_Ch_type, _Rx_traits>& __re,
1996 regex_constants::match_flag_type __flags
1997 = regex_constants::match_default)
1998 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2001 * @brief Indicates if there is a match between the regular expression @p e
2002 * and a C-style null-terminated string.
2004 * @param s The C-style null-terminated string to match.
2005 * @param re The regular expression.
2006 * @param f Controls how the regular expression is matched.
2008 * @retval true A match exists.
2009 * @retval false Otherwise.
2011 * @throws an exception of type regex_error.
2013 template<typename _Ch_type, class _Rx_traits>
2015 regex_match(const _Ch_type* __s,
2016 const basic_regex<_Ch_type, _Rx_traits>& __re,
2017 regex_constants::match_flag_type __f
2018 = regex_constants::match_default)
2019 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2022 * @brief Indicates if there is a match between the regular expression @p e
2025 * @param s [IN] The string to match.
2026 * @param re [IN] The regular expression.
2027 * @param flags [IN] Controls how the regular expression is matched.
2029 * @retval true A match exists.
2030 * @retval false Otherwise.
2032 * @throws an exception of type regex_error.
2034 template<typename _Ch_traits, typename _Str_allocator,
2035 typename _Ch_type, typename _Rx_traits>
2037 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2038 const basic_regex<_Ch_type, _Rx_traits>& __re,
2039 regex_constants::match_flag_type __flags
2040 = regex_constants::match_default)
2041 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2043 // [7.11.3] Function template regex_search
2045 * Searches for a regular expression within a range.
2046 * @param first [IN] The start of the string to search.
2047 * @param last [IN] One-past-the-end of the string to search.
2048 * @param m [OUT] The match results.
2049 * @param re [IN] The regular expression to search for.
2050 * @param flags [IN] Search policy flags.
2051 * @retval true A match was found within the string.
2052 * @retval false No match was found within the string, the content of %m is
2055 * @throws an exception of type regex_error.
2057 * @todo Implement this function.
2059 template<typename _Bi_iter, typename _Allocator,
2060 typename _Ch_type, typename _Rx_traits>
2062 regex_search(_Bi_iter __first, _Bi_iter __last,
2063 match_results<_Bi_iter, _Allocator>& __m,
2064 const basic_regex<_Ch_type, _Rx_traits>& __re,
2065 regex_constants::match_flag_type __flags
2066 = regex_constants::match_default)
2070 * Searches for a regular expression within a range.
2071 * @param first [IN] The start of the string to search.
2072 * @param last [IN] One-past-the-end of the string to search.
2073 * @param re [IN] The regular expression to search for.
2074 * @param flags [IN] Search policy flags.
2075 * @retval true A match was found within the string.
2076 * @retval false No match was found within the string.
2079 * @throws an exception of type regex_error.
2081 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2083 regex_search(_Bi_iter __first, _Bi_iter __last,
2084 const basic_regex<_Ch_type, _Rx_traits>& __re,
2085 regex_constants::match_flag_type __flags
2086 = regex_constants::match_default)
2088 match_results<_Bi_iter> __what;
2089 return regex_search(__first, __last, __what, __re, __flags);
2093 * @brief Searches for a regular expression within a C-string.
2094 * @param s [IN] A C-string to search for the regex.
2095 * @param m [OUT] The set of regex matches.
2096 * @param e [IN] The regex to search for in @p s.
2097 * @param f [IN] The search flags.
2098 * @retval true A match was found within the string.
2099 * @retval false No match was found within the string, the content of %m is
2103 * @throws an exception of type regex_error.
2105 template<typename _Ch_type, class _Allocator, class _Rx_traits>
2107 regex_search(const _Ch_type* __s,
2108 match_results<const _Ch_type*, _Allocator>& __m,
2109 const basic_regex<_Ch_type, _Rx_traits>& __e,
2110 regex_constants::match_flag_type __f
2111 = regex_constants::match_default)
2112 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2115 * @brief Searches for a regular expression within a C-string.
2116 * @param s [IN] The C-string to search.
2117 * @param e [IN] The regular expression to search for.
2118 * @param f [IN] Search policy flags.
2119 * @retval true A match was found within the string.
2120 * @retval false No match was found within the string.
2123 * @throws an exception of type regex_error.
2125 template<typename _Ch_type, typename _Rx_traits>
2127 regex_search(const _Ch_type* __s,
2128 const basic_regex<_Ch_type, _Rx_traits>& __e,
2129 regex_constants::match_flag_type __f
2130 = regex_constants::match_default)
2131 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2134 * @brief Searches for a regular expression within a string.
2135 * @param s [IN] The string to search.
2136 * @param e [IN] The regular expression to search for.
2137 * @param flags [IN] Search policy flags.
2138 * @retval true A match was found within the string.
2139 * @retval false No match was found within the string.
2142 * @throws an exception of type regex_error.
2144 template<typename _Ch_traits, typename _String_allocator,
2145 typename _Ch_type, typename _Rx_traits>
2147 regex_search(const basic_string<_Ch_type, _Ch_traits,
2148 _String_allocator>& __s,
2149 const basic_regex<_Ch_type, _Rx_traits>& __e,
2150 regex_constants::match_flag_type __flags
2151 = regex_constants::match_default)
2152 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2155 * @brief Searches for a regular expression within a string.
2156 * @param s [IN] A C++ string to search for the regex.
2157 * @param m [OUT] The set of regex matches.
2158 * @param e [IN] The regex to search for in @p s.
2159 * @param f [IN] The search flags.
2160 * @retval true A match was found within the string.
2161 * @retval false No match was found within the string, the content of %m is
2164 * @throws an exception of type regex_error.
2166 template<typename _Ch_traits, typename _Ch_alloc,
2167 typename _Allocator, typename _Ch_type,
2168 typename _Rx_traits>
2170 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2171 match_results<typename basic_string<_Ch_type,
2172 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
2173 const basic_regex<_Ch_type, _Rx_traits>& __e,
2174 regex_constants::match_flag_type __f
2175 = regex_constants::match_default)
2176 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2178 // std [28.11.4] Function template regex_replace
2189 * @throws an exception of type regex_error.
2191 * @todo Implement this function.
2193 template<typename _Out_iter, typename _Bi_iter,
2194 typename _Rx_traits, typename _Ch_type>
2196 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2197 const basic_regex<_Ch_type, _Rx_traits>& __e,
2198 const basic_string<_Ch_type>& __fmt,
2199 regex_constants::match_flag_type __flags
2200 = regex_constants::match_default)
2210 * @returns a copy of string @p s with replacements.
2212 * @throws an exception of type regex_error.
2214 template<typename _Rx_traits, typename _Ch_type>
2215 inline basic_string<_Ch_type>
2216 regex_replace(const basic_string<_Ch_type>& __s,
2217 const basic_regex<_Ch_type, _Rx_traits>& __e,
2218 const basic_string<_Ch_type>& __fmt,
2219 regex_constants::match_flag_type __flags
2220 = regex_constants::match_default)
2222 std::string __result;
2223 regex_replace(std::back_inserter(__result),
2224 __s.begin(), __s.end(), __e, __fmt, __flags);
2230 // std [28.12] Class template regex_iterator
2232 * An iterator adaptor that will provide repeated calls of regex_search over
2233 * a range until no more matches remain.
2235 template<typename _Bi_iter,
2236 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2237 typename _Rx_traits = regex_traits<_Ch_type> >
2238 class regex_iterator
2241 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2242 typedef match_results<_Bi_iter> value_type;
2243 typedef std::ptrdiff_t difference_type;
2244 typedef const value_type* pointer;
2245 typedef const value_type& reference;
2246 typedef std::forward_iterator_tag iterator_category;
2250 * @brief Provides a singular iterator, useful for indicating
2251 * one-past-the-end of a range.
2252 * @todo Implement this function.
2258 * Constructs a %regex_iterator...
2259 * @param a [IN] The start of a text range to search.
2260 * @param b [IN] One-past-the-end of the text range to search.
2261 * @param re [IN] The regular expression to match.
2262 * @param m [IN] Policy flags for match rules.
2263 * @todo Implement this function.
2266 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2267 regex_constants::match_flag_type __m
2268 = regex_constants::match_default);
2271 * Copy constructs a %regex_iterator.
2272 * @todo Implement this function.
2275 regex_iterator(const regex_iterator& __rhs);
2278 * @todo Implement this function.
2282 operator=(const regex_iterator& __rhs);
2285 * @todo Implement this function.
2289 operator==(const regex_iterator& __rhs);
2292 * @todo Implement this function.
2296 operator!=(const regex_iterator& __rhs);
2299 * @todo Implement this function.
2306 * @todo Implement this function.
2313 * @todo Implement this function.
2320 * @todo Implement this function.
2327 // these members are shown for exposition only:
2330 const regex_type* pregex;
2331 regex_constants::match_flag_type flags;
2332 match_results<_Bi_iter> match;
2335 typedef regex_iterator<const char*> cregex_iterator;
2336 typedef regex_iterator<string::const_iterator> sregex_iterator;
2337 #ifdef _GLIBCXX_USE_WCHAR_T
2338 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2339 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2342 // [7.12.2] Class template regex_token_iterator
2344 * Iterates over submatches in a range (or @a splits a text string).
2346 * The purpose of this iterator is to enumerate all, or all specified,
2347 * matches of a regular expression within a text range. The dereferenced
2348 * value of an iterator of this class is a std::sub_match object.
2350 template<typename _Bi_iter,
2351 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2352 typename _Rx_traits = regex_traits<_Ch_type> >
2353 class regex_token_iterator
2356 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2357 typedef sub_match<_Bi_iter> value_type;
2358 typedef std::ptrdiff_t difference_type;
2359 typedef const value_type* pointer;
2360 typedef const value_type& reference;
2361 typedef std::forward_iterator_tag iterator_category;
2365 * @brief Default constructs a %regex_token_iterator.
2366 * @todo Implement this function.
2368 * A default-constructed %regex_token_iterator is a singular iterator
2369 * that will compare equal to the one-past-the-end value for any
2370 * iterator of the same type.
2372 regex_token_iterator();
2375 * Constructs a %regex_token_iterator...
2376 * @param a [IN] The start of the text to search.
2377 * @param b [IN] One-past-the-end of the text to search.
2378 * @param re [IN] The regular expression to search for.
2379 * @param submatch [IN] Which submatch to return. There are some
2380 * special values for this parameter:
2381 * - -1 each enumerated subexpression does NOT
2382 * match the regular expression (aka field
2384 * - 0 the entire string matching the
2385 * subexpression is returned for each match
2387 * - >0 enumerates only the indicated
2388 * subexpression from a match within the text.
2389 * @param m [IN] Policy flags for match rules.
2391 * @todo Implement this function.
2394 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2396 regex_constants::match_flag_type __m
2397 = regex_constants::match_default);
2400 * Constructs a %regex_token_iterator...
2401 * @param a [IN] The start of the text to search.
2402 * @param b [IN] One-past-the-end of the text to search.
2403 * @param re [IN] The regular expression to search for.
2404 * @param submatches [IN] A list of subexpressions to return for each
2405 * regular expression match within the text.
2406 * @param m [IN] Policy flags for match rules.
2408 * @todo Implement this function.
2411 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2412 const regex_type& __re,
2413 const std::vector<int>& __submatches,
2414 regex_constants::match_flag_type __m
2415 = regex_constants::match_default);
2418 * Constructs a %regex_token_iterator...
2419 * @param a [IN] The start of the text to search.
2420 * @param b [IN] One-past-the-end of the text to search.
2421 * @param re [IN] The regular expression to search for.
2422 * @param submatches [IN] A list of subexpressions to return for each
2423 * regular expression match within the text.
2424 * @param m [IN] Policy flags for match rules.
2426 * @todo Implement this function.
2429 template<std::size_t _Nm>
2430 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2431 const regex_type& __re,
2432 const int (&__submatches)[_Nm],
2433 regex_constants::match_flag_type __m
2434 = regex_constants::match_default);
2437 * @brief Copy constructs a %regex_token_iterator.
2438 * @param rhs [IN] A %regex_token_iterator to copy.
2439 * @todo Implement this function.
2441 regex_token_iterator(const regex_token_iterator& __rhs);
2444 * @brief Assigns a %regex_token_iterator to another.
2445 * @param rhs [IN] A %regex_token_iterator to copy.
2446 * @todo Implement this function.
2448 regex_token_iterator&
2449 operator=(const regex_token_iterator& __rhs);
2452 * @brief Compares a %regex_token_iterator to another for equality.
2453 * @todo Implement this function.
2456 operator==(const regex_token_iterator& __rhs);
2459 * @brief Compares a %regex_token_iterator to another for inequality.
2460 * @todo Implement this function.
2463 operator!=(const regex_token_iterator& __rhs);
2466 * @brief Dereferences a %regex_token_iterator.
2467 * @todo Implement this function.
2473 * @brief Selects a %regex_token_iterator member.
2474 * @todo Implement this function.
2480 * @brief Increments a %regex_token_iterator.
2481 * @todo Implement this function.
2483 regex_token_iterator&
2487 * @brief Postincrements a %regex_token_iterator.
2488 * @todo Implement this function.
2490 regex_token_iterator
2493 private: // data members for exposition only:
2494 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> position_iterator;
2496 position_iterator __position;
2497 const value_type* __result;
2498 value_type __suffix;
2500 std::vector<int> __subs;
2503 /** @brief Token iterator for C-style NULL-terminated strings. */
2504 typedef regex_token_iterator<const char*> cregex_token_iterator;
2505 /** @brief Token iterator for standard strings. */
2506 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2507 #ifdef _GLIBCXX_USE_WCHAR_T
2508 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2509 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2510 /** @brief Token iterator for standard wide-character strings. */
2511 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2515 _GLIBCXX_END_NAMESPACE_VERSION