2017-09-26 Thomas Koenig <tkoenig@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / regex.h
blob32e7159eec916640ae3a4781ad682cfc4f0fdedf
1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2017 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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/>.
25 /**
26 * @file bits/regex.h
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
34 _GLIBCXX_BEGIN_NAMESPACE_CXX11
35 template<typename, typename>
36 class basic_regex;
38 template<typename, typename>
39 class match_results;
41 _GLIBCXX_END_NAMESPACE_CXX11
43 namespace __detail
45 enum class _RegexExecutorPolicy : int
46 { _S_auto, _S_alternate };
48 template<typename _BiIter, typename _Alloc,
49 typename _CharT, typename _TraitsT,
50 _RegexExecutorPolicy __policy,
51 bool __match_mode>
52 bool
53 __regex_algo_impl(_BiIter __s,
54 _BiIter __e,
55 match_results<_BiIter, _Alloc>& __m,
56 const basic_regex<_CharT, _TraitsT>& __re,
57 regex_constants::match_flag_type __flags);
59 template<typename, typename, typename, bool>
60 class _Executor;
63 _GLIBCXX_BEGIN_NAMESPACE_CXX11
65 /**
66 * @addtogroup regex
67 * @{
70 /**
71 * @brief Describes aspects of a regular expression.
73 * A regular expression traits class that satisfies the requirements of
74 * section [28.7].
76 * The class %regex is parameterized around a set of related types and
77 * functions used to complete the definition of its semantics. This class
78 * satisfies the requirements of such a traits class.
80 template<typename _Ch_type>
81 struct regex_traits
83 public:
84 typedef _Ch_type char_type;
85 typedef std::basic_string<char_type> string_type;
86 typedef std::locale locale_type;
87 private:
88 struct _RegexMask
90 typedef std::ctype_base::mask _BaseType;
91 _BaseType _M_base;
92 unsigned char _M_extended;
93 static constexpr unsigned char _S_under = 1 << 0;
94 static constexpr unsigned char _S_valid_mask = 0x1;
96 constexpr _RegexMask(_BaseType __base = 0,
97 unsigned char __extended = 0)
98 : _M_base(__base), _M_extended(__extended)
99 { }
101 constexpr _RegexMask
102 operator&(_RegexMask __other) const
104 return _RegexMask(_M_base & __other._M_base,
105 _M_extended & __other._M_extended);
108 constexpr _RegexMask
109 operator|(_RegexMask __other) const
111 return _RegexMask(_M_base | __other._M_base,
112 _M_extended | __other._M_extended);
115 constexpr _RegexMask
116 operator^(_RegexMask __other) const
118 return _RegexMask(_M_base ^ __other._M_base,
119 _M_extended ^ __other._M_extended);
122 constexpr _RegexMask
123 operator~() const
124 { return _RegexMask(~_M_base, ~_M_extended); }
126 _RegexMask&
127 operator&=(_RegexMask __other)
128 { return *this = (*this) & __other; }
130 _RegexMask&
131 operator|=(_RegexMask __other)
132 { return *this = (*this) | __other; }
134 _RegexMask&
135 operator^=(_RegexMask __other)
136 { return *this = (*this) ^ __other; }
138 constexpr bool
139 operator==(_RegexMask __other) const
141 return (_M_extended & _S_valid_mask)
142 == (__other._M_extended & _S_valid_mask)
143 && _M_base == __other._M_base;
146 constexpr bool
147 operator!=(_RegexMask __other) const
148 { return !((*this) == __other); }
151 public:
152 typedef _RegexMask char_class_type;
154 public:
156 * @brief Constructs a default traits object.
158 regex_traits() { }
161 * @brief Gives the length of a C-style string starting at @p __p.
163 * @param __p a pointer to the start of a character sequence.
165 * @returns the number of characters between @p *__p and the first
166 * default-initialized value of type @p char_type. In other words, uses
167 * the C-string algorithm for determining the length of a sequence of
168 * characters.
170 static std::size_t
171 length(const char_type* __p)
172 { return string_type::traits_type::length(__p); }
175 * @brief Performs the identity translation.
177 * @param __c A character to the locale-specific character set.
179 * @returns __c.
181 char_type
182 translate(char_type __c) const
183 { return __c; }
186 * @brief Translates a character into a case-insensitive equivalent.
188 * @param __c A character to the locale-specific character set.
190 * @returns the locale-specific lower-case equivalent of __c.
191 * @throws std::bad_cast if the imbued locale does not support the ctype
192 * facet.
194 char_type
195 translate_nocase(char_type __c) const
197 typedef std::ctype<char_type> __ctype_type;
198 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
199 return __fctyp.tolower(__c);
203 * @brief Gets a sort key for a character sequence.
205 * @param __first beginning of the character sequence.
206 * @param __last one-past-the-end of the character sequence.
208 * Returns a sort key for the character sequence designated by the
209 * iterator range [F1, F2) such that if the character sequence [G1, G2)
210 * sorts before the character sequence [H1, H2) then
211 * v.transform(G1, G2) < v.transform(H1, H2).
213 * What this really does is provide a more efficient way to compare a
214 * string to multiple other strings in locales with fancy collation
215 * rules and equivalence classes.
217 * @returns a locale-specific sort key equivalent to the input range.
219 * @throws std::bad_cast if the current locale does not have a collate
220 * facet.
222 template<typename _Fwd_iter>
223 string_type
224 transform(_Fwd_iter __first, _Fwd_iter __last) const
226 typedef std::collate<char_type> __collate_type;
227 const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
228 string_type __s(__first, __last);
229 return __fclt.transform(__s.data(), __s.data() + __s.size());
233 * @brief Gets a sort key for a character sequence, independent of case.
235 * @param __first beginning of the character sequence.
236 * @param __last one-past-the-end of the character sequence.
238 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
239 * typeid(collate_byname<_Ch_type>) and the form of the sort key
240 * returned by collate_byname<_Ch_type>::transform(__first, __last)
241 * is known and can be converted into a primary sort key
242 * then returns that key, otherwise returns an empty string.
244 * @todo Implement this function correctly.
246 template<typename _Fwd_iter>
247 string_type
248 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
250 // TODO : this is not entirely correct.
251 // This function requires extra support from the platform.
253 // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
254 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
255 // for details.
256 typedef std::ctype<char_type> __ctype_type;
257 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
258 std::vector<char_type> __s(__first, __last);
259 __fctyp.tolower(__s.data(), __s.data() + __s.size());
260 return this->transform(__s.data(), __s.data() + __s.size());
264 * @brief Gets a collation element by name.
266 * @param __first beginning of the collation element name.
267 * @param __last one-past-the-end of the collation element name.
269 * @returns a sequence of one or more characters that represents the
270 * collating element consisting of the character sequence designated by
271 * the iterator range [__first, __last). Returns an empty string if the
272 * character sequence is not a valid collating element.
274 template<typename _Fwd_iter>
275 string_type
276 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
279 * @brief Maps one or more characters to a named character
280 * classification.
282 * @param __first beginning of the character sequence.
283 * @param __last one-past-the-end of the character sequence.
284 * @param __icase ignores the case of the classification name.
286 * @returns an unspecified value that represents the character
287 * classification named by the character sequence designated by
288 * the iterator range [__first, __last). If @p icase is true,
289 * the returned mask identifies the classification regardless of
290 * the case of the characters to be matched (for example,
291 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
292 * case-dependent classification is returned. The value
293 * returned shall be independent of the case of the characters
294 * in the character sequence. If the name is not recognized then
295 * returns a value that compares equal to 0.
297 * At least the following names (or their wide-character equivalent) are
298 * supported.
299 * - d
300 * - w
301 * - s
302 * - alnum
303 * - alpha
304 * - blank
305 * - cntrl
306 * - digit
307 * - graph
308 * - lower
309 * - print
310 * - punct
311 * - space
312 * - upper
313 * - xdigit
315 template<typename _Fwd_iter>
316 char_class_type
317 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
318 bool __icase = false) const;
321 * @brief Determines if @p c is a member of an identified class.
323 * @param __c a character.
324 * @param __f a class type (as returned from lookup_classname).
326 * @returns true if the character @p __c is a member of the classification
327 * represented by @p __f, false otherwise.
329 * @throws std::bad_cast if the current locale does not have a ctype
330 * facet.
332 bool
333 isctype(_Ch_type __c, char_class_type __f) const;
336 * @brief Converts a digit to an int.
338 * @param __ch a character representing a digit.
339 * @param __radix the radix if the numeric conversion (limited to 8, 10,
340 * or 16).
342 * @returns the value represented by the digit __ch in base radix if the
343 * character __ch is a valid digit in base radix; otherwise returns -1.
346 value(_Ch_type __ch, int __radix) const;
349 * @brief Imbues the regex_traits object with a copy of a new locale.
351 * @param __loc A locale.
353 * @returns a copy of the previous locale in use by the regex_traits
354 * object.
356 * @note Calling imbue with a different locale than the one currently in
357 * use invalidates all cached data held by *this.
359 locale_type
360 imbue(locale_type __loc)
362 std::swap(_M_locale, __loc);
363 return __loc;
367 * @brief Gets a copy of the current locale in use by the regex_traits
368 * object.
370 locale_type
371 getloc() const
372 { return _M_locale; }
374 protected:
375 locale_type _M_locale;
378 // [7.8] Class basic_regex
380 * Objects of specializations of this class represent regular expressions
381 * constructed from sequences of character type @p _Ch_type.
383 * Storage for the regular expression is allocated and deallocated as
384 * necessary by the member functions of this class.
386 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
387 class basic_regex
389 public:
390 static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
391 "regex traits class must have the same char_type");
393 // types:
394 typedef _Ch_type value_type;
395 typedef _Rx_traits traits_type;
396 typedef typename traits_type::string_type string_type;
397 typedef regex_constants::syntax_option_type flag_type;
398 typedef typename traits_type::locale_type locale_type;
401 * @name Constants
402 * std [28.8.1](1)
404 //@{
405 static constexpr flag_type icase = regex_constants::icase;
406 static constexpr flag_type nosubs = regex_constants::nosubs;
407 static constexpr flag_type optimize = regex_constants::optimize;
408 static constexpr flag_type collate = regex_constants::collate;
409 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
410 static constexpr flag_type basic = regex_constants::basic;
411 static constexpr flag_type extended = regex_constants::extended;
412 static constexpr flag_type awk = regex_constants::awk;
413 static constexpr flag_type grep = regex_constants::grep;
414 static constexpr flag_type egrep = regex_constants::egrep;
415 //@}
417 // [7.8.2] construct/copy/destroy
419 * Constructs a basic regular expression that does not match any
420 * character sequence.
422 basic_regex()
423 : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
427 * @brief Constructs a basic regular expression from the
428 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
429 * interpreted according to the flags in @p __f.
431 * @param __p A pointer to the start of a C-style null-terminated string
432 * containing a regular expression.
433 * @param __f Flags indicating the syntax rules and options.
435 * @throws regex_error if @p __p is not a valid regular expression.
437 explicit
438 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
439 : basic_regex(__p, __p + char_traits<_Ch_type>::length(__p), __f)
443 * @brief Constructs a basic regular expression from the sequence
444 * [p, p + len) interpreted according to the flags in @p f.
446 * @param __p A pointer to the start of a string containing a regular
447 * expression.
448 * @param __len The length of the string containing the regular
449 * expression.
450 * @param __f Flags indicating the syntax rules and options.
452 * @throws regex_error if @p __p is not a valid regular expression.
454 basic_regex(const _Ch_type* __p, std::size_t __len,
455 flag_type __f = ECMAScript)
456 : basic_regex(__p, __p + __len, __f)
460 * @brief Copy-constructs a basic regular expression.
462 * @param __rhs A @p regex object.
464 basic_regex(const basic_regex& __rhs) = default;
467 * @brief Move-constructs a basic regular expression.
469 * @param __rhs A @p regex object.
471 basic_regex(basic_regex&& __rhs) noexcept = default;
474 * @brief Constructs a basic regular expression from the string
475 * @p s interpreted according to the flags in @p f.
477 * @param __s A string containing a regular expression.
478 * @param __f Flags indicating the syntax rules and options.
480 * @throws regex_error if @p __s is not a valid regular expression.
482 template<typename _Ch_traits, typename _Ch_alloc>
483 explicit
484 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
485 _Ch_alloc>& __s,
486 flag_type __f = ECMAScript)
487 : basic_regex(__s.data(), __s.data() + __s.size(), __f)
491 * @brief Constructs a basic regular expression from the range
492 * [first, last) interpreted according to the flags in @p f.
494 * @param __first The start of a range containing a valid regular
495 * expression.
496 * @param __last The end of a range containing a valid regular
497 * expression.
498 * @param __f The format flags of the regular expression.
500 * @throws regex_error if @p [__first, __last) is not a valid regular
501 * expression.
503 template<typename _FwdIter>
504 basic_regex(_FwdIter __first, _FwdIter __last,
505 flag_type __f = ECMAScript)
506 : basic_regex(std::move(__first), std::move(__last), locale_type(), __f)
510 * @brief Constructs a basic regular expression from an initializer list.
512 * @param __l The initializer list.
513 * @param __f The format flags of the regular expression.
515 * @throws regex_error if @p __l is not a valid regular expression.
517 basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
518 : basic_regex(__l.begin(), __l.end(), __f)
522 * @brief Destroys a basic regular expression.
524 ~basic_regex()
528 * @brief Assigns one regular expression to another.
530 basic_regex&
531 operator=(const basic_regex& __rhs)
532 { return this->assign(__rhs); }
535 * @brief Move-assigns one regular expression to another.
537 basic_regex&
538 operator=(basic_regex&& __rhs) noexcept
539 { return this->assign(std::move(__rhs)); }
542 * @brief Replaces a regular expression with a new one constructed from
543 * a C-style null-terminated string.
545 * @param __p A pointer to the start of a null-terminated C-style string
546 * containing a regular expression.
548 basic_regex&
549 operator=(const _Ch_type* __p)
550 { return this->assign(__p); }
553 * @brief Replaces a regular expression with a new one constructed from
554 * an initializer list.
556 * @param __l The initializer list.
558 * @throws regex_error if @p __l is not a valid regular expression.
560 basic_regex&
561 operator=(initializer_list<_Ch_type> __l)
562 { return this->assign(__l.begin(), __l.end()); }
565 * @brief Replaces a regular expression with a new one constructed from
566 * a string.
568 * @param __s A pointer to a string containing a regular expression.
570 template<typename _Ch_traits, typename _Alloc>
571 basic_regex&
572 operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
573 { return this->assign(__s); }
575 // [7.8.3] assign
577 * @brief the real assignment operator.
579 * @param __rhs Another regular expression object.
581 basic_regex&
582 assign(const basic_regex& __rhs)
584 basic_regex __tmp(__rhs);
585 this->swap(__tmp);
586 return *this;
590 * @brief The move-assignment operator.
592 * @param __rhs Another regular expression object.
594 basic_regex&
595 assign(basic_regex&& __rhs) noexcept
597 basic_regex __tmp(std::move(__rhs));
598 this->swap(__tmp);
599 return *this;
603 * @brief Assigns a new regular expression to a regex object from a
604 * C-style null-terminated string containing a regular expression
605 * pattern.
607 * @param __p A pointer to a C-style null-terminated string containing
608 * a regular expression pattern.
609 * @param __flags Syntax option flags.
611 * @throws regex_error if __p does not contain a valid regular
612 * expression pattern interpreted according to @p __flags. If
613 * regex_error is thrown, *this remains unchanged.
615 basic_regex&
616 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
617 { return this->assign(string_type(__p), __flags); }
620 * @brief Assigns a new regular expression to a regex object from a
621 * C-style string containing a regular expression pattern.
623 * @param __p A pointer to a C-style string containing a
624 * regular expression pattern.
625 * @param __len The length of the regular expression pattern string.
626 * @param __flags Syntax option flags.
628 * @throws regex_error if p does not contain a valid regular
629 * expression pattern interpreted according to @p __flags. If
630 * regex_error is thrown, *this remains unchanged.
632 basic_regex&
633 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
634 { return this->assign(string_type(__p, __len), __flags); }
637 * @brief Assigns a new regular expression to a regex object from a
638 * string containing a regular expression pattern.
640 * @param __s A string containing a regular expression pattern.
641 * @param __flags Syntax option flags.
643 * @throws regex_error if __s does not contain a valid regular
644 * expression pattern interpreted according to @p __flags. If
645 * regex_error is thrown, *this remains unchanged.
647 template<typename _Ch_traits, typename _Alloc>
648 basic_regex&
649 assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
650 flag_type __flags = ECMAScript)
652 return this->assign(basic_regex(__s.data(), __s.data() + __s.size(),
653 _M_loc, __flags));
657 * @brief Assigns a new regular expression to a regex object.
659 * @param __first The start of a range containing a valid regular
660 * expression.
661 * @param __last The end of a range containing a valid regular
662 * expression.
663 * @param __flags Syntax option flags.
665 * @throws regex_error if p does not contain a valid regular
666 * expression pattern interpreted according to @p __flags. If
667 * regex_error is thrown, the object remains unchanged.
669 template<typename _InputIterator>
670 basic_regex&
671 assign(_InputIterator __first, _InputIterator __last,
672 flag_type __flags = ECMAScript)
673 { return this->assign(string_type(__first, __last), __flags); }
676 * @brief Assigns a new regular expression to a regex object.
678 * @param __l An initializer list representing a regular expression.
679 * @param __flags Syntax option flags.
681 * @throws regex_error if @p __l does not contain a valid
682 * regular expression pattern interpreted according to @p
683 * __flags. If regex_error is thrown, the object remains
684 * unchanged.
686 basic_regex&
687 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
688 { return this->assign(__l.begin(), __l.end(), __flags); }
690 // [7.8.4] const operations
692 * @brief Gets the number of marked subexpressions within the regular
693 * expression.
695 unsigned int
696 mark_count() const
698 if (_M_automaton)
699 return _M_automaton->_M_sub_count() - 1;
700 return 0;
704 * @brief Gets the flags used to construct the regular expression
705 * or in the last call to assign().
707 flag_type
708 flags() const
709 { return _M_flags; }
711 // [7.8.5] locale
713 * @brief Imbues the regular expression object with the given locale.
715 * @param __loc A locale.
717 locale_type
718 imbue(locale_type __loc)
720 std::swap(__loc, _M_loc);
721 _M_automaton.reset();
722 return __loc;
726 * @brief Gets the locale currently imbued in the regular expression
727 * object.
729 locale_type
730 getloc() const
731 { return _M_loc; }
733 // [7.8.6] swap
735 * @brief Swaps the contents of two regular expression objects.
737 * @param __rhs Another regular expression object.
739 void
740 swap(basic_regex& __rhs)
742 std::swap(_M_flags, __rhs._M_flags);
743 std::swap(_M_loc, __rhs._M_loc);
744 std::swap(_M_automaton, __rhs._M_automaton);
747 #ifdef _GLIBCXX_DEBUG
748 void
749 _M_dot(std::ostream& __ostr)
750 { _M_automaton->_M_dot(__ostr); }
751 #endif
753 private:
754 typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
756 template<typename _FwdIter>
757 basic_regex(_FwdIter __first, _FwdIter __last, locale_type __loc,
758 flag_type __f)
759 : _M_flags((__f & (ECMAScript | basic | extended | awk | grep | egrep))
760 ? __f : (__f | ECMAScript)),
761 _M_loc(std::move(__loc)),
762 _M_automaton(__detail::__compile_nfa<_Rx_traits>(
763 std::move(__first), std::move(__last), _M_loc, _M_flags))
766 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
767 __detail::_RegexExecutorPolicy, bool>
768 friend bool
769 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
770 const basic_regex<_Cp, _Rp>&,
771 regex_constants::match_flag_type);
773 template<typename, typename, typename, bool>
774 friend class __detail::_Executor;
776 flag_type _M_flags;
777 locale_type _M_loc;
778 _AutomatonPtr _M_automaton;
781 #if __cpp_deduction_guides >= 201606
782 template<typename _ForwardIterator>
783 basic_regex(_ForwardIterator, _ForwardIterator,
784 regex_constants::syntax_option_type = {})
785 -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
786 #endif
788 /** @brief Standard regular expressions. */
789 typedef basic_regex<char> regex;
791 #ifdef _GLIBCXX_USE_WCHAR_T
792 /** @brief Standard wide-character regular expressions. */
793 typedef basic_regex<wchar_t> wregex;
794 #endif
797 // [7.8.6] basic_regex swap
799 * @brief Swaps the contents of two regular expression objects.
800 * @param __lhs First regular expression.
801 * @param __rhs Second regular expression.
803 template<typename _Ch_type, typename _Rx_traits>
804 inline void
805 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
806 basic_regex<_Ch_type, _Rx_traits>& __rhs)
807 { __lhs.swap(__rhs); }
810 // [7.9] Class template sub_match
812 * A sequence of characters matched by a particular marked sub-expression.
814 * An object of this class is essentially a pair of iterators marking a
815 * matched subexpression within a regular expression pattern match. Such
816 * objects can be converted to and compared with std::basic_string objects
817 * of a similar base character type as the pattern matched by the regular
818 * expression.
820 * The iterators that make up the pair are the usual half-open interval
821 * referencing the actual original pattern matched.
823 template<typename _BiIter>
824 class sub_match : public std::pair<_BiIter, _BiIter>
826 typedef iterator_traits<_BiIter> __iter_traits;
828 public:
829 typedef typename __iter_traits::value_type value_type;
830 typedef typename __iter_traits::difference_type difference_type;
831 typedef _BiIter iterator;
832 typedef std::basic_string<value_type> string_type;
834 bool matched;
836 constexpr sub_match() : matched() { }
839 * Gets the length of the matching sequence.
841 difference_type
842 length() const
843 { return this->matched ? std::distance(this->first, this->second) : 0; }
846 * @brief Gets the matching sequence as a string.
848 * @returns the matching sequence as a string.
850 * This is the implicit conversion operator. It is identical to the
851 * str() member function except that it will want to pop up in
852 * unexpected places and cause a great deal of confusion and cursing
853 * from the unwary.
855 operator string_type() const
857 return this->matched
858 ? string_type(this->first, this->second)
859 : string_type();
863 * @brief Gets the matching sequence as a string.
865 * @returns the matching sequence as a string.
867 string_type
868 str() const
870 return this->matched
871 ? string_type(this->first, this->second)
872 : string_type();
876 * @brief Compares this and another matched sequence.
878 * @param __s Another matched sequence to compare to this one.
880 * @retval <0 this matched sequence will collate before @p __s.
881 * @retval =0 this matched sequence is equivalent to @p __s.
882 * @retval <0 this matched sequence will collate after @p __s.
885 compare(const sub_match& __s) const
886 { return this->str().compare(__s.str()); }
889 * @brief Compares this sub_match to a string.
891 * @param __s A string to compare to this sub_match.
893 * @retval <0 this matched sequence will collate before @p __s.
894 * @retval =0 this matched sequence is equivalent to @p __s.
895 * @retval <0 this matched sequence will collate after @p __s.
898 compare(const string_type& __s) const
899 { return this->str().compare(__s); }
902 * @brief Compares this sub_match to a C-style string.
904 * @param __s A C-style string to compare to this sub_match.
906 * @retval <0 this matched sequence will collate before @p __s.
907 * @retval =0 this matched sequence is equivalent to @p __s.
908 * @retval <0 this matched sequence will collate after @p __s.
911 compare(const value_type* __s) const
912 { return this->str().compare(__s); }
916 /** @brief Standard regex submatch over a C-style null-terminated string. */
917 typedef sub_match<const char*> csub_match;
919 /** @brief Standard regex submatch over a standard string. */
920 typedef sub_match<string::const_iterator> ssub_match;
922 #ifdef _GLIBCXX_USE_WCHAR_T
923 /** @brief Regex submatch over a C-style null-terminated wide string. */
924 typedef sub_match<const wchar_t*> wcsub_match;
926 /** @brief Regex submatch over a standard wide string. */
927 typedef sub_match<wstring::const_iterator> wssub_match;
928 #endif
930 // [7.9.2] sub_match non-member operators
933 * @brief Tests the equivalence of two regular expression submatches.
934 * @param __lhs First regular expression submatch.
935 * @param __rhs Second regular expression submatch.
936 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
938 template<typename _BiIter>
939 inline bool
940 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
941 { return __lhs.compare(__rhs) == 0; }
944 * @brief Tests the inequivalence of two regular expression submatches.
945 * @param __lhs First regular expression submatch.
946 * @param __rhs Second regular expression submatch.
947 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
949 template<typename _BiIter>
950 inline bool
951 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
952 { return __lhs.compare(__rhs) != 0; }
955 * @brief Tests the ordering of two regular expression submatches.
956 * @param __lhs First regular expression submatch.
957 * @param __rhs Second regular expression submatch.
958 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
960 template<typename _BiIter>
961 inline bool
962 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
963 { return __lhs.compare(__rhs) < 0; }
966 * @brief Tests the ordering of two regular expression submatches.
967 * @param __lhs First regular expression submatch.
968 * @param __rhs Second regular expression submatch.
969 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
971 template<typename _BiIter>
972 inline bool
973 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
974 { return __lhs.compare(__rhs) <= 0; }
977 * @brief Tests the ordering of two regular expression submatches.
978 * @param __lhs First regular expression submatch.
979 * @param __rhs Second regular expression submatch.
980 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
982 template<typename _BiIter>
983 inline bool
984 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
985 { return __lhs.compare(__rhs) >= 0; }
988 * @brief Tests the ordering of two regular expression submatches.
989 * @param __lhs First regular expression submatch.
990 * @param __rhs Second regular expression submatch.
991 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
993 template<typename _BiIter>
994 inline bool
995 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
996 { return __lhs.compare(__rhs) > 0; }
998 // Alias for sub_match'd string.
999 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1000 using __sub_match_string = basic_string<
1001 typename iterator_traits<_Bi_iter>::value_type,
1002 _Ch_traits, _Ch_alloc>;
1005 * @brief Tests the equivalence of a string and a regular expression
1006 * submatch.
1007 * @param __lhs A string.
1008 * @param __rhs A regular expression submatch.
1009 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1011 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1012 inline bool
1013 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1014 const sub_match<_Bi_iter>& __rhs)
1016 typedef typename sub_match<_Bi_iter>::string_type string_type;
1017 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) == 0;
1021 * @brief Tests the inequivalence of a string and a regular expression
1022 * submatch.
1023 * @param __lhs A string.
1024 * @param __rhs A regular expression submatch.
1025 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1027 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1028 inline bool
1029 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1030 const sub_match<_Bi_iter>& __rhs)
1031 { return !(__lhs == __rhs); }
1034 * @brief Tests the ordering of a string and a regular expression submatch.
1035 * @param __lhs A string.
1036 * @param __rhs A regular expression submatch.
1037 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1039 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1040 inline bool
1041 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1042 const sub_match<_Bi_iter>& __rhs)
1044 typedef typename sub_match<_Bi_iter>::string_type string_type;
1045 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) > 0;
1049 * @brief Tests the ordering of a string and a regular expression submatch.
1050 * @param __lhs A string.
1051 * @param __rhs A regular expression submatch.
1052 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1054 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1055 inline bool
1056 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1057 const sub_match<_Bi_iter>& __rhs)
1058 { return __rhs < __lhs; }
1061 * @brief Tests the ordering of a string and a regular expression submatch.
1062 * @param __lhs A string.
1063 * @param __rhs A regular expression submatch.
1064 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1066 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1067 inline bool
1068 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1069 const sub_match<_Bi_iter>& __rhs)
1070 { return !(__lhs < __rhs); }
1073 * @brief Tests the ordering of a string and a regular expression submatch.
1074 * @param __lhs A string.
1075 * @param __rhs A regular expression submatch.
1076 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1078 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1079 inline bool
1080 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1081 const sub_match<_Bi_iter>& __rhs)
1082 { return !(__rhs < __lhs); }
1085 * @brief Tests the equivalence of a regular expression submatch and a
1086 * string.
1087 * @param __lhs A regular expression submatch.
1088 * @param __rhs A string.
1089 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1091 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1092 inline bool
1093 operator==(const sub_match<_Bi_iter>& __lhs,
1094 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1096 typedef typename sub_match<_Bi_iter>::string_type string_type;
1097 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) == 0;
1101 * @brief Tests the inequivalence of a regular expression submatch and a
1102 * string.
1103 * @param __lhs A regular expression submatch.
1104 * @param __rhs A string.
1105 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1107 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1108 inline bool
1109 operator!=(const sub_match<_Bi_iter>& __lhs,
1110 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1111 { return !(__lhs == __rhs); }
1114 * @brief Tests the ordering of a regular expression submatch and a string.
1115 * @param __lhs A regular expression submatch.
1116 * @param __rhs A string.
1117 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1119 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1120 inline bool
1121 operator<(const sub_match<_Bi_iter>& __lhs,
1122 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1124 typedef typename sub_match<_Bi_iter>::string_type string_type;
1125 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) < 0;
1129 * @brief Tests the ordering of a regular expression submatch and a string.
1130 * @param __lhs A regular expression submatch.
1131 * @param __rhs A string.
1132 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1134 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1135 inline bool
1136 operator>(const sub_match<_Bi_iter>& __lhs,
1137 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1138 { return __rhs < __lhs; }
1141 * @brief Tests the ordering of a regular expression submatch and a string.
1142 * @param __lhs A regular expression submatch.
1143 * @param __rhs A string.
1144 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1146 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1147 inline bool
1148 operator>=(const sub_match<_Bi_iter>& __lhs,
1149 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1150 { return !(__lhs < __rhs); }
1153 * @brief Tests the ordering of a regular expression submatch and a string.
1154 * @param __lhs A regular expression submatch.
1155 * @param __rhs A string.
1156 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1158 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1159 inline bool
1160 operator<=(const sub_match<_Bi_iter>& __lhs,
1161 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1162 { return !(__rhs < __lhs); }
1165 * @brief Tests the equivalence of a C string and a regular expression
1166 * submatch.
1167 * @param __lhs A C string.
1168 * @param __rhs A regular expression submatch.
1169 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1171 template<typename _Bi_iter>
1172 inline bool
1173 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1174 const sub_match<_Bi_iter>& __rhs)
1175 { return __rhs.compare(__lhs) == 0; }
1178 * @brief Tests the inequivalence of an iterator value and a regular
1179 * expression submatch.
1180 * @param __lhs A regular expression submatch.
1181 * @param __rhs A string.
1182 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1184 template<typename _Bi_iter>
1185 inline bool
1186 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1187 const sub_match<_Bi_iter>& __rhs)
1188 { return !(__lhs == __rhs); }
1191 * @brief Tests the ordering of a string and a regular expression submatch.
1192 * @param __lhs A string.
1193 * @param __rhs A regular expression submatch.
1194 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1196 template<typename _Bi_iter>
1197 inline bool
1198 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1199 const sub_match<_Bi_iter>& __rhs)
1200 { return __rhs.compare(__lhs) > 0; }
1203 * @brief Tests the ordering of a string and a regular expression submatch.
1204 * @param __lhs A string.
1205 * @param __rhs A regular expression submatch.
1206 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1208 template<typename _Bi_iter>
1209 inline bool
1210 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1211 const sub_match<_Bi_iter>& __rhs)
1212 { return __rhs < __lhs; }
1215 * @brief Tests the ordering of a string and a regular expression submatch.
1216 * @param __lhs A string.
1217 * @param __rhs A regular expression submatch.
1218 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1220 template<typename _Bi_iter>
1221 inline bool
1222 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1223 const sub_match<_Bi_iter>& __rhs)
1224 { return !(__lhs < __rhs); }
1227 * @brief Tests the ordering of a string and a regular expression submatch.
1228 * @param __lhs A string.
1229 * @param __rhs A regular expression submatch.
1230 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1232 template<typename _Bi_iter>
1233 inline bool
1234 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1235 const sub_match<_Bi_iter>& __rhs)
1236 { return !(__rhs < __lhs); }
1239 * @brief Tests the equivalence of a regular expression submatch and a
1240 * string.
1241 * @param __lhs A regular expression submatch.
1242 * @param __rhs A pointer to a string?
1243 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1245 template<typename _Bi_iter>
1246 inline bool
1247 operator==(const sub_match<_Bi_iter>& __lhs,
1248 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1249 { return __lhs.compare(__rhs) == 0; }
1252 * @brief Tests the inequivalence of a regular expression submatch and a
1253 * string.
1254 * @param __lhs A regular expression submatch.
1255 * @param __rhs A pointer to a string.
1256 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1258 template<typename _Bi_iter>
1259 inline bool
1260 operator!=(const sub_match<_Bi_iter>& __lhs,
1261 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1262 { return !(__lhs == __rhs); }
1265 * @brief Tests the ordering of a regular expression submatch and a string.
1266 * @param __lhs A regular expression submatch.
1267 * @param __rhs A string.
1268 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1270 template<typename _Bi_iter>
1271 inline bool
1272 operator<(const sub_match<_Bi_iter>& __lhs,
1273 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1274 { return __lhs.compare(__rhs) < 0; }
1277 * @brief Tests the ordering of a regular expression submatch and a string.
1278 * @param __lhs A regular expression submatch.
1279 * @param __rhs A string.
1280 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1282 template<typename _Bi_iter>
1283 inline bool
1284 operator>(const sub_match<_Bi_iter>& __lhs,
1285 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1286 { return __rhs < __lhs; }
1289 * @brief Tests the ordering of a regular expression submatch and a string.
1290 * @param __lhs A regular expression submatch.
1291 * @param __rhs A string.
1292 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1294 template<typename _Bi_iter>
1295 inline bool
1296 operator>=(const sub_match<_Bi_iter>& __lhs,
1297 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1298 { return !(__lhs < __rhs); }
1301 * @brief Tests the ordering of a regular expression submatch and a string.
1302 * @param __lhs A regular expression submatch.
1303 * @param __rhs A string.
1304 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1306 template<typename _Bi_iter>
1307 inline bool
1308 operator<=(const sub_match<_Bi_iter>& __lhs,
1309 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1310 { return !(__rhs < __lhs); }
1313 * @brief Tests the equivalence of a string and a regular expression
1314 * submatch.
1315 * @param __lhs A string.
1316 * @param __rhs A regular expression submatch.
1317 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1319 template<typename _Bi_iter>
1320 inline bool
1321 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1322 const sub_match<_Bi_iter>& __rhs)
1324 typedef typename sub_match<_Bi_iter>::string_type string_type;
1325 return __rhs.compare(string_type(1, __lhs)) == 0;
1329 * @brief Tests the inequivalence of a string and a regular expression
1330 * submatch.
1331 * @param __lhs A string.
1332 * @param __rhs A regular expression submatch.
1333 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1335 template<typename _Bi_iter>
1336 inline bool
1337 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1338 const sub_match<_Bi_iter>& __rhs)
1339 { return !(__lhs == __rhs); }
1342 * @brief Tests the ordering of a string and a regular expression submatch.
1343 * @param __lhs A string.
1344 * @param __rhs A regular expression submatch.
1345 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1347 template<typename _Bi_iter>
1348 inline bool
1349 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1350 const sub_match<_Bi_iter>& __rhs)
1352 typedef typename sub_match<_Bi_iter>::string_type string_type;
1353 return __rhs.compare(string_type(1, __lhs)) > 0;
1357 * @brief Tests the ordering of a string and a regular expression submatch.
1358 * @param __lhs A string.
1359 * @param __rhs A regular expression submatch.
1360 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1362 template<typename _Bi_iter>
1363 inline bool
1364 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1365 const sub_match<_Bi_iter>& __rhs)
1366 { return __rhs < __lhs; }
1369 * @brief Tests the ordering of a string and a regular expression submatch.
1370 * @param __lhs A string.
1371 * @param __rhs A regular expression submatch.
1372 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1374 template<typename _Bi_iter>
1375 inline bool
1376 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1377 const sub_match<_Bi_iter>& __rhs)
1378 { return !(__lhs < __rhs); }
1381 * @brief Tests the ordering of a string and a regular expression submatch.
1382 * @param __lhs A string.
1383 * @param __rhs A regular expression submatch.
1384 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1386 template<typename _Bi_iter>
1387 inline bool
1388 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1389 const sub_match<_Bi_iter>& __rhs)
1390 { return !(__rhs < __lhs); }
1393 * @brief Tests the equivalence of a regular expression submatch and a
1394 * string.
1395 * @param __lhs A regular expression submatch.
1396 * @param __rhs A const string reference.
1397 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1399 template<typename _Bi_iter>
1400 inline bool
1401 operator==(const sub_match<_Bi_iter>& __lhs,
1402 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1404 typedef typename sub_match<_Bi_iter>::string_type string_type;
1405 return __lhs.compare(string_type(1, __rhs)) == 0;
1409 * @brief Tests the inequivalence of a regular expression submatch and a
1410 * string.
1411 * @param __lhs A regular expression submatch.
1412 * @param __rhs A const string reference.
1413 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1415 template<typename _Bi_iter>
1416 inline bool
1417 operator!=(const sub_match<_Bi_iter>& __lhs,
1418 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1419 { return !(__lhs == __rhs); }
1422 * @brief Tests the ordering of a regular expression submatch and a string.
1423 * @param __lhs A regular expression submatch.
1424 * @param __rhs A const string reference.
1425 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1427 template<typename _Bi_iter>
1428 inline bool
1429 operator<(const sub_match<_Bi_iter>& __lhs,
1430 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1432 typedef typename sub_match<_Bi_iter>::string_type string_type;
1433 return __lhs.compare(string_type(1, __rhs)) < 0;
1437 * @brief Tests the ordering of a regular expression submatch and a string.
1438 * @param __lhs A regular expression submatch.
1439 * @param __rhs A const string reference.
1440 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1442 template<typename _Bi_iter>
1443 inline bool
1444 operator>(const sub_match<_Bi_iter>& __lhs,
1445 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1446 { return __rhs < __lhs; }
1449 * @brief Tests the ordering of a regular expression submatch and a string.
1450 * @param __lhs A regular expression submatch.
1451 * @param __rhs A const string reference.
1452 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1454 template<typename _Bi_iter>
1455 inline bool
1456 operator>=(const sub_match<_Bi_iter>& __lhs,
1457 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1458 { return !(__lhs < __rhs); }
1461 * @brief Tests the ordering of a regular expression submatch and a string.
1462 * @param __lhs A regular expression submatch.
1463 * @param __rhs A const string reference.
1464 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1466 template<typename _Bi_iter>
1467 inline bool
1468 operator<=(const sub_match<_Bi_iter>& __lhs,
1469 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1470 { return !(__rhs < __lhs); }
1473 * @brief Inserts a matched string into an output stream.
1475 * @param __os The output stream.
1476 * @param __m A submatch string.
1478 * @returns the output stream with the submatch string inserted.
1480 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1481 inline
1482 basic_ostream<_Ch_type, _Ch_traits>&
1483 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1484 const sub_match<_Bi_iter>& __m)
1485 { return __os << __m.str(); }
1487 // [7.10] Class template match_results
1490 * @brief The results of a match or search operation.
1492 * A collection of character sequences representing the result of a regular
1493 * expression match. Storage for the collection is allocated and freed as
1494 * necessary by the member functions of class template match_results.
1496 * This class satisfies the Sequence requirements, with the exception that
1497 * only the operations defined for a const-qualified Sequence are supported.
1499 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1500 * the whole match. In this case the %sub_match member matched is always true.
1501 * The sub_match object stored at index n denotes what matched the marked
1502 * sub-expression n within the matched expression. If the sub-expression n
1503 * participated in a regular expression match then the %sub_match member
1504 * matched evaluates to true, and members first and second denote the range
1505 * of characters [first, second) which formed that match. Otherwise matched
1506 * is false, and members first and second point to the end of the sequence
1507 * that was searched.
1509 * @nosubgrouping
1511 template<typename _Bi_iter,
1512 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1513 class match_results
1514 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1516 private:
1518 * The vector base is empty if this does not represent a match (!ready());
1519 * Otherwise if it's a match failure, it contains 3 elements:
1520 * [0] unmatched
1521 * [1] prefix
1522 * [2] suffix
1523 * Otherwise it contains n+4 elements where n is the number of marked
1524 * sub-expressions:
1525 * [0] entire match
1526 * [1] 1st marked subexpression
1527 * ...
1528 * [n] nth marked subexpression
1529 * [n+1] unmatched
1530 * [n+2] prefix
1531 * [n+3] suffix
1533 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1534 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1535 typedef regex_constants::match_flag_type match_flag_type;
1537 public:
1539 * @name 10.? Public Types
1541 //@{
1542 typedef sub_match<_Bi_iter> value_type;
1543 typedef const value_type& const_reference;
1544 typedef const_reference reference;
1545 typedef typename _Base_type::const_iterator const_iterator;
1546 typedef const_iterator iterator;
1547 typedef typename __iter_traits::difference_type difference_type;
1548 typedef typename allocator_traits<_Alloc>::size_type size_type;
1549 typedef _Alloc allocator_type;
1550 typedef typename __iter_traits::value_type char_type;
1551 typedef std::basic_string<char_type> string_type;
1552 //@}
1554 public:
1556 * @name 28.10.1 Construction, Copying, and Destruction
1558 //@{
1561 * @brief Constructs a default %match_results container.
1562 * @post size() returns 0 and str() returns an empty string.
1564 explicit
1565 match_results(const _Alloc& __a = _Alloc())
1566 : _Base_type(__a)
1570 * @brief Copy constructs a %match_results.
1572 match_results(const match_results& __rhs) = default;
1575 * @brief Move constructs a %match_results.
1577 match_results(match_results&& __rhs) noexcept = default;
1580 * @brief Assigns rhs to *this.
1582 match_results&
1583 operator=(const match_results& __rhs) = default;
1586 * @brief Move-assigns rhs to *this.
1588 match_results&
1589 operator=(match_results&& __rhs) = default;
1592 * @brief Destroys a %match_results object.
1594 ~match_results()
1597 //@}
1599 // 28.10.2, state:
1601 * @brief Indicates if the %match_results is ready.
1602 * @retval true The object has a fully-established result state.
1603 * @retval false The object is not ready.
1605 bool ready() const { return !_Base_type::empty(); }
1608 * @name 28.10.2 Size
1610 //@{
1613 * @brief Gets the number of matches and submatches.
1615 * The number of matches for a given regular expression will be either 0
1616 * if there was no match or mark_count() + 1 if a match was successful.
1617 * Some matches may be empty.
1619 * @returns the number of matches found.
1621 size_type
1622 size() const
1623 { return _Base_type::empty() ? 0 : _Base_type::size() - 3; }
1625 size_type
1626 max_size() const
1627 { return _Base_type::max_size(); }
1630 * @brief Indicates if the %match_results contains no results.
1631 * @retval true The %match_results object is empty.
1632 * @retval false The %match_results object is not empty.
1634 bool
1635 empty() const
1636 { return size() == 0; }
1638 //@}
1641 * @name 10.3 Element Access
1643 //@{
1646 * @brief Gets the length of the indicated submatch.
1647 * @param __sub indicates the submatch.
1648 * @pre ready() == true
1650 * This function returns the length of the indicated submatch, or the
1651 * length of the entire match if @p __sub is zero (the default).
1653 difference_type
1654 length(size_type __sub = 0) const
1655 { return (*this)[__sub].length(); }
1658 * @brief Gets the offset of the beginning of the indicated submatch.
1659 * @param __sub indicates the submatch.
1660 * @pre ready() == true
1662 * This function returns the offset from the beginning of the target
1663 * sequence to the beginning of the submatch, unless the value of @p __sub
1664 * is zero (the default), in which case this function returns the offset
1665 * from the beginning of the target sequence to the beginning of the
1666 * match.
1668 difference_type
1669 position(size_type __sub = 0) const
1670 { return std::distance(_M_begin, (*this)[__sub].first); }
1673 * @brief Gets the match or submatch converted to a string type.
1674 * @param __sub indicates the submatch.
1675 * @pre ready() == true
1677 * This function gets the submatch (or match, if @p __sub is
1678 * zero) extracted from the target range and converted to the
1679 * associated string type.
1681 string_type
1682 str(size_type __sub = 0) const
1683 { return string_type((*this)[__sub]); }
1686 * @brief Gets a %sub_match reference for the match or submatch.
1687 * @param __sub indicates the submatch.
1688 * @pre ready() == true
1690 * This function gets a reference to the indicated submatch, or
1691 * the entire match if @p __sub is zero.
1693 * If @p __sub >= size() then this function returns a %sub_match with a
1694 * special value indicating no submatch.
1696 const_reference
1697 operator[](size_type __sub) const
1699 __glibcxx_assert( ready() );
1700 return __sub < size()
1701 ? _Base_type::operator[](__sub)
1702 : _M_unmatched_sub();
1706 * @brief Gets a %sub_match representing the match prefix.
1707 * @pre ready() == true
1709 * This function gets a reference to a %sub_match object representing the
1710 * part of the target range between the start of the target range and the
1711 * start of the match.
1713 const_reference
1714 prefix() const
1716 __glibcxx_assert( ready() );
1717 return !empty() ? _M_prefix() : _M_unmatched_sub();
1721 * @brief Gets a %sub_match representing the match suffix.
1722 * @pre ready() == true
1724 * This function gets a reference to a %sub_match object representing the
1725 * part of the target range between the end of the match and the end of
1726 * the target range.
1728 const_reference
1729 suffix() const
1731 __glibcxx_assert( ready() );
1732 return !empty() ? _M_suffix() : _M_unmatched_sub();
1736 * @brief Gets an iterator to the start of the %sub_match collection.
1738 const_iterator
1739 begin() const
1740 { return _Base_type::begin(); }
1743 * @brief Gets an iterator to the start of the %sub_match collection.
1745 const_iterator
1746 cbegin() const
1747 { return this->begin(); }
1750 * @brief Gets an iterator to one-past-the-end of the collection.
1752 const_iterator
1753 end() const
1754 { return _Base_type::end() - 3; }
1757 * @brief Gets an iterator to one-past-the-end of the collection.
1759 const_iterator
1760 cend() const
1761 { return this->end(); }
1763 //@}
1766 * @name 10.4 Formatting
1768 * These functions perform formatted substitution of the matched
1769 * character sequences into their target. The format specifiers and
1770 * escape sequences accepted by these functions are determined by
1771 * their @p flags parameter as documented above.
1773 //@{
1776 * @pre ready() == true
1778 template<typename _Out_iter>
1779 _Out_iter
1780 format(_Out_iter __out, const char_type* __fmt_first,
1781 const char_type* __fmt_last,
1782 match_flag_type __flags = regex_constants::format_default) const;
1785 * @pre ready() == true
1787 template<typename _Out_iter, typename _St, typename _Sa>
1788 _Out_iter
1789 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1790 match_flag_type __flags = regex_constants::format_default) const
1792 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1793 __flags);
1797 * @pre ready() == true
1799 template<typename _St, typename _Sa>
1800 basic_string<char_type, _St, _Sa>
1801 format(const basic_string<char_type, _St, _Sa>& __fmt,
1802 match_flag_type __flags = regex_constants::format_default) const
1804 basic_string<char_type, _St, _Sa> __result;
1805 format(std::back_inserter(__result), __fmt, __flags);
1806 return __result;
1810 * @pre ready() == true
1812 string_type
1813 format(const char_type* __fmt,
1814 match_flag_type __flags = regex_constants::format_default) const
1816 string_type __result;
1817 format(std::back_inserter(__result),
1818 __fmt,
1819 __fmt + char_traits<char_type>::length(__fmt),
1820 __flags);
1821 return __result;
1824 //@}
1827 * @name 10.5 Allocator
1829 //@{
1832 * @brief Gets a copy of the allocator.
1834 allocator_type
1835 get_allocator() const
1836 { return _Base_type::get_allocator(); }
1838 //@}
1841 * @name 10.6 Swap
1843 //@{
1846 * @brief Swaps the contents of two match_results.
1848 void
1849 swap(match_results& __that)
1851 using std::swap;
1852 _Base_type::swap(__that);
1853 swap(_M_begin, __that._M_begin);
1855 //@}
1857 private:
1858 template<typename, typename, typename, bool>
1859 friend class __detail::_Executor;
1861 template<typename, typename, typename>
1862 friend class regex_iterator;
1864 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
1865 __detail::_RegexExecutorPolicy, bool>
1866 friend bool
1867 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
1868 const basic_regex<_Cp, _Rp>&,
1869 regex_constants::match_flag_type);
1871 void
1872 _M_resize(unsigned int __size)
1873 { _Base_type::resize(__size + 3); }
1875 const_reference
1876 _M_unmatched_sub() const
1877 { return _Base_type::operator[](_Base_type::size() - 3); }
1879 sub_match<_Bi_iter>&
1880 _M_unmatched_sub()
1881 { return _Base_type::operator[](_Base_type::size() - 3); }
1883 const_reference
1884 _M_prefix() const
1885 { return _Base_type::operator[](_Base_type::size() - 2); }
1887 sub_match<_Bi_iter>&
1888 _M_prefix()
1889 { return _Base_type::operator[](_Base_type::size() - 2); }
1891 const_reference
1892 _M_suffix() const
1893 { return _Base_type::operator[](_Base_type::size() - 1); }
1895 sub_match<_Bi_iter>&
1896 _M_suffix()
1897 { return _Base_type::operator[](_Base_type::size() - 1); }
1899 _Bi_iter _M_begin;
1902 typedef match_results<const char*> cmatch;
1903 typedef match_results<string::const_iterator> smatch;
1904 #ifdef _GLIBCXX_USE_WCHAR_T
1905 typedef match_results<const wchar_t*> wcmatch;
1906 typedef match_results<wstring::const_iterator> wsmatch;
1907 #endif
1909 // match_results comparisons
1911 * @brief Compares two match_results for equality.
1912 * @returns true if the two objects refer to the same match,
1913 * false otherwise.
1915 template<typename _Bi_iter, typename _Alloc>
1916 inline bool
1917 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
1918 const match_results<_Bi_iter, _Alloc>& __m2)
1920 if (__m1.ready() != __m2.ready())
1921 return false;
1922 if (!__m1.ready()) // both are not ready
1923 return true;
1924 if (__m1.empty() != __m2.empty())
1925 return false;
1926 if (__m1.empty()) // both are empty
1927 return true;
1928 return __m1.prefix() == __m2.prefix()
1929 && __m1.size() == __m2.size()
1930 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1931 && __m1.suffix() == __m2.suffix();
1935 * @brief Compares two match_results for inequality.
1936 * @returns true if the two objects do not refer to the same match,
1937 * false otherwise.
1939 template<typename _Bi_iter, class _Alloc>
1940 inline bool
1941 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
1942 const match_results<_Bi_iter, _Alloc>& __m2)
1943 { return !(__m1 == __m2); }
1945 // [7.10.6] match_results swap
1947 * @brief Swaps two match results.
1948 * @param __lhs A match result.
1949 * @param __rhs A match result.
1951 * The contents of the two match_results objects are swapped.
1953 template<typename _Bi_iter, typename _Alloc>
1954 inline void
1955 swap(match_results<_Bi_iter, _Alloc>& __lhs,
1956 match_results<_Bi_iter, _Alloc>& __rhs)
1957 { __lhs.swap(__rhs); }
1959 _GLIBCXX_END_NAMESPACE_CXX11
1961 // [7.11.2] Function template regex_match
1963 * @name Matching, Searching, and Replacing
1965 //@{
1968 * @brief Determines if there is a match between the regular expression @p e
1969 * and all of the character sequence [first, last).
1971 * @param __s Start of the character sequence to match.
1972 * @param __e One-past-the-end of the character sequence to match.
1973 * @param __m The match results.
1974 * @param __re The regular expression.
1975 * @param __flags Controls how the regular expression is matched.
1977 * @retval true A match exists.
1978 * @retval false Otherwise.
1980 * @throws an exception of type regex_error.
1982 template<typename _Bi_iter, typename _Alloc,
1983 typename _Ch_type, typename _Rx_traits>
1984 inline bool
1985 regex_match(_Bi_iter __s,
1986 _Bi_iter __e,
1987 match_results<_Bi_iter, _Alloc>& __m,
1988 const basic_regex<_Ch_type, _Rx_traits>& __re,
1989 regex_constants::match_flag_type __flags
1990 = regex_constants::match_default)
1992 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
1993 __detail::_RegexExecutorPolicy::_S_auto, true>
1994 (__s, __e, __m, __re, __flags);
1998 * @brief Indicates if there is a match between the regular expression @p e
1999 * and all of the character sequence [first, last).
2001 * @param __first Beginning of the character sequence to match.
2002 * @param __last One-past-the-end of the character sequence to match.
2003 * @param __re The regular expression.
2004 * @param __flags Controls how the regular expression is matched.
2006 * @retval true A match exists.
2007 * @retval false Otherwise.
2009 * @throws an exception of type regex_error.
2011 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2012 inline bool
2013 regex_match(_Bi_iter __first, _Bi_iter __last,
2014 const basic_regex<_Ch_type, _Rx_traits>& __re,
2015 regex_constants::match_flag_type __flags
2016 = regex_constants::match_default)
2018 match_results<_Bi_iter> __what;
2019 return regex_match(__first, __last, __what, __re, __flags);
2023 * @brief Determines if there is a match between the regular expression @p e
2024 * and a C-style null-terminated string.
2026 * @param __s The C-style null-terminated string to match.
2027 * @param __m The match results.
2028 * @param __re The regular expression.
2029 * @param __f Controls how the regular expression is matched.
2031 * @retval true A match exists.
2032 * @retval false Otherwise.
2034 * @throws an exception of type regex_error.
2036 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2037 inline bool
2038 regex_match(const _Ch_type* __s,
2039 match_results<const _Ch_type*, _Alloc>& __m,
2040 const basic_regex<_Ch_type, _Rx_traits>& __re,
2041 regex_constants::match_flag_type __f
2042 = regex_constants::match_default)
2043 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2046 * @brief Determines if there is a match between the regular expression @p e
2047 * and a string.
2049 * @param __s The string to match.
2050 * @param __m The match results.
2051 * @param __re The regular expression.
2052 * @param __flags Controls how the regular expression is matched.
2054 * @retval true A match exists.
2055 * @retval false Otherwise.
2057 * @throws an exception of type regex_error.
2059 template<typename _Ch_traits, typename _Ch_alloc,
2060 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2061 inline bool
2062 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2063 match_results<typename basic_string<_Ch_type,
2064 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2065 const basic_regex<_Ch_type, _Rx_traits>& __re,
2066 regex_constants::match_flag_type __flags
2067 = regex_constants::match_default)
2068 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2070 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2071 // 2329. regex_match() with match_results should forbid temporary strings
2072 /// Prevent unsafe attempts to get match_results from a temporary string.
2073 template<typename _Ch_traits, typename _Ch_alloc,
2074 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2075 bool
2076 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2077 match_results<typename basic_string<_Ch_type,
2078 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2079 const basic_regex<_Ch_type, _Rx_traits>&,
2080 regex_constants::match_flag_type
2081 = regex_constants::match_default) = delete;
2084 * @brief Indicates if there is a match between the regular expression @p e
2085 * and a C-style null-terminated string.
2087 * @param __s The C-style null-terminated string to match.
2088 * @param __re The regular expression.
2089 * @param __f Controls how the regular expression is matched.
2091 * @retval true A match exists.
2092 * @retval false Otherwise.
2094 * @throws an exception of type regex_error.
2096 template<typename _Ch_type, class _Rx_traits>
2097 inline bool
2098 regex_match(const _Ch_type* __s,
2099 const basic_regex<_Ch_type, _Rx_traits>& __re,
2100 regex_constants::match_flag_type __f
2101 = regex_constants::match_default)
2102 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2105 * @brief Indicates if there is a match between the regular expression @p e
2106 * and a string.
2108 * @param __s [IN] The string to match.
2109 * @param __re [IN] The regular expression.
2110 * @param __flags [IN] Controls how the regular expression is matched.
2112 * @retval true A match exists.
2113 * @retval false Otherwise.
2115 * @throws an exception of type regex_error.
2117 template<typename _Ch_traits, typename _Str_allocator,
2118 typename _Ch_type, typename _Rx_traits>
2119 inline bool
2120 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2121 const basic_regex<_Ch_type, _Rx_traits>& __re,
2122 regex_constants::match_flag_type __flags
2123 = regex_constants::match_default)
2124 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2126 // [7.11.3] Function template regex_search
2128 * Searches for a regular expression within a range.
2129 * @param __s [IN] The start of the string to search.
2130 * @param __e [IN] One-past-the-end of the string to search.
2131 * @param __m [OUT] The match results.
2132 * @param __re [IN] The regular expression to search for.
2133 * @param __flags [IN] Search policy flags.
2134 * @retval true A match was found within the string.
2135 * @retval false No match was found within the string, the content of %m is
2136 * undefined.
2138 * @throws an exception of type regex_error.
2140 template<typename _Bi_iter, typename _Alloc,
2141 typename _Ch_type, typename _Rx_traits>
2142 inline bool
2143 regex_search(_Bi_iter __s, _Bi_iter __e,
2144 match_results<_Bi_iter, _Alloc>& __m,
2145 const basic_regex<_Ch_type, _Rx_traits>& __re,
2146 regex_constants::match_flag_type __flags
2147 = regex_constants::match_default)
2149 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2150 __detail::_RegexExecutorPolicy::_S_auto, false>
2151 (__s, __e, __m, __re, __flags);
2155 * Searches for a regular expression within a range.
2156 * @param __first [IN] The start of the string to search.
2157 * @param __last [IN] One-past-the-end of the string to search.
2158 * @param __re [IN] The regular expression to search for.
2159 * @param __flags [IN] Search policy flags.
2160 * @retval true A match was found within the string.
2161 * @retval false No match was found within the string.
2163 * @throws an exception of type regex_error.
2165 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2166 inline bool
2167 regex_search(_Bi_iter __first, _Bi_iter __last,
2168 const basic_regex<_Ch_type, _Rx_traits>& __re,
2169 regex_constants::match_flag_type __flags
2170 = regex_constants::match_default)
2172 match_results<_Bi_iter> __what;
2173 return regex_search(__first, __last, __what, __re, __flags);
2177 * @brief Searches for a regular expression within a C-string.
2178 * @param __s [IN] A C-string to search for the regex.
2179 * @param __m [OUT] The set of regex matches.
2180 * @param __e [IN] The regex to search for in @p s.
2181 * @param __f [IN] The search flags.
2182 * @retval true A match was found within the string.
2183 * @retval false No match was found within the string, the content of %m is
2184 * undefined.
2186 * @throws an exception of type regex_error.
2188 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2189 inline bool
2190 regex_search(const _Ch_type* __s,
2191 match_results<const _Ch_type*, _Alloc>& __m,
2192 const basic_regex<_Ch_type, _Rx_traits>& __e,
2193 regex_constants::match_flag_type __f
2194 = regex_constants::match_default)
2195 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2198 * @brief Searches for a regular expression within a C-string.
2199 * @param __s [IN] The C-string to search.
2200 * @param __e [IN] The regular expression to search for.
2201 * @param __f [IN] Search policy flags.
2202 * @retval true A match was found within the string.
2203 * @retval false No match was found within the string.
2205 * @throws an exception of type regex_error.
2207 template<typename _Ch_type, typename _Rx_traits>
2208 inline bool
2209 regex_search(const _Ch_type* __s,
2210 const basic_regex<_Ch_type, _Rx_traits>& __e,
2211 regex_constants::match_flag_type __f
2212 = regex_constants::match_default)
2213 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2216 * @brief Searches for a regular expression within a string.
2217 * @param __s [IN] The string to search.
2218 * @param __e [IN] The regular expression to search for.
2219 * @param __flags [IN] Search policy flags.
2220 * @retval true A match was found within the string.
2221 * @retval false No match was found within the string.
2223 * @throws an exception of type regex_error.
2225 template<typename _Ch_traits, typename _String_allocator,
2226 typename _Ch_type, typename _Rx_traits>
2227 inline bool
2228 regex_search(const basic_string<_Ch_type, _Ch_traits,
2229 _String_allocator>& __s,
2230 const basic_regex<_Ch_type, _Rx_traits>& __e,
2231 regex_constants::match_flag_type __flags
2232 = regex_constants::match_default)
2233 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2236 * @brief Searches for a regular expression within a string.
2237 * @param __s [IN] A C++ string to search for the regex.
2238 * @param __m [OUT] The set of regex matches.
2239 * @param __e [IN] The regex to search for in @p s.
2240 * @param __f [IN] The search flags.
2241 * @retval true A match was found within the string.
2242 * @retval false No match was found within the string, the content of %m is
2243 * undefined.
2245 * @throws an exception of type regex_error.
2247 template<typename _Ch_traits, typename _Ch_alloc,
2248 typename _Alloc, typename _Ch_type,
2249 typename _Rx_traits>
2250 inline bool
2251 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2252 match_results<typename basic_string<_Ch_type,
2253 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2254 const basic_regex<_Ch_type, _Rx_traits>& __e,
2255 regex_constants::match_flag_type __f
2256 = regex_constants::match_default)
2257 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2260 // 2329. regex_search() with match_results should forbid temporary strings
2261 /// Prevent unsafe attempts to get match_results from a temporary string.
2262 template<typename _Ch_traits, typename _Ch_alloc,
2263 typename _Alloc, typename _Ch_type,
2264 typename _Rx_traits>
2265 bool
2266 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2267 match_results<typename basic_string<_Ch_type,
2268 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2269 const basic_regex<_Ch_type, _Rx_traits>&,
2270 regex_constants::match_flag_type
2271 = regex_constants::match_default) = delete;
2273 // std [28.11.4] Function template regex_replace
2275 * @brief Search for a regular expression within a range for multiple times,
2276 and replace the matched parts through filling a format string.
2277 * @param __out [OUT] The output iterator.
2278 * @param __first [IN] The start of the string to search.
2279 * @param __last [IN] One-past-the-end of the string to search.
2280 * @param __e [IN] The regular expression to search for.
2281 * @param __fmt [IN] The format string.
2282 * @param __flags [IN] Search and replace policy flags.
2284 * @returns __out
2285 * @throws an exception of type regex_error.
2287 template<typename _Out_iter, typename _Bi_iter,
2288 typename _Rx_traits, typename _Ch_type,
2289 typename _St, typename _Sa>
2290 inline _Out_iter
2291 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2292 const basic_regex<_Ch_type, _Rx_traits>& __e,
2293 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2294 regex_constants::match_flag_type __flags
2295 = regex_constants::match_default)
2297 return regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
2301 * @brief Search for a regular expression within a range for multiple times,
2302 and replace the matched parts through filling a format C-string.
2303 * @param __out [OUT] The output iterator.
2304 * @param __first [IN] The start of the string to search.
2305 * @param __last [IN] One-past-the-end of the string to search.
2306 * @param __e [IN] The regular expression to search for.
2307 * @param __fmt [IN] The format C-string.
2308 * @param __flags [IN] Search and replace policy flags.
2310 * @returns __out
2311 * @throws an exception of type regex_error.
2313 template<typename _Out_iter, typename _Bi_iter,
2314 typename _Rx_traits, typename _Ch_type>
2315 _Out_iter
2316 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2317 const basic_regex<_Ch_type, _Rx_traits>& __e,
2318 const _Ch_type* __fmt,
2319 regex_constants::match_flag_type __flags
2320 = regex_constants::match_default);
2323 * @brief Search for a regular expression within a string for multiple times,
2324 and replace the matched parts through filling a format string.
2325 * @param __s [IN] The string to search and replace.
2326 * @param __e [IN] The regular expression to search for.
2327 * @param __fmt [IN] The format string.
2328 * @param __flags [IN] Search and replace policy flags.
2330 * @returns The string after replacing.
2331 * @throws an exception of type regex_error.
2333 template<typename _Rx_traits, typename _Ch_type,
2334 typename _St, typename _Sa, typename _Fst, typename _Fsa>
2335 inline basic_string<_Ch_type, _St, _Sa>
2336 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2337 const basic_regex<_Ch_type, _Rx_traits>& __e,
2338 const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2339 regex_constants::match_flag_type __flags
2340 = regex_constants::match_default)
2342 basic_string<_Ch_type, _St, _Sa> __result;
2343 regex_replace(std::back_inserter(__result),
2344 __s.begin(), __s.end(), __e, __fmt, __flags);
2345 return __result;
2349 * @brief Search for a regular expression within a string for multiple times,
2350 and replace the matched parts through filling a format C-string.
2351 * @param __s [IN] The string to search and replace.
2352 * @param __e [IN] The regular expression to search for.
2353 * @param __fmt [IN] The format C-string.
2354 * @param __flags [IN] Search and replace policy flags.
2356 * @returns The string after replacing.
2357 * @throws an exception of type regex_error.
2359 template<typename _Rx_traits, typename _Ch_type,
2360 typename _St, typename _Sa>
2361 inline basic_string<_Ch_type, _St, _Sa>
2362 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2363 const basic_regex<_Ch_type, _Rx_traits>& __e,
2364 const _Ch_type* __fmt,
2365 regex_constants::match_flag_type __flags
2366 = regex_constants::match_default)
2368 basic_string<_Ch_type, _St, _Sa> __result;
2369 regex_replace(std::back_inserter(__result),
2370 __s.begin(), __s.end(), __e, __fmt, __flags);
2371 return __result;
2375 * @brief Search for a regular expression within a C-string for multiple
2376 times, and replace the matched parts through filling a format string.
2377 * @param __s [IN] The C-string to search and replace.
2378 * @param __e [IN] The regular expression to search for.
2379 * @param __fmt [IN] The format string.
2380 * @param __flags [IN] Search and replace policy flags.
2382 * @returns The string after replacing.
2383 * @throws an exception of type regex_error.
2385 template<typename _Rx_traits, typename _Ch_type,
2386 typename _St, typename _Sa>
2387 inline basic_string<_Ch_type>
2388 regex_replace(const _Ch_type* __s,
2389 const basic_regex<_Ch_type, _Rx_traits>& __e,
2390 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2391 regex_constants::match_flag_type __flags
2392 = regex_constants::match_default)
2394 basic_string<_Ch_type> __result;
2395 regex_replace(std::back_inserter(__result), __s,
2396 __s + char_traits<_Ch_type>::length(__s),
2397 __e, __fmt, __flags);
2398 return __result;
2402 * @brief Search for a regular expression within a C-string for multiple
2403 times, and replace the matched parts through filling a format C-string.
2404 * @param __s [IN] The C-string to search and replace.
2405 * @param __e [IN] The regular expression to search for.
2406 * @param __fmt [IN] The format C-string.
2407 * @param __flags [IN] Search and replace policy flags.
2409 * @returns The string after replacing.
2410 * @throws an exception of type regex_error.
2412 template<typename _Rx_traits, typename _Ch_type>
2413 inline basic_string<_Ch_type>
2414 regex_replace(const _Ch_type* __s,
2415 const basic_regex<_Ch_type, _Rx_traits>& __e,
2416 const _Ch_type* __fmt,
2417 regex_constants::match_flag_type __flags
2418 = regex_constants::match_default)
2420 basic_string<_Ch_type> __result;
2421 regex_replace(std::back_inserter(__result), __s,
2422 __s + char_traits<_Ch_type>::length(__s),
2423 __e, __fmt, __flags);
2424 return __result;
2427 //@}
2429 _GLIBCXX_BEGIN_NAMESPACE_CXX11
2431 // std [28.12] Class template regex_iterator
2433 * An iterator adaptor that will provide repeated calls of regex_search over
2434 * a range until no more matches remain.
2436 template<typename _Bi_iter,
2437 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2438 typename _Rx_traits = regex_traits<_Ch_type> >
2439 class regex_iterator
2441 public:
2442 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2443 typedef match_results<_Bi_iter> value_type;
2444 typedef std::ptrdiff_t difference_type;
2445 typedef const value_type* pointer;
2446 typedef const value_type& reference;
2447 typedef std::forward_iterator_tag iterator_category;
2450 * @brief Provides a singular iterator, useful for indicating
2451 * one-past-the-end of a range.
2453 regex_iterator()
2454 : _M_pregex()
2458 * Constructs a %regex_iterator...
2459 * @param __a [IN] The start of a text range to search.
2460 * @param __b [IN] One-past-the-end of the text range to search.
2461 * @param __re [IN] The regular expression to match.
2462 * @param __m [IN] Policy flags for match rules.
2464 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2465 regex_constants::match_flag_type __m
2466 = regex_constants::match_default)
2467 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2469 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2470 *this = regex_iterator();
2473 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2474 // 2332. regex_iterator should forbid temporary regexes
2475 regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2476 regex_constants::match_flag_type
2477 = regex_constants::match_default) = delete;
2479 * Copy constructs a %regex_iterator.
2481 regex_iterator(const regex_iterator& __rhs) = default;
2484 * @brief Assigns one %regex_iterator to another.
2486 regex_iterator&
2487 operator=(const regex_iterator& __rhs) = default;
2490 * @brief Tests the equivalence of two regex iterators.
2492 bool
2493 operator==(const regex_iterator& __rhs) const;
2496 * @brief Tests the inequivalence of two regex iterators.
2498 bool
2499 operator!=(const regex_iterator& __rhs) const
2500 { return !(*this == __rhs); }
2503 * @brief Dereferences a %regex_iterator.
2505 const value_type&
2506 operator*() const
2507 { return _M_match; }
2510 * @brief Selects a %regex_iterator member.
2512 const value_type*
2513 operator->() const
2514 { return &_M_match; }
2517 * @brief Increments a %regex_iterator.
2519 regex_iterator&
2520 operator++();
2523 * @brief Postincrements a %regex_iterator.
2525 regex_iterator
2526 operator++(int)
2528 auto __tmp = *this;
2529 ++(*this);
2530 return __tmp;
2533 private:
2534 _Bi_iter _M_begin;
2535 _Bi_iter _M_end;
2536 const regex_type* _M_pregex;
2537 regex_constants::match_flag_type _M_flags;
2538 match_results<_Bi_iter> _M_match;
2541 typedef regex_iterator<const char*> cregex_iterator;
2542 typedef regex_iterator<string::const_iterator> sregex_iterator;
2543 #ifdef _GLIBCXX_USE_WCHAR_T
2544 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2545 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2546 #endif
2548 // [7.12.2] Class template regex_token_iterator
2550 * Iterates over submatches in a range (or @a splits a text string).
2552 * The purpose of this iterator is to enumerate all, or all specified,
2553 * matches of a regular expression within a text range. The dereferenced
2554 * value of an iterator of this class is a std::sub_match object.
2556 template<typename _Bi_iter,
2557 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2558 typename _Rx_traits = regex_traits<_Ch_type> >
2559 class regex_token_iterator
2561 public:
2562 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2563 typedef sub_match<_Bi_iter> value_type;
2564 typedef std::ptrdiff_t difference_type;
2565 typedef const value_type* pointer;
2566 typedef const value_type& reference;
2567 typedef std::forward_iterator_tag iterator_category;
2569 public:
2571 * @brief Default constructs a %regex_token_iterator.
2573 * A default-constructed %regex_token_iterator is a singular iterator
2574 * that will compare equal to the one-past-the-end value for any
2575 * iterator of the same type.
2577 regex_token_iterator()
2578 : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2579 _M_has_m1(false)
2583 * Constructs a %regex_token_iterator...
2584 * @param __a [IN] The start of the text to search.
2585 * @param __b [IN] One-past-the-end of the text to search.
2586 * @param __re [IN] The regular expression to search for.
2587 * @param __submatch [IN] Which submatch to return. There are some
2588 * special values for this parameter:
2589 * - -1 each enumerated subexpression does NOT
2590 * match the regular expression (aka field
2591 * splitting)
2592 * - 0 the entire string matching the
2593 * subexpression is returned for each match
2594 * within the text.
2595 * - >0 enumerates only the indicated
2596 * subexpression from a match within the text.
2597 * @param __m [IN] Policy flags for match rules.
2599 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2600 int __submatch = 0,
2601 regex_constants::match_flag_type __m
2602 = regex_constants::match_default)
2603 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2604 { _M_init(__a, __b); }
2607 * Constructs a %regex_token_iterator...
2608 * @param __a [IN] The start of the text to search.
2609 * @param __b [IN] One-past-the-end of the text to search.
2610 * @param __re [IN] The regular expression to search for.
2611 * @param __submatches [IN] A list of subexpressions to return for each
2612 * regular expression match within the text.
2613 * @param __m [IN] Policy flags for match rules.
2615 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2616 const regex_type& __re,
2617 const std::vector<int>& __submatches,
2618 regex_constants::match_flag_type __m
2619 = regex_constants::match_default)
2620 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2621 { _M_init(__a, __b); }
2624 * Constructs a %regex_token_iterator...
2625 * @param __a [IN] The start of the text to search.
2626 * @param __b [IN] One-past-the-end of the text to search.
2627 * @param __re [IN] The regular expression to search for.
2628 * @param __submatches [IN] A list of subexpressions to return for each
2629 * regular expression match within the text.
2630 * @param __m [IN] Policy flags for match rules.
2632 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2633 const regex_type& __re,
2634 initializer_list<int> __submatches,
2635 regex_constants::match_flag_type __m
2636 = regex_constants::match_default)
2637 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2638 { _M_init(__a, __b); }
2641 * Constructs a %regex_token_iterator...
2642 * @param __a [IN] The start of the text to search.
2643 * @param __b [IN] One-past-the-end of the text to search.
2644 * @param __re [IN] The regular expression to search for.
2645 * @param __submatches [IN] A list of subexpressions to return for each
2646 * regular expression match within the text.
2647 * @param __m [IN] Policy flags for match rules.
2649 template<std::size_t _Nm>
2650 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2651 const regex_type& __re,
2652 const int (&__submatches)[_Nm],
2653 regex_constants::match_flag_type __m
2654 = regex_constants::match_default)
2655 : _M_position(__a, __b, __re, __m),
2656 _M_subs(__submatches, __submatches + _Nm), _M_n(0)
2657 { _M_init(__a, __b); }
2659 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2660 // 2332. regex_token_iterator should forbid temporary regexes
2661 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
2662 regex_constants::match_flag_type =
2663 regex_constants::match_default) = delete;
2664 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2665 const std::vector<int>&,
2666 regex_constants::match_flag_type =
2667 regex_constants::match_default) = delete;
2668 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2669 initializer_list<int>,
2670 regex_constants::match_flag_type =
2671 regex_constants::match_default) = delete;
2672 template <std::size_t _Nm>
2673 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2674 const int (&)[_Nm],
2675 regex_constants::match_flag_type =
2676 regex_constants::match_default) = delete;
2679 * @brief Copy constructs a %regex_token_iterator.
2680 * @param __rhs [IN] A %regex_token_iterator to copy.
2682 regex_token_iterator(const regex_token_iterator& __rhs)
2683 : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2684 _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_has_m1(__rhs._M_has_m1)
2685 { _M_normalize_result(); }
2688 * @brief Assigns a %regex_token_iterator to another.
2689 * @param __rhs [IN] A %regex_token_iterator to copy.
2691 regex_token_iterator&
2692 operator=(const regex_token_iterator& __rhs);
2695 * @brief Compares a %regex_token_iterator to another for equality.
2697 bool
2698 operator==(const regex_token_iterator& __rhs) const;
2701 * @brief Compares a %regex_token_iterator to another for inequality.
2703 bool
2704 operator!=(const regex_token_iterator& __rhs) const
2705 { return !(*this == __rhs); }
2708 * @brief Dereferences a %regex_token_iterator.
2710 const value_type&
2711 operator*() const
2712 { return *_M_result; }
2715 * @brief Selects a %regex_token_iterator member.
2717 const value_type*
2718 operator->() const
2719 { return _M_result; }
2722 * @brief Increments a %regex_token_iterator.
2724 regex_token_iterator&
2725 operator++();
2728 * @brief Postincrements a %regex_token_iterator.
2730 regex_token_iterator
2731 operator++(int)
2733 auto __tmp = *this;
2734 ++(*this);
2735 return __tmp;
2738 private:
2739 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2741 void
2742 _M_init(_Bi_iter __a, _Bi_iter __b);
2744 const value_type&
2745 _M_current_match() const
2747 if (_M_subs[_M_n] == -1)
2748 return (*_M_position).prefix();
2749 else
2750 return (*_M_position)[_M_subs[_M_n]];
2753 constexpr bool
2754 _M_end_of_seq() const
2755 { return _M_result == nullptr; }
2757 // [28.12.2.2.4]
2758 void
2759 _M_normalize_result()
2761 if (_M_position != _Position())
2762 _M_result = &_M_current_match();
2763 else if (_M_has_m1)
2764 _M_result = &_M_suffix;
2765 else
2766 _M_result = nullptr;
2769 _Position _M_position;
2770 std::vector<int> _M_subs;
2771 value_type _M_suffix;
2772 std::size_t _M_n;
2773 const value_type* _M_result;
2775 // Show whether _M_subs contains -1
2776 bool _M_has_m1;
2779 /** @brief Token iterator for C-style NULL-terminated strings. */
2780 typedef regex_token_iterator<const char*> cregex_token_iterator;
2782 /** @brief Token iterator for standard strings. */
2783 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2785 #ifdef _GLIBCXX_USE_WCHAR_T
2786 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2787 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2789 /** @brief Token iterator for standard wide-character strings. */
2790 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2791 #endif
2793 //@} // group regex
2795 _GLIBCXX_END_NAMESPACE_CXX11
2796 _GLIBCXX_END_NAMESPACE_VERSION
2797 } // namespace
2799 #include <bits/regex.tcc>