1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2013 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
41 * @brief Class regex_traits. Describes aspects of a regular expression.
43 * A regular expression traits class that satisfies the requirements of
46 * The class %regex is parameterized around a set of related types and
47 * functions used to complete the definition of its semantics. This class
48 * satisfies the requirements of such a traits class.
50 template<typename _Ch_type
>
54 typedef _Ch_type char_type
;
55 typedef std::basic_string
<char_type
> string_type
;
56 typedef std::locale locale_type
;
60 typedef typename
std::ctype
<char_type
>::mask _BaseType
;
62 unsigned char _M_extended
;
63 static constexpr unsigned char _S_under
= 1 << 0;
64 // FIXME: _S_blank should be removed in the future, when locale's complete.
65 static constexpr unsigned char _S_blank
= 1 << 1;
66 static constexpr unsigned char _S_valid_mask
= 0x3;
68 constexpr _RegexMask(_BaseType __base
= 0,
69 unsigned char __extended
= 0)
70 : _M_base(__base
), _M_extended(__extended
)
74 operator&(_RegexMask __other
) const
76 return _RegexMask(_M_base
& __other
._M_base
,
77 _M_extended
& __other
._M_extended
);
81 operator|(_RegexMask __other
) const
83 return _RegexMask(_M_base
| __other
._M_base
,
84 _M_extended
| __other
._M_extended
);
88 operator^(_RegexMask __other
) const
90 return _RegexMask(_M_base
^ __other
._M_base
,
91 _M_extended
^ __other
._M_extended
);
96 { return _RegexMask(~_M_base
, ~_M_extended
); }
99 operator&=(_RegexMask __other
)
100 { return *this = (*this) & __other
; }
102 constexpr _RegexMask
&
103 operator|=(_RegexMask __other
)
104 { return *this = (*this) | __other
; }
106 constexpr _RegexMask
&
107 operator^=(_RegexMask __other
)
108 { return *this = (*this) ^ __other
; }
111 operator==(_RegexMask __other
) const
113 return (_M_extended
& _S_valid_mask
)
114 == (__other
._M_extended
& _S_valid_mask
)
115 && _M_base
== __other
._M_base
;
119 operator!=(_RegexMask __other
) const
120 { return !((*this) == __other
); }
124 typedef _RegexMask char_class_type
;
128 * @brief Constructs a default traits object.
133 * @brief Gives the length of a C-style string starting at @p __p.
135 * @param __p a pointer to the start of a character sequence.
137 * @returns the number of characters between @p *__p and the first
138 * default-initialized value of type @p char_type. In other words, uses
139 * the C-string algorithm for determining the length of a sequence of
143 length(const char_type
* __p
)
144 { return string_type::traits_type::length(__p
); }
147 * @brief Performs the identity translation.
149 * @param __c A character to the locale-specific character set.
154 translate(char_type __c
) const
158 * @brief Translates a character into a case-insensitive equivalent.
160 * @param __c A character to the locale-specific character set.
162 * @returns the locale-specific lower-case equivalent of __c.
163 * @throws std::bad_cast if the imbued locale does not support the ctype
167 translate_nocase(char_type __c
) const
169 typedef std::ctype
<char_type
> __ctype_type
;
170 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
171 return __fctyp
.tolower(__c
);
175 * @brief Gets a sort key for a character sequence.
177 * @param __first beginning of the character sequence.
178 * @param __last one-past-the-end of the character sequence.
180 * Returns a sort key for the character sequence designated by the
181 * iterator range [F1, F2) such that if the character sequence [G1, G2)
182 * sorts before the character sequence [H1, H2) then
183 * v.transform(G1, G2) < v.transform(H1, H2).
185 * What this really does is provide a more efficient way to compare a
186 * string to multiple other strings in locales with fancy collation
187 * rules and equivalence classes.
189 * @returns a locale-specific sort key equivalent to the input range.
191 * @throws std::bad_cast if the current locale does not have a collate
194 template<typename _Fwd_iter
>
196 transform(_Fwd_iter __first
, _Fwd_iter __last
) const
198 typedef std::collate
<char_type
> __collate_type
;
199 const __collate_type
& __fclt(use_facet
<__collate_type
>(_M_locale
));
200 string_type
__s(__first
, __last
);
201 return __fclt
.transform(__s
.data(), __s
.data() + __s
.size());
205 * @brief Gets a sort key for a character sequence, independent of case.
207 * @param __first beginning of the character sequence.
208 * @param __last one-past-the-end of the character sequence.
210 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
211 * typeid(collate_byname<_Ch_type>) and the form of the sort key
212 * returned by collate_byname<_Ch_type>::transform(__first, __last)
213 * is known and can be converted into a primary sort key
214 * then returns that key, otherwise returns an empty string.
216 * @todo Implement this function.
218 template<typename _Fwd_iter
>
220 transform_primary(_Fwd_iter __first
, _Fwd_iter __last
) const
224 typedef std::ctype
<char_type
> __ctype_type
;
225 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
226 std::vector
<char_type
> __v(__first
, __last
);
227 // FIXME : this is not entirely correct
228 __fctyp
.tolower(&*__v
.begin(), &*__v
.end());
229 return this->transform(&*__v
.begin(), &*__v
.end());
234 return string_type();
238 * @brief Gets a collation element by name.
240 * @param __first beginning of the collation element name.
241 * @param __last one-past-the-end of the collation element name.
243 * @returns a sequence of one or more characters that represents the
244 * collating element consisting of the character sequence designated by
245 * the iterator range [__first, __last). Returns an empty string if the
246 * character sequence is not a valid collating element.
248 template<typename _Fwd_iter
>
250 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const;
253 * @brief Maps one or more characters to a named character
256 * @param __first beginning of the character sequence.
257 * @param __last one-past-the-end of the character sequence.
258 * @param __icase ignores the case of the classification name.
260 * @returns an unspecified value that represents the character
261 * classification named by the character sequence designated by
262 * the iterator range [__first, __last). If @p icase is true,
263 * the returned mask identifies the classification regardless of
264 * the case of the characters to be matched (for example,
265 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
266 * case-dependent classification is returned. The value
267 * returned shall be independent of the case of the characters
268 * in the character sequence. If the name is not recognized then
269 * returns a value that compares equal to 0.
271 * At least the following names (or their wide-character equivalent) are
289 template<typename _Fwd_iter
>
291 lookup_classname(_Fwd_iter __first
, _Fwd_iter __last
,
292 bool __icase
= false) const;
295 * @brief Determines if @p c is a member of an identified class.
297 * @param __c a character.
298 * @param __f a class type (as returned from lookup_classname).
300 * @returns true if the character @p __c is a member of the classification
301 * represented by @p __f, false otherwise.
303 * @throws std::bad_cast if the current locale does not have a ctype
307 isctype(_Ch_type __c
, char_class_type __f
) const;
310 * @brief Converts a digit to an int.
312 * @param __ch a character representing a digit.
313 * @param __radix the radix if the numeric conversion (limited to 8, 10,
316 * @returns the value represented by the digit __ch in base radix if the
317 * character __ch is a valid digit in base radix; otherwise returns -1.
320 value(_Ch_type __ch
, int __radix
) const;
323 * @brief Imbues the regex_traits object with a copy of a new locale.
325 * @param __loc A locale.
327 * @returns a copy of the previous locale in use by the regex_traits
330 * @note Calling imbue with a different locale than the one currently in
331 * use invalidates all cached data held by *this.
334 imbue(locale_type __loc
)
336 std::swap(_M_locale
, __loc
);
341 * @brief Gets a copy of the current locale in use by the regex_traits
346 { return _M_locale
; }
349 locale_type _M_locale
;
352 template<typename _Ch_type
>
353 template<typename _Fwd_iter
>
354 typename regex_traits
<_Ch_type
>::string_type
355 regex_traits
<_Ch_type
>::
356 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const
358 typedef std::ctype
<char_type
> __ctype_type
;
359 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
361 static const char* __collatenames
[] =
454 "left-square-bracket",
456 "right-square-bracket",
486 "left-curly-bracket",
488 "right-curly-bracket",
495 static const char* __digraphs
[] =
521 std::string
__s(__last
- __first
, '?');
522 string_type
a(__first
, __last
);
523 __fctyp
.narrow(__first
, __last
, '?', &*__s
.begin());
525 for (unsigned int __i
= 0; *__collatenames
[__i
]; __i
++)
526 if (__s
== __collatenames
[__i
])
527 return string_type(1, __fctyp
.widen((char)__i
));
529 for (unsigned int __i
= 0; *__digraphs
[__i
]; __i
++)
531 const char* __now
= __digraphs
[__i
];
534 string_type
ret(__s
.size(), __fctyp
.widen('?'));
535 __fctyp
.widen(__now
, __now
+ 2/* ouch */, &*ret
.begin());
539 return string_type();
542 template<typename _Ch_type
>
543 template<typename _Fwd_iter
>
544 typename regex_traits
<_Ch_type
>::char_class_type
545 regex_traits
<_Ch_type
>::
546 lookup_classname(_Fwd_iter __first
, _Fwd_iter __last
, bool __icase
) const
548 typedef std::ctype
<char_type
> __ctype_type
;
549 typedef std::ctype
<char> __cctype_type
;
550 typedef const pair
<const char*, char_class_type
> _ClassnameEntry
;
551 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
552 const __cctype_type
& __cctyp(use_facet
<__cctype_type
>(_M_locale
));
554 static _ClassnameEntry __classnames
[] =
556 {"d", ctype_base::digit
},
557 {"w", {ctype_base::alnum
, _RegexMask::_S_under
}},
558 {"s", ctype_base::space
},
559 {"alnum", ctype_base::alnum
},
560 {"alpha", ctype_base::alpha
},
561 {"blank", {0, _RegexMask::_S_blank
}},
562 {"cntrl", ctype_base::cntrl
},
563 {"digit", ctype_base::digit
},
564 {"graph", ctype_base::graph
},
565 {"lower", ctype_base::lower
},
566 {"print", ctype_base::print
},
567 {"punct", ctype_base::punct
},
568 {"space", ctype_base::space
},
569 {"upper", ctype_base::upper
},
570 {"xdigit", ctype_base::xdigit
},
573 std::string
__s(__last
- __first
, '?');
574 __fctyp
.narrow(__first
, __last
, '?', &__s
[0]);
575 __cctyp
.tolower(&*__s
.begin(), &*__s
.end());
576 for (_ClassnameEntry
* __it
= __classnames
;
577 __it
< *(&__classnames
+ 1);
580 if (__s
== __it
->first
)
583 && ((__it
->second
& (ctype_base::lower
| ctype_base::upper
)) != 0))
584 return ctype_base::alpha
;
591 template<typename _Ch_type
>
593 regex_traits
<_Ch_type
>::
594 isctype(_Ch_type __c
, char_class_type __f
) const
596 typedef std::ctype
<char_type
> __ctype_type
;
597 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
599 return __fctyp
.is(__f
._M_base
, __c
)
601 || ((__f
._M_extended
& _RegexMask::_S_under
)
602 && __c
== __fctyp
.widen('_'))
604 || ((__f
._M_extended
& _RegexMask::_S_blank
)
605 && (__c
== __fctyp
.widen(' ')
606 || __c
== __fctyp
.widen('\t')));
609 template<typename _Ch_type
>
611 regex_traits
<_Ch_type
>::
612 value(_Ch_type __ch
, int __radix
) const
614 std::basic_istringstream
<char_type
> __is(string_type(1, __ch
));
618 else if (__radix
== 16)
621 return __is
.fail() ? -1 : __v
;
624 // [7.8] Class basic_regex
626 * Objects of specializations of this class represent regular expressions
627 * constructed from sequences of character type @p _Ch_type.
629 * Storage for the regular expression is allocated and deallocated as
630 * necessary by the member functions of this class.
632 template<typename _Ch_type
, typename _Rx_traits
= regex_traits
<_Ch_type
> >
637 typedef _Ch_type value_type
;
638 typedef _Rx_traits traits_type
;
639 typedef typename
traits_type::string_type string_type
;
640 typedef regex_constants::syntax_option_type flag_type
;
641 typedef typename
traits_type::locale_type locale_type
;
648 static constexpr flag_type icase
= regex_constants::icase
;
649 static constexpr flag_type nosubs
= regex_constants::nosubs
;
650 static constexpr flag_type optimize
= regex_constants::optimize
;
651 static constexpr flag_type collate
= regex_constants::collate
;
652 static constexpr flag_type ECMAScript
= regex_constants::ECMAScript
;
653 static constexpr flag_type basic
= regex_constants::basic
;
654 static constexpr flag_type extended
= regex_constants::extended
;
655 static constexpr flag_type awk
= regex_constants::awk
;
656 static constexpr flag_type grep
= regex_constants::grep
;
657 static constexpr flag_type egrep
= regex_constants::egrep
;
660 // [7.8.2] construct/copy/destroy
662 * Constructs a basic regular expression that does not match any
663 * character sequence.
666 : _M_flags(ECMAScript
),
667 _M_automaton(__detail::__compile
<const _Ch_type
*, _Rx_traits
>(0, 0,
668 _M_traits
, _M_flags
))
672 * @brief Constructs a basic regular expression from the
673 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
674 * interpreted according to the flags in @p __f.
676 * @param __p A pointer to the start of a C-style null-terminated string
677 * containing a regular expression.
678 * @param __f Flags indicating the syntax rules and options.
680 * @throws regex_error if @p __p is not a valid regular expression.
683 basic_regex(const _Ch_type
* __p
, flag_type __f
= ECMAScript
)
685 _M_automaton(__detail::__compile(__p
, __p
+ _Rx_traits::length(__p
),
686 _M_traits
, _M_flags
))
690 * @brief Constructs a basic regular expression from the sequence
691 * [p, p + len) interpreted according to the flags in @p f.
693 * @param __p A pointer to the start of a string containing a regular
695 * @param __len The length of the string containing the regular
697 * @param __f Flags indicating the syntax rules and options.
699 * @throws regex_error if @p __p is not a valid regular expression.
701 basic_regex(const _Ch_type
* __p
, std::size_t __len
, flag_type __f
)
703 _M_automaton(__detail::__compile(__p
, __p
+ __len
, _M_traits
, _M_flags
))
707 * @brief Copy-constructs a basic regular expression.
709 * @param __rhs A @p regex object.
711 basic_regex(const basic_regex
& __rhs
)
712 : _M_flags(__rhs
._M_flags
), _M_traits(__rhs
._M_traits
),
713 _M_automaton(__rhs
._M_automaton
)
717 * @brief Move-constructs a basic regular expression.
719 * @param __rhs A @p regex object.
721 basic_regex(const basic_regex
&& __rhs
) noexcept
722 : _M_flags(__rhs
._M_flags
), _M_traits(__rhs
._M_traits
),
723 _M_automaton(std::move(__rhs
._M_automaton
))
727 * @brief Constructs a basic regular expression from the string
728 * @p s interpreted according to the flags in @p f.
730 * @param __s A string containing a regular expression.
731 * @param __f Flags indicating the syntax rules and options.
733 * @throws regex_error if @p __s is not a valid regular expression.
735 template<typename _Ch_traits
, typename _Ch_alloc
>
737 basic_regex(const std::basic_string
<_Ch_type
, _Ch_traits
,
739 flag_type __f
= ECMAScript
)
741 _M_automaton(__detail::__compile(__s
.begin(), __s
.end(),
742 _M_traits
, _M_flags
))
746 * @brief Constructs a basic regular expression from the range
747 * [first, last) interpreted according to the flags in @p f.
749 * @param __first The start of a range containing a valid regular
751 * @param __last The end of a range containing a valid regular
753 * @param __f The format flags of the regular expression.
755 * @throws regex_error if @p [__first, __last) is not a valid regular
758 template<typename _InputIterator
>
759 basic_regex(_InputIterator __first
, _InputIterator __last
,
760 flag_type __f
= ECMAScript
)
762 _M_automaton(__detail::__compile(__first
, __last
, _M_traits
, _M_flags
))
766 * @brief Constructs a basic regular expression from an initializer list.
768 * @param __l The initializer list.
769 * @param __f The format flags of the regular expression.
771 * @throws regex_error if @p __l is not a valid regular expression.
773 basic_regex(initializer_list
<_Ch_type
> __l
,
774 flag_type __f
= ECMAScript
)
776 _M_automaton(__detail::__compile(__l
.begin(), __l
.end(),
777 _M_traits
, _M_flags
))
781 * @brief Destroys a basic regular expression.
787 * @brief Assigns one regular expression to another.
790 operator=(const basic_regex
& __rhs
)
791 { return this->assign(__rhs
); }
794 * @brief Move-assigns one regular expression to another.
797 operator=(basic_regex
&& __rhs
) noexcept
798 { return this->assign(std::move(__rhs
)); }
801 * @brief Replaces a regular expression with a new one constructed from
802 * a C-style null-terminated string.
804 * @param __p A pointer to the start of a null-terminated C-style string
805 * containing a regular expression.
808 operator=(const _Ch_type
* __p
)
809 { return this->assign(__p
, flags()); }
812 * @brief Replaces a regular expression with a new one constructed from
815 * @param __s A pointer to a string containing a regular expression.
817 template<typename _Ch_typeraits
, typename _Alloc
>
819 operator=(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
)
820 { return this->assign(__s
, flags()); }
824 * @brief the real assignment operator.
826 * @param __rhs Another regular expression object.
829 assign(const basic_regex
& __rhs
)
831 basic_regex
__tmp(__rhs
);
837 * @brief The move-assignment operator.
839 * @param __rhs Another regular expression object.
842 assign(basic_regex
&& __rhs
) noexcept
844 basic_regex
__tmp(std::move(__rhs
));
850 * @brief Assigns a new regular expression to a regex object from a
851 * C-style null-terminated string containing a regular expression
854 * @param __p A pointer to a C-style null-terminated string containing
855 * a regular expression pattern.
856 * @param __flags Syntax option flags.
858 * @throws regex_error if __p does not contain a valid regular
859 * expression pattern interpreted according to @p __flags. If
860 * regex_error is thrown, *this remains unchanged.
863 assign(const _Ch_type
* __p
, flag_type __flags
= ECMAScript
)
864 { return this->assign(string_type(__p
), __flags
); }
867 * @brief Assigns a new regular expression to a regex object from a
868 * C-style string containing a regular expression pattern.
870 * @param __p A pointer to a C-style string containing a
871 * regular expression pattern.
872 * @param __len The length of the regular expression pattern string.
873 * @param __flags Syntax option flags.
875 * @throws regex_error if p does not contain a valid regular
876 * expression pattern interpreted according to @p __flags. If
877 * regex_error is thrown, *this remains unchanged.
880 assign(const _Ch_type
* __p
, std::size_t __len
, flag_type __flags
)
881 { return this->assign(string_type(__p
, __len
), __flags
); }
884 * @brief Assigns a new regular expression to a regex object from a
885 * string containing a regular expression pattern.
887 * @param __s A string containing a regular expression pattern.
888 * @param __flags Syntax option flags.
890 * @throws regex_error if __s does not contain a valid regular
891 * expression pattern interpreted according to @p __flags. If
892 * regex_error is thrown, *this remains unchanged.
894 template<typename _Ch_typeraits
, typename _Alloc
>
896 assign(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
,
897 flag_type __flags
= ECMAScript
)
899 basic_regex
__tmp(__s
, __flags
);
905 * @brief Assigns a new regular expression to a regex object.
907 * @param __first The start of a range containing a valid regular
909 * @param __last The end of a range containing a valid regular
911 * @param __flags Syntax option flags.
913 * @throws regex_error if p does not contain a valid regular
914 * expression pattern interpreted according to @p __flags. If
915 * regex_error is thrown, the object remains unchanged.
917 template<typename _InputIterator
>
919 assign(_InputIterator __first
, _InputIterator __last
,
920 flag_type __flags
= ECMAScript
)
921 { return this->assign(string_type(__first
, __last
), __flags
); }
924 * @brief Assigns a new regular expression to a regex object.
926 * @param __l An initializer list representing a regular expression.
927 * @param __flags Syntax option flags.
929 * @throws regex_error if @p __l does not contain a valid
930 * regular expression pattern interpreted according to @p
931 * __flags. If regex_error is thrown, the object remains
935 assign(initializer_list
<_Ch_type
> __l
, flag_type __flags
= ECMAScript
)
936 { return this->assign(__l
.begin(), __l
.end(), __flags
); }
938 // [7.8.4] const operations
940 * @brief Gets the number of marked subexpressions within the regular
945 { return _M_automaton
->_M_sub_count() - 1; }
948 * @brief Gets the flags used to construct the regular expression
949 * or in the last call to assign().
957 * @brief Imbues the regular expression object with the given locale.
959 * @param __loc A locale.
962 imbue(locale_type __loc
)
963 { return _M_traits
.imbue(__loc
); }
966 * @brief Gets the locale currently imbued in the regular expression
971 { return _M_traits
.getloc(); }
975 * @brief Swaps the contents of two regular expression objects.
977 * @param __rhs Another regular expression object.
980 swap(basic_regex
& __rhs
)
982 std::swap(_M_flags
, __rhs
._M_flags
);
983 std::swap(_M_traits
, __rhs
._M_traits
);
984 std::swap(_M_automaton
, __rhs
._M_automaton
);
987 #ifdef _GLIBCXX_DEBUG
989 _M_dot(std::ostream
& __ostr
)
990 { _M_automaton
->_M_dot(__ostr
); }
993 const __detail::_AutomatonPtr
&
994 _M_get_automaton() const
995 { return _M_automaton
; }
999 _Rx_traits _M_traits
;
1000 __detail::_AutomatonPtr _M_automaton
;
1003 /** @brief Standard regular expressions. */
1004 typedef basic_regex
<char> regex
;
1006 #ifdef _GLIBCXX_USE_WCHAR_T
1007 /** @brief Standard wide-character regular expressions. */
1008 typedef basic_regex
<wchar_t> wregex
;
1012 // [7.8.6] basic_regex swap
1014 * @brief Swaps the contents of two regular expression objects.
1015 * @param __lhs First regular expression.
1016 * @param __rhs Second regular expression.
1018 template<typename _Ch_type
, typename _Rx_traits
>
1020 swap(basic_regex
<_Ch_type
, _Rx_traits
>& __lhs
,
1021 basic_regex
<_Ch_type
, _Rx_traits
>& __rhs
)
1022 { __lhs
.swap(__rhs
); }
1025 // [7.9] Class template sub_match
1027 * A sequence of characters matched by a particular marked sub-expression.
1029 * An object of this class is essentially a pair of iterators marking a
1030 * matched subexpression within a regular expression pattern match. Such
1031 * objects can be converted to and compared with std::basic_string objects
1032 * of a similar base character type as the pattern matched by the regular
1035 * The iterators that make up the pair are the usual half-open interval
1036 * referencing the actual original pattern matched.
1038 template<typename _BiIter
>
1039 class sub_match
: public std::pair
<_BiIter
, _BiIter
>
1041 typedef iterator_traits
<_BiIter
> __iter_traits
;
1044 typedef typename
__iter_traits::value_type value_type
;
1045 typedef typename
__iter_traits::difference_type difference_type
;
1046 typedef _BiIter iterator
;
1047 typedef std::basic_string
<value_type
> string_type
;
1051 constexpr sub_match() : matched() { }
1054 * Gets the length of the matching sequence.
1058 { return this->matched
? std::distance(this->first
, this->second
) : 0; }
1061 * @brief Gets the matching sequence as a string.
1063 * @returns the matching sequence as a string.
1065 * This is the implicit conversion operator. It is identical to the
1066 * str() member function except that it will want to pop up in
1067 * unexpected places and cause a great deal of confusion and cursing
1070 operator string_type() const
1072 return this->matched
1073 ? string_type(this->first
, this->second
)
1078 * @brief Gets the matching sequence as a string.
1080 * @returns the matching sequence as a string.
1085 return this->matched
1086 ? string_type(this->first
, this->second
)
1091 * @brief Compares this and another matched sequence.
1093 * @param __s Another matched sequence to compare to this one.
1095 * @retval <0 this matched sequence will collate before @p __s.
1096 * @retval =0 this matched sequence is equivalent to @p __s.
1097 * @retval <0 this matched sequence will collate after @p __s.
1100 compare(const sub_match
& __s
) const
1101 { return this->str().compare(__s
.str()); }
1104 * @brief Compares this sub_match to a string.
1106 * @param __s A string to compare to this sub_match.
1108 * @retval <0 this matched sequence will collate before @p __s.
1109 * @retval =0 this matched sequence is equivalent to @p __s.
1110 * @retval <0 this matched sequence will collate after @p __s.
1113 compare(const string_type
& __s
) const
1114 { return this->str().compare(__s
); }
1117 * @brief Compares this sub_match to a C-style string.
1119 * @param __s A C-style string to compare to this sub_match.
1121 * @retval <0 this matched sequence will collate before @p __s.
1122 * @retval =0 this matched sequence is equivalent to @p __s.
1123 * @retval <0 this matched sequence will collate after @p __s.
1126 compare(const value_type
* __s
) const
1127 { return this->str().compare(__s
); }
1131 /** @brief Standard regex submatch over a C-style null-terminated string. */
1132 typedef sub_match
<const char*> csub_match
;
1134 /** @brief Standard regex submatch over a standard string. */
1135 typedef sub_match
<string::const_iterator
> ssub_match
;
1137 #ifdef _GLIBCXX_USE_WCHAR_T
1138 /** @brief Regex submatch over a C-style null-terminated wide string. */
1139 typedef sub_match
<const wchar_t*> wcsub_match
;
1141 /** @brief Regex submatch over a standard wide string. */
1142 typedef sub_match
<wstring::const_iterator
> wssub_match
;
1145 // [7.9.2] sub_match non-member operators
1148 * @brief Tests the equivalence of two regular expression submatches.
1149 * @param __lhs First regular expression submatch.
1150 * @param __rhs Second regular expression submatch.
1151 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1153 template<typename _BiIter
>
1155 operator==(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1156 { return __lhs
.compare(__rhs
) == 0; }
1159 * @brief Tests the inequivalence of two regular expression submatches.
1160 * @param __lhs First regular expression submatch.
1161 * @param __rhs Second regular expression submatch.
1162 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1164 template<typename _BiIter
>
1166 operator!=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1167 { return __lhs
.compare(__rhs
) != 0; }
1170 * @brief Tests the ordering of two regular expression submatches.
1171 * @param __lhs First regular expression submatch.
1172 * @param __rhs Second regular expression submatch.
1173 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1175 template<typename _BiIter
>
1177 operator<(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1178 { return __lhs
.compare(__rhs
) < 0; }
1181 * @brief Tests the ordering of two regular expression submatches.
1182 * @param __lhs First regular expression submatch.
1183 * @param __rhs Second regular expression submatch.
1184 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1186 template<typename _BiIter
>
1188 operator<=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1189 { return __lhs
.compare(__rhs
) <= 0; }
1192 * @brief Tests the ordering of two regular expression submatches.
1193 * @param __lhs First regular expression submatch.
1194 * @param __rhs Second regular expression submatch.
1195 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1197 template<typename _BiIter
>
1199 operator>=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1200 { return __lhs
.compare(__rhs
) >= 0; }
1203 * @brief Tests the ordering of two regular expression submatches.
1204 * @param __lhs First regular expression submatch.
1205 * @param __rhs Second regular expression submatch.
1206 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1208 template<typename _BiIter
>
1210 operator>(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1211 { return __lhs
.compare(__rhs
) > 0; }
1213 // Alias for sub_match'd string.
1214 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1215 using __sub_match_string
= basic_string
<
1216 typename iterator_traits
<_Bi_iter
>::value_type
,
1217 _Ch_traits
, _Ch_alloc
>;
1220 * @brief Tests the equivalence of a string and a regular expression
1222 * @param __lhs A string.
1223 * @param __rhs A regular expression submatch.
1224 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1226 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1228 operator==(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1229 const sub_match
<_Bi_iter
>& __rhs
)
1230 { return __rhs
.compare(__lhs
.c_str()) == 0; }
1233 * @brief Tests the inequivalence of a string and a regular expression
1235 * @param __lhs A string.
1236 * @param __rhs A regular expression submatch.
1237 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1239 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1241 operator!=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1242 const sub_match
<_Bi_iter
>& __rhs
)
1243 { return !(__lhs
== __rhs
); }
1246 * @brief Tests the ordering of a string and a regular expression submatch.
1247 * @param __lhs A string.
1248 * @param __rhs A regular expression submatch.
1249 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1251 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1253 operator<(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1254 const sub_match
<_Bi_iter
>& __rhs
)
1255 { return __rhs
.compare(__lhs
.c_str()) > 0; }
1258 * @brief Tests the ordering of a string and a regular expression submatch.
1259 * @param __lhs A string.
1260 * @param __rhs A regular expression submatch.
1261 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1263 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1265 operator>(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1266 const sub_match
<_Bi_iter
>& __rhs
)
1267 { return __rhs
< __lhs
; }
1270 * @brief Tests the ordering of a string and a regular expression submatch.
1271 * @param __lhs A string.
1272 * @param __rhs A regular expression submatch.
1273 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1275 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1277 operator>=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1278 const sub_match
<_Bi_iter
>& __rhs
)
1279 { return !(__lhs
< __rhs
); }
1282 * @brief Tests the ordering of a string and a regular expression submatch.
1283 * @param __lhs A string.
1284 * @param __rhs A regular expression submatch.
1285 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1287 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1289 operator<=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1290 const sub_match
<_Bi_iter
>& __rhs
)
1291 { return !(__rhs
< __lhs
); }
1294 * @brief Tests the equivalence of a regular expression submatch and a
1296 * @param __lhs A regular expression submatch.
1297 * @param __rhs A string.
1298 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1300 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1302 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1303 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1304 { return __lhs
.compare(__rhs
.c_str()) == 0; }
1307 * @brief Tests the inequivalence of a regular expression submatch and a
1309 * @param __lhs A regular expression submatch.
1310 * @param __rhs A string.
1311 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1313 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1315 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1316 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1317 { return !(__lhs
== __rhs
); }
1320 * @brief Tests the ordering of a regular expression submatch and a string.
1321 * @param __lhs A regular expression submatch.
1322 * @param __rhs A string.
1323 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1325 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1327 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1328 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1329 { return __lhs
.compare(__rhs
.c_str()) < 0; }
1332 * @brief Tests the ordering of a regular expression submatch and a string.
1333 * @param __lhs A regular expression submatch.
1334 * @param __rhs A string.
1335 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1337 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1339 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1340 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1341 { return __rhs
< __lhs
; }
1344 * @brief Tests the ordering of a regular expression submatch and a string.
1345 * @param __lhs A regular expression submatch.
1346 * @param __rhs A string.
1347 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1349 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1351 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1352 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1353 { return !(__lhs
< __rhs
); }
1356 * @brief Tests the ordering of a regular expression submatch and a string.
1357 * @param __lhs A regular expression submatch.
1358 * @param __rhs A string.
1359 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1361 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1363 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1364 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1365 { return !(__rhs
< __lhs
); }
1368 * @brief Tests the equivalence of a C string and a regular expression
1370 * @param __lhs A C string.
1371 * @param __rhs A regular expression submatch.
1372 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1374 template<typename _Bi_iter
>
1376 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1377 const sub_match
<_Bi_iter
>& __rhs
)
1378 { return __rhs
.compare(__lhs
) == 0; }
1381 * @brief Tests the inequivalence of an iterator value and a regular
1382 * expression submatch.
1383 * @param __lhs A regular expression submatch.
1384 * @param __rhs A string.
1385 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1387 template<typename _Bi_iter
>
1389 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1390 const sub_match
<_Bi_iter
>& __rhs
)
1391 { return !(__lhs
== __rhs
); }
1394 * @brief Tests the ordering of a string and a regular expression submatch.
1395 * @param __lhs A string.
1396 * @param __rhs A regular expression submatch.
1397 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1399 template<typename _Bi_iter
>
1401 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1402 const sub_match
<_Bi_iter
>& __rhs
)
1403 { return __rhs
.compare(__lhs
) > 0; }
1406 * @brief Tests the ordering of a string and a regular expression submatch.
1407 * @param __lhs A string.
1408 * @param __rhs A regular expression submatch.
1409 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1411 template<typename _Bi_iter
>
1413 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1414 const sub_match
<_Bi_iter
>& __rhs
)
1415 { return __rhs
< __lhs
; }
1418 * @brief Tests the ordering of a string and a regular expression submatch.
1419 * @param __lhs A string.
1420 * @param __rhs A regular expression submatch.
1421 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1423 template<typename _Bi_iter
>
1425 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1426 const sub_match
<_Bi_iter
>& __rhs
)
1427 { return !(__lhs
< __rhs
); }
1430 * @brief Tests the ordering of a string and a regular expression submatch.
1431 * @param __lhs A string.
1432 * @param __rhs A regular expression submatch.
1433 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1435 template<typename _Bi_iter
>
1437 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1438 const sub_match
<_Bi_iter
>& __rhs
)
1439 { return !(__rhs
< __lhs
); }
1442 * @brief Tests the equivalence of a regular expression submatch and a
1444 * @param __lhs A regular expression submatch.
1445 * @param __rhs A pointer to a string?
1446 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1448 template<typename _Bi_iter
>
1450 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1451 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1452 { return __lhs
.compare(__rhs
) == 0; }
1455 * @brief Tests the inequivalence of a regular expression submatch and a
1457 * @param __lhs A regular expression submatch.
1458 * @param __rhs A pointer to a string.
1459 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1461 template<typename _Bi_iter
>
1463 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1464 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1465 { return !(__lhs
== __rhs
); }
1468 * @brief Tests the ordering of a regular expression submatch and a string.
1469 * @param __lhs A regular expression submatch.
1470 * @param __rhs A string.
1471 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1473 template<typename _Bi_iter
>
1475 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1476 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1477 { return __lhs
.compare(__rhs
) < 0; }
1480 * @brief Tests the ordering of a regular expression submatch and a string.
1481 * @param __lhs A regular expression submatch.
1482 * @param __rhs A string.
1483 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1485 template<typename _Bi_iter
>
1487 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1488 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1489 { return __rhs
< __lhs
; }
1492 * @brief Tests the ordering of a regular expression submatch and a string.
1493 * @param __lhs A regular expression submatch.
1494 * @param __rhs A string.
1495 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1497 template<typename _Bi_iter
>
1499 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1500 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1501 { return !(__lhs
< __rhs
); }
1504 * @brief Tests the ordering of a regular expression submatch and a string.
1505 * @param __lhs A regular expression submatch.
1506 * @param __rhs A string.
1507 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1509 template<typename _Bi_iter
>
1511 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1512 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1513 { return !(__rhs
< __lhs
); }
1516 * @brief Tests the equivalence of a string and a regular expression
1518 * @param __lhs A string.
1519 * @param __rhs A regular expression submatch.
1520 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1522 template<typename _Bi_iter
>
1524 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1525 const sub_match
<_Bi_iter
>& __rhs
)
1527 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1528 return __rhs
.compare(string_type(1, __lhs
)) == 0;
1532 * @brief Tests the inequivalence of a string and a regular expression
1534 * @param __lhs A string.
1535 * @param __rhs A regular expression submatch.
1536 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1538 template<typename _Bi_iter
>
1540 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1541 const sub_match
<_Bi_iter
>& __rhs
)
1542 { return !(__lhs
== __rhs
); }
1545 * @brief Tests the ordering of a string and a regular expression submatch.
1546 * @param __lhs A string.
1547 * @param __rhs A regular expression submatch.
1548 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1550 template<typename _Bi_iter
>
1552 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1553 const sub_match
<_Bi_iter
>& __rhs
)
1555 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1556 return __rhs
.compare(string_type(1, __lhs
)) > 0;
1560 * @brief Tests the ordering of a string and a regular expression submatch.
1561 * @param __lhs A string.
1562 * @param __rhs A regular expression submatch.
1563 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1565 template<typename _Bi_iter
>
1567 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1568 const sub_match
<_Bi_iter
>& __rhs
)
1569 { return __rhs
< __lhs
; }
1572 * @brief Tests the ordering of a string and a regular expression submatch.
1573 * @param __lhs A string.
1574 * @param __rhs A regular expression submatch.
1575 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1577 template<typename _Bi_iter
>
1579 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1580 const sub_match
<_Bi_iter
>& __rhs
)
1581 { return !(__lhs
< __rhs
); }
1584 * @brief Tests the ordering of a string and a regular expression submatch.
1585 * @param __lhs A string.
1586 * @param __rhs A regular expression submatch.
1587 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1589 template<typename _Bi_iter
>
1591 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1592 const sub_match
<_Bi_iter
>& __rhs
)
1593 { return !(__rhs
< __lhs
); }
1596 * @brief Tests the equivalence of a regular expression submatch and a
1598 * @param __lhs A regular expression submatch.
1599 * @param __rhs A const string reference.
1600 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1602 template<typename _Bi_iter
>
1604 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1605 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1607 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1608 return __lhs
.compare(string_type(1, __rhs
)) == 0;
1612 * @brief Tests the inequivalence of a regular expression submatch and a
1614 * @param __lhs A regular expression submatch.
1615 * @param __rhs A const string reference.
1616 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1618 template<typename _Bi_iter
>
1620 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1621 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1622 { return !(__lhs
== __rhs
); }
1625 * @brief Tests the ordering of a regular expression submatch and a string.
1626 * @param __lhs A regular expression submatch.
1627 * @param __rhs A const string reference.
1628 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1630 template<typename _Bi_iter
>
1632 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1633 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1635 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1636 return __lhs
.compare(string_type(1, __rhs
)) < 0;
1640 * @brief Tests the ordering of a regular expression submatch and a string.
1641 * @param __lhs A regular expression submatch.
1642 * @param __rhs A const string reference.
1643 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1645 template<typename _Bi_iter
>
1647 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1648 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1649 { return __rhs
< __lhs
; }
1652 * @brief Tests the ordering of a regular expression submatch and a string.
1653 * @param __lhs A regular expression submatch.
1654 * @param __rhs A const string reference.
1655 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1657 template<typename _Bi_iter
>
1659 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1660 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1661 { return !(__lhs
< __rhs
); }
1664 * @brief Tests the ordering of a regular expression submatch and a string.
1665 * @param __lhs A regular expression submatch.
1666 * @param __rhs A const string reference.
1667 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1669 template<typename _Bi_iter
>
1671 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1672 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1673 { return !(__rhs
< __lhs
); }
1676 * @brief Inserts a matched string into an output stream.
1678 * @param __os The output stream.
1679 * @param __m A submatch string.
1681 * @returns the output stream with the submatch string inserted.
1683 template<typename _Ch_type
, typename _Ch_traits
, typename _Bi_iter
>
1685 basic_ostream
<_Ch_type
, _Ch_traits
>&
1686 operator<<(basic_ostream
<_Ch_type
, _Ch_traits
>& __os
,
1687 const sub_match
<_Bi_iter
>& __m
)
1688 { return __os
<< __m
.str(); }
1690 // [7.10] Class template match_results
1693 * Special sub_match object representing an unmatched sub-expression.
1695 template<typename _Bi_iter
>
1696 inline const sub_match
<_Bi_iter
>&
1699 static const sub_match
<_Bi_iter
> __unmatched
= sub_match
<_Bi_iter
>();
1704 * @brief The results of a match or search operation.
1706 * A collection of character sequences representing the result of a regular
1707 * expression match. Storage for the collection is allocated and freed as
1708 * necessary by the member functions of class template match_results.
1710 * This class satisfies the Sequence requirements, with the exception that
1711 * only the operations defined for a const-qualified Sequence are supported.
1713 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1714 * the whole match. In this case the %sub_match member matched is always true.
1715 * The sub_match object stored at index n denotes what matched the marked
1716 * sub-expression n within the matched expression. If the sub-expression n
1717 * participated in a regular expression match then the %sub_match member
1718 * matched evaluates to true, and members first and second denote the range
1719 * of characters [first, second) which formed that match. Otherwise matched
1720 * is false, and members first and second point to the end of the sequence
1721 * that was searched.
1725 template<typename _Bi_iter
,
1726 typename _Alloc
= allocator
<sub_match
<_Bi_iter
> > >
1728 : private std::vector
<sub_match
<_Bi_iter
>, _Alloc
>
1732 * The vector base is empty if this does not represent a successful match.
1733 * Otherwise it contains n+3 elements where n is the number of marked
1736 * [1] 1st marked subexpression
1738 * [n] nth marked subexpression
1742 typedef std::vector
<sub_match
<_Bi_iter
>, _Alloc
> _Base_type
;
1743 typedef std::iterator_traits
<_Bi_iter
> __iter_traits
;
1744 typedef regex_constants::match_flag_type match_flag_type
;
1748 * @name 10.? Public Types
1751 typedef _Alloc allocator_type
;
1752 typedef sub_match
<_Bi_iter
> value_type
;
1753 typedef const value_type
& const_reference
;
1754 typedef const_reference reference
;
1755 typedef typename
_Base_type::const_iterator const_iterator
;
1756 typedef const_iterator iterator
;
1757 typedef typename
__iter_traits::difference_type difference_type
;
1758 typedef typename
__iter_traits::value_type char_type
;
1759 typedef typename allocator_traits
<_Alloc
>::size_type size_type
;
1762 typedef std::basic_string
<char_type
> string_type
;
1767 * @name 28.10.1 Construction, Copying, and Destruction
1772 * @brief Constructs a default %match_results container.
1773 * @post size() returns 0 and str() returns an empty string.
1776 match_results(const _Alloc
& __a
= _Alloc())
1781 * @brief Copy constructs a %match_results.
1783 match_results(const match_results
& __rhs
)
1788 * @brief Move constructs a %match_results.
1790 match_results(match_results
&& __rhs
) noexcept
1791 : _Base_type(std::move(__rhs
))
1795 * @brief Assigns rhs to *this.
1798 operator=(const match_results
& __rhs
)
1800 match_results(__rhs
).swap(*this);
1805 * @brief Move-assigns rhs to *this.
1808 operator=(match_results
&& __rhs
)
1810 match_results(std::move(__rhs
)).swap(*this);
1815 * @brief Destroys a %match_results object.
1824 * @brief Indicates if the %match_results is ready.
1825 * @retval true The object has a fully-established result state.
1826 * @retval false The object is not ready.
1828 bool ready() const { return !_Base_type::empty(); }
1831 * @name 28.10.2 Size
1836 * @brief Gets the number of matches and submatches.
1838 * The number of matches for a given regular expression will be either 0
1839 * if there was no match or mark_count() + 1 if a match was successful.
1840 * Some matches may be empty.
1842 * @returns the number of matches found.
1847 size_type __size
= _Base_type::size();
1848 return (__size
&& _Base_type::operator[](0).matched
) ? __size
- 2 : 0;
1853 { return _Base_type::max_size(); }
1856 * @brief Indicates if the %match_results contains no results.
1857 * @retval true The %match_results object is empty.
1858 * @retval false The %match_results object is not empty.
1862 { return size() == 0; }
1867 * @name 10.3 Element Access
1872 * @brief Gets the length of the indicated submatch.
1873 * @param __sub indicates the submatch.
1874 * @pre ready() == true
1876 * This function returns the length of the indicated submatch, or the
1877 * length of the entire match if @p __sub is zero (the default).
1880 length(size_type __sub
= 0) const
1881 { return (*this)[__sub
].length(); }
1884 * @brief Gets the offset of the beginning of the indicated submatch.
1885 * @param __sub indicates the submatch.
1886 * @pre ready() == true
1888 * This function returns the offset from the beginning of the target
1889 * sequence to the beginning of the submatch, unless the value of @p __sub
1890 * is zero (the default), in which case this function returns the offset
1891 * from the beginning of the target sequence to the beginning of the
1894 * Returns -1 if @p __sub is out of range.
1897 position(size_type __sub
= 0) const
1899 return __sub
< size() ? std::distance(this->prefix().first
,
1900 (*this)[__sub
].first
) : -1;
1904 * @brief Gets the match or submatch converted to a string type.
1905 * @param __sub indicates the submatch.
1906 * @pre ready() == true
1908 * This function gets the submatch (or match, if @p __sub is
1909 * zero) extracted from the target range and converted to the
1910 * associated string type.
1913 str(size_type __sub
= 0) const
1914 { return (*this)[__sub
].str(); }
1917 * @brief Gets a %sub_match reference for the match or submatch.
1918 * @param __sub indicates the submatch.
1919 * @pre ready() == true
1921 * This function gets a reference to the indicated submatch, or
1922 * the entire match if @p __sub is zero.
1924 * If @p __sub >= size() then this function returns a %sub_match with a
1925 * special value indicating no submatch.
1928 operator[](size_type __sub
) const
1930 _GLIBCXX_DEBUG_ASSERT( ready() );
1931 return __sub
< size()
1932 ? _Base_type::operator[](__sub
)
1933 : __unmatched_sub
<_Bi_iter
>();
1937 * @brief Gets a %sub_match representing the match prefix.
1938 * @pre ready() == true
1940 * This function gets a reference to a %sub_match object representing the
1941 * part of the target range between the start of the target range and the
1942 * start of the match.
1947 _GLIBCXX_DEBUG_ASSERT( ready() );
1949 ? _Base_type::operator[](_Base_type::size() - 2)
1950 : __unmatched_sub
<_Bi_iter
>();
1954 * @brief Gets a %sub_match representing the match suffix.
1955 * @pre ready() == true
1957 * This function gets a reference to a %sub_match object representing the
1958 * part of the target range between the end of the match and the end of
1964 _GLIBCXX_DEBUG_ASSERT( ready() );
1966 ? _Base_type::operator[](_Base_type::size() - 1)
1967 : __unmatched_sub
<_Bi_iter
>();
1971 * @brief Gets an iterator to the start of the %sub_match collection.
1975 { return _Base_type::begin(); }
1978 * @brief Gets an iterator to the start of the %sub_match collection.
1982 { return _Base_type::cbegin(); }
1985 * @brief Gets an iterator to one-past-the-end of the collection.
1989 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
1992 * @brief Gets an iterator to one-past-the-end of the collection.
2001 * @name 10.4 Formatting
2003 * These functions perform formatted substitution of the matched
2004 * character sequences into their target. The format specifiers and
2005 * escape sequences accepted by these functions are determined by
2006 * their @p flags parameter as documented above.
2011 * @pre ready() == true
2012 * @todo Implement this function.
2014 template<typename _Out_iter
>
2016 format(_Out_iter __out
, const char_type
* __fmt_first
,
2017 const char_type
* __fmt_last
,
2018 match_flag_type __flags
= regex_constants::format_default
) const
2022 * @pre ready() == true
2024 template<typename _Out_iter
, typename _St
, typename _Sa
>
2026 format(_Out_iter __out
, const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2027 match_flag_type __flags
= regex_constants::format_default
) const
2029 return format(__out
, __fmt
.data(), __fmt
.data() + __fmt
.size(),
2034 * @pre ready() == true
2036 template<typename _Out_iter
, typename _St
, typename _Sa
>
2037 basic_string
<char_type
, _St
, _Sa
>
2038 format(const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2039 match_flag_type __flags
= regex_constants::format_default
) const
2041 basic_string
<char_type
, _St
, _Sa
> __result
;
2042 format(std::back_inserter(__result
), __fmt
, __flags
);
2047 * @pre ready() == true
2050 format(const char_type
* __fmt
,
2051 match_flag_type __flags
= regex_constants::format_default
) const
2053 string_type __result
;
2054 format(std::back_inserter(__result
),
2055 __fmt
+ char_traits
<char_type
>::length(__fmt
),
2063 * @name 10.5 Allocator
2068 * @brief Gets a copy of the allocator.
2071 get_allocator() const
2072 { return _Base_type::get_allocator(); }
2082 * @brief Swaps the contents of two match_results.
2085 swap(match_results
& __that
)
2086 { _Base_type::swap(__that
); }
2090 friend class __detail::_SpecializedResults
<_Bi_iter
, _Alloc
>;
2093 typedef match_results
<const char*> cmatch
;
2094 typedef match_results
<string::const_iterator
> smatch
;
2095 #ifdef _GLIBCXX_USE_WCHAR_T
2096 typedef match_results
<const wchar_t*> wcmatch
;
2097 typedef match_results
<wstring::const_iterator
> wsmatch
;
2100 // match_results comparisons
2102 * @brief Compares two match_results for equality.
2103 * @returns true if the two objects refer to the same match,
2106 template<typename _Bi_iter
, typename _Alloc
>
2108 operator==(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2109 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2111 if (__m1
.ready() != __m2
.ready())
2113 if (!__m1
.ready()) // both are not ready
2115 if (__m1
.empty() != __m2
.empty())
2117 if (__m1
.empty()) // both are empty
2119 return __m1
.prefix() == __m2
.prefix()
2120 && __m1
.size() == __m2
.size()
2121 && std::equal(__m1
.begin(), __m1
.end(), __m2
.begin())
2122 && __m1
.suffix() == __m2
.suffix();
2126 * @brief Compares two match_results for inequality.
2127 * @returns true if the two objects do not refer to the same match,
2130 template<typename _Bi_iter
, class _Alloc
>
2132 operator!=(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2133 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2134 { return !(__m1
== __m2
); }
2136 // [7.10.6] match_results swap
2138 * @brief Swaps two match results.
2139 * @param __lhs A match result.
2140 * @param __rhs A match result.
2142 * The contents of the two match_results objects are swapped.
2144 template<typename _Bi_iter
, typename _Alloc
>
2146 swap(match_results
<_Bi_iter
, _Alloc
>& __lhs
,
2147 match_results
<_Bi_iter
, _Alloc
>& __rhs
)
2148 { __lhs
.swap(__rhs
); }
2150 // [7.11.2] Function template regex_match
2152 * @name Matching, Searching, and Replacing
2157 * @brief Determines if there is a match between the regular expression @p e
2158 * and all of the character sequence [first, last).
2160 * @param __s Start of the character sequence to match.
2161 * @param __e One-past-the-end of the character sequence to match.
2162 * @param __m The match results.
2163 * @param __re The regular expression.
2164 * @param __flags Controls how the regular expression is matched.
2166 * @retval true A match exists.
2167 * @retval false Otherwise.
2169 * @throws an exception of type regex_error.
2171 * @todo Implement this function.
2173 template<typename _Bi_iter
, typename _Alloc
,
2174 typename _Ch_type
, typename _Rx_traits
>
2176 regex_match(_Bi_iter __s
,
2178 match_results
<_Bi_iter
, _Alloc
>& __m
,
2179 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2180 regex_constants::match_flag_type __flags
2181 = regex_constants::match_default
)
2183 __detail::_AutomatonPtr __a
= __re
._M_get_automaton();
2184 __detail::_Automaton::_SizeT __sz
= __a
->_M_sub_count();
2185 __detail::_SpecializedCursor
<_Bi_iter
> __cs(__s
, __e
);
2186 __detail::_SpecializedResults
<_Bi_iter
, _Alloc
> __r(__sz
, __cs
, __m
);
2187 __detail::_Grep_matcher
__matcher(__cs
, __r
, __a
, __flags
);
2188 return __matcher
._M_dfs_match();
2192 * @brief Indicates if there is a match between the regular expression @p e
2193 * and all of the character sequence [first, last).
2195 * @param __first Beginning of the character sequence to match.
2196 * @param __last One-past-the-end of the character sequence to match.
2197 * @param __re The regular expression.
2198 * @param __flags Controls how the regular expression is matched.
2200 * @retval true A match exists.
2201 * @retval false Otherwise.
2203 * @throws an exception of type regex_error.
2205 template<typename _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2207 regex_match(_Bi_iter __first
, _Bi_iter __last
,
2208 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2209 regex_constants::match_flag_type __flags
2210 = regex_constants::match_default
)
2212 match_results
<_Bi_iter
> __what
;
2213 return regex_match(__first
, __last
, __what
, __re
, __flags
);
2217 * @brief Determines if there is a match between the regular expression @p e
2218 * and a C-style null-terminated string.
2220 * @param __s The C-style null-terminated string to match.
2221 * @param __m The match results.
2222 * @param __re The regular expression.
2223 * @param __f Controls how the regular expression is matched.
2225 * @retval true A match exists.
2226 * @retval false Otherwise.
2228 * @throws an exception of type regex_error.
2230 template<typename _Ch_type
, typename _Alloc
, typename _Rx_traits
>
2232 regex_match(const _Ch_type
* __s
,
2233 match_results
<const _Ch_type
*, _Alloc
>& __m
,
2234 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2235 regex_constants::match_flag_type __f
2236 = regex_constants::match_default
)
2237 { return regex_match(__s
, __s
+ _Rx_traits::length(__s
), __m
, __re
, __f
); }
2240 * @brief Determines if there is a match between the regular expression @p e
2243 * @param __s The string to match.
2244 * @param __m The match results.
2245 * @param __re The regular expression.
2246 * @param __flags Controls how the regular expression is matched.
2248 * @retval true A match exists.
2249 * @retval false Otherwise.
2251 * @throws an exception of type regex_error.
2253 template<typename _Ch_traits
, typename _Ch_alloc
,
2254 typename _Alloc
, typename _Ch_type
, typename _Rx_traits
>
2256 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2257 match_results
<typename basic_string
<_Ch_type
,
2258 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2259 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2260 regex_constants::match_flag_type __flags
2261 = regex_constants::match_default
)
2262 { return regex_match(__s
.begin(), __s
.end(), __m
, __re
, __flags
); }
2265 * @brief Indicates if there is a match between the regular expression @p e
2266 * and a C-style null-terminated string.
2268 * @param __s The C-style null-terminated string to match.
2269 * @param __re The regular expression.
2270 * @param __f Controls how the regular expression is matched.
2272 * @retval true A match exists.
2273 * @retval false Otherwise.
2275 * @throws an exception of type regex_error.
2277 template<typename _Ch_type
, class _Rx_traits
>
2279 regex_match(const _Ch_type
* __s
,
2280 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2281 regex_constants::match_flag_type __f
2282 = regex_constants::match_default
)
2283 { return regex_match(__s
, __s
+ _Rx_traits::length(__s
), __re
, __f
); }
2286 * @brief Indicates if there is a match between the regular expression @p e
2289 * @param __s [IN] The string to match.
2290 * @param __re [IN] The regular expression.
2291 * @param __flags [IN] Controls how the regular expression is matched.
2293 * @retval true A match exists.
2294 * @retval false Otherwise.
2296 * @throws an exception of type regex_error.
2298 template<typename _Ch_traits
, typename _Str_allocator
,
2299 typename _Ch_type
, typename _Rx_traits
>
2301 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Str_allocator
>& __s
,
2302 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2303 regex_constants::match_flag_type __flags
2304 = regex_constants::match_default
)
2305 { return regex_match(__s
.begin(), __s
.end(), __re
, __flags
); }
2307 // [7.11.3] Function template regex_search
2309 * Searches for a regular expression within a range.
2310 * @param __first [IN] The start of the string to search.
2311 * @param __last [IN] One-past-the-end of the string to search.
2312 * @param __m [OUT] The match results.
2313 * @param __re [IN] The regular expression to search for.
2314 * @param __flags [IN] Search policy flags.
2315 * @retval true A match was found within the string.
2316 * @retval false No match was found within the string, the content of %m is
2319 * @throws an exception of type regex_error.
2321 * @todo Implement this function.
2323 template<typename _Bi_iter
, typename _Alloc
,
2324 typename _Ch_type
, typename _Rx_traits
>
2326 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2327 match_results
<_Bi_iter
, _Alloc
>& __m
,
2328 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2329 regex_constants::match_flag_type __flags
2330 = regex_constants::match_default
)
2332 __detail::_AutomatonPtr __a
= __re
._M_get_automaton();
2333 __detail::_Automaton::_SizeT __sz
= __a
->_M_sub_count();
2334 __detail::_SpecializedCursor
<_Bi_iter
> __cs(__first
, __last
);
2335 __detail::_SpecializedResults
<_Bi_iter
, _Alloc
> __r(__sz
, __cs
, __m
);
2336 for (auto __cur
= __first
; __cur
!= __last
; ++__cur
) // Any KMP-like algo?
2338 __detail::_SpecializedCursor
<_Bi_iter
> __curs(__cur
, __last
);
2339 __detail::_Grep_matcher
__matcher(__curs
, __r
, __a
, __flags
);
2340 if (__matcher
._M_dfs_search_from_first())
2342 __r
._M_set_range(__m
.size(),
2343 __detail::_SpecializedCursor
<_Bi_iter
>
2344 {__first
, __m
[0].first
});
2345 __r
._M_set_range(__m
.size()+1,
2346 __detail::_SpecializedCursor
<_Bi_iter
>
2347 {__m
[0].second
, __last
});
2348 __r
._M_set_matched(__m
.size(),
2349 __m
.prefix().first
!= __m
.prefix().second
);
2350 __r
._M_set_matched(__m
.size()+1,
2351 __m
.suffix().first
!= __m
.suffix().second
);
2360 * Searches for a regular expression within a range.
2361 * @param __first [IN] The start of the string to search.
2362 * @param __last [IN] One-past-the-end of the string to search.
2363 * @param __re [IN] The regular expression to search for.
2364 * @param __flags [IN] Search policy flags.
2365 * @retval true A match was found within the string.
2366 * @retval false No match was found within the string.
2368 * @throws an exception of type regex_error.
2370 template<typename _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2372 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2373 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2374 regex_constants::match_flag_type __flags
2375 = regex_constants::match_default
)
2377 match_results
<_Bi_iter
> __what
;
2378 return regex_search(__first
, __last
, __what
, __re
, __flags
);
2382 * @brief Searches for a regular expression within a C-string.
2383 * @param __s [IN] A C-string to search for the regex.
2384 * @param __m [OUT] The set of regex matches.
2385 * @param __e [IN] The regex to search for in @p s.
2386 * @param __f [IN] The search flags.
2387 * @retval true A match was found within the string.
2388 * @retval false No match was found within the string, the content of %m is
2391 * @throws an exception of type regex_error.
2393 template<typename _Ch_type
, class _Alloc
, class _Rx_traits
>
2395 regex_search(const _Ch_type
* __s
,
2396 match_results
<const _Ch_type
*, _Alloc
>& __m
,
2397 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2398 regex_constants::match_flag_type __f
2399 = regex_constants::match_default
)
2400 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __m
, __e
, __f
); }
2403 * @brief Searches for a regular expression within a C-string.
2404 * @param __s [IN] The C-string to search.
2405 * @param __e [IN] The regular expression to search for.
2406 * @param __f [IN] Search policy flags.
2407 * @retval true A match was found within the string.
2408 * @retval false No match was found within the string.
2410 * @throws an exception of type regex_error.
2412 template<typename _Ch_type
, typename _Rx_traits
>
2414 regex_search(const _Ch_type
* __s
,
2415 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2416 regex_constants::match_flag_type __f
2417 = regex_constants::match_default
)
2418 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __e
, __f
); }
2421 * @brief Searches for a regular expression within a string.
2422 * @param __s [IN] The string to search.
2423 * @param __e [IN] The regular expression to search for.
2424 * @param __flags [IN] Search policy flags.
2425 * @retval true A match was found within the string.
2426 * @retval false No match was found within the string.
2428 * @throws an exception of type regex_error.
2430 template<typename _Ch_traits
, typename _String_allocator
,
2431 typename _Ch_type
, typename _Rx_traits
>
2433 regex_search(const basic_string
<_Ch_type
, _Ch_traits
,
2434 _String_allocator
>& __s
,
2435 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2436 regex_constants::match_flag_type __flags
2437 = regex_constants::match_default
)
2438 { return regex_search(__s
.begin(), __s
.end(), __e
, __flags
); }
2441 * @brief Searches for a regular expression within a string.
2442 * @param __s [IN] A C++ string to search for the regex.
2443 * @param __m [OUT] The set of regex matches.
2444 * @param __e [IN] The regex to search for in @p s.
2445 * @param __f [IN] The search flags.
2446 * @retval true A match was found within the string.
2447 * @retval false No match was found within the string, the content of %m is
2450 * @throws an exception of type regex_error.
2452 template<typename _Ch_traits
, typename _Ch_alloc
,
2453 typename _Alloc
, typename _Ch_type
,
2454 typename _Rx_traits
>
2456 regex_search(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2457 match_results
<typename basic_string
<_Ch_type
,
2458 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2459 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2460 regex_constants::match_flag_type __f
2461 = regex_constants::match_default
)
2462 { return regex_search(__s
.begin(), __s
.end(), __m
, __e
, __f
); }
2464 // std [28.11.4] Function template regex_replace
2475 * @throws an exception of type regex_error.
2477 * @todo Implement this function.
2479 template<typename _Out_iter
, typename _Bi_iter
,
2480 typename _Rx_traits
, typename _Ch_type
>
2482 regex_replace(_Out_iter __out
, _Bi_iter __first
, _Bi_iter __last
,
2483 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2484 const basic_string
<_Ch_type
>& __fmt
,
2485 regex_constants::match_flag_type __flags
2486 = regex_constants::match_default
)
2496 * @returns a copy of string @p s with replacements.
2498 * @throws an exception of type regex_error.
2500 template<typename _Rx_traits
, typename _Ch_type
>
2501 inline basic_string
<_Ch_type
>
2502 regex_replace(const basic_string
<_Ch_type
>& __s
,
2503 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2504 const basic_string
<_Ch_type
>& __fmt
,
2505 regex_constants::match_flag_type __flags
2506 = regex_constants::match_default
)
2508 basic_string
<_Ch_type
> __result
;
2509 regex_replace(std::back_inserter(__result
),
2510 __s
.begin(), __s
.end(), __e
, __fmt
, __flags
);
2516 // std [28.12] Class template regex_iterator
2518 * An iterator adaptor that will provide repeated calls of regex_search over
2519 * a range until no more matches remain.
2521 template<typename _Bi_iter
,
2522 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2523 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2524 class regex_iterator
2527 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2528 typedef match_results
<_Bi_iter
> value_type
;
2529 typedef std::ptrdiff_t difference_type
;
2530 typedef const value_type
* pointer
;
2531 typedef const value_type
& reference
;
2532 typedef std::forward_iterator_tag iterator_category
;
2535 * @brief Provides a singular iterator, useful for indicating
2536 * one-past-the-end of a range.
2543 * Constructs a %regex_iterator...
2544 * @param __a [IN] The start of a text range to search.
2545 * @param __b [IN] One-past-the-end of the text range to search.
2546 * @param __re [IN] The regular expression to match.
2547 * @param __m [IN] Policy flags for match rules.
2549 regex_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2550 regex_constants::match_flag_type __m
2551 = regex_constants::match_default
)
2552 : _M_begin(__a
), _M_end(__b
), _M_pregex(&__re
), _M_flags(__m
), _M_match()
2553 { regex_search(_M_begin
, _M_end
, _M_match
, *_M_pregex
, _M_flags
); }
2556 * Copy constructs a %regex_iterator.
2558 regex_iterator(const regex_iterator
& __rhs
) = default;
2561 * @brief Assigns one %regex_iterator to another.
2564 operator=(const regex_iterator
& __rhs
) = default;
2567 * @brief Tests the equivalence of two regex iterators.
2570 operator==(const regex_iterator
& __rhs
) const;
2573 * @brief Tests the inequivalence of two regex iterators.
2576 operator!=(const regex_iterator
& __rhs
) const
2577 { return !(*this == __rhs
); }
2580 * @brief Dereferences a %regex_iterator.
2584 { return _M_match
; }
2587 * @brief Selects a %regex_iterator member.
2591 { return &_M_match
; }
2594 * @brief Increments a %regex_iterator.
2600 * @brief Postincrements a %regex_iterator.
2613 const regex_type
* _M_pregex
;
2614 regex_constants::match_flag_type _M_flags
;
2615 match_results
<_Bi_iter
> _M_match
;
2618 template<typename _Bi_iter
,
2620 typename _Rx_traits
>
2622 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2623 operator==(const regex_iterator
& __rhs
) const
2625 return (_M_match
.empty() && __rhs
._M_match
.empty())
2626 || (_M_begin
== __rhs
._M_begin
2627 && _M_end
== __rhs
._M_end
2628 && _M_pregex
== __rhs
._M_pregex
2629 && _M_flags
== __rhs
._M_flags
2630 && _M_match
[0] == __rhs
._M_match
[0]);
2633 template<typename _Bi_iter
,
2635 typename _Rx_traits
>
2636 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2637 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2640 // FIXME: In all cases in which the call to regex_search returns true,
2641 // match.prefix().first shall be equal to the previous value of
2642 // match[0].second, and for each index i in the half-open range
2643 // [0, match.size()) for which match[i].matched is true,
2644 // match[i].position() shall return distance(begin, match[i].first).
2646 if (_M_match
[0].matched
)
2648 auto __start
= _M_match
[0].second
;
2649 if (_M_match
[0].first
== _M_match
[0].second
)
2650 if (__start
== _M_end
)
2652 _M_match
= value_type();
2657 if (regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
2658 | regex_constants::match_not_null
2659 | regex_constants::match_continuous
))
2664 _M_flags
|= regex_constants::match_prev_avail
;
2665 if (!regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
))
2666 _M_match
= value_type();
2671 typedef regex_iterator
<const char*> cregex_iterator
;
2672 typedef regex_iterator
<string::const_iterator
> sregex_iterator
;
2673 #ifdef _GLIBCXX_USE_WCHAR_T
2674 typedef regex_iterator
<const wchar_t*> wcregex_iterator
;
2675 typedef regex_iterator
<wstring::const_iterator
> wsregex_iterator
;
2678 // [7.12.2] Class template regex_token_iterator
2680 * Iterates over submatches in a range (or @a splits a text string).
2682 * The purpose of this iterator is to enumerate all, or all specified,
2683 * matches of a regular expression within a text range. The dereferenced
2684 * value of an iterator of this class is a std::sub_match object.
2686 template<typename _Bi_iter
,
2687 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2688 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2689 class regex_token_iterator
2692 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2693 typedef sub_match
<_Bi_iter
> value_type
;
2694 typedef std::ptrdiff_t difference_type
;
2695 typedef const value_type
* pointer
;
2696 typedef const value_type
& reference
;
2697 typedef std::forward_iterator_tag iterator_category
;
2701 * @brief Default constructs a %regex_token_iterator.
2703 * A default-constructed %regex_token_iterator is a singular iterator
2704 * that will compare equal to the one-past-the-end value for any
2705 * iterator of the same type.
2707 regex_token_iterator()
2708 : _M_position(), _M_result(nullptr), _M_suffix(), _M_n(0), _M_subs()
2712 * Constructs a %regex_token_iterator...
2713 * @param __a [IN] The start of the text to search.
2714 * @param __b [IN] One-past-the-end of the text to search.
2715 * @param __re [IN] The regular expression to search for.
2716 * @param __submatch [IN] Which submatch to return. There are some
2717 * special values for this parameter:
2718 * - -1 each enumerated subexpression does NOT
2719 * match the regular expression (aka field
2721 * - 0 the entire string matching the
2722 * subexpression is returned for each match
2724 * - >0 enumerates only the indicated
2725 * subexpression from a match within the text.
2726 * @param __m [IN] Policy flags for match rules.
2728 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2730 regex_constants::match_flag_type __m
2731 = regex_constants::match_default
)
2732 : _M_position(__a
, __b
, __re
, __m
), _M_subs(1, __submatch
), _M_n(0)
2733 { _M_init(__a
, __b
); }
2736 * Constructs a %regex_token_iterator...
2737 * @param __a [IN] The start of the text to search.
2738 * @param __b [IN] One-past-the-end of the text to search.
2739 * @param __re [IN] The regular expression to search for.
2740 * @param __submatches [IN] A list of subexpressions to return for each
2741 * regular expression match within the text.
2742 * @param __m [IN] Policy flags for match rules.
2744 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2745 const regex_type
& __re
,
2746 const std::vector
<int>& __submatches
,
2747 regex_constants::match_flag_type __m
2748 = regex_constants::match_default
)
2749 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2750 { _M_init(__a
, __b
); }
2753 * Constructs a %regex_token_iterator...
2754 * @param __a [IN] The start of the text to search.
2755 * @param __b [IN] One-past-the-end of the text to search.
2756 * @param __re [IN] The regular expression to search for.
2757 * @param __submatches [IN] A list of subexpressions to return for each
2758 * regular expression match within the text.
2759 * @param __m [IN] Policy flags for match rules.
2761 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2762 const regex_type
& __re
,
2763 initializer_list
<int> __submatches
,
2764 regex_constants::match_flag_type __m
2765 = regex_constants::match_default
)
2766 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2767 { _M_init(__a
, __b
); }
2770 * Constructs a %regex_token_iterator...
2771 * @param __a [IN] The start of the text to search.
2772 * @param __b [IN] One-past-the-end of the text to search.
2773 * @param __re [IN] The regular expression to search for.
2774 * @param __submatches [IN] A list of subexpressions to return for each
2775 * regular expression match within the text.
2776 * @param __m [IN] Policy flags for match rules.
2778 template<std::size_t _Nm
>
2779 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2780 const regex_type
& __re
,
2781 const int (&__submatches
)[_Nm
],
2782 regex_constants::match_flag_type __m
2783 = regex_constants::match_default
)
2784 : _M_position(__a
, __b
, __re
, __m
),
2785 _M_subs(__submatches
, *(&__submatches
+1)), _M_n(0)
2786 { _M_init(__a
, __b
); }
2789 * @brief Copy constructs a %regex_token_iterator.
2790 * @param __rhs [IN] A %regex_token_iterator to copy.
2792 regex_token_iterator(const regex_token_iterator
& __rhs
)
2793 : _M_position(__rhs
.position
), _M_subs(__rhs
.subs
), _M_n(__rhs
.N
),
2794 _M_result(__rhs
.result
), _M_suffix(__rhs
.suffix
),
2795 _M_has_m1(__rhs
._M_has_m1
)
2797 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2798 _M_result
= &_M_suffix
;
2802 * @brief Assigns a %regex_token_iterator to another.
2803 * @param __rhs [IN] A %regex_token_iterator to copy.
2805 regex_token_iterator
&
2806 operator=(const regex_token_iterator
& __rhs
);
2809 * @brief Compares a %regex_token_iterator to another for equality.
2812 operator==(const regex_token_iterator
& __rhs
) const;
2815 * @brief Compares a %regex_token_iterator to another for inequality.
2818 operator!=(const regex_token_iterator
& __rhs
) const
2819 { return !(*this == __rhs
); }
2822 * @brief Dereferences a %regex_token_iterator.
2826 { return *_M_result
; }
2829 * @brief Selects a %regex_token_iterator member.
2833 { return _M_result
; }
2836 * @brief Increments a %regex_token_iterator.
2838 regex_token_iterator
&
2842 * @brief Postincrements a %regex_token_iterator.
2844 regex_token_iterator
2853 typedef regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
> _Position
;
2856 _M_init(_Bi_iter __a
, _Bi_iter __b
);
2859 _M_current_match() const
2861 if (_M_subs
[_M_n
] == -1)
2862 return (*_M_position
).prefix();
2864 return (*_M_position
)[_M_subs
[_M_n
]];
2868 _M_end_of_seq() const
2869 { return _M_result
!= nullptr; }
2871 _Position _M_position
;
2872 const value_type
* _M_result
;
2873 value_type _M_suffix
;
2875 std::vector
<int> _M_subs
;
2877 // Show whether _M_subs contains -1
2881 template<typename _Bi_iter
,
2883 typename _Rx_traits
>
2884 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2885 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2886 operator=(const regex_token_iterator
& __rhs
)
2888 _M_position
= __rhs
._M_position
;
2889 _M_subs
= __rhs
._M_subs
;
2891 _M_result
= __rhs
._M_result
;
2892 _M_suffix
= __rhs
._M_suffix
;
2893 _M_has_m1
= __rhs
._M_has_m1
;
2894 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2895 _M_result
= &_M_suffix
;
2898 template<typename _Bi_iter
,
2900 typename _Rx_traits
>
2902 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2903 operator==(const regex_token_iterator
& __rhs
) const
2905 if (_M_end_of_seq() && __rhs
._M_end_of_seq())
2907 if (_M_suffix
.matched
&& __rhs
._M_suffix
.matched
2908 && _M_suffix
== __rhs
._M_suffix
)
2910 if (_M_end_of_seq() || _M_suffix
.matched
2911 || __rhs
._M_end_of_seq() || __rhs
._M_suffix
.matched
)
2913 return _M_position
== __rhs
._M_position
2914 && _M_n
== __rhs
._M_n
2915 && _M_subs
== __rhs
._M_subs
;
2918 template<typename _Bi_iter
,
2920 typename _Rx_traits
>
2921 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2922 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2925 _Position __prev
= _M_position
;
2926 if (_M_suffix
.matched
)
2927 *this = regex_token_iterator();
2928 else if (_M_n
+ 1 < _M_subs
.size())
2931 _M_result
= &_M_current_match();
2937 if (_M_position
!= _Position())
2938 _M_result
= &_M_current_match();
2939 else if (_M_has_m1
&& __prev
->suffix().length() != 0)
2941 _M_suffix
.matched
= true;
2942 _M_suffix
.first
= __prev
->suffix().first
;
2943 _M_suffix
.second
= __prev
->suffix().second
;
2944 _M_result
= &_M_suffix
;
2947 *this = regex_token_iterator();
2952 template<typename _Bi_iter
,
2954 typename _Rx_traits
>
2956 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2957 _M_init(_Bi_iter __a
, _Bi_iter __b
)
2960 for (auto __it
: _M_subs
)
2966 if (_M_position
!= _Position())
2967 _M_result
= &_M_current_match();
2970 _M_suffix
.matched
= true;
2971 _M_suffix
.first
= __a
;
2972 _M_suffix
.second
= __b
;
2973 _M_result
= &_M_suffix
;
2976 _M_result
= nullptr;
2979 /** @brief Token iterator for C-style NULL-terminated strings. */
2980 typedef regex_token_iterator
<const char*> cregex_token_iterator
;
2982 /** @brief Token iterator for standard strings. */
2983 typedef regex_token_iterator
<string::const_iterator
> sregex_token_iterator
;
2985 #ifdef _GLIBCXX_USE_WCHAR_T
2986 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2987 typedef regex_token_iterator
<const wchar_t*> wcregex_token_iterator
;
2989 /** @brief Token iterator for standard wide-character strings. */
2990 typedef regex_token_iterator
<wstring::const_iterator
> wsregex_token_iterator
;
2994 _GLIBCXX_END_NAMESPACE_VERSION