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
225 typedef std::ctype
<char_type
> __ctype_type
;
226 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
227 std::vector
<char_type
> __v(__first
, __last
);
228 // FIXME : this is not entirely correct
229 __fctyp
.tolower(&*__v
.begin(), &*__v
.end());
230 return this->transform(&*__v
.begin(), &*__v
.end());
232 __catch (std::bad_cast
)
235 return string_type();
239 * @brief Gets a collation element by name.
241 * @param __first beginning of the collation element name.
242 * @param __last one-past-the-end of the collation element name.
244 * @returns a sequence of one or more characters that represents the
245 * collating element consisting of the character sequence designated by
246 * the iterator range [__first, __last). Returns an empty string if the
247 * character sequence is not a valid collating element.
249 template<typename _Fwd_iter
>
251 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const;
254 * @brief Maps one or more characters to a named character
257 * @param __first beginning of the character sequence.
258 * @param __last one-past-the-end of the character sequence.
259 * @param __icase ignores the case of the classification name.
261 * @returns an unspecified value that represents the character
262 * classification named by the character sequence designated by
263 * the iterator range [__first, __last). If @p icase is true,
264 * the returned mask identifies the classification regardless of
265 * the case of the characters to be matched (for example,
266 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
267 * case-dependent classification is returned. The value
268 * returned shall be independent of the case of the characters
269 * in the character sequence. If the name is not recognized then
270 * returns a value that compares equal to 0.
272 * At least the following names (or their wide-character equivalent) are
290 template<typename _Fwd_iter
>
292 lookup_classname(_Fwd_iter __first
, _Fwd_iter __last
,
293 bool __icase
= false) const;
296 * @brief Determines if @p c is a member of an identified class.
298 * @param __c a character.
299 * @param __f a class type (as returned from lookup_classname).
301 * @returns true if the character @p __c is a member of the classification
302 * represented by @p __f, false otherwise.
304 * @throws std::bad_cast if the current locale does not have a ctype
308 isctype(_Ch_type __c
, char_class_type __f
) const;
311 * @brief Converts a digit to an int.
313 * @param __ch a character representing a digit.
314 * @param __radix the radix if the numeric conversion (limited to 8, 10,
317 * @returns the value represented by the digit __ch in base radix if the
318 * character __ch is a valid digit in base radix; otherwise returns -1.
321 value(_Ch_type __ch
, int __radix
) const;
324 * @brief Imbues the regex_traits object with a copy of a new locale.
326 * @param __loc A locale.
328 * @returns a copy of the previous locale in use by the regex_traits
331 * @note Calling imbue with a different locale than the one currently in
332 * use invalidates all cached data held by *this.
335 imbue(locale_type __loc
)
337 std::swap(_M_locale
, __loc
);
342 * @brief Gets a copy of the current locale in use by the regex_traits
347 { return _M_locale
; }
350 locale_type _M_locale
;
353 template<typename _Ch_type
>
354 template<typename _Fwd_iter
>
355 typename regex_traits
<_Ch_type
>::string_type
356 regex_traits
<_Ch_type
>::
357 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const
359 typedef std::ctype
<char_type
> __ctype_type
;
360 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
362 static const char* __collatenames
[] =
455 "left-square-bracket",
457 "right-square-bracket",
487 "left-curly-bracket",
489 "right-curly-bracket",
496 static const char* __digraphs
[] =
522 std::string
__s(__last
- __first
, '?');
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
)
584 & (ctype_base::lower
| ctype_base::upper
)) != 0))
585 return ctype_base::alpha
;
592 template<typename _Ch_type
>
594 regex_traits
<_Ch_type
>::
595 isctype(_Ch_type __c
, char_class_type __f
) const
597 typedef std::ctype
<char_type
> __ctype_type
;
598 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
600 return __fctyp
.is(__f
._M_base
, __c
)
602 || ((__f
._M_extended
& _RegexMask::_S_under
)
603 && __c
== __fctyp
.widen('_'))
605 || ((__f
._M_extended
& _RegexMask::_S_blank
)
606 && (__c
== __fctyp
.widen(' ')
607 || __c
== __fctyp
.widen('\t')));
610 template<typename _Ch_type
>
612 regex_traits
<_Ch_type
>::
613 value(_Ch_type __ch
, int __radix
) const
615 std::basic_istringstream
<char_type
> __is(string_type(1, __ch
));
619 else if (__radix
== 16)
622 return __is
.fail() ? -1 : __v
;
625 // [7.8] Class basic_regex
627 * Objects of specializations of this class represent regular expressions
628 * constructed from sequences of character type @p _Ch_type.
630 * Storage for the regular expression is allocated and deallocated as
631 * necessary by the member functions of this class.
633 template<typename _Ch_type
, typename _Rx_traits
= regex_traits
<_Ch_type
> >
638 typedef _Ch_type value_type
;
639 typedef _Rx_traits traits_type
;
640 typedef typename
traits_type::string_type string_type
;
641 typedef regex_constants::syntax_option_type flag_type
;
642 typedef typename
traits_type::locale_type locale_type
;
649 static constexpr flag_type icase
= regex_constants::icase
;
650 static constexpr flag_type nosubs
= regex_constants::nosubs
;
651 static constexpr flag_type optimize
= regex_constants::optimize
;
652 static constexpr flag_type collate
= regex_constants::collate
;
653 static constexpr flag_type ECMAScript
= regex_constants::ECMAScript
;
654 static constexpr flag_type basic
= regex_constants::basic
;
655 static constexpr flag_type extended
= regex_constants::extended
;
656 static constexpr flag_type awk
= regex_constants::awk
;
657 static constexpr flag_type grep
= regex_constants::grep
;
658 static constexpr flag_type egrep
= regex_constants::egrep
;
661 // [7.8.2] construct/copy/destroy
663 * Constructs a basic regular expression that does not match any
664 * character sequence.
667 : _M_flags(ECMAScript
), _M_automaton(nullptr)
671 * @brief Constructs a basic regular expression from the
672 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
673 * interpreted according to the flags in @p __f.
675 * @param __p A pointer to the start of a C-style null-terminated string
676 * containing a regular expression.
677 * @param __f Flags indicating the syntax rules and options.
679 * @throws regex_error if @p __p is not a valid regular expression.
682 basic_regex(const _Ch_type
* __p
, flag_type __f
= ECMAScript
)
683 : basic_regex(__p
, __p
+ _Rx_traits::length(__p
), __f
)
687 * @brief Constructs a basic regular expression from the sequence
688 * [p, p + len) interpreted according to the flags in @p f.
690 * @param __p A pointer to the start of a string containing a regular
692 * @param __len The length of the string containing the regular
694 * @param __f Flags indicating the syntax rules and options.
696 * @throws regex_error if @p __p is not a valid regular expression.
698 basic_regex(const _Ch_type
* __p
,
699 std::size_t __len
, flag_type __f
= ECMAScript
)
700 : basic_regex(__p
, __p
+ __len
, __f
)
704 * @brief Copy-constructs a basic regular expression.
706 * @param __rhs A @p regex object.
708 basic_regex(const basic_regex
& __rhs
) = default;
711 * @brief Move-constructs a basic regular expression.
713 * @param __rhs A @p regex object.
715 basic_regex(const basic_regex
&& __rhs
) noexcept
716 : _M_flags(__rhs
._M_flags
), _M_traits(__rhs
._M_traits
),
717 _M_automaton(std::move(__rhs
._M_automaton
))
721 * @brief Constructs a basic regular expression from the string
722 * @p s interpreted according to the flags in @p f.
724 * @param __s A string containing a regular expression.
725 * @param __f Flags indicating the syntax rules and options.
727 * @throws regex_error if @p __s is not a valid regular expression.
729 template<typename _Ch_traits
, typename _Ch_alloc
>
731 basic_regex(const std::basic_string
<_Ch_type
, _Ch_traits
,
733 flag_type __f
= ECMAScript
)
734 : basic_regex(__s
.begin(), __s
.end(), __f
)
738 * @brief Constructs a basic regular expression from the range
739 * [first, last) interpreted according to the flags in @p f.
741 * @param __first The start of a range containing a valid regular
743 * @param __last The end of a range containing a valid regular
745 * @param __f The format flags of the regular expression.
747 * @throws regex_error if @p [__first, __last) is not a valid regular
750 template<typename _InputIterator
>
751 basic_regex(_InputIterator __first
, _InputIterator __last
,
752 flag_type __f
= ECMAScript
)
754 _M_automaton(__detail::_Compiler
<_InputIterator
, _Ch_type
, _Rx_traits
>
755 (__first
, __last
, _M_traits
, _M_flags
)._M_get_nfa())
759 * @brief Constructs a basic regular expression from an initializer list.
761 * @param __l The initializer list.
762 * @param __f The format flags of the regular expression.
764 * @throws regex_error if @p __l is not a valid regular expression.
766 basic_regex(initializer_list
<_Ch_type
> __l
,
767 flag_type __f
= ECMAScript
)
768 : basic_regex(__l
.begin(), __l
.end(), __f
)
772 * @brief Destroys a basic regular expression.
778 * @brief Assigns one regular expression to another.
781 operator=(const basic_regex
& __rhs
)
782 { return this->assign(__rhs
); }
785 * @brief Move-assigns one regular expression to another.
788 operator=(basic_regex
&& __rhs
) noexcept
789 { return this->assign(std::move(__rhs
)); }
792 * @brief Replaces a regular expression with a new one constructed from
793 * a C-style null-terminated string.
795 * @param __p A pointer to the start of a null-terminated C-style string
796 * containing a regular expression.
799 operator=(const _Ch_type
* __p
)
800 { return this->assign(__p
, flags()); }
803 * @brief Replaces a regular expression with a new one constructed from
806 * @param __s A pointer to a string containing a regular expression.
808 template<typename _Ch_typeraits
, typename _Alloc
>
810 operator=(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
)
811 { return this->assign(__s
, flags()); }
815 * @brief the real assignment operator.
817 * @param __rhs Another regular expression object.
820 assign(const basic_regex
& __rhs
)
822 basic_regex
__tmp(__rhs
);
828 * @brief The move-assignment operator.
830 * @param __rhs Another regular expression object.
833 assign(basic_regex
&& __rhs
) noexcept
835 basic_regex
__tmp(std::move(__rhs
));
841 * @brief Assigns a new regular expression to a regex object from a
842 * C-style null-terminated string containing a regular expression
845 * @param __p A pointer to a C-style null-terminated string containing
846 * a regular expression pattern.
847 * @param __flags Syntax option flags.
849 * @throws regex_error if __p does not contain a valid regular
850 * expression pattern interpreted according to @p __flags. If
851 * regex_error is thrown, *this remains unchanged.
854 assign(const _Ch_type
* __p
, flag_type __flags
= ECMAScript
)
855 { return this->assign(string_type(__p
), __flags
); }
858 * @brief Assigns a new regular expression to a regex object from a
859 * C-style string containing a regular expression pattern.
861 * @param __p A pointer to a C-style string containing a
862 * regular expression pattern.
863 * @param __len The length of the regular expression pattern string.
864 * @param __flags Syntax option flags.
866 * @throws regex_error if p does not contain a valid regular
867 * expression pattern interpreted according to @p __flags. If
868 * regex_error is thrown, *this remains unchanged.
871 assign(const _Ch_type
* __p
, std::size_t __len
, flag_type __flags
)
872 { return this->assign(string_type(__p
, __len
), __flags
); }
875 * @brief Assigns a new regular expression to a regex object from a
876 * string containing a regular expression pattern.
878 * @param __s A string containing a regular expression pattern.
879 * @param __flags Syntax option flags.
881 * @throws regex_error if __s does not contain a valid regular
882 * expression pattern interpreted according to @p __flags. If
883 * regex_error is thrown, *this remains unchanged.
885 template<typename _Ch_typeraits
, typename _Alloc
>
887 assign(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
,
888 flag_type __flags
= ECMAScript
)
890 basic_regex
__tmp(__s
, __flags
);
896 * @brief Assigns a new regular expression to a regex object.
898 * @param __first The start of a range containing a valid regular
900 * @param __last The end of a range containing a valid regular
902 * @param __flags Syntax option flags.
904 * @throws regex_error if p does not contain a valid regular
905 * expression pattern interpreted according to @p __flags. If
906 * regex_error is thrown, the object remains unchanged.
908 template<typename _InputIterator
>
910 assign(_InputIterator __first
, _InputIterator __last
,
911 flag_type __flags
= ECMAScript
)
912 { return this->assign(string_type(__first
, __last
), __flags
); }
915 * @brief Assigns a new regular expression to a regex object.
917 * @param __l An initializer list representing a regular expression.
918 * @param __flags Syntax option flags.
920 * @throws regex_error if @p __l does not contain a valid
921 * regular expression pattern interpreted according to @p
922 * __flags. If regex_error is thrown, the object remains
926 assign(initializer_list
<_Ch_type
> __l
, flag_type __flags
= ECMAScript
)
927 { return this->assign(__l
.begin(), __l
.end(), __flags
); }
929 // [7.8.4] const operations
931 * @brief Gets the number of marked subexpressions within the regular
936 { return _M_automaton
->_M_sub_count() - 1; }
939 * @brief Gets the flags used to construct the regular expression
940 * or in the last call to assign().
948 * @brief Imbues the regular expression object with the given locale.
950 * @param __loc A locale.
953 imbue(locale_type __loc
)
954 { return _M_traits
.imbue(__loc
); }
957 * @brief Gets the locale currently imbued in the regular expression
962 { return _M_traits
.getloc(); }
966 * @brief Swaps the contents of two regular expression objects.
968 * @param __rhs Another regular expression object.
971 swap(basic_regex
& __rhs
)
973 std::swap(_M_flags
, __rhs
._M_flags
);
974 std::swap(_M_traits
, __rhs
._M_traits
);
975 std::swap(_M_automaton
, __rhs
._M_automaton
);
978 #ifdef _GLIBCXX_DEBUG
980 _M_dot(std::ostream
& __ostr
)
981 { _M_automaton
->_M_dot(__ostr
); }
985 typedef std::shared_ptr
<__detail::_Automaton
<_Ch_type
, _Rx_traits
>>
988 template<typename _BiIter
, typename _Alloc
,
989 typename _CharT
, typename _TraitsT
>
990 friend std::unique_ptr
<
991 __detail::_Executor
<_BiIter
, _Alloc
, _CharT
, _TraitsT
>>
992 __detail::__get_executor(_BiIter
,
994 match_results
<_BiIter
, _Alloc
>&,
995 const basic_regex
<_CharT
, _TraitsT
>&,
996 regex_constants::match_flag_type
);
998 template<typename _B
, typename _A
, typename _C
, typename _R
>
1001 match_results
<_B
, _A
>&,
1002 const basic_regex
<_C
, _R
>&,
1003 regex_constants::match_flag_type
);
1005 template<typename _B
, typename _A
, typename _C
, typename _R
>
1007 regex_search(_B
, _B
,
1008 match_results
<_B
, _A
>&,
1009 const basic_regex
<_C
, _R
>&,
1010 regex_constants::match_flag_type
);
1013 _Rx_traits _M_traits
;
1014 _AutomatonPtr _M_automaton
;
1017 /** @brief Standard regular expressions. */
1018 typedef basic_regex
<char> regex
;
1020 #ifdef _GLIBCXX_USE_WCHAR_T
1021 /** @brief Standard wide-character regular expressions. */
1022 typedef basic_regex
<wchar_t> wregex
;
1026 // [7.8.6] basic_regex swap
1028 * @brief Swaps the contents of two regular expression objects.
1029 * @param __lhs First regular expression.
1030 * @param __rhs Second regular expression.
1032 template<typename _Ch_type
, typename _Rx_traits
>
1034 swap(basic_regex
<_Ch_type
, _Rx_traits
>& __lhs
,
1035 basic_regex
<_Ch_type
, _Rx_traits
>& __rhs
)
1036 { __lhs
.swap(__rhs
); }
1039 // [7.9] Class template sub_match
1041 * A sequence of characters matched by a particular marked sub-expression.
1043 * An object of this class is essentially a pair of iterators marking a
1044 * matched subexpression within a regular expression pattern match. Such
1045 * objects can be converted to and compared with std::basic_string objects
1046 * of a similar base character type as the pattern matched by the regular
1049 * The iterators that make up the pair are the usual half-open interval
1050 * referencing the actual original pattern matched.
1052 template<typename _BiIter
>
1053 class sub_match
: public std::pair
<_BiIter
, _BiIter
>
1055 typedef iterator_traits
<_BiIter
> __iter_traits
;
1058 typedef typename
__iter_traits::value_type value_type
;
1059 typedef typename
__iter_traits::difference_type difference_type
;
1060 typedef _BiIter iterator
;
1061 typedef std::basic_string
<value_type
> string_type
;
1065 constexpr sub_match() : matched() { }
1068 * Gets the length of the matching sequence.
1072 { return this->matched
? std::distance(this->first
, this->second
) : 0; }
1075 * @brief Gets the matching sequence as a string.
1077 * @returns the matching sequence as a string.
1079 * This is the implicit conversion operator. It is identical to the
1080 * str() member function except that it will want to pop up in
1081 * unexpected places and cause a great deal of confusion and cursing
1084 operator string_type() const
1086 return this->matched
1087 ? string_type(this->first
, this->second
)
1092 * @brief Gets the matching sequence as a string.
1094 * @returns the matching sequence as a string.
1099 return this->matched
1100 ? string_type(this->first
, this->second
)
1105 * @brief Compares this and another matched sequence.
1107 * @param __s Another matched sequence to compare to this one.
1109 * @retval <0 this matched sequence will collate before @p __s.
1110 * @retval =0 this matched sequence is equivalent to @p __s.
1111 * @retval <0 this matched sequence will collate after @p __s.
1114 compare(const sub_match
& __s
) const
1115 { return this->str().compare(__s
.str()); }
1118 * @brief Compares this sub_match to a string.
1120 * @param __s A string to compare to this sub_match.
1122 * @retval <0 this matched sequence will collate before @p __s.
1123 * @retval =0 this matched sequence is equivalent to @p __s.
1124 * @retval <0 this matched sequence will collate after @p __s.
1127 compare(const string_type
& __s
) const
1128 { return this->str().compare(__s
); }
1131 * @brief Compares this sub_match to a C-style string.
1133 * @param __s A C-style string to compare to this sub_match.
1135 * @retval <0 this matched sequence will collate before @p __s.
1136 * @retval =0 this matched sequence is equivalent to @p __s.
1137 * @retval <0 this matched sequence will collate after @p __s.
1140 compare(const value_type
* __s
) const
1141 { return this->str().compare(__s
); }
1145 /** @brief Standard regex submatch over a C-style null-terminated string. */
1146 typedef sub_match
<const char*> csub_match
;
1148 /** @brief Standard regex submatch over a standard string. */
1149 typedef sub_match
<string::const_iterator
> ssub_match
;
1151 #ifdef _GLIBCXX_USE_WCHAR_T
1152 /** @brief Regex submatch over a C-style null-terminated wide string. */
1153 typedef sub_match
<const wchar_t*> wcsub_match
;
1155 /** @brief Regex submatch over a standard wide string. */
1156 typedef sub_match
<wstring::const_iterator
> wssub_match
;
1159 // [7.9.2] sub_match non-member operators
1162 * @brief Tests the equivalence of two regular expression submatches.
1163 * @param __lhs First regular expression submatch.
1164 * @param __rhs Second regular expression submatch.
1165 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1167 template<typename _BiIter
>
1169 operator==(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1170 { return __lhs
.compare(__rhs
) == 0; }
1173 * @brief Tests the inequivalence of two regular expression submatches.
1174 * @param __lhs First regular expression submatch.
1175 * @param __rhs Second regular expression submatch.
1176 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1178 template<typename _BiIter
>
1180 operator!=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1181 { return __lhs
.compare(__rhs
) != 0; }
1184 * @brief Tests the ordering of two regular expression submatches.
1185 * @param __lhs First regular expression submatch.
1186 * @param __rhs Second regular expression submatch.
1187 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1189 template<typename _BiIter
>
1191 operator<(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1192 { return __lhs
.compare(__rhs
) < 0; }
1195 * @brief Tests the ordering of two regular expression submatches.
1196 * @param __lhs First regular expression submatch.
1197 * @param __rhs Second regular expression submatch.
1198 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1200 template<typename _BiIter
>
1202 operator<=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1203 { return __lhs
.compare(__rhs
) <= 0; }
1206 * @brief Tests the ordering of two regular expression submatches.
1207 * @param __lhs First regular expression submatch.
1208 * @param __rhs Second regular expression submatch.
1209 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1211 template<typename _BiIter
>
1213 operator>=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1214 { return __lhs
.compare(__rhs
) >= 0; }
1217 * @brief Tests the ordering of two regular expression submatches.
1218 * @param __lhs First regular expression submatch.
1219 * @param __rhs Second regular expression submatch.
1220 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1222 template<typename _BiIter
>
1224 operator>(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1225 { return __lhs
.compare(__rhs
) > 0; }
1227 // Alias for sub_match'd string.
1228 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1229 using __sub_match_string
= basic_string
<
1230 typename iterator_traits
<_Bi_iter
>::value_type
,
1231 _Ch_traits
, _Ch_alloc
>;
1234 * @brief Tests the equivalence of a string and a regular expression
1236 * @param __lhs A string.
1237 * @param __rhs A regular expression submatch.
1238 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1240 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1242 operator==(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1243 const sub_match
<_Bi_iter
>& __rhs
)
1244 { return __rhs
.compare(__lhs
.c_str()) == 0; }
1247 * @brief Tests the inequivalence of a string and a regular expression
1249 * @param __lhs A string.
1250 * @param __rhs A regular expression submatch.
1251 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1253 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1255 operator!=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1256 const sub_match
<_Bi_iter
>& __rhs
)
1257 { return !(__lhs
== __rhs
); }
1260 * @brief Tests the ordering of a string and a regular expression submatch.
1261 * @param __lhs A string.
1262 * @param __rhs A regular expression submatch.
1263 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1265 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1267 operator<(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1268 const sub_match
<_Bi_iter
>& __rhs
)
1269 { return __rhs
.compare(__lhs
.c_str()) > 0; }
1272 * @brief Tests the ordering of a string and a regular expression submatch.
1273 * @param __lhs A string.
1274 * @param __rhs A regular expression submatch.
1275 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1277 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1279 operator>(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1280 const sub_match
<_Bi_iter
>& __rhs
)
1281 { return __rhs
< __lhs
; }
1284 * @brief Tests the ordering of a string and a regular expression submatch.
1285 * @param __lhs A string.
1286 * @param __rhs A regular expression submatch.
1287 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1289 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1291 operator>=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1292 const sub_match
<_Bi_iter
>& __rhs
)
1293 { return !(__lhs
< __rhs
); }
1296 * @brief Tests the ordering of a string and a regular expression submatch.
1297 * @param __lhs A string.
1298 * @param __rhs A regular expression submatch.
1299 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1301 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1303 operator<=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1304 const sub_match
<_Bi_iter
>& __rhs
)
1305 { return !(__rhs
< __lhs
); }
1308 * @brief Tests the equivalence of a regular expression submatch and a
1310 * @param __lhs A regular expression submatch.
1311 * @param __rhs A string.
1312 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1314 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1316 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1317 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1318 { return __lhs
.compare(__rhs
.c_str()) == 0; }
1321 * @brief Tests the inequivalence of a regular expression submatch and a
1323 * @param __lhs A regular expression submatch.
1324 * @param __rhs A string.
1325 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1327 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1329 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1330 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1331 { return !(__lhs
== __rhs
); }
1334 * @brief Tests the ordering of a regular expression submatch and a string.
1335 * @param __lhs A regular expression submatch.
1336 * @param __rhs A string.
1337 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1339 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1341 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1342 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1343 { return __lhs
.compare(__rhs
.c_str()) < 0; }
1346 * @brief Tests the ordering of a regular expression submatch and a string.
1347 * @param __lhs A regular expression submatch.
1348 * @param __rhs A string.
1349 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1351 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1353 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1354 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1355 { return __rhs
< __lhs
; }
1358 * @brief Tests the ordering of a regular expression submatch and a string.
1359 * @param __lhs A regular expression submatch.
1360 * @param __rhs A string.
1361 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1363 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1365 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1366 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1367 { return !(__lhs
< __rhs
); }
1370 * @brief Tests the ordering of a regular expression submatch and a string.
1371 * @param __lhs A regular expression submatch.
1372 * @param __rhs A string.
1373 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1375 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1377 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1378 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1379 { return !(__rhs
< __lhs
); }
1382 * @brief Tests the equivalence of a C string and a regular expression
1384 * @param __lhs A C string.
1385 * @param __rhs A regular expression submatch.
1386 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1388 template<typename _Bi_iter
>
1390 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1391 const sub_match
<_Bi_iter
>& __rhs
)
1392 { return __rhs
.compare(__lhs
) == 0; }
1395 * @brief Tests the inequivalence of an iterator value and a regular
1396 * expression submatch.
1397 * @param __lhs A regular expression submatch.
1398 * @param __rhs A string.
1399 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1401 template<typename _Bi_iter
>
1403 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1404 const sub_match
<_Bi_iter
>& __rhs
)
1405 { return !(__lhs
== __rhs
); }
1408 * @brief Tests the ordering of a string and a regular expression submatch.
1409 * @param __lhs A string.
1410 * @param __rhs A regular expression submatch.
1411 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1413 template<typename _Bi_iter
>
1415 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1416 const sub_match
<_Bi_iter
>& __rhs
)
1417 { return __rhs
.compare(__lhs
) > 0; }
1420 * @brief Tests the ordering of a string and a regular expression submatch.
1421 * @param __lhs A string.
1422 * @param __rhs A regular expression submatch.
1423 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1425 template<typename _Bi_iter
>
1427 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1428 const sub_match
<_Bi_iter
>& __rhs
)
1429 { return __rhs
< __lhs
; }
1432 * @brief Tests the ordering of a string and a regular expression submatch.
1433 * @param __lhs A string.
1434 * @param __rhs A regular expression submatch.
1435 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1437 template<typename _Bi_iter
>
1439 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1440 const sub_match
<_Bi_iter
>& __rhs
)
1441 { return !(__lhs
< __rhs
); }
1444 * @brief Tests the ordering of a string and a regular expression submatch.
1445 * @param __lhs A string.
1446 * @param __rhs A regular expression submatch.
1447 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1449 template<typename _Bi_iter
>
1451 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1452 const sub_match
<_Bi_iter
>& __rhs
)
1453 { return !(__rhs
< __lhs
); }
1456 * @brief Tests the equivalence of a regular expression submatch and a
1458 * @param __lhs A regular expression submatch.
1459 * @param __rhs A pointer to a string?
1460 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1462 template<typename _Bi_iter
>
1464 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1465 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1466 { return __lhs
.compare(__rhs
) == 0; }
1469 * @brief Tests the inequivalence of a regular expression submatch and a
1471 * @param __lhs A regular expression submatch.
1472 * @param __rhs A pointer to a string.
1473 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1475 template<typename _Bi_iter
>
1477 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1478 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1479 { return !(__lhs
== __rhs
); }
1482 * @brief Tests the ordering of a regular expression submatch and a string.
1483 * @param __lhs A regular expression submatch.
1484 * @param __rhs A string.
1485 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1487 template<typename _Bi_iter
>
1489 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1490 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1491 { return __lhs
.compare(__rhs
) < 0; }
1494 * @brief Tests the ordering of a regular expression submatch and a string.
1495 * @param __lhs A regular expression submatch.
1496 * @param __rhs A string.
1497 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1499 template<typename _Bi_iter
>
1501 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1502 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1503 { return __rhs
< __lhs
; }
1506 * @brief Tests the ordering of a regular expression submatch and a string.
1507 * @param __lhs A regular expression submatch.
1508 * @param __rhs A string.
1509 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1511 template<typename _Bi_iter
>
1513 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1514 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1515 { return !(__lhs
< __rhs
); }
1518 * @brief Tests the ordering of a regular expression submatch and a string.
1519 * @param __lhs A regular expression submatch.
1520 * @param __rhs A string.
1521 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1523 template<typename _Bi_iter
>
1525 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1526 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1527 { return !(__rhs
< __lhs
); }
1530 * @brief Tests the equivalence of a string and a regular expression
1532 * @param __lhs A string.
1533 * @param __rhs A regular expression submatch.
1534 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1536 template<typename _Bi_iter
>
1538 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1539 const sub_match
<_Bi_iter
>& __rhs
)
1541 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1542 return __rhs
.compare(string_type(1, __lhs
)) == 0;
1546 * @brief Tests the inequivalence of a string and a regular expression
1548 * @param __lhs A string.
1549 * @param __rhs A regular expression submatch.
1550 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1552 template<typename _Bi_iter
>
1554 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1555 const sub_match
<_Bi_iter
>& __rhs
)
1556 { return !(__lhs
== __rhs
); }
1559 * @brief Tests the ordering of a string and a regular expression submatch.
1560 * @param __lhs A string.
1561 * @param __rhs A regular expression submatch.
1562 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1564 template<typename _Bi_iter
>
1566 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1567 const sub_match
<_Bi_iter
>& __rhs
)
1569 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1570 return __rhs
.compare(string_type(1, __lhs
)) > 0;
1574 * @brief Tests the ordering of a string and a regular expression submatch.
1575 * @param __lhs A string.
1576 * @param __rhs A regular expression submatch.
1577 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1579 template<typename _Bi_iter
>
1581 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1582 const sub_match
<_Bi_iter
>& __rhs
)
1583 { return __rhs
< __lhs
; }
1586 * @brief Tests the ordering of a string and a regular expression submatch.
1587 * @param __lhs A string.
1588 * @param __rhs A regular expression submatch.
1589 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1591 template<typename _Bi_iter
>
1593 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1594 const sub_match
<_Bi_iter
>& __rhs
)
1595 { return !(__lhs
< __rhs
); }
1598 * @brief Tests the ordering of a string and a regular expression submatch.
1599 * @param __lhs A string.
1600 * @param __rhs A regular expression submatch.
1601 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1603 template<typename _Bi_iter
>
1605 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1606 const sub_match
<_Bi_iter
>& __rhs
)
1607 { return !(__rhs
< __lhs
); }
1610 * @brief Tests the equivalence of a regular expression submatch and a
1612 * @param __lhs A regular expression submatch.
1613 * @param __rhs A const string reference.
1614 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1616 template<typename _Bi_iter
>
1618 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1619 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1621 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1622 return __lhs
.compare(string_type(1, __rhs
)) == 0;
1626 * @brief Tests the inequivalence of a regular expression submatch and a
1628 * @param __lhs A regular expression submatch.
1629 * @param __rhs A const string reference.
1630 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1632 template<typename _Bi_iter
>
1634 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1635 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1636 { return !(__lhs
== __rhs
); }
1639 * @brief Tests the ordering of a regular expression submatch and a string.
1640 * @param __lhs A regular expression submatch.
1641 * @param __rhs A const string reference.
1642 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1644 template<typename _Bi_iter
>
1646 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1647 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1649 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1650 return __lhs
.compare(string_type(1, __rhs
)) < 0;
1654 * @brief Tests the ordering of a regular expression submatch and a string.
1655 * @param __lhs A regular expression submatch.
1656 * @param __rhs A const string reference.
1657 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1659 template<typename _Bi_iter
>
1661 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1662 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1663 { return __rhs
< __lhs
; }
1666 * @brief Tests the ordering of a regular expression submatch and a string.
1667 * @param __lhs A regular expression submatch.
1668 * @param __rhs A const string reference.
1669 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1671 template<typename _Bi_iter
>
1673 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1674 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1675 { return !(__lhs
< __rhs
); }
1678 * @brief Tests the ordering of a regular expression submatch and a string.
1679 * @param __lhs A regular expression submatch.
1680 * @param __rhs A const string reference.
1681 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1683 template<typename _Bi_iter
>
1685 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1686 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1687 { return !(__rhs
< __lhs
); }
1690 * @brief Inserts a matched string into an output stream.
1692 * @param __os The output stream.
1693 * @param __m A submatch string.
1695 * @returns the output stream with the submatch string inserted.
1697 template<typename _Ch_type
, typename _Ch_traits
, typename _Bi_iter
>
1699 basic_ostream
<_Ch_type
, _Ch_traits
>&
1700 operator<<(basic_ostream
<_Ch_type
, _Ch_traits
>& __os
,
1701 const sub_match
<_Bi_iter
>& __m
)
1702 { return __os
<< __m
.str(); }
1704 // [7.10] Class template match_results
1707 * Special sub_match object representing an unmatched sub-expression.
1709 template<typename _Bi_iter
>
1710 inline const sub_match
<_Bi_iter
>&
1713 static const sub_match
<_Bi_iter
> __unmatched
= sub_match
<_Bi_iter
>();
1718 * @brief The results of a match or search operation.
1720 * A collection of character sequences representing the result of a regular
1721 * expression match. Storage for the collection is allocated and freed as
1722 * necessary by the member functions of class template match_results.
1724 * This class satisfies the Sequence requirements, with the exception that
1725 * only the operations defined for a const-qualified Sequence are supported.
1727 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1728 * the whole match. In this case the %sub_match member matched is always true.
1729 * The sub_match object stored at index n denotes what matched the marked
1730 * sub-expression n within the matched expression. If the sub-expression n
1731 * participated in a regular expression match then the %sub_match member
1732 * matched evaluates to true, and members first and second denote the range
1733 * of characters [first, second) which formed that match. Otherwise matched
1734 * is false, and members first and second point to the end of the sequence
1735 * that was searched.
1739 template<typename _Bi_iter
,
1740 typename _Alloc
= allocator
<sub_match
<_Bi_iter
> > >
1742 : private std::vector
<sub_match
<_Bi_iter
>, _Alloc
>
1746 * The vector base is empty if this does not represent a successful match.
1747 * Otherwise it contains n+3 elements where n is the number of marked
1750 * [1] 1st marked subexpression
1752 * [n] nth marked subexpression
1756 typedef std::vector
<sub_match
<_Bi_iter
>, _Alloc
> _Base_type
;
1757 typedef std::iterator_traits
<_Bi_iter
> __iter_traits
;
1758 typedef regex_constants::match_flag_type match_flag_type
;
1762 * @name 10.? Public Types
1765 typedef _Alloc allocator_type
;
1766 typedef sub_match
<_Bi_iter
> value_type
;
1767 typedef const value_type
& const_reference
;
1768 typedef const_reference reference
;
1769 typedef typename
_Base_type::const_iterator const_iterator
;
1770 typedef const_iterator iterator
;
1771 typedef typename
__iter_traits::difference_type difference_type
;
1772 typedef typename
__iter_traits::value_type char_type
;
1773 typedef typename allocator_traits
<_Alloc
>::size_type size_type
;
1776 typedef std::basic_string
<char_type
> string_type
;
1781 * @name 28.10.1 Construction, Copying, and Destruction
1786 * @brief Constructs a default %match_results container.
1787 * @post size() returns 0 and str() returns an empty string.
1790 match_results(const _Alloc
& __a
= _Alloc())
1795 * @brief Copy constructs a %match_results.
1797 match_results(const match_results
& __rhs
)
1802 * @brief Move constructs a %match_results.
1804 match_results(match_results
&& __rhs
) noexcept
1805 : _Base_type(std::move(__rhs
))
1809 * @brief Assigns rhs to *this.
1812 operator=(const match_results
& __rhs
)
1814 match_results(__rhs
).swap(*this);
1819 * @brief Move-assigns rhs to *this.
1822 operator=(match_results
&& __rhs
)
1824 match_results(std::move(__rhs
)).swap(*this);
1829 * @brief Destroys a %match_results object.
1838 * @brief Indicates if the %match_results is ready.
1839 * @retval true The object has a fully-established result state.
1840 * @retval false The object is not ready.
1842 bool ready() const { return !_Base_type::empty(); }
1845 * @name 28.10.2 Size
1850 * @brief Gets the number of matches and submatches.
1852 * The number of matches for a given regular expression will be either 0
1853 * if there was no match or mark_count() + 1 if a match was successful.
1854 * Some matches may be empty.
1856 * @returns the number of matches found.
1861 size_type __size
= _Base_type::size();
1862 return (__size
&& _Base_type::operator[](0).matched
) ? __size
- 2 : 0;
1867 { return _Base_type::max_size(); }
1870 * @brief Indicates if the %match_results contains no results.
1871 * @retval true The %match_results object is empty.
1872 * @retval false The %match_results object is not empty.
1876 { return size() == 0; }
1881 * @name 10.3 Element Access
1886 * @brief Gets the length of the indicated submatch.
1887 * @param __sub indicates the submatch.
1888 * @pre ready() == true
1890 * This function returns the length of the indicated submatch, or the
1891 * length of the entire match if @p __sub is zero (the default).
1894 length(size_type __sub
= 0) const
1895 { return (*this)[__sub
].length(); }
1898 * @brief Gets the offset of the beginning of the indicated submatch.
1899 * @param __sub indicates the submatch.
1900 * @pre ready() == true
1902 * This function returns the offset from the beginning of the target
1903 * sequence to the beginning of the submatch, unless the value of @p __sub
1904 * is zero (the default), in which case this function returns the offset
1905 * from the beginning of the target sequence to the beginning of the
1908 * Returns -1 if @p __sub is out of range.
1911 position(size_type __sub
= 0) const
1913 return __sub
< size() ? std::distance(this->prefix().first
,
1914 (*this)[__sub
].first
) : -1;
1918 * @brief Gets the match or submatch converted to a string type.
1919 * @param __sub indicates the submatch.
1920 * @pre ready() == true
1922 * This function gets the submatch (or match, if @p __sub is
1923 * zero) extracted from the target range and converted to the
1924 * associated string type.
1927 str(size_type __sub
= 0) const
1928 { return (*this)[__sub
].str(); }
1931 * @brief Gets a %sub_match reference for the match or submatch.
1932 * @param __sub indicates the submatch.
1933 * @pre ready() == true
1935 * This function gets a reference to the indicated submatch, or
1936 * the entire match if @p __sub is zero.
1938 * If @p __sub >= size() then this function returns a %sub_match with a
1939 * special value indicating no submatch.
1942 operator[](size_type __sub
) const
1944 _GLIBCXX_DEBUG_ASSERT( ready() );
1945 return __sub
< size()
1946 ? _Base_type::operator[](__sub
)
1947 : __unmatched_sub
<_Bi_iter
>();
1951 * @brief Gets a %sub_match representing the match prefix.
1952 * @pre ready() == true
1954 * This function gets a reference to a %sub_match object representing the
1955 * part of the target range between the start of the target range and the
1956 * start of the match.
1961 _GLIBCXX_DEBUG_ASSERT( ready() );
1963 ? _Base_type::operator[](_Base_type::size() - 2)
1964 : __unmatched_sub
<_Bi_iter
>();
1968 * @brief Gets a %sub_match representing the match suffix.
1969 * @pre ready() == true
1971 * This function gets a reference to a %sub_match object representing the
1972 * part of the target range between the end of the match and the end of
1978 _GLIBCXX_DEBUG_ASSERT( ready() );
1980 ? _Base_type::operator[](_Base_type::size() - 1)
1981 : __unmatched_sub
<_Bi_iter
>();
1985 * @brief Gets an iterator to the start of the %sub_match collection.
1989 { return _Base_type::begin(); }
1992 * @brief Gets an iterator to the start of the %sub_match collection.
1996 { return _Base_type::cbegin(); }
1999 * @brief Gets an iterator to one-past-the-end of the collection.
2003 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
2006 * @brief Gets an iterator to one-past-the-end of the collection.
2015 * @name 10.4 Formatting
2017 * These functions perform formatted substitution of the matched
2018 * character sequences into their target. The format specifiers and
2019 * escape sequences accepted by these functions are determined by
2020 * their @p flags parameter as documented above.
2025 * @pre ready() == true
2026 * @todo Implement this function.
2028 template<typename _Out_iter
>
2030 format(_Out_iter __out
, const char_type
* __fmt_first
,
2031 const char_type
* __fmt_last
,
2032 match_flag_type __flags
= regex_constants::format_default
) const
2036 * @pre ready() == true
2038 template<typename _Out_iter
, typename _St
, typename _Sa
>
2040 format(_Out_iter __out
, const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2041 match_flag_type __flags
= regex_constants::format_default
) const
2043 return format(__out
, __fmt
.data(), __fmt
.data() + __fmt
.size(),
2048 * @pre ready() == true
2050 template<typename _Out_iter
, typename _St
, typename _Sa
>
2051 basic_string
<char_type
, _St
, _Sa
>
2052 format(const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2053 match_flag_type __flags
= regex_constants::format_default
) const
2055 basic_string
<char_type
, _St
, _Sa
> __result
;
2056 format(std::back_inserter(__result
), __fmt
, __flags
);
2061 * @pre ready() == true
2064 format(const char_type
* __fmt
,
2065 match_flag_type __flags
= regex_constants::format_default
) const
2067 string_type __result
;
2068 format(std::back_inserter(__result
),
2070 __fmt
+ char_traits
<char_type
>::length(__fmt
),
2078 * @name 10.5 Allocator
2083 * @brief Gets a copy of the allocator.
2086 get_allocator() const
2087 { return _Base_type::get_allocator(); }
2097 * @brief Swaps the contents of two match_results.
2100 swap(match_results
& __that
)
2101 { _Base_type::swap(__that
); }
2105 template<typename
, typename
, typename
, typename
>
2106 friend class __detail::_Executor
;
2108 template<typename
, typename
, typename
, typename
>
2109 friend class __detail::_DFSExecutor
;
2111 template<typename
, typename
, typename
, typename
>
2112 friend class __detail::_BFSExecutor
;
2114 template<typename _B
, typename _A
, typename _Ch_type
, typename _Rx_traits
>
2116 regex_match(_B
, _B
, match_results
<_B
, _A
>&,
2117 const basic_regex
<_Ch_type
,
2119 regex_constants::match_flag_type
);
2121 template<typename _B
, typename _A
, typename _Ch_type
, typename _Rx_traits
>
2123 regex_search(_B
, _B
, match_results
<_B
, _A
>&,
2124 const basic_regex
<_Ch_type
,
2126 regex_constants::match_flag_type
);
2129 typedef match_results
<const char*> cmatch
;
2130 typedef match_results
<string::const_iterator
> smatch
;
2131 #ifdef _GLIBCXX_USE_WCHAR_T
2132 typedef match_results
<const wchar_t*> wcmatch
;
2133 typedef match_results
<wstring::const_iterator
> wsmatch
;
2136 // match_results comparisons
2138 * @brief Compares two match_results for equality.
2139 * @returns true if the two objects refer to the same match,
2142 template<typename _Bi_iter
, typename _Alloc
>
2144 operator==(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2145 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2147 if (__m1
.ready() != __m2
.ready())
2149 if (!__m1
.ready()) // both are not ready
2151 if (__m1
.empty() != __m2
.empty())
2153 if (__m1
.empty()) // both are empty
2155 return __m1
.prefix() == __m2
.prefix()
2156 && __m1
.size() == __m2
.size()
2157 && std::equal(__m1
.begin(), __m1
.end(), __m2
.begin())
2158 && __m1
.suffix() == __m2
.suffix();
2162 * @brief Compares two match_results for inequality.
2163 * @returns true if the two objects do not refer to the same match,
2166 template<typename _Bi_iter
, class _Alloc
>
2168 operator!=(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2169 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2170 { return !(__m1
== __m2
); }
2172 // [7.10.6] match_results swap
2174 * @brief Swaps two match results.
2175 * @param __lhs A match result.
2176 * @param __rhs A match result.
2178 * The contents of the two match_results objects are swapped.
2180 template<typename _Bi_iter
, typename _Alloc
>
2182 swap(match_results
<_Bi_iter
, _Alloc
>& __lhs
,
2183 match_results
<_Bi_iter
, _Alloc
>& __rhs
)
2184 { __lhs
.swap(__rhs
); }
2186 // [7.11.2] Function template regex_match
2188 * @name Matching, Searching, and Replacing
2193 * @brief Determines if there is a match between the regular expression @p e
2194 * and all of the character sequence [first, last).
2196 * @param __s Start of the character sequence to match.
2197 * @param __e One-past-the-end of the character sequence to match.
2198 * @param __m The match results.
2199 * @param __re The regular expression.
2200 * @param __flags Controls how the regular expression is matched.
2202 * @retval true A match exists.
2203 * @retval false Otherwise.
2205 * @throws an exception of type regex_error.
2207 * @todo Implement this function.
2209 template<typename _Bi_iter
, typename _Alloc
,
2210 typename _Ch_type
, typename _Rx_traits
>
2212 regex_match(_Bi_iter __s
,
2214 match_results
<_Bi_iter
, _Alloc
>& __m
,
2215 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2216 regex_constants::match_flag_type __flags
2217 = regex_constants::match_default
)
2219 if (__re
._M_automaton
== nullptr)
2221 if (__detail::__get_executor(__s
, __e
, __m
, __re
, __flags
)->_M_match())
2223 for (auto __it
: __m
)
2225 __it
.first
= __it
.second
= __e
;
2226 __m
.at(__m
.size()).matched
= false;
2227 __m
.at(__m
.size()).first
= __s
;
2228 __m
.at(__m
.size()).second
= __s
;
2229 __m
.at(__m
.size()+1).matched
= false;
2230 __m
.at(__m
.size()+1).first
= __e
;
2231 __m
.at(__m
.size()+1).second
= __e
;
2238 * @brief Indicates if there is a match between the regular expression @p e
2239 * and all of the character sequence [first, last).
2241 * @param __first Beginning of the character sequence to match.
2242 * @param __last One-past-the-end of the character sequence to match.
2243 * @param __re The regular expression.
2244 * @param __flags Controls how the regular expression is matched.
2246 * @retval true A match exists.
2247 * @retval false Otherwise.
2249 * @throws an exception of type regex_error.
2251 template<typename _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2253 regex_match(_Bi_iter __first
, _Bi_iter __last
,
2254 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2255 regex_constants::match_flag_type __flags
2256 = regex_constants::match_default
)
2258 match_results
<_Bi_iter
> __what
;
2259 return regex_match(__first
, __last
, __what
, __re
, __flags
);
2263 * @brief Determines if there is a match between the regular expression @p e
2264 * and a C-style null-terminated string.
2266 * @param __s The C-style null-terminated string to match.
2267 * @param __m The match results.
2268 * @param __re The regular expression.
2269 * @param __f Controls how the regular expression is matched.
2271 * @retval true A match exists.
2272 * @retval false Otherwise.
2274 * @throws an exception of type regex_error.
2276 template<typename _Ch_type
, typename _Alloc
, typename _Rx_traits
>
2278 regex_match(const _Ch_type
* __s
,
2279 match_results
<const _Ch_type
*, _Alloc
>& __m
,
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
), __m
, __re
, __f
); }
2286 * @brief Determines if there is a match between the regular expression @p e
2289 * @param __s The string to match.
2290 * @param __m The match results.
2291 * @param __re The regular expression.
2292 * @param __flags Controls how the regular expression is matched.
2294 * @retval true A match exists.
2295 * @retval false Otherwise.
2297 * @throws an exception of type regex_error.
2299 template<typename _Ch_traits
, typename _Ch_alloc
,
2300 typename _Alloc
, typename _Ch_type
, typename _Rx_traits
>
2302 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2303 match_results
<typename basic_string
<_Ch_type
,
2304 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2305 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2306 regex_constants::match_flag_type __flags
2307 = regex_constants::match_default
)
2308 { return regex_match(__s
.begin(), __s
.end(), __m
, __re
, __flags
); }
2311 * @brief Indicates if there is a match between the regular expression @p e
2312 * and a C-style null-terminated string.
2314 * @param __s The C-style null-terminated string to match.
2315 * @param __re The regular expression.
2316 * @param __f Controls how the regular expression is matched.
2318 * @retval true A match exists.
2319 * @retval false Otherwise.
2321 * @throws an exception of type regex_error.
2323 template<typename _Ch_type
, class _Rx_traits
>
2325 regex_match(const _Ch_type
* __s
,
2326 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2327 regex_constants::match_flag_type __f
2328 = regex_constants::match_default
)
2329 { return regex_match(__s
, __s
+ _Rx_traits::length(__s
), __re
, __f
); }
2332 * @brief Indicates if there is a match between the regular expression @p e
2335 * @param __s [IN] The string to match.
2336 * @param __re [IN] The regular expression.
2337 * @param __flags [IN] Controls how the regular expression is matched.
2339 * @retval true A match exists.
2340 * @retval false Otherwise.
2342 * @throws an exception of type regex_error.
2344 template<typename _Ch_traits
, typename _Str_allocator
,
2345 typename _Ch_type
, typename _Rx_traits
>
2347 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Str_allocator
>& __s
,
2348 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2349 regex_constants::match_flag_type __flags
2350 = regex_constants::match_default
)
2351 { return regex_match(__s
.begin(), __s
.end(), __re
, __flags
); }
2353 // [7.11.3] Function template regex_search
2355 * Searches for a regular expression within a range.
2356 * @param __first [IN] The start of the string to search.
2357 * @param __last [IN] One-past-the-end of the string to search.
2358 * @param __m [OUT] The match results.
2359 * @param __re [IN] The regular expression to search for.
2360 * @param __flags [IN] Search policy flags.
2361 * @retval true A match was found within the string.
2362 * @retval false No match was found within the string, the content of %m is
2365 * @throws an exception of type regex_error.
2367 * @todo Implement this function.
2369 template<typename _Bi_iter
, typename _Alloc
,
2370 typename _Ch_type
, typename _Rx_traits
>
2372 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2373 match_results
<_Bi_iter
, _Alloc
>& __m
,
2374 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2375 regex_constants::match_flag_type __flags
2376 = regex_constants::match_default
)
2378 if (__re
._M_automaton
== nullptr)
2380 for (auto __cur
= __first
; __cur
!= __last
; ++__cur
) // Any KMP-like algo?
2381 if (__detail::__get_executor(__cur
, __last
, __m
, __re
, __flags
)
2382 ->_M_search_from_first())
2384 for (auto __it
: __m
)
2386 __it
.first
= __it
.second
= __last
;
2387 __m
.at(__m
.size()).first
= __first
;
2388 __m
.at(__m
.size()).second
= __m
[0].first
;
2389 __m
.at(__m
.size()+1).first
= __m
[0].second
;
2390 __m
.at(__m
.size()+1).second
= __last
;
2391 __m
.at(__m
.size()).matched
=
2392 (__m
.prefix().first
!= __m
.prefix().second
);
2393 __m
.at(__m
.size()+1).matched
=
2394 (__m
.suffix().first
!= __m
.suffix().second
);
2402 * Searches for a regular expression within a range.
2403 * @param __first [IN] The start of the string to search.
2404 * @param __last [IN] One-past-the-end of the string to search.
2405 * @param __re [IN] The regular expression to search for.
2406 * @param __flags [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 _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2414 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2415 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2416 regex_constants::match_flag_type __flags
2417 = regex_constants::match_default
)
2419 match_results
<_Bi_iter
> __what
;
2420 return regex_search(__first
, __last
, __what
, __re
, __flags
);
2424 * @brief Searches for a regular expression within a C-string.
2425 * @param __s [IN] A C-string to search for the regex.
2426 * @param __m [OUT] The set of regex matches.
2427 * @param __e [IN] The regex to search for in @p s.
2428 * @param __f [IN] The search flags.
2429 * @retval true A match was found within the string.
2430 * @retval false No match was found within the string, the content of %m is
2433 * @throws an exception of type regex_error.
2435 template<typename _Ch_type
, class _Alloc
, class _Rx_traits
>
2437 regex_search(const _Ch_type
* __s
,
2438 match_results
<const _Ch_type
*, _Alloc
>& __m
,
2439 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2440 regex_constants::match_flag_type __f
2441 = regex_constants::match_default
)
2442 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __m
, __e
, __f
); }
2445 * @brief Searches for a regular expression within a C-string.
2446 * @param __s [IN] The C-string to search.
2447 * @param __e [IN] The regular expression to search for.
2448 * @param __f [IN] Search policy flags.
2449 * @retval true A match was found within the string.
2450 * @retval false No match was found within the string.
2452 * @throws an exception of type regex_error.
2454 template<typename _Ch_type
, typename _Rx_traits
>
2456 regex_search(const _Ch_type
* __s
,
2457 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2458 regex_constants::match_flag_type __f
2459 = regex_constants::match_default
)
2460 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __e
, __f
); }
2463 * @brief Searches for a regular expression within a string.
2464 * @param __s [IN] The string to search.
2465 * @param __e [IN] The regular expression to search for.
2466 * @param __flags [IN] Search policy flags.
2467 * @retval true A match was found within the string.
2468 * @retval false No match was found within the string.
2470 * @throws an exception of type regex_error.
2472 template<typename _Ch_traits
, typename _String_allocator
,
2473 typename _Ch_type
, typename _Rx_traits
>
2475 regex_search(const basic_string
<_Ch_type
, _Ch_traits
,
2476 _String_allocator
>& __s
,
2477 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2478 regex_constants::match_flag_type __flags
2479 = regex_constants::match_default
)
2480 { return regex_search(__s
.begin(), __s
.end(), __e
, __flags
); }
2483 * @brief Searches for a regular expression within a string.
2484 * @param __s [IN] A C++ string to search for the regex.
2485 * @param __m [OUT] The set of regex matches.
2486 * @param __e [IN] The regex to search for in @p s.
2487 * @param __f [IN] The search flags.
2488 * @retval true A match was found within the string.
2489 * @retval false No match was found within the string, the content of %m is
2492 * @throws an exception of type regex_error.
2494 template<typename _Ch_traits
, typename _Ch_alloc
,
2495 typename _Alloc
, typename _Ch_type
,
2496 typename _Rx_traits
>
2498 regex_search(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2499 match_results
<typename basic_string
<_Ch_type
,
2500 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2501 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2502 regex_constants::match_flag_type __f
2503 = regex_constants::match_default
)
2504 { return regex_search(__s
.begin(), __s
.end(), __m
, __e
, __f
); }
2506 // std [28.11.4] Function template regex_replace
2517 * @throws an exception of type regex_error.
2519 * @todo Implement this function.
2521 template<typename _Out_iter
, typename _Bi_iter
,
2522 typename _Rx_traits
, typename _Ch_type
>
2524 regex_replace(_Out_iter __out
, _Bi_iter __first
, _Bi_iter __last
,
2525 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2526 const basic_string
<_Ch_type
>& __fmt
,
2527 regex_constants::match_flag_type __flags
2528 = regex_constants::match_default
)
2538 * @returns a copy of string @p s with replacements.
2540 * @throws an exception of type regex_error.
2542 template<typename _Rx_traits
, typename _Ch_type
>
2543 inline basic_string
<_Ch_type
>
2544 regex_replace(const basic_string
<_Ch_type
>& __s
,
2545 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2546 const basic_string
<_Ch_type
>& __fmt
,
2547 regex_constants::match_flag_type __flags
2548 = regex_constants::match_default
)
2550 basic_string
<_Ch_type
> __result
;
2551 regex_replace(std::back_inserter(__result
),
2552 __s
.begin(), __s
.end(), __e
, __fmt
, __flags
);
2558 // std [28.12] Class template regex_iterator
2560 * An iterator adaptor that will provide repeated calls of regex_search over
2561 * a range until no more matches remain.
2563 template<typename _Bi_iter
,
2564 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2565 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2566 class regex_iterator
2569 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2570 typedef match_results
<_Bi_iter
> value_type
;
2571 typedef std::ptrdiff_t difference_type
;
2572 typedef const value_type
* pointer
;
2573 typedef const value_type
& reference
;
2574 typedef std::forward_iterator_tag iterator_category
;
2577 * @brief Provides a singular iterator, useful for indicating
2578 * one-past-the-end of a range.
2585 * Constructs a %regex_iterator...
2586 * @param __a [IN] The start of a text range to search.
2587 * @param __b [IN] One-past-the-end of the text range to search.
2588 * @param __re [IN] The regular expression to match.
2589 * @param __m [IN] Policy flags for match rules.
2591 regex_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2592 regex_constants::match_flag_type __m
2593 = regex_constants::match_default
)
2594 : _M_begin(__a
), _M_end(__b
), _M_pregex(&__re
), _M_flags(__m
), _M_match()
2595 { regex_search(_M_begin
, _M_end
, _M_match
, *_M_pregex
, _M_flags
); }
2598 * Copy constructs a %regex_iterator.
2600 regex_iterator(const regex_iterator
& __rhs
) = default;
2603 * @brief Assigns one %regex_iterator to another.
2606 operator=(const regex_iterator
& __rhs
) = default;
2609 * @brief Tests the equivalence of two regex iterators.
2612 operator==(const regex_iterator
& __rhs
) const;
2615 * @brief Tests the inequivalence of two regex iterators.
2618 operator!=(const regex_iterator
& __rhs
) const
2619 { return !(*this == __rhs
); }
2622 * @brief Dereferences a %regex_iterator.
2626 { return _M_match
; }
2629 * @brief Selects a %regex_iterator member.
2633 { return &_M_match
; }
2636 * @brief Increments a %regex_iterator.
2642 * @brief Postincrements a %regex_iterator.
2655 const regex_type
* _M_pregex
;
2656 regex_constants::match_flag_type _M_flags
;
2657 match_results
<_Bi_iter
> _M_match
;
2660 template<typename _Bi_iter
,
2662 typename _Rx_traits
>
2664 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2665 operator==(const regex_iterator
& __rhs
) const
2667 return (_M_match
.empty() && __rhs
._M_match
.empty())
2668 || (_M_begin
== __rhs
._M_begin
2669 && _M_end
== __rhs
._M_end
2670 && _M_pregex
== __rhs
._M_pregex
2671 && _M_flags
== __rhs
._M_flags
2672 && _M_match
[0] == __rhs
._M_match
[0]);
2675 template<typename _Bi_iter
,
2677 typename _Rx_traits
>
2678 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2679 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2682 // FIXME: In all cases in which the call to regex_search returns true,
2683 // match.prefix().first shall be equal to the previous value of
2684 // match[0].second, and for each index i in the half-open range
2685 // [0, match.size()) for which match[i].matched is true,
2686 // match[i].position() shall return distance(begin, match[i].first).
2688 if (_M_match
[0].matched
)
2690 auto __start
= _M_match
[0].second
;
2691 if (_M_match
[0].first
== _M_match
[0].second
)
2692 if (__start
== _M_end
)
2694 _M_match
= value_type();
2699 if (regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
2700 | regex_constants::match_not_null
2701 | regex_constants::match_continuous
))
2706 _M_flags
|= regex_constants::match_prev_avail
;
2707 if (!regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
))
2708 _M_match
= value_type();
2713 typedef regex_iterator
<const char*> cregex_iterator
;
2714 typedef regex_iterator
<string::const_iterator
> sregex_iterator
;
2715 #ifdef _GLIBCXX_USE_WCHAR_T
2716 typedef regex_iterator
<const wchar_t*> wcregex_iterator
;
2717 typedef regex_iterator
<wstring::const_iterator
> wsregex_iterator
;
2720 // [7.12.2] Class template regex_token_iterator
2722 * Iterates over submatches in a range (or @a splits a text string).
2724 * The purpose of this iterator is to enumerate all, or all specified,
2725 * matches of a regular expression within a text range. The dereferenced
2726 * value of an iterator of this class is a std::sub_match object.
2728 template<typename _Bi_iter
,
2729 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2730 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2731 class regex_token_iterator
2734 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2735 typedef sub_match
<_Bi_iter
> value_type
;
2736 typedef std::ptrdiff_t difference_type
;
2737 typedef const value_type
* pointer
;
2738 typedef const value_type
& reference
;
2739 typedef std::forward_iterator_tag iterator_category
;
2743 * @brief Default constructs a %regex_token_iterator.
2745 * A default-constructed %regex_token_iterator is a singular iterator
2746 * that will compare equal to the one-past-the-end value for any
2747 * iterator of the same type.
2749 regex_token_iterator()
2750 : _M_position(), _M_result(nullptr), _M_suffix(), _M_n(0), _M_subs()
2754 * Constructs a %regex_token_iterator...
2755 * @param __a [IN] The start of the text to search.
2756 * @param __b [IN] One-past-the-end of the text to search.
2757 * @param __re [IN] The regular expression to search for.
2758 * @param __submatch [IN] Which submatch to return. There are some
2759 * special values for this parameter:
2760 * - -1 each enumerated subexpression does NOT
2761 * match the regular expression (aka field
2763 * - 0 the entire string matching the
2764 * subexpression is returned for each match
2766 * - >0 enumerates only the indicated
2767 * subexpression from a match within the text.
2768 * @param __m [IN] Policy flags for match rules.
2770 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2772 regex_constants::match_flag_type __m
2773 = regex_constants::match_default
)
2774 : _M_position(__a
, __b
, __re
, __m
), _M_subs(1, __submatch
), _M_n(0)
2775 { _M_init(__a
, __b
); }
2778 * Constructs a %regex_token_iterator...
2779 * @param __a [IN] The start of the text to search.
2780 * @param __b [IN] One-past-the-end of the text to search.
2781 * @param __re [IN] The regular expression to search for.
2782 * @param __submatches [IN] A list of subexpressions to return for each
2783 * regular expression match within the text.
2784 * @param __m [IN] Policy flags for match rules.
2786 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2787 const regex_type
& __re
,
2788 const std::vector
<int>& __submatches
,
2789 regex_constants::match_flag_type __m
2790 = regex_constants::match_default
)
2791 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2792 { _M_init(__a
, __b
); }
2795 * Constructs a %regex_token_iterator...
2796 * @param __a [IN] The start of the text to search.
2797 * @param __b [IN] One-past-the-end of the text to search.
2798 * @param __re [IN] The regular expression to search for.
2799 * @param __submatches [IN] A list of subexpressions to return for each
2800 * regular expression match within the text.
2801 * @param __m [IN] Policy flags for match rules.
2803 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2804 const regex_type
& __re
,
2805 initializer_list
<int> __submatches
,
2806 regex_constants::match_flag_type __m
2807 = regex_constants::match_default
)
2808 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2809 { _M_init(__a
, __b
); }
2812 * Constructs a %regex_token_iterator...
2813 * @param __a [IN] The start of the text to search.
2814 * @param __b [IN] One-past-the-end of the text to search.
2815 * @param __re [IN] The regular expression to search for.
2816 * @param __submatches [IN] A list of subexpressions to return for each
2817 * regular expression match within the text.
2818 * @param __m [IN] Policy flags for match rules.
2820 template<std::size_t _Nm
>
2821 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2822 const regex_type
& __re
,
2823 const int (&__submatches
)[_Nm
],
2824 regex_constants::match_flag_type __m
2825 = regex_constants::match_default
)
2826 : _M_position(__a
, __b
, __re
, __m
),
2827 _M_subs(__submatches
, *(&__submatches
+1)), _M_n(0)
2828 { _M_init(__a
, __b
); }
2831 * @brief Copy constructs a %regex_token_iterator.
2832 * @param __rhs [IN] A %regex_token_iterator to copy.
2834 regex_token_iterator(const regex_token_iterator
& __rhs
)
2835 : _M_position(__rhs
.position
), _M_subs(__rhs
.subs
), _M_n(__rhs
.N
),
2836 _M_result(__rhs
.result
), _M_suffix(__rhs
.suffix
),
2837 _M_has_m1(__rhs
._M_has_m1
)
2839 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2840 _M_result
= &_M_suffix
;
2844 * @brief Assigns a %regex_token_iterator to another.
2845 * @param __rhs [IN] A %regex_token_iterator to copy.
2847 regex_token_iterator
&
2848 operator=(const regex_token_iterator
& __rhs
);
2851 * @brief Compares a %regex_token_iterator to another for equality.
2854 operator==(const regex_token_iterator
& __rhs
) const;
2857 * @brief Compares a %regex_token_iterator to another for inequality.
2860 operator!=(const regex_token_iterator
& __rhs
) const
2861 { return !(*this == __rhs
); }
2864 * @brief Dereferences a %regex_token_iterator.
2868 { return *_M_result
; }
2871 * @brief Selects a %regex_token_iterator member.
2875 { return _M_result
; }
2878 * @brief Increments a %regex_token_iterator.
2880 regex_token_iterator
&
2884 * @brief Postincrements a %regex_token_iterator.
2886 regex_token_iterator
2895 typedef regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
> _Position
;
2898 _M_init(_Bi_iter __a
, _Bi_iter __b
);
2901 _M_current_match() const
2903 if (_M_subs
[_M_n
] == -1)
2904 return (*_M_position
).prefix();
2906 return (*_M_position
)[_M_subs
[_M_n
]];
2910 _M_end_of_seq() const
2911 { return _M_result
!= nullptr; }
2913 _Position _M_position
;
2914 const value_type
* _M_result
;
2915 value_type _M_suffix
;
2917 std::vector
<int> _M_subs
;
2919 // Show whether _M_subs contains -1
2923 template<typename _Bi_iter
,
2925 typename _Rx_traits
>
2926 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2927 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2928 operator=(const regex_token_iterator
& __rhs
)
2930 _M_position
= __rhs
._M_position
;
2931 _M_subs
= __rhs
._M_subs
;
2933 _M_result
= __rhs
._M_result
;
2934 _M_suffix
= __rhs
._M_suffix
;
2935 _M_has_m1
= __rhs
._M_has_m1
;
2936 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2937 _M_result
= &_M_suffix
;
2940 template<typename _Bi_iter
,
2942 typename _Rx_traits
>
2944 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2945 operator==(const regex_token_iterator
& __rhs
) const
2947 if (_M_end_of_seq() && __rhs
._M_end_of_seq())
2949 if (_M_suffix
.matched
&& __rhs
._M_suffix
.matched
2950 && _M_suffix
== __rhs
._M_suffix
)
2952 if (_M_end_of_seq() || _M_suffix
.matched
2953 || __rhs
._M_end_of_seq() || __rhs
._M_suffix
.matched
)
2955 return _M_position
== __rhs
._M_position
2956 && _M_n
== __rhs
._M_n
2957 && _M_subs
== __rhs
._M_subs
;
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
>::
2967 _Position __prev
= _M_position
;
2968 if (_M_suffix
.matched
)
2969 *this = regex_token_iterator();
2970 else if (_M_n
+ 1 < _M_subs
.size())
2973 _M_result
= &_M_current_match();
2979 if (_M_position
!= _Position())
2980 _M_result
= &_M_current_match();
2981 else if (_M_has_m1
&& __prev
->suffix().length() != 0)
2983 _M_suffix
.matched
= true;
2984 _M_suffix
.first
= __prev
->suffix().first
;
2985 _M_suffix
.second
= __prev
->suffix().second
;
2986 _M_result
= &_M_suffix
;
2989 *this = regex_token_iterator();
2994 template<typename _Bi_iter
,
2996 typename _Rx_traits
>
2998 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2999 _M_init(_Bi_iter __a
, _Bi_iter __b
)
3002 for (auto __it
: _M_subs
)
3008 if (_M_position
!= _Position())
3009 _M_result
= &_M_current_match();
3012 _M_suffix
.matched
= true;
3013 _M_suffix
.first
= __a
;
3014 _M_suffix
.second
= __b
;
3015 _M_result
= &_M_suffix
;
3018 _M_result
= nullptr;
3021 /** @brief Token iterator for C-style NULL-terminated strings. */
3022 typedef regex_token_iterator
<const char*> cregex_token_iterator
;
3024 /** @brief Token iterator for standard strings. */
3025 typedef regex_token_iterator
<string::const_iterator
> sregex_token_iterator
;
3027 #ifdef _GLIBCXX_USE_WCHAR_T
3028 /** @brief Token iterator for C-style NULL-terminated wide strings. */
3029 typedef regex_token_iterator
<const wchar_t*> wcregex_token_iterator
;
3031 /** @brief Token iterator for standard wide-character strings. */
3032 typedef regex_token_iterator
<wstring::const_iterator
> wsregex_token_iterator
;
3036 _GLIBCXX_END_NAMESPACE_VERSION