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,
65 // when locale's complete.
66 static constexpr unsigned char _S_blank
= 1 << 1;
67 static constexpr unsigned char _S_valid_mask
= 0x3;
69 constexpr _RegexMask(_BaseType __base
= 0,
70 unsigned char __extended
= 0)
71 : _M_base(__base
), _M_extended(__extended
)
75 operator&(_RegexMask __other
) const
77 return _RegexMask(_M_base
& __other
._M_base
,
78 _M_extended
& __other
._M_extended
);
82 operator|(_RegexMask __other
) const
84 return _RegexMask(_M_base
| __other
._M_base
,
85 _M_extended
| __other
._M_extended
);
89 operator^(_RegexMask __other
) const
91 return _RegexMask(_M_base
^ __other
._M_base
,
92 _M_extended
^ __other
._M_extended
);
97 { return _RegexMask(~_M_base
, ~_M_extended
); }
100 operator&=(_RegexMask __other
)
101 { return *this = (*this) & __other
; }
104 operator|=(_RegexMask __other
)
105 { return *this = (*this) | __other
; }
108 operator^=(_RegexMask __other
)
109 { return *this = (*this) ^ __other
; }
112 operator==(_RegexMask __other
) const
114 return (_M_extended
& _S_valid_mask
)
115 == (__other
._M_extended
& _S_valid_mask
)
116 && _M_base
== __other
._M_base
;
120 operator!=(_RegexMask __other
) const
121 { return !((*this) == __other
); }
125 typedef _RegexMask char_class_type
;
129 * @brief Constructs a default traits object.
134 * @brief Gives the length of a C-style string starting at @p __p.
136 * @param __p a pointer to the start of a character sequence.
138 * @returns the number of characters between @p *__p and the first
139 * default-initialized value of type @p char_type. In other words, uses
140 * the C-string algorithm for determining the length of a sequence of
144 length(const char_type
* __p
)
145 { return string_type::traits_type::length(__p
); }
148 * @brief Performs the identity translation.
150 * @param __c A character to the locale-specific character set.
155 translate(char_type __c
) const
159 * @brief Translates a character into a case-insensitive equivalent.
161 * @param __c A character to the locale-specific character set.
163 * @returns the locale-specific lower-case equivalent of __c.
164 * @throws std::bad_cast if the imbued locale does not support the ctype
168 translate_nocase(char_type __c
) const
170 typedef std::ctype
<char_type
> __ctype_type
;
171 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
172 return __fctyp
.tolower(__c
);
176 * @brief Gets a sort key for a character sequence.
178 * @param __first beginning of the character sequence.
179 * @param __last one-past-the-end of the character sequence.
181 * Returns a sort key for the character sequence designated by the
182 * iterator range [F1, F2) such that if the character sequence [G1, G2)
183 * sorts before the character sequence [H1, H2) then
184 * v.transform(G1, G2) < v.transform(H1, H2).
186 * What this really does is provide a more efficient way to compare a
187 * string to multiple other strings in locales with fancy collation
188 * rules and equivalence classes.
190 * @returns a locale-specific sort key equivalent to the input range.
192 * @throws std::bad_cast if the current locale does not have a collate
195 template<typename _Fwd_iter
>
197 transform(_Fwd_iter __first
, _Fwd_iter __last
) const
199 typedef std::collate
<char_type
> __collate_type
;
200 const __collate_type
& __fclt(use_facet
<__collate_type
>(_M_locale
));
201 string_type
__s(__first
, __last
);
202 return __fclt
.transform(__s
.data(), __s
.data() + __s
.size());
206 * @brief Gets a sort key for a character sequence, independent of case.
208 * @param __first beginning of the character sequence.
209 * @param __last one-past-the-end of the character sequence.
211 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
212 * typeid(collate_byname<_Ch_type>) and the form of the sort key
213 * returned by collate_byname<_Ch_type>::transform(__first, __last)
214 * is known and can be converted into a primary sort key
215 * then returns that key, otherwise returns an empty string.
217 * @todo Implement this function.
219 template<typename _Fwd_iter
>
221 transform_primary(_Fwd_iter __first
, _Fwd_iter __last
) const
223 typedef std::ctype
<char_type
> __ctype_type
;
224 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
225 std::vector
<char_type
> __s(__first
, __last
);
226 // FIXME : this is not entirely correct
227 __fctyp
.tolower(__s
.data(), __s
.data() + __s
.size());
228 return this->transform(__s
.data(), __s
.data() + __s
.size());
232 * @brief Gets a collation element by name.
234 * @param __first beginning of the collation element name.
235 * @param __last one-past-the-end of the collation element name.
237 * @returns a sequence of one or more characters that represents the
238 * collating element consisting of the character sequence designated by
239 * the iterator range [__first, __last). Returns an empty string if the
240 * character sequence is not a valid collating element.
242 template<typename _Fwd_iter
>
244 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const;
247 * @brief Maps one or more characters to a named character
250 * @param __first beginning of the character sequence.
251 * @param __last one-past-the-end of the character sequence.
252 * @param __icase ignores the case of the classification name.
254 * @returns an unspecified value that represents the character
255 * classification named by the character sequence designated by
256 * the iterator range [__first, __last). If @p icase is true,
257 * the returned mask identifies the classification regardless of
258 * the case of the characters to be matched (for example,
259 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
260 * case-dependent classification is returned. The value
261 * returned shall be independent of the case of the characters
262 * in the character sequence. If the name is not recognized then
263 * returns a value that compares equal to 0.
265 * At least the following names (or their wide-character equivalent) are
283 template<typename _Fwd_iter
>
285 lookup_classname(_Fwd_iter __first
, _Fwd_iter __last
,
286 bool __icase
= false) const;
289 * @brief Determines if @p c is a member of an identified class.
291 * @param __c a character.
292 * @param __f a class type (as returned from lookup_classname).
294 * @returns true if the character @p __c is a member of the classification
295 * represented by @p __f, false otherwise.
297 * @throws std::bad_cast if the current locale does not have a ctype
301 isctype(_Ch_type __c
, char_class_type __f
) const;
304 * @brief Converts a digit to an int.
306 * @param __ch a character representing a digit.
307 * @param __radix the radix if the numeric conversion (limited to 8, 10,
310 * @returns the value represented by the digit __ch in base radix if the
311 * character __ch is a valid digit in base radix; otherwise returns -1.
314 value(_Ch_type __ch
, int __radix
) const;
317 * @brief Imbues the regex_traits object with a copy of a new locale.
319 * @param __loc A locale.
321 * @returns a copy of the previous locale in use by the regex_traits
324 * @note Calling imbue with a different locale than the one currently in
325 * use invalidates all cached data held by *this.
328 imbue(locale_type __loc
)
330 std::swap(_M_locale
, __loc
);
335 * @brief Gets a copy of the current locale in use by the regex_traits
340 { return _M_locale
; }
343 locale_type _M_locale
;
346 template<typename _Ch_type
>
347 template<typename _Fwd_iter
>
348 typename regex_traits
<_Ch_type
>::string_type
349 regex_traits
<_Ch_type
>::
350 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const
352 typedef std::ctype
<char_type
> __ctype_type
;
353 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
355 static const char* __collatenames
[] =
448 "left-square-bracket",
450 "right-square-bracket",
480 "left-curly-bracket",
482 "right-curly-bracket",
489 static const char* __digraphs
[] =
515 std::string
__s(__last
- __first
, '?');
516 __fctyp
.narrow(__first
, __last
, '?', &*__s
.begin());
518 for (unsigned int __i
= 0; *__collatenames
[__i
]; __i
++)
519 if (__s
== __collatenames
[__i
])
520 return string_type(1, __fctyp
.widen((char)__i
));
522 for (unsigned int __i
= 0; *__digraphs
[__i
]; __i
++)
524 const char* __now
= __digraphs
[__i
];
527 string_type
ret(__s
.size(), __fctyp
.widen('?'));
528 __fctyp
.widen(__now
, __now
+ 2/* ouch */, &*ret
.begin());
532 return string_type();
535 template<typename _Ch_type
>
536 template<typename _Fwd_iter
>
537 typename regex_traits
<_Ch_type
>::char_class_type
538 regex_traits
<_Ch_type
>::
539 lookup_classname(_Fwd_iter __first
, _Fwd_iter __last
, bool __icase
) const
541 typedef std::ctype
<char_type
> __ctype_type
;
542 typedef std::ctype
<char> __cctype_type
;
543 typedef const pair
<const char*, char_class_type
> _ClassnameEntry
;
544 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
545 const __cctype_type
& __cctyp(use_facet
<__cctype_type
>(_M_locale
));
547 static _ClassnameEntry __classnames
[] =
549 {"d", ctype_base::digit
},
550 {"w", {ctype_base::alnum
, _RegexMask::_S_under
}},
551 {"s", ctype_base::space
},
552 {"alnum", ctype_base::alnum
},
553 {"alpha", ctype_base::alpha
},
554 {"blank", {0, _RegexMask::_S_blank
}},
555 {"cntrl", ctype_base::cntrl
},
556 {"digit", ctype_base::digit
},
557 {"graph", ctype_base::graph
},
558 {"lower", ctype_base::lower
},
559 {"print", ctype_base::print
},
560 {"punct", ctype_base::punct
},
561 {"space", ctype_base::space
},
562 {"upper", ctype_base::upper
},
563 {"xdigit", ctype_base::xdigit
},
566 std::string
__s(__last
- __first
, '?');
567 __fctyp
.narrow(__first
, __last
, '?', &__s
[0]);
568 __cctyp
.tolower(&*__s
.begin(), &*__s
.begin() + __s
.size());
569 for (_ClassnameEntry
* __it
= __classnames
;
570 __it
< *(&__classnames
+ 1);
573 if (__s
== __it
->first
)
577 & (ctype_base::lower
| ctype_base::upper
)) != 0))
578 return ctype_base::alpha
;
585 template<typename _Ch_type
>
587 regex_traits
<_Ch_type
>::
588 isctype(_Ch_type __c
, char_class_type __f
) const
590 typedef std::ctype
<char_type
> __ctype_type
;
591 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
593 return __fctyp
.is(__f
._M_base
, __c
)
595 || ((__f
._M_extended
& _RegexMask::_S_under
)
596 && __c
== __fctyp
.widen('_'))
598 || ((__f
._M_extended
& _RegexMask::_S_blank
)
599 && (__c
== __fctyp
.widen(' ')
600 || __c
== __fctyp
.widen('\t')));
603 template<typename _Ch_type
>
605 regex_traits
<_Ch_type
>::
606 value(_Ch_type __ch
, int __radix
) const
608 std::basic_istringstream
<char_type
> __is(string_type(1, __ch
));
612 else if (__radix
== 16)
615 return __is
.fail() ? -1 : __v
;
618 // [7.8] Class basic_regex
620 * Objects of specializations of this class represent regular expressions
621 * constructed from sequences of character type @p _Ch_type.
623 * Storage for the regular expression is allocated and deallocated as
624 * necessary by the member functions of this class.
626 template<typename _Ch_type
, typename _Rx_traits
= regex_traits
<_Ch_type
> >
631 typedef _Ch_type value_type
;
632 typedef _Rx_traits traits_type
;
633 typedef typename
traits_type::string_type string_type
;
634 typedef regex_constants::syntax_option_type flag_type
;
635 typedef typename
traits_type::locale_type locale_type
;
642 static constexpr flag_type icase
= regex_constants::icase
;
643 static constexpr flag_type nosubs
= regex_constants::nosubs
;
644 static constexpr flag_type optimize
= regex_constants::optimize
;
645 static constexpr flag_type collate
= regex_constants::collate
;
646 static constexpr flag_type ECMAScript
= regex_constants::ECMAScript
;
647 static constexpr flag_type basic
= regex_constants::basic
;
648 static constexpr flag_type extended
= regex_constants::extended
;
649 static constexpr flag_type awk
= regex_constants::awk
;
650 static constexpr flag_type grep
= regex_constants::grep
;
651 static constexpr flag_type egrep
= regex_constants::egrep
;
654 // [7.8.2] construct/copy/destroy
656 * Constructs a basic regular expression that does not match any
657 * character sequence.
660 : _M_flags(ECMAScript
), _M_automaton(nullptr)
664 * @brief Constructs a basic regular expression from the
665 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
666 * interpreted according to the flags in @p __f.
668 * @param __p A pointer to the start of a C-style null-terminated string
669 * containing a regular expression.
670 * @param __f Flags indicating the syntax rules and options.
672 * @throws regex_error if @p __p is not a valid regular expression.
675 basic_regex(const _Ch_type
* __p
, flag_type __f
= ECMAScript
)
676 : basic_regex(__p
, __p
+ _Rx_traits::length(__p
), __f
)
680 * @brief Constructs a basic regular expression from the sequence
681 * [p, p + len) interpreted according to the flags in @p f.
683 * @param __p A pointer to the start of a string containing a regular
685 * @param __len The length of the string containing the regular
687 * @param __f Flags indicating the syntax rules and options.
689 * @throws regex_error if @p __p is not a valid regular expression.
691 basic_regex(const _Ch_type
* __p
,
692 std::size_t __len
, flag_type __f
= ECMAScript
)
693 : basic_regex(__p
, __p
+ __len
, __f
)
697 * @brief Copy-constructs a basic regular expression.
699 * @param __rhs A @p regex object.
701 basic_regex(const basic_regex
& __rhs
) = default;
704 * @brief Move-constructs a basic regular expression.
706 * @param __rhs A @p regex object.
708 basic_regex(const basic_regex
&& __rhs
) noexcept
709 : _M_flags(__rhs
._M_flags
), _M_traits(__rhs
._M_traits
),
710 _M_automaton(std::move(__rhs
._M_automaton
))
714 * @brief Constructs a basic regular expression from the string
715 * @p s interpreted according to the flags in @p f.
717 * @param __s A string containing a regular expression.
718 * @param __f Flags indicating the syntax rules and options.
720 * @throws regex_error if @p __s is not a valid regular expression.
722 template<typename _Ch_traits
, typename _Ch_alloc
>
724 basic_regex(const std::basic_string
<_Ch_type
, _Ch_traits
,
726 flag_type __f
= ECMAScript
)
727 : basic_regex(__s
.begin(), __s
.end(), __f
)
731 * @brief Constructs a basic regular expression from the range
732 * [first, last) interpreted according to the flags in @p f.
734 * @param __first The start of a range containing a valid regular
736 * @param __last The end of a range containing a valid regular
738 * @param __f The format flags of the regular expression.
740 * @throws regex_error if @p [__first, __last) is not a valid regular
743 template<typename _FwdIter
>
744 basic_regex(_FwdIter __first
, _FwdIter __last
,
745 flag_type __f
= ECMAScript
)
747 _M_automaton(__detail::_Compiler
<_FwdIter
, _Ch_type
, _Rx_traits
>
748 (__first
, __last
, _M_traits
, _M_flags
)._M_get_nfa())
752 * @brief Constructs a basic regular expression from an initializer list.
754 * @param __l The initializer list.
755 * @param __f The format flags of the regular expression.
757 * @throws regex_error if @p __l is not a valid regular expression.
759 basic_regex(initializer_list
<_Ch_type
> __l
,
760 flag_type __f
= ECMAScript
)
761 : basic_regex(__l
.begin(), __l
.end(), __f
)
765 * @brief Destroys a basic regular expression.
771 * @brief Assigns one regular expression to another.
774 operator=(const basic_regex
& __rhs
)
775 { return this->assign(__rhs
); }
778 * @brief Move-assigns one regular expression to another.
781 operator=(basic_regex
&& __rhs
) noexcept
782 { return this->assign(std::move(__rhs
)); }
785 * @brief Replaces a regular expression with a new one constructed from
786 * a C-style null-terminated string.
788 * @param __p A pointer to the start of a null-terminated C-style string
789 * containing a regular expression.
792 operator=(const _Ch_type
* __p
)
793 { return this->assign(__p
, flags()); }
796 * @brief Replaces a regular expression with a new one constructed from
799 * @param __s A pointer to a string containing a regular expression.
801 template<typename _Ch_typeraits
, typename _Alloc
>
803 operator=(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
)
804 { return this->assign(__s
, flags()); }
808 * @brief the real assignment operator.
810 * @param __rhs Another regular expression object.
813 assign(const basic_regex
& __rhs
)
815 basic_regex
__tmp(__rhs
);
821 * @brief The move-assignment operator.
823 * @param __rhs Another regular expression object.
826 assign(basic_regex
&& __rhs
) noexcept
828 basic_regex
__tmp(std::move(__rhs
));
834 * @brief Assigns a new regular expression to a regex object from a
835 * C-style null-terminated string containing a regular expression
838 * @param __p A pointer to a C-style null-terminated string containing
839 * a regular expression pattern.
840 * @param __flags Syntax option flags.
842 * @throws regex_error if __p does not contain a valid regular
843 * expression pattern interpreted according to @p __flags. If
844 * regex_error is thrown, *this remains unchanged.
847 assign(const _Ch_type
* __p
, flag_type __flags
= ECMAScript
)
848 { return this->assign(string_type(__p
), __flags
); }
851 * @brief Assigns a new regular expression to a regex object from a
852 * C-style string containing a regular expression pattern.
854 * @param __p A pointer to a C-style string containing a
855 * regular expression pattern.
856 * @param __len The length of the regular expression pattern string.
857 * @param __flags Syntax option flags.
859 * @throws regex_error if p does not contain a valid regular
860 * expression pattern interpreted according to @p __flags. If
861 * regex_error is thrown, *this remains unchanged.
864 assign(const _Ch_type
* __p
, std::size_t __len
, flag_type __flags
)
865 { return this->assign(string_type(__p
, __len
), __flags
); }
868 * @brief Assigns a new regular expression to a regex object from a
869 * string containing a regular expression pattern.
871 * @param __s A string containing a regular expression pattern.
872 * @param __flags Syntax option flags.
874 * @throws regex_error if __s does not contain a valid regular
875 * expression pattern interpreted according to @p __flags. If
876 * regex_error is thrown, *this remains unchanged.
878 template<typename _Ch_typeraits
, typename _Alloc
>
880 assign(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
,
881 flag_type __flags
= ECMAScript
)
885 __detail::_Compiler
<decltype(__s
.begin()), _Ch_type
, _Rx_traits
>
886 (__s
.begin(), __s
.end(), _M_traits
, _M_flags
)._M_get_nfa();
891 * @brief Assigns a new regular expression to a regex object.
893 * @param __first The start of a range containing a valid regular
895 * @param __last The end of a range containing a valid regular
897 * @param __flags Syntax option flags.
899 * @throws regex_error if p does not contain a valid regular
900 * expression pattern interpreted according to @p __flags. If
901 * regex_error is thrown, the object remains unchanged.
903 template<typename _InputIterator
>
905 assign(_InputIterator __first
, _InputIterator __last
,
906 flag_type __flags
= ECMAScript
)
907 { return this->assign(string_type(__first
, __last
), __flags
); }
910 * @brief Assigns a new regular expression to a regex object.
912 * @param __l An initializer list representing a regular expression.
913 * @param __flags Syntax option flags.
915 * @throws regex_error if @p __l does not contain a valid
916 * regular expression pattern interpreted according to @p
917 * __flags. If regex_error is thrown, the object remains
921 assign(initializer_list
<_Ch_type
> __l
, flag_type __flags
= ECMAScript
)
922 { return this->assign(__l
.begin(), __l
.end(), __flags
); }
924 // [7.8.4] const operations
926 * @brief Gets the number of marked subexpressions within the regular
931 { return _M_automaton
->_M_sub_count() - 1; }
934 * @brief Gets the flags used to construct the regular expression
935 * or in the last call to assign().
943 * @brief Imbues the regular expression object with the given locale.
945 * @param __loc A locale.
948 imbue(locale_type __loc
)
949 { return _M_traits
.imbue(__loc
); }
952 * @brief Gets the locale currently imbued in the regular expression
957 { return _M_traits
.getloc(); }
961 * @brief Swaps the contents of two regular expression objects.
963 * @param __rhs Another regular expression object.
966 swap(basic_regex
& __rhs
)
968 std::swap(_M_flags
, __rhs
._M_flags
);
969 std::swap(_M_traits
, __rhs
._M_traits
);
970 std::swap(_M_automaton
, __rhs
._M_automaton
);
973 #ifdef _GLIBCXX_DEBUG
975 _M_dot(std::ostream
& __ostr
)
976 { _M_automaton
->_M_dot(__ostr
); }
980 typedef std::shared_ptr
<__detail::_Automaton
<_Ch_type
, _Rx_traits
>>
983 template<typename _BiIter
, typename _Alloc
,
984 typename _CharT
, typename _TraitsT
>
985 friend std::unique_ptr
<
986 __detail::_Executor
<_BiIter
, _Alloc
, _CharT
, _TraitsT
>>
987 __detail::__get_executor(_BiIter
,
989 match_results
<_BiIter
, _Alloc
>&,
990 const basic_regex
<_CharT
, _TraitsT
>&,
991 regex_constants::match_flag_type
);
993 template<typename _Bp
, typename _Ap
, typename _Cp
, typename _Rp
>
995 regex_match(_Bp
, _Bp
,
996 match_results
<_Bp
, _Ap
>&,
997 const basic_regex
<_Cp
, _Rp
>&,
998 regex_constants::match_flag_type
);
1000 template<typename _Bp
, typename _Ap
, typename _Cp
, typename _Rp
>
1002 regex_search(_Bp
, _Bp
,
1003 match_results
<_Bp
, _Ap
>&,
1004 const basic_regex
<_Cp
, _Rp
>&,
1005 regex_constants::match_flag_type
);
1007 template<typename
, typename
, typename
, typename
>
1008 friend class __detail::_Executor
;
1010 template<typename
, typename
, typename
, typename
>
1011 friend class __detail::_DFSExecutor
;
1013 template<typename
, typename
, typename
, typename
>
1014 friend class __detail::_BFSExecutor
;
1017 _Rx_traits _M_traits
;
1018 _AutomatonPtr _M_automaton
;
1021 /** @brief Standard regular expressions. */
1022 typedef basic_regex
<char> regex
;
1024 #ifdef _GLIBCXX_USE_WCHAR_T
1025 /** @brief Standard wide-character regular expressions. */
1026 typedef basic_regex
<wchar_t> wregex
;
1030 // [7.8.6] basic_regex swap
1032 * @brief Swaps the contents of two regular expression objects.
1033 * @param __lhs First regular expression.
1034 * @param __rhs Second regular expression.
1036 template<typename _Ch_type
, typename _Rx_traits
>
1038 swap(basic_regex
<_Ch_type
, _Rx_traits
>& __lhs
,
1039 basic_regex
<_Ch_type
, _Rx_traits
>& __rhs
)
1040 { __lhs
.swap(__rhs
); }
1043 // [7.9] Class template sub_match
1045 * A sequence of characters matched by a particular marked sub-expression.
1047 * An object of this class is essentially a pair of iterators marking a
1048 * matched subexpression within a regular expression pattern match. Such
1049 * objects can be converted to and compared with std::basic_string objects
1050 * of a similar base character type as the pattern matched by the regular
1053 * The iterators that make up the pair are the usual half-open interval
1054 * referencing the actual original pattern matched.
1056 template<typename _BiIter
>
1057 class sub_match
: public std::pair
<_BiIter
, _BiIter
>
1059 typedef iterator_traits
<_BiIter
> __iter_traits
;
1062 typedef typename
__iter_traits::value_type value_type
;
1063 typedef typename
__iter_traits::difference_type difference_type
;
1064 typedef _BiIter iterator
;
1065 typedef std::basic_string
<value_type
> string_type
;
1069 constexpr sub_match() : matched() { }
1072 * Gets the length of the matching sequence.
1076 { return this->matched
? std::distance(this->first
, this->second
) : 0; }
1079 * @brief Gets the matching sequence as a string.
1081 * @returns the matching sequence as a string.
1083 * This is the implicit conversion operator. It is identical to the
1084 * str() member function except that it will want to pop up in
1085 * unexpected places and cause a great deal of confusion and cursing
1088 operator string_type() const
1090 return this->matched
1091 ? string_type(this->first
, this->second
)
1096 * @brief Gets the matching sequence as a string.
1098 * @returns the matching sequence as a string.
1103 return this->matched
1104 ? string_type(this->first
, this->second
)
1109 * @brief Compares this and another matched sequence.
1111 * @param __s Another matched sequence to compare to this one.
1113 * @retval <0 this matched sequence will collate before @p __s.
1114 * @retval =0 this matched sequence is equivalent to @p __s.
1115 * @retval <0 this matched sequence will collate after @p __s.
1118 compare(const sub_match
& __s
) const
1119 { return this->str().compare(__s
.str()); }
1122 * @brief Compares this sub_match to a string.
1124 * @param __s A string to compare to this sub_match.
1126 * @retval <0 this matched sequence will collate before @p __s.
1127 * @retval =0 this matched sequence is equivalent to @p __s.
1128 * @retval <0 this matched sequence will collate after @p __s.
1131 compare(const string_type
& __s
) const
1132 { return this->str().compare(__s
); }
1135 * @brief Compares this sub_match to a C-style string.
1137 * @param __s A C-style string to compare to this sub_match.
1139 * @retval <0 this matched sequence will collate before @p __s.
1140 * @retval =0 this matched sequence is equivalent to @p __s.
1141 * @retval <0 this matched sequence will collate after @p __s.
1144 compare(const value_type
* __s
) const
1145 { return this->str().compare(__s
); }
1149 /** @brief Standard regex submatch over a C-style null-terminated string. */
1150 typedef sub_match
<const char*> csub_match
;
1152 /** @brief Standard regex submatch over a standard string. */
1153 typedef sub_match
<string::const_iterator
> ssub_match
;
1155 #ifdef _GLIBCXX_USE_WCHAR_T
1156 /** @brief Regex submatch over a C-style null-terminated wide string. */
1157 typedef sub_match
<const wchar_t*> wcsub_match
;
1159 /** @brief Regex submatch over a standard wide string. */
1160 typedef sub_match
<wstring::const_iterator
> wssub_match
;
1163 // [7.9.2] sub_match non-member operators
1166 * @brief Tests the equivalence of two regular expression submatches.
1167 * @param __lhs First regular expression submatch.
1168 * @param __rhs Second regular expression submatch.
1169 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1171 template<typename _BiIter
>
1173 operator==(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1174 { return __lhs
.compare(__rhs
) == 0; }
1177 * @brief Tests the inequivalence of two regular expression submatches.
1178 * @param __lhs First regular expression submatch.
1179 * @param __rhs Second regular expression submatch.
1180 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1182 template<typename _BiIter
>
1184 operator!=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1185 { return __lhs
.compare(__rhs
) != 0; }
1188 * @brief Tests the ordering of two regular expression submatches.
1189 * @param __lhs First regular expression submatch.
1190 * @param __rhs Second regular expression submatch.
1191 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1193 template<typename _BiIter
>
1195 operator<(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1196 { return __lhs
.compare(__rhs
) < 0; }
1199 * @brief Tests the ordering of two regular expression submatches.
1200 * @param __lhs First regular expression submatch.
1201 * @param __rhs Second regular expression submatch.
1202 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1204 template<typename _BiIter
>
1206 operator<=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1207 { return __lhs
.compare(__rhs
) <= 0; }
1210 * @brief Tests the ordering of two regular expression submatches.
1211 * @param __lhs First regular expression submatch.
1212 * @param __rhs Second regular expression submatch.
1213 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1215 template<typename _BiIter
>
1217 operator>=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1218 { return __lhs
.compare(__rhs
) >= 0; }
1221 * @brief Tests the ordering of two regular expression submatches.
1222 * @param __lhs First regular expression submatch.
1223 * @param __rhs Second regular expression submatch.
1224 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1226 template<typename _BiIter
>
1228 operator>(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1229 { return __lhs
.compare(__rhs
) > 0; }
1231 // Alias for sub_match'd string.
1232 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1233 using __sub_match_string
= basic_string
<
1234 typename iterator_traits
<_Bi_iter
>::value_type
,
1235 _Ch_traits
, _Ch_alloc
>;
1238 * @brief Tests the equivalence of a string and a regular expression
1240 * @param __lhs A string.
1241 * @param __rhs A regular expression submatch.
1242 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1244 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1246 operator==(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1247 const sub_match
<_Bi_iter
>& __rhs
)
1248 { return __rhs
.compare(__lhs
.c_str()) == 0; }
1251 * @brief Tests the inequivalence of a string and a regular expression
1253 * @param __lhs A string.
1254 * @param __rhs A regular expression submatch.
1255 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1257 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1259 operator!=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1260 const sub_match
<_Bi_iter
>& __rhs
)
1261 { return !(__lhs
== __rhs
); }
1264 * @brief Tests the ordering of a string and a regular expression submatch.
1265 * @param __lhs A string.
1266 * @param __rhs A regular expression submatch.
1267 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1269 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1271 operator<(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1272 const sub_match
<_Bi_iter
>& __rhs
)
1273 { return __rhs
.compare(__lhs
.c_str()) > 0; }
1276 * @brief Tests the ordering of a string and a regular expression submatch.
1277 * @param __lhs A string.
1278 * @param __rhs A regular expression submatch.
1279 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1281 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1283 operator>(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1284 const sub_match
<_Bi_iter
>& __rhs
)
1285 { return __rhs
< __lhs
; }
1288 * @brief Tests the ordering of a string and a regular expression submatch.
1289 * @param __lhs A string.
1290 * @param __rhs A regular expression submatch.
1291 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1293 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1295 operator>=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1296 const sub_match
<_Bi_iter
>& __rhs
)
1297 { return !(__lhs
< __rhs
); }
1300 * @brief Tests the ordering of a string and a regular expression submatch.
1301 * @param __lhs A string.
1302 * @param __rhs A regular expression submatch.
1303 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1305 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1307 operator<=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1308 const sub_match
<_Bi_iter
>& __rhs
)
1309 { return !(__rhs
< __lhs
); }
1312 * @brief Tests the equivalence of a regular expression submatch and a
1314 * @param __lhs A regular expression submatch.
1315 * @param __rhs A string.
1316 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1318 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1320 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1321 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1322 { return __lhs
.compare(__rhs
.c_str()) == 0; }
1325 * @brief Tests the inequivalence of a regular expression submatch and a
1327 * @param __lhs A regular expression submatch.
1328 * @param __rhs A string.
1329 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1331 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1333 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1334 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1335 { return !(__lhs
== __rhs
); }
1338 * @brief Tests the ordering of a regular expression submatch and a string.
1339 * @param __lhs A regular expression submatch.
1340 * @param __rhs A string.
1341 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1343 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1345 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1346 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1347 { return __lhs
.compare(__rhs
.c_str()) < 0; }
1350 * @brief Tests the ordering of a regular expression submatch and a string.
1351 * @param __lhs A regular expression submatch.
1352 * @param __rhs A string.
1353 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1355 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1357 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1358 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1359 { return __rhs
< __lhs
; }
1362 * @brief Tests the ordering of a regular expression submatch and a string.
1363 * @param __lhs A regular expression submatch.
1364 * @param __rhs A string.
1365 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1367 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1369 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1370 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1371 { return !(__lhs
< __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 string.
1377 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1379 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1381 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1382 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1383 { return !(__rhs
< __lhs
); }
1386 * @brief Tests the equivalence of a C string and a regular expression
1388 * @param __lhs A C string.
1389 * @param __rhs A regular expression submatch.
1390 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1392 template<typename _Bi_iter
>
1394 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1395 const sub_match
<_Bi_iter
>& __rhs
)
1396 { return __rhs
.compare(__lhs
) == 0; }
1399 * @brief Tests the inequivalence of an iterator value and a regular
1400 * expression submatch.
1401 * @param __lhs A regular expression submatch.
1402 * @param __rhs A string.
1403 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1405 template<typename _Bi_iter
>
1407 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1408 const sub_match
<_Bi_iter
>& __rhs
)
1409 { return !(__lhs
== __rhs
); }
1412 * @brief Tests the ordering of a string and a regular expression submatch.
1413 * @param __lhs A string.
1414 * @param __rhs A regular expression submatch.
1415 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1417 template<typename _Bi_iter
>
1419 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1420 const sub_match
<_Bi_iter
>& __rhs
)
1421 { return __rhs
.compare(__lhs
) > 0; }
1424 * @brief Tests the ordering of a string and a regular expression submatch.
1425 * @param __lhs A string.
1426 * @param __rhs A regular expression submatch.
1427 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1429 template<typename _Bi_iter
>
1431 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1432 const sub_match
<_Bi_iter
>& __rhs
)
1433 { return __rhs
< __lhs
; }
1436 * @brief Tests the ordering of a string and a regular expression submatch.
1437 * @param __lhs A string.
1438 * @param __rhs A regular expression submatch.
1439 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1441 template<typename _Bi_iter
>
1443 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1444 const sub_match
<_Bi_iter
>& __rhs
)
1445 { return !(__lhs
< __rhs
); }
1448 * @brief Tests the ordering of a string and a regular expression submatch.
1449 * @param __lhs A string.
1450 * @param __rhs A regular expression submatch.
1451 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1453 template<typename _Bi_iter
>
1455 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1456 const sub_match
<_Bi_iter
>& __rhs
)
1457 { return !(__rhs
< __lhs
); }
1460 * @brief Tests the equivalence of a regular expression submatch and a
1462 * @param __lhs A regular expression submatch.
1463 * @param __rhs A pointer to a string?
1464 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1466 template<typename _Bi_iter
>
1468 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1469 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1470 { return __lhs
.compare(__rhs
) == 0; }
1473 * @brief Tests the inequivalence of a regular expression submatch and a
1475 * @param __lhs A regular expression submatch.
1476 * @param __rhs A pointer to a string.
1477 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1479 template<typename _Bi_iter
>
1481 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1482 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1483 { return !(__lhs
== __rhs
); }
1486 * @brief Tests the ordering of a regular expression submatch and a string.
1487 * @param __lhs A regular expression submatch.
1488 * @param __rhs A string.
1489 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1491 template<typename _Bi_iter
>
1493 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1494 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1495 { return __lhs
.compare(__rhs
) < 0; }
1498 * @brief Tests the ordering of a regular expression submatch and a string.
1499 * @param __lhs A regular expression submatch.
1500 * @param __rhs A string.
1501 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1503 template<typename _Bi_iter
>
1505 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1506 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1507 { return __rhs
< __lhs
; }
1510 * @brief Tests the ordering of a regular expression submatch and a string.
1511 * @param __lhs A regular expression submatch.
1512 * @param __rhs A string.
1513 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1515 template<typename _Bi_iter
>
1517 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1518 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1519 { return !(__lhs
< __rhs
); }
1522 * @brief Tests the ordering of a regular expression submatch and a string.
1523 * @param __lhs A regular expression submatch.
1524 * @param __rhs A string.
1525 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1527 template<typename _Bi_iter
>
1529 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1530 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1531 { return !(__rhs
< __lhs
); }
1534 * @brief Tests the equivalence of a string and a regular expression
1536 * @param __lhs A string.
1537 * @param __rhs A regular expression submatch.
1538 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1540 template<typename _Bi_iter
>
1542 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1543 const sub_match
<_Bi_iter
>& __rhs
)
1545 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1546 return __rhs
.compare(string_type(1, __lhs
)) == 0;
1550 * @brief Tests the inequivalence of a string and a regular expression
1552 * @param __lhs A string.
1553 * @param __rhs A regular expression submatch.
1554 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1556 template<typename _Bi_iter
>
1558 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1559 const sub_match
<_Bi_iter
>& __rhs
)
1560 { return !(__lhs
== __rhs
); }
1563 * @brief Tests the ordering of a string and a regular expression submatch.
1564 * @param __lhs A string.
1565 * @param __rhs A regular expression submatch.
1566 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1568 template<typename _Bi_iter
>
1570 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1571 const sub_match
<_Bi_iter
>& __rhs
)
1573 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1574 return __rhs
.compare(string_type(1, __lhs
)) > 0;
1578 * @brief Tests the ordering of a string and a regular expression submatch.
1579 * @param __lhs A string.
1580 * @param __rhs A regular expression submatch.
1581 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1583 template<typename _Bi_iter
>
1585 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1586 const sub_match
<_Bi_iter
>& __rhs
)
1587 { return __rhs
< __lhs
; }
1590 * @brief Tests the ordering of a string and a regular expression submatch.
1591 * @param __lhs A string.
1592 * @param __rhs A regular expression submatch.
1593 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1595 template<typename _Bi_iter
>
1597 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1598 const sub_match
<_Bi_iter
>& __rhs
)
1599 { return !(__lhs
< __rhs
); }
1602 * @brief Tests the ordering of a string and a regular expression submatch.
1603 * @param __lhs A string.
1604 * @param __rhs A regular expression submatch.
1605 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1607 template<typename _Bi_iter
>
1609 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1610 const sub_match
<_Bi_iter
>& __rhs
)
1611 { return !(__rhs
< __lhs
); }
1614 * @brief Tests the equivalence of a regular expression submatch and a
1616 * @param __lhs A regular expression submatch.
1617 * @param __rhs A const string reference.
1618 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1620 template<typename _Bi_iter
>
1622 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1623 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1625 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1626 return __lhs
.compare(string_type(1, __rhs
)) == 0;
1630 * @brief Tests the inequivalence of a regular expression submatch and a
1632 * @param __lhs A regular expression submatch.
1633 * @param __rhs A const string reference.
1634 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1636 template<typename _Bi_iter
>
1638 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1639 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1640 { return !(__lhs
== __rhs
); }
1643 * @brief Tests the ordering of a regular expression submatch and a string.
1644 * @param __lhs A regular expression submatch.
1645 * @param __rhs A const string reference.
1646 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1648 template<typename _Bi_iter
>
1650 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1651 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1653 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1654 return __lhs
.compare(string_type(1, __rhs
)) < 0;
1658 * @brief Tests the ordering of a regular expression submatch and a string.
1659 * @param __lhs A regular expression submatch.
1660 * @param __rhs A const string reference.
1661 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1663 template<typename _Bi_iter
>
1665 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1666 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1667 { return __rhs
< __lhs
; }
1670 * @brief Tests the ordering of a regular expression submatch and a string.
1671 * @param __lhs A regular expression submatch.
1672 * @param __rhs A const string reference.
1673 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1675 template<typename _Bi_iter
>
1677 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1678 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1679 { return !(__lhs
< __rhs
); }
1682 * @brief Tests the ordering of a regular expression submatch and a string.
1683 * @param __lhs A regular expression submatch.
1684 * @param __rhs A const string reference.
1685 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1687 template<typename _Bi_iter
>
1689 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1690 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1691 { return !(__rhs
< __lhs
); }
1694 * @brief Inserts a matched string into an output stream.
1696 * @param __os The output stream.
1697 * @param __m A submatch string.
1699 * @returns the output stream with the submatch string inserted.
1701 template<typename _Ch_type
, typename _Ch_traits
, typename _Bi_iter
>
1703 basic_ostream
<_Ch_type
, _Ch_traits
>&
1704 operator<<(basic_ostream
<_Ch_type
, _Ch_traits
>& __os
,
1705 const sub_match
<_Bi_iter
>& __m
)
1706 { return __os
<< __m
.str(); }
1708 // [7.10] Class template match_results
1711 * Special sub_match object representing an unmatched sub-expression.
1713 template<typename _Bi_iter
>
1714 inline const sub_match
<_Bi_iter
>&
1717 static const sub_match
<_Bi_iter
> __unmatched
= sub_match
<_Bi_iter
>();
1722 * @brief The results of a match or search operation.
1724 * A collection of character sequences representing the result of a regular
1725 * expression match. Storage for the collection is allocated and freed as
1726 * necessary by the member functions of class template match_results.
1728 * This class satisfies the Sequence requirements, with the exception that
1729 * only the operations defined for a const-qualified Sequence are supported.
1731 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1732 * the whole match. In this case the %sub_match member matched is always true.
1733 * The sub_match object stored at index n denotes what matched the marked
1734 * sub-expression n within the matched expression. If the sub-expression n
1735 * participated in a regular expression match then the %sub_match member
1736 * matched evaluates to true, and members first and second denote the range
1737 * of characters [first, second) which formed that match. Otherwise matched
1738 * is false, and members first and second point to the end of the sequence
1739 * that was searched.
1743 template<typename _Bi_iter
,
1744 typename _Alloc
= allocator
<sub_match
<_Bi_iter
> > >
1746 : private std::vector
<sub_match
<_Bi_iter
>, _Alloc
>
1750 * The vector base is empty if this does not represent a successful match.
1751 * Otherwise it contains n+3 elements where n is the number of marked
1754 * [1] 1st marked subexpression
1756 * [n] nth marked subexpression
1760 typedef std::vector
<sub_match
<_Bi_iter
>, _Alloc
> _Base_type
;
1761 typedef std::iterator_traits
<_Bi_iter
> __iter_traits
;
1762 typedef regex_constants::match_flag_type match_flag_type
;
1766 * @name 10.? Public Types
1769 typedef _Alloc allocator_type
;
1770 typedef sub_match
<_Bi_iter
> value_type
;
1771 typedef const value_type
& const_reference
;
1772 typedef const_reference reference
;
1773 typedef typename
_Base_type::const_iterator const_iterator
;
1774 typedef const_iterator iterator
;
1775 typedef typename
__iter_traits::difference_type difference_type
;
1776 typedef typename
__iter_traits::value_type char_type
;
1777 typedef typename allocator_traits
<_Alloc
>::size_type size_type
;
1780 typedef std::basic_string
<char_type
> string_type
;
1785 * @name 28.10.1 Construction, Copying, and Destruction
1790 * @brief Constructs a default %match_results container.
1791 * @post size() returns 0 and str() returns an empty string.
1794 match_results(const _Alloc
& __a
= _Alloc())
1795 : _Base_type(__a
), _M_in_iterator(false)
1799 * @brief Copy constructs a %match_results.
1801 match_results(const match_results
& __rhs
)
1802 : _Base_type(__rhs
), _M_in_iterator(false)
1806 * @brief Move constructs a %match_results.
1808 match_results(match_results
&& __rhs
) noexcept
1809 : _Base_type(std::move(__rhs
)), _M_in_iterator(false)
1813 * @brief Assigns rhs to *this.
1816 operator=(const match_results
& __rhs
)
1818 match_results(__rhs
).swap(*this);
1823 * @brief Move-assigns rhs to *this.
1826 operator=(match_results
&& __rhs
)
1828 match_results(std::move(__rhs
)).swap(*this);
1833 * @brief Destroys a %match_results object.
1842 * @brief Indicates if the %match_results is ready.
1843 * @retval true The object has a fully-established result state.
1844 * @retval false The object is not ready.
1846 bool ready() const { return !_Base_type::empty(); }
1849 * @name 28.10.2 Size
1854 * @brief Gets the number of matches and submatches.
1856 * The number of matches for a given regular expression will be either 0
1857 * if there was no match or mark_count() + 1 if a match was successful.
1858 * Some matches may be empty.
1860 * @returns the number of matches found.
1865 size_type __size
= _Base_type::size();
1866 return (__size
&& _Base_type::operator[](0).matched
) ? __size
- 2 : 0;
1871 { return _Base_type::max_size(); }
1874 * @brief Indicates if the %match_results contains no results.
1875 * @retval true The %match_results object is empty.
1876 * @retval false The %match_results object is not empty.
1880 { return size() == 0; }
1885 * @name 10.3 Element Access
1890 * @brief Gets the length of the indicated submatch.
1891 * @param __sub indicates the submatch.
1892 * @pre ready() == true
1894 * This function returns the length of the indicated submatch, or the
1895 * length of the entire match if @p __sub is zero (the default).
1898 length(size_type __sub
= 0) const
1899 { return (*this)[__sub
].length(); }
1902 * @brief Gets the offset of the beginning of the indicated submatch.
1903 * @param __sub indicates the submatch.
1904 * @pre ready() == true
1906 * This function returns the offset from the beginning of the target
1907 * sequence to the beginning of the submatch, unless the value of @p __sub
1908 * is zero (the default), in which case this function returns the offset
1909 * from the beginning of the target sequence to the beginning of the
1912 * Returns -1 if @p __sub is out of range.
1915 position(size_type __sub
= 0) const
1919 return __sub
< size() ? std::distance(_M_begin
,
1920 (*this)[__sub
].first
) : -1;
1922 return __sub
< size() ? std::distance(this->prefix().first
,
1923 (*this)[__sub
].first
) : -1;
1927 * @brief Gets the match or submatch converted to a string type.
1928 * @param __sub indicates the submatch.
1929 * @pre ready() == true
1931 * This function gets the submatch (or match, if @p __sub is
1932 * zero) extracted from the target range and converted to the
1933 * associated string type.
1936 str(size_type __sub
= 0) const
1937 { return (*this)[__sub
].str(); }
1940 * @brief Gets a %sub_match reference for the match or submatch.
1941 * @param __sub indicates the submatch.
1942 * @pre ready() == true
1944 * This function gets a reference to the indicated submatch, or
1945 * the entire match if @p __sub is zero.
1947 * If @p __sub >= size() then this function returns a %sub_match with a
1948 * special value indicating no submatch.
1951 operator[](size_type __sub
) const
1953 _GLIBCXX_DEBUG_ASSERT( ready() );
1954 return __sub
< size()
1955 ? _Base_type::operator[](__sub
)
1956 : __unmatched_sub
<_Bi_iter
>();
1960 * @brief Gets a %sub_match representing the match prefix.
1961 * @pre ready() == true
1963 * This function gets a reference to a %sub_match object representing the
1964 * part of the target range between the start of the target range and the
1965 * start of the match.
1970 _GLIBCXX_DEBUG_ASSERT( ready() );
1972 ? _Base_type::operator[](_Base_type::size() - 2)
1973 : __unmatched_sub
<_Bi_iter
>();
1977 * @brief Gets a %sub_match representing the match suffix.
1978 * @pre ready() == true
1980 * This function gets a reference to a %sub_match object representing the
1981 * part of the target range between the end of the match and the end of
1987 _GLIBCXX_DEBUG_ASSERT( ready() );
1989 ? _Base_type::operator[](_Base_type::size() - 1)
1990 : __unmatched_sub
<_Bi_iter
>();
1994 * @brief Gets an iterator to the start of the %sub_match collection.
1998 { return _Base_type::begin(); }
2001 * @brief Gets an iterator to the start of the %sub_match collection.
2005 { return _Base_type::cbegin(); }
2008 * @brief Gets an iterator to one-past-the-end of the collection.
2012 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
2015 * @brief Gets an iterator to one-past-the-end of the collection.
2024 * @name 10.4 Formatting
2026 * These functions perform formatted substitution of the matched
2027 * character sequences into their target. The format specifiers and
2028 * escape sequences accepted by these functions are determined by
2029 * their @p flags parameter as documented above.
2034 * @pre ready() == true
2035 * @todo Implement this function.
2037 template<typename _Out_iter
>
2039 format(_Out_iter __out
, const char_type
* __fmt_first
,
2040 const char_type
* __fmt_last
,
2041 match_flag_type __flags
= regex_constants::format_default
) const
2045 * @pre ready() == true
2047 template<typename _Out_iter
, typename _St
, typename _Sa
>
2049 format(_Out_iter __out
, const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2050 match_flag_type __flags
= regex_constants::format_default
) const
2052 return format(__out
, __fmt
.data(), __fmt
.data() + __fmt
.size(),
2057 * @pre ready() == true
2059 template<typename _Out_iter
, typename _St
, typename _Sa
>
2060 basic_string
<char_type
, _St
, _Sa
>
2061 format(const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2062 match_flag_type __flags
= regex_constants::format_default
) const
2064 basic_string
<char_type
, _St
, _Sa
> __result
;
2065 format(std::back_inserter(__result
), __fmt
, __flags
);
2070 * @pre ready() == true
2073 format(const char_type
* __fmt
,
2074 match_flag_type __flags
= regex_constants::format_default
) const
2076 string_type __result
;
2077 format(std::back_inserter(__result
),
2079 __fmt
+ char_traits
<char_type
>::length(__fmt
),
2087 * @name 10.5 Allocator
2092 * @brief Gets a copy of the allocator.
2095 get_allocator() const
2096 { return _Base_type::get_allocator(); }
2106 * @brief Swaps the contents of two match_results.
2109 swap(match_results
& __that
)
2110 { _Base_type::swap(__that
); }
2114 template<typename
, typename
, typename
, typename
>
2115 friend class __detail::_Executor
;
2117 template<typename
, typename
, typename
, typename
>
2118 friend class __detail::_DFSExecutor
;
2120 template<typename
, typename
, typename
, typename
>
2121 friend class __detail::_BFSExecutor
;
2123 template<typename
, typename
, typename
>
2124 friend class regex_iterator
;
2126 template<typename _Bp
, typename _Ap
,
2127 typename _Ch_type
, typename _Rx_traits
>
2129 regex_match(_Bp
, _Bp
, match_results
<_Bp
, _Ap
>&,
2130 const basic_regex
<_Ch_type
,
2132 regex_constants::match_flag_type
);
2134 template<typename _Bp
, typename _Ap
,
2135 typename _Ch_type
, typename _Rx_traits
>
2137 regex_search(_Bp
, _Bp
, match_results
<_Bp
, _Ap
>&,
2138 const basic_regex
<_Ch_type
,
2140 regex_constants::match_flag_type
);
2143 bool _M_in_iterator
;
2146 typedef match_results
<const char*> cmatch
;
2147 typedef match_results
<string::const_iterator
> smatch
;
2148 #ifdef _GLIBCXX_USE_WCHAR_T
2149 typedef match_results
<const wchar_t*> wcmatch
;
2150 typedef match_results
<wstring::const_iterator
> wsmatch
;
2153 // match_results comparisons
2155 * @brief Compares two match_results for equality.
2156 * @returns true if the two objects refer to the same match,
2159 template<typename _Bi_iter
, typename _Alloc
>
2161 operator==(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2162 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2164 if (__m1
.ready() != __m2
.ready())
2166 if (!__m1
.ready()) // both are not ready
2168 if (__m1
.empty() != __m2
.empty())
2170 if (__m1
.empty()) // both are empty
2172 return __m1
.prefix() == __m2
.prefix()
2173 && __m1
.size() == __m2
.size()
2174 && std::equal(__m1
.begin(), __m1
.end(), __m2
.begin())
2175 && __m1
.suffix() == __m2
.suffix();
2179 * @brief Compares two match_results for inequality.
2180 * @returns true if the two objects do not refer to the same match,
2183 template<typename _Bi_iter
, class _Alloc
>
2185 operator!=(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2186 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2187 { return !(__m1
== __m2
); }
2189 // [7.10.6] match_results swap
2191 * @brief Swaps two match results.
2192 * @param __lhs A match result.
2193 * @param __rhs A match result.
2195 * The contents of the two match_results objects are swapped.
2197 template<typename _Bi_iter
, typename _Alloc
>
2199 swap(match_results
<_Bi_iter
, _Alloc
>& __lhs
,
2200 match_results
<_Bi_iter
, _Alloc
>& __rhs
)
2201 { __lhs
.swap(__rhs
); }
2203 // [7.11.2] Function template regex_match
2205 * @name Matching, Searching, and Replacing
2210 * @brief Determines if there is a match between the regular expression @p e
2211 * and all of the character sequence [first, last).
2213 * @param __s Start of the character sequence to match.
2214 * @param __e One-past-the-end of the character sequence to match.
2215 * @param __m The match results.
2216 * @param __re The regular expression.
2217 * @param __flags Controls how the regular expression is matched.
2219 * @retval true A match exists.
2220 * @retval false Otherwise.
2222 * @throws an exception of type regex_error.
2224 template<typename _Bi_iter
, typename _Alloc
,
2225 typename _Ch_type
, typename _Rx_traits
>
2227 regex_match(_Bi_iter __s
,
2229 match_results
<_Bi_iter
, _Alloc
>& __m
,
2230 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2231 regex_constants::match_flag_type __flags
2232 = regex_constants::match_default
)
2234 if (__re
._M_automaton
== nullptr)
2237 auto __size
= __re
._M_automaton
->_M_sub_count();
2240 for (decltype(__size
) __i
= 0; __i
< __size
; ++__i
)
2241 __m
.at(__i
).matched
= false;
2243 if (__detail::__get_executor(__s
, __e
, __m
, __re
, __flags
)->_M_match())
2245 for (auto __it
: __m
)
2247 __it
.first
= __it
.second
= __e
;
2248 __m
.at(__m
.size()).matched
= false;
2249 __m
.at(__m
.size()).first
= __s
;
2250 __m
.at(__m
.size()).second
= __s
;
2251 __m
.at(__m
.size()+1).matched
= false;
2252 __m
.at(__m
.size()+1).first
= __e
;
2253 __m
.at(__m
.size()+1).second
= __e
;
2260 * @brief Indicates if there is a match between the regular expression @p e
2261 * and all of the character sequence [first, last).
2263 * @param __first Beginning of the character sequence to match.
2264 * @param __last One-past-the-end of the character sequence to match.
2265 * @param __re The regular expression.
2266 * @param __flags Controls how the regular expression is matched.
2268 * @retval true A match exists.
2269 * @retval false Otherwise.
2271 * @throws an exception of type regex_error.
2273 template<typename _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2275 regex_match(_Bi_iter __first
, _Bi_iter __last
,
2276 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2277 regex_constants::match_flag_type __flags
2278 = regex_constants::match_default
)
2280 match_results
<_Bi_iter
> __what
;
2281 return regex_match(__first
, __last
, __what
, __re
, __flags
);
2285 * @brief Determines if there is a match between the regular expression @p e
2286 * and a C-style null-terminated string.
2288 * @param __s The C-style null-terminated string to match.
2289 * @param __m The match results.
2290 * @param __re The regular expression.
2291 * @param __f 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_type
, typename _Alloc
, typename _Rx_traits
>
2300 regex_match(const _Ch_type
* __s
,
2301 match_results
<const _Ch_type
*, _Alloc
>& __m
,
2302 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2303 regex_constants::match_flag_type __f
2304 = regex_constants::match_default
)
2305 { return regex_match(__s
, __s
+ _Rx_traits::length(__s
), __m
, __re
, __f
); }
2308 * @brief Determines if there is a match between the regular expression @p e
2311 * @param __s The string to match.
2312 * @param __m The match results.
2313 * @param __re The regular expression.
2314 * @param __flags Controls how the regular expression is matched.
2316 * @retval true A match exists.
2317 * @retval false Otherwise.
2319 * @throws an exception of type regex_error.
2321 template<typename _Ch_traits
, typename _Ch_alloc
,
2322 typename _Alloc
, typename _Ch_type
, typename _Rx_traits
>
2324 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2325 match_results
<typename basic_string
<_Ch_type
,
2326 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2327 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2328 regex_constants::match_flag_type __flags
2329 = regex_constants::match_default
)
2330 { return regex_match(__s
.begin(), __s
.end(), __m
, __re
, __flags
); }
2333 * @brief Indicates if there is a match between the regular expression @p e
2334 * and a C-style null-terminated string.
2336 * @param __s The C-style null-terminated string to match.
2337 * @param __re The regular expression.
2338 * @param __f Controls how the regular expression is matched.
2340 * @retval true A match exists.
2341 * @retval false Otherwise.
2343 * @throws an exception of type regex_error.
2345 template<typename _Ch_type
, class _Rx_traits
>
2347 regex_match(const _Ch_type
* __s
,
2348 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2349 regex_constants::match_flag_type __f
2350 = regex_constants::match_default
)
2351 { return regex_match(__s
, __s
+ _Rx_traits::length(__s
), __re
, __f
); }
2354 * @brief Indicates if there is a match between the regular expression @p e
2357 * @param __s [IN] The string to match.
2358 * @param __re [IN] The regular expression.
2359 * @param __flags [IN] Controls how the regular expression is matched.
2361 * @retval true A match exists.
2362 * @retval false Otherwise.
2364 * @throws an exception of type regex_error.
2366 template<typename _Ch_traits
, typename _Str_allocator
,
2367 typename _Ch_type
, typename _Rx_traits
>
2369 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Str_allocator
>& __s
,
2370 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2371 regex_constants::match_flag_type __flags
2372 = regex_constants::match_default
)
2373 { return regex_match(__s
.begin(), __s
.end(), __re
, __flags
); }
2375 // [7.11.3] Function template regex_search
2377 * Searches for a regular expression within a range.
2378 * @param __first [IN] The start of the string to search.
2379 * @param __last [IN] One-past-the-end of the string to search.
2380 * @param __m [OUT] The match results.
2381 * @param __re [IN] The regular expression to search for.
2382 * @param __flags [IN] Search policy flags.
2383 * @retval true A match was found within the string.
2384 * @retval false No match was found within the string, the content of %m is
2387 * @throws an exception of type regex_error.
2389 template<typename _Bi_iter
, typename _Alloc
,
2390 typename _Ch_type
, typename _Rx_traits
>
2392 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2393 match_results
<_Bi_iter
, _Alloc
>& __m
,
2394 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2395 regex_constants::match_flag_type __flags
2396 = regex_constants::match_default
)
2398 if (__re
._M_automaton
== nullptr)
2401 auto __size
= __re
._M_automaton
->_M_sub_count();
2404 for (decltype(__size
) __i
= 0; __i
< __size
; ++__i
)
2405 __m
.at(__i
).matched
= false;
2407 if (__detail::__get_executor(__first
, __last
, __m
, __re
, __flags
)
2410 for (auto __it
: __m
)
2412 __it
.first
= __it
.second
= __last
;
2413 __m
.at(__m
.size()).first
= __first
;
2414 __m
.at(__m
.size()).second
= __m
[0].first
;
2415 __m
.at(__m
.size()+1).first
= __m
[0].second
;
2416 __m
.at(__m
.size()+1).second
= __last
;
2417 __m
.at(__m
.size()).matched
=
2418 (__m
.prefix().first
!= __m
.prefix().second
);
2419 __m
.at(__m
.size()+1).matched
=
2420 (__m
.suffix().first
!= __m
.suffix().second
);
2427 * Searches for a regular expression within a range.
2428 * @param __first [IN] The start of the string to search.
2429 * @param __last [IN] One-past-the-end of the string to search.
2430 * @param __re [IN] The regular expression to search for.
2431 * @param __flags [IN] Search policy flags.
2432 * @retval true A match was found within the string.
2433 * @retval false No match was found within the string.
2435 * @throws an exception of type regex_error.
2437 template<typename _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2439 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2440 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2441 regex_constants::match_flag_type __flags
2442 = regex_constants::match_default
)
2444 match_results
<_Bi_iter
> __what
;
2445 return regex_search(__first
, __last
, __what
, __re
, __flags
);
2449 * @brief Searches for a regular expression within a C-string.
2450 * @param __s [IN] A C-string to search for the regex.
2451 * @param __m [OUT] The set of regex matches.
2452 * @param __e [IN] The regex to search for in @p s.
2453 * @param __f [IN] The search flags.
2454 * @retval true A match was found within the string.
2455 * @retval false No match was found within the string, the content of %m is
2458 * @throws an exception of type regex_error.
2460 template<typename _Ch_type
, class _Alloc
, class _Rx_traits
>
2462 regex_search(const _Ch_type
* __s
,
2463 match_results
<const _Ch_type
*, _Alloc
>& __m
,
2464 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2465 regex_constants::match_flag_type __f
2466 = regex_constants::match_default
)
2467 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __m
, __e
, __f
); }
2470 * @brief Searches for a regular expression within a C-string.
2471 * @param __s [IN] The C-string to search.
2472 * @param __e [IN] The regular expression to search for.
2473 * @param __f [IN] Search policy flags.
2474 * @retval true A match was found within the string.
2475 * @retval false No match was found within the string.
2477 * @throws an exception of type regex_error.
2479 template<typename _Ch_type
, typename _Rx_traits
>
2481 regex_search(const _Ch_type
* __s
,
2482 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2483 regex_constants::match_flag_type __f
2484 = regex_constants::match_default
)
2485 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __e
, __f
); }
2488 * @brief Searches for a regular expression within a string.
2489 * @param __s [IN] The string to search.
2490 * @param __e [IN] The regular expression to search for.
2491 * @param __flags [IN] Search policy flags.
2492 * @retval true A match was found within the string.
2493 * @retval false No match was found within the string.
2495 * @throws an exception of type regex_error.
2497 template<typename _Ch_traits
, typename _String_allocator
,
2498 typename _Ch_type
, typename _Rx_traits
>
2500 regex_search(const basic_string
<_Ch_type
, _Ch_traits
,
2501 _String_allocator
>& __s
,
2502 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2503 regex_constants::match_flag_type __flags
2504 = regex_constants::match_default
)
2505 { return regex_search(__s
.begin(), __s
.end(), __e
, __flags
); }
2508 * @brief Searches for a regular expression within a string.
2509 * @param __s [IN] A C++ string to search for the regex.
2510 * @param __m [OUT] The set of regex matches.
2511 * @param __e [IN] The regex to search for in @p s.
2512 * @param __f [IN] The search flags.
2513 * @retval true A match was found within the string.
2514 * @retval false No match was found within the string, the content of %m is
2517 * @throws an exception of type regex_error.
2519 template<typename _Ch_traits
, typename _Ch_alloc
,
2520 typename _Alloc
, typename _Ch_type
,
2521 typename _Rx_traits
>
2523 regex_search(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2524 match_results
<typename basic_string
<_Ch_type
,
2525 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2526 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2527 regex_constants::match_flag_type __f
2528 = regex_constants::match_default
)
2529 { return regex_search(__s
.begin(), __s
.end(), __m
, __e
, __f
); }
2531 // std [28.11.4] Function template regex_replace
2542 * @throws an exception of type regex_error.
2544 * @todo Implement this function.
2546 template<typename _Out_iter
, typename _Bi_iter
,
2547 typename _Rx_traits
, typename _Ch_type
>
2549 regex_replace(_Out_iter __out
, _Bi_iter __first
, _Bi_iter __last
,
2550 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2551 const basic_string
<_Ch_type
>& __fmt
,
2552 regex_constants::match_flag_type __flags
2553 = regex_constants::match_default
)
2563 * @returns a copy of string @p s with replacements.
2565 * @throws an exception of type regex_error.
2567 template<typename _Rx_traits
, typename _Ch_type
>
2568 inline basic_string
<_Ch_type
>
2569 regex_replace(const basic_string
<_Ch_type
>& __s
,
2570 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2571 const basic_string
<_Ch_type
>& __fmt
,
2572 regex_constants::match_flag_type __flags
2573 = regex_constants::match_default
)
2575 basic_string
<_Ch_type
> __result
;
2576 regex_replace(std::back_inserter(__result
),
2577 __s
.begin(), __s
.end(), __e
, __fmt
, __flags
);
2583 // std [28.12] Class template regex_iterator
2585 * An iterator adaptor that will provide repeated calls of regex_search over
2586 * a range until no more matches remain.
2588 template<typename _Bi_iter
,
2589 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2590 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2591 class regex_iterator
2594 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2595 typedef match_results
<_Bi_iter
> value_type
;
2596 typedef std::ptrdiff_t difference_type
;
2597 typedef const value_type
* pointer
;
2598 typedef const value_type
& reference
;
2599 typedef std::forward_iterator_tag iterator_category
;
2602 * @brief Provides a singular iterator, useful for indicating
2603 * one-past-the-end of a range.
2610 * Constructs a %regex_iterator...
2611 * @param __a [IN] The start of a text range to search.
2612 * @param __b [IN] One-past-the-end of the text range to search.
2613 * @param __re [IN] The regular expression to match.
2614 * @param __m [IN] Policy flags for match rules.
2616 regex_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2617 regex_constants::match_flag_type __m
2618 = regex_constants::match_default
)
2619 : _M_begin(__a
), _M_end(__b
), _M_pregex(&__re
), _M_flags(__m
), _M_match()
2621 if (!regex_search(_M_begin
, _M_end
, _M_match
, *_M_pregex
, _M_flags
))
2622 *this = regex_iterator();
2626 * Copy constructs a %regex_iterator.
2628 regex_iterator(const regex_iterator
& __rhs
) = default;
2631 * @brief Assigns one %regex_iterator to another.
2634 operator=(const regex_iterator
& __rhs
) = default;
2637 * @brief Tests the equivalence of two regex iterators.
2640 operator==(const regex_iterator
& __rhs
) const;
2643 * @brief Tests the inequivalence of two regex iterators.
2646 operator!=(const regex_iterator
& __rhs
) const
2647 { return !(*this == __rhs
); }
2650 * @brief Dereferences a %regex_iterator.
2654 { return _M_match
; }
2657 * @brief Selects a %regex_iterator member.
2661 { return &_M_match
; }
2664 * @brief Increments a %regex_iterator.
2670 * @brief Postincrements a %regex_iterator.
2683 const regex_type
* _M_pregex
;
2684 regex_constants::match_flag_type _M_flags
;
2685 match_results
<_Bi_iter
> _M_match
;
2688 template<typename _Bi_iter
,
2690 typename _Rx_traits
>
2692 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2693 operator==(const regex_iterator
& __rhs
) const
2695 return (_M_match
.empty() && __rhs
._M_match
.empty())
2696 || (_M_begin
== __rhs
._M_begin
2697 && _M_end
== __rhs
._M_end
2698 && _M_pregex
== __rhs
._M_pregex
2699 && _M_flags
== __rhs
._M_flags
2700 && _M_match
[0] == __rhs
._M_match
[0]);
2703 template<typename _Bi_iter
,
2705 typename _Rx_traits
>
2706 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2707 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2710 // In all cases in which the call to regex_search returns true,
2711 // match.prefix().first shall be equal to the previous value of
2712 // match[0].second, and for each index i in the half-open range
2713 // [0, match.size()) for which match[i].matched is true,
2714 // match[i].position() shall return distance(begin, match[i].first).
2716 if (_M_match
[0].matched
)
2718 auto __start
= _M_match
[0].second
;
2719 if (_M_match
[0].first
== _M_match
[0].second
)
2720 if (__start
== _M_end
)
2722 _M_match
= value_type();
2727 if (regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
2728 | regex_constants::match_not_null
2729 | regex_constants::match_continuous
))
2731 _M_match
._M_in_iterator
= true;
2732 _M_match
._M_begin
= _M_begin
;
2738 _M_flags
|= regex_constants::match_prev_avail
;
2739 if (regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
))
2741 _M_match
._M_in_iterator
= true;
2742 _M_match
._M_begin
= _M_begin
;
2745 _M_match
= value_type();
2750 typedef regex_iterator
<const char*> cregex_iterator
;
2751 typedef regex_iterator
<string::const_iterator
> sregex_iterator
;
2752 #ifdef _GLIBCXX_USE_WCHAR_T
2753 typedef regex_iterator
<const wchar_t*> wcregex_iterator
;
2754 typedef regex_iterator
<wstring::const_iterator
> wsregex_iterator
;
2757 // [7.12.2] Class template regex_token_iterator
2759 * Iterates over submatches in a range (or @a splits a text string).
2761 * The purpose of this iterator is to enumerate all, or all specified,
2762 * matches of a regular expression within a text range. The dereferenced
2763 * value of an iterator of this class is a std::sub_match object.
2765 template<typename _Bi_iter
,
2766 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2767 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2768 class regex_token_iterator
2771 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2772 typedef sub_match
<_Bi_iter
> value_type
;
2773 typedef std::ptrdiff_t difference_type
;
2774 typedef const value_type
* pointer
;
2775 typedef const value_type
& reference
;
2776 typedef std::forward_iterator_tag iterator_category
;
2780 * @brief Default constructs a %regex_token_iterator.
2782 * A default-constructed %regex_token_iterator is a singular iterator
2783 * that will compare equal to the one-past-the-end value for any
2784 * iterator of the same type.
2786 regex_token_iterator()
2787 : _M_position(), _M_result(nullptr), _M_suffix(), _M_n(0), _M_subs()
2791 * Constructs a %regex_token_iterator...
2792 * @param __a [IN] The start of the text to search.
2793 * @param __b [IN] One-past-the-end of the text to search.
2794 * @param __re [IN] The regular expression to search for.
2795 * @param __submatch [IN] Which submatch to return. There are some
2796 * special values for this parameter:
2797 * - -1 each enumerated subexpression does NOT
2798 * match the regular expression (aka field
2800 * - 0 the entire string matching the
2801 * subexpression is returned for each match
2803 * - >0 enumerates only the indicated
2804 * subexpression from a match within the text.
2805 * @param __m [IN] Policy flags for match rules.
2807 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2809 regex_constants::match_flag_type __m
2810 = regex_constants::match_default
)
2811 : _M_position(__a
, __b
, __re
, __m
), _M_subs(1, __submatch
), _M_n(0)
2812 { _M_init(__a
, __b
); }
2815 * Constructs a %regex_token_iterator...
2816 * @param __a [IN] The start of the text to search.
2817 * @param __b [IN] One-past-the-end of the text to search.
2818 * @param __re [IN] The regular expression to search for.
2819 * @param __submatches [IN] A list of subexpressions to return for each
2820 * regular expression match within the text.
2821 * @param __m [IN] Policy flags for match rules.
2823 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2824 const regex_type
& __re
,
2825 const std::vector
<int>& __submatches
,
2826 regex_constants::match_flag_type __m
2827 = regex_constants::match_default
)
2828 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2829 { _M_init(__a
, __b
); }
2832 * Constructs a %regex_token_iterator...
2833 * @param __a [IN] The start of the text to search.
2834 * @param __b [IN] One-past-the-end of the text to search.
2835 * @param __re [IN] The regular expression to search for.
2836 * @param __submatches [IN] A list of subexpressions to return for each
2837 * regular expression match within the text.
2838 * @param __m [IN] Policy flags for match rules.
2840 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2841 const regex_type
& __re
,
2842 initializer_list
<int> __submatches
,
2843 regex_constants::match_flag_type __m
2844 = regex_constants::match_default
)
2845 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2846 { _M_init(__a
, __b
); }
2849 * Constructs a %regex_token_iterator...
2850 * @param __a [IN] The start of the text to search.
2851 * @param __b [IN] One-past-the-end of the text to search.
2852 * @param __re [IN] The regular expression to search for.
2853 * @param __submatches [IN] A list of subexpressions to return for each
2854 * regular expression match within the text.
2855 * @param __m [IN] Policy flags for match rules.
2857 template<std::size_t _Nm
>
2858 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2859 const regex_type
& __re
,
2860 const int (&__submatches
)[_Nm
],
2861 regex_constants::match_flag_type __m
2862 = regex_constants::match_default
)
2863 : _M_position(__a
, __b
, __re
, __m
),
2864 _M_subs(__submatches
, *(&__submatches
+1)), _M_n(0)
2865 { _M_init(__a
, __b
); }
2868 * @brief Copy constructs a %regex_token_iterator.
2869 * @param __rhs [IN] A %regex_token_iterator to copy.
2871 regex_token_iterator(const regex_token_iterator
& __rhs
)
2872 : _M_position(__rhs
.position
), _M_subs(__rhs
.subs
), _M_n(__rhs
.N
),
2873 _M_result(__rhs
.result
), _M_suffix(__rhs
.suffix
),
2874 _M_has_m1(__rhs
._M_has_m1
)
2876 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2877 _M_result
= &_M_suffix
;
2881 * @brief Assigns a %regex_token_iterator to another.
2882 * @param __rhs [IN] A %regex_token_iterator to copy.
2884 regex_token_iterator
&
2885 operator=(const regex_token_iterator
& __rhs
);
2888 * @brief Compares a %regex_token_iterator to another for equality.
2891 operator==(const regex_token_iterator
& __rhs
) const;
2894 * @brief Compares a %regex_token_iterator to another for inequality.
2897 operator!=(const regex_token_iterator
& __rhs
) const
2898 { return !(*this == __rhs
); }
2901 * @brief Dereferences a %regex_token_iterator.
2905 { return *_M_result
; }
2908 * @brief Selects a %regex_token_iterator member.
2912 { return _M_result
; }
2915 * @brief Increments a %regex_token_iterator.
2917 regex_token_iterator
&
2921 * @brief Postincrements a %regex_token_iterator.
2923 regex_token_iterator
2932 typedef regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
> _Position
;
2935 _M_init(_Bi_iter __a
, _Bi_iter __b
);
2938 _M_current_match() const
2940 if (_M_subs
[_M_n
] == -1)
2941 return (*_M_position
).prefix();
2943 return (*_M_position
)[_M_subs
[_M_n
]];
2948 { return _M_result
== nullptr; }
2950 _Position _M_position
;
2951 const value_type
* _M_result
;
2952 value_type _M_suffix
;
2954 std::vector
<int> _M_subs
;
2956 // Show whether _M_subs contains -1
2960 template<typename _Bi_iter
,
2962 typename _Rx_traits
>
2963 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2964 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2965 operator=(const regex_token_iterator
& __rhs
)
2967 _M_position
= __rhs
._M_position
;
2968 _M_subs
= __rhs
._M_subs
;
2970 _M_result
= __rhs
._M_result
;
2971 _M_suffix
= __rhs
._M_suffix
;
2972 _M_has_m1
= __rhs
._M_has_m1
;
2973 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2974 _M_result
= &_M_suffix
;
2977 template<typename _Bi_iter
,
2979 typename _Rx_traits
>
2981 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2982 operator==(const regex_token_iterator
& __rhs
) const
2984 if (_M_end_of_seq() && __rhs
._M_end_of_seq())
2986 if (_M_suffix
.matched
&& __rhs
._M_suffix
.matched
2987 && _M_suffix
== __rhs
._M_suffix
)
2989 if (_M_end_of_seq() || _M_suffix
.matched
2990 || __rhs
._M_end_of_seq() || __rhs
._M_suffix
.matched
)
2992 return _M_position
== __rhs
._M_position
2993 && _M_n
== __rhs
._M_n
2994 && _M_subs
== __rhs
._M_subs
;
2997 template<typename _Bi_iter
,
2999 typename _Rx_traits
>
3000 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
3001 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
3004 _Position __prev
= _M_position
;
3005 if (_M_suffix
.matched
)
3006 *this = regex_token_iterator();
3007 else if (_M_n
+ 1 < _M_subs
.size())
3010 _M_result
= &_M_current_match();
3016 if (_M_position
!= _Position())
3017 _M_result
= &_M_current_match();
3018 else if (_M_has_m1
&& __prev
->suffix().length() != 0)
3020 _M_suffix
.matched
= true;
3021 _M_suffix
.first
= __prev
->suffix().first
;
3022 _M_suffix
.second
= __prev
->suffix().second
;
3023 _M_result
= &_M_suffix
;
3026 *this = regex_token_iterator();
3031 template<typename _Bi_iter
,
3033 typename _Rx_traits
>
3035 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
3036 _M_init(_Bi_iter __a
, _Bi_iter __b
)
3039 for (auto __it
: _M_subs
)
3045 if (_M_position
!= _Position())
3046 _M_result
= &_M_current_match();
3049 _M_suffix
.matched
= true;
3050 _M_suffix
.first
= __a
;
3051 _M_suffix
.second
= __b
;
3052 _M_result
= &_M_suffix
;
3055 _M_result
= nullptr;
3058 /** @brief Token iterator for C-style NULL-terminated strings. */
3059 typedef regex_token_iterator
<const char*> cregex_token_iterator
;
3061 /** @brief Token iterator for standard strings. */
3062 typedef regex_token_iterator
<string::const_iterator
> sregex_token_iterator
;
3064 #ifdef _GLIBCXX_USE_WCHAR_T
3065 /** @brief Token iterator for C-style NULL-terminated wide strings. */
3066 typedef regex_token_iterator
<const wchar_t*> wcregex_token_iterator
;
3068 /** @brief Token iterator for standard wide-character strings. */
3069 typedef regex_token_iterator
<wstring::const_iterator
> wsregex_token_iterator
;
3073 _GLIBCXX_END_NAMESPACE_VERSION