1 // class template regex -*- C++ -*-
2 
3 // Copyright (C) 2010-2022 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.
10 
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.
15 
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.
19 
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/>.
24 
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}
29  */
30 
31 #if __cplusplus >= 202002L
32 # include <bits/iterator_concepts.h>	// std::default_sentinel_t
33 #endif
34 
_GLIBCXX_VISIBILITY(default)35 namespace std _GLIBCXX_VISIBILITY(default)
36 {
37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 _GLIBCXX_BEGIN_NAMESPACE_CXX11
39   template<typename, typename>
40     class basic_regex;
41 
42   template<typename _Bi_iter, typename _Alloc>
43     class match_results;
44 
45 _GLIBCXX_END_NAMESPACE_CXX11
46 
47 namespace __detail
48 {
49   enum class _RegexExecutorPolicy : int { _S_auto, _S_alternate };
50 
51   template<typename _BiIter, typename _Alloc,
52 	   typename _CharT, typename _TraitsT>
53     bool
54     __regex_algo_impl(_BiIter __s, _BiIter __e,
55 		      match_results<_BiIter, _Alloc>&      __m,
56 		      const basic_regex<_CharT, _TraitsT>& __re,
57 		      regex_constants::match_flag_type     __flags,
58 		      _RegexExecutorPolicy		   __policy,
59 		      bool				   __match_mode);
60 
61   template<typename, typename, typename, bool>
62     class _Executor;
63 
64   template<typename _Tp>
65     struct __is_contiguous_iter : false_type { };
66 
67   template<typename _Tp>
68     struct __is_contiguous_iter<_Tp*> : true_type { };
69 
70   template<typename _Tp, typename _Cont>
71     struct __is_contiguous_iter<__gnu_cxx::__normal_iterator<_Tp*, _Cont>>
72     : true_type { };
73 }
74 
75 _GLIBCXX_BEGIN_NAMESPACE_CXX11
76 
77   /**
78    * @addtogroup regex
79    * @{
80    */
81 
82   /**
83    * @brief Describes aspects of a regular expression.
84    *
85    * A regular expression traits class that satisfies the requirements of
86    * section [28.7].
87    *
88    * The class %regex is parameterized around a set of related types and
89    * functions used to complete the definition of its semantics.  This class
90    * satisfies the requirements of such a traits class.
91    */
92   template<typename _Ch_type>
93     class regex_traits
94     {
95     public:
96       typedef _Ch_type				char_type;
97       typedef std::basic_string<char_type>	string_type;
98       typedef std::locale			locale_type;
99 
100     private:
101       struct _RegexMask
102 	{
103 	  typedef std::ctype_base::mask _BaseType;
104 	  _BaseType _M_base;
105 	  unsigned char _M_extended;
106 	  static constexpr unsigned char _S_under = 1 << 0;
107 	  static constexpr unsigned char _S_valid_mask = 0x1;
108 
109 	  constexpr _RegexMask(_BaseType __base = 0,
110 			       unsigned char __extended = 0)
111 	  : _M_base(__base), _M_extended(__extended)
112 	  { }
113 
114 	  constexpr _RegexMask
115 	  operator&(_RegexMask __other) const
116 	  {
117 	    return _RegexMask(_M_base & __other._M_base,
118 			      _M_extended & __other._M_extended);
119 	  }
120 
121 	  constexpr _RegexMask
122 	  operator|(_RegexMask __other) const
123 	  {
124 	    return _RegexMask(_M_base | __other._M_base,
125 			      _M_extended | __other._M_extended);
126 	  }
127 
128 	  constexpr _RegexMask
129 	  operator^(_RegexMask __other) const
130 	  {
131 	    return _RegexMask(_M_base ^ __other._M_base,
132 			      _M_extended ^ __other._M_extended);
133 	  }
134 
135 	  constexpr _RegexMask
136 	  operator~() const
137 	  { return _RegexMask(~_M_base, ~_M_extended); }
138 
139 	  _RegexMask&
140 	  operator&=(_RegexMask __other)
141 	  { return *this = (*this) & __other; }
142 
143 	  _RegexMask&
144 	  operator|=(_RegexMask __other)
145 	  { return *this = (*this) | __other; }
146 
147 	  _RegexMask&
148 	  operator^=(_RegexMask __other)
149 	  { return *this = (*this) ^ __other; }
150 
151 	  constexpr bool
152 	  operator==(_RegexMask __other) const
153 	  {
154 	    return (_M_extended & _S_valid_mask)
155 		   == (__other._M_extended & _S_valid_mask)
156 		     && _M_base == __other._M_base;
157 	  }
158 
159 #if __cpp_impl_three_way_comparison < 201907L
160 	  constexpr bool
161 	  operator!=(_RegexMask __other) const
162 	  { return !((*this) == __other); }
163 #endif
164 	};
165 
166     public:
167       typedef _RegexMask char_class_type;
168 
169     public:
170       /**
171        * @brief Constructs a default traits object.
172        */
173       regex_traits() { }
174 
175       /**
176        * @brief Gives the length of a C-style string starting at @p __p.
177        *
178        * @param __p a pointer to the start of a character sequence.
179        *
180        * @returns the number of characters between @p *__p and the first
181        * default-initialized value of type @p char_type.  In other words, uses
182        * the C-string algorithm for determining the length of a sequence of
183        * characters.
184        */
185       static std::size_t
186       length(const char_type* __p)
187       { return string_type::traits_type::length(__p); }
188 
189       /**
190        * @brief Performs the identity translation.
191        *
192        * @param __c A character to the locale-specific character set.
193        *
194        * @returns __c.
195        */
196       char_type
197       translate(char_type __c) const
198       { return __c; }
199 
200       /**
201        * @brief Translates a character into a case-insensitive equivalent.
202        *
203        * @param __c A character to the locale-specific character set.
204        *
205        * @returns the locale-specific lower-case equivalent of __c.
206        * @throws std::bad_cast if the imbued locale does not support the ctype
207        *         facet.
208        */
209       char_type
210       translate_nocase(char_type __c) const
211       {
212 	typedef std::ctype<char_type> __ctype_type;
213 	const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
214 	return __fctyp.tolower(__c);
215       }
216 
217       /**
218        * @brief Gets a sort key for a character sequence.
219        *
220        * @param __first beginning of the character sequence.
221        * @param __last  one-past-the-end of the character sequence.
222        *
223        * Returns a sort key for the character sequence designated by the
224        * iterator range [F1, F2) such that if the character sequence [G1, G2)
225        * sorts before the character sequence [H1, H2) then
226        * v.transform(G1, G2) < v.transform(H1, H2).
227        *
228        * What this really does is provide a more efficient way to compare a
229        * string to multiple other strings in locales with fancy collation
230        * rules and equivalence classes.
231        *
232        * @returns a locale-specific sort key equivalent to the input range.
233        *
234        * @throws std::bad_cast if the current locale does not have a collate
235        *         facet.
236        */
237       template<typename _Fwd_iter>
238 	string_type
239 	transform(_Fwd_iter __first, _Fwd_iter __last) const
240 	{
241 	  typedef std::collate<char_type> __collate_type;
242 	  const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
243 	  string_type __s(__first, __last);
244 	  return __fclt.transform(__s.data(), __s.data() + __s.size());
245 	}
246 
247       /**
248        * @brief Gets a sort key for a character sequence, independent of case.
249        *
250        * @param __first beginning of the character sequence.
251        * @param __last  one-past-the-end of the character sequence.
252        *
253        * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
254        * typeid(collate_byname<_Ch_type>) and the form of the sort key
255        * returned by collate_byname<_Ch_type>::transform(__first, __last)
256        * is known and can be converted into a primary sort key
257        * then returns that key, otherwise returns an empty string.
258        *
259        * @todo Implement this function correctly.
260        */
261       template<typename _Fwd_iter>
262 	string_type
263 	transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
264 	{
265 	  // TODO : this is not entirely correct.
266 	  // This function requires extra support from the platform.
267 	  //
268 	  // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
269 	  // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
270 	  // for details.
271 	  typedef std::ctype<char_type> __ctype_type;
272 	  const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
273 	  _GLIBCXX_STD_C::vector<char_type> __s(__first, __last);
274 	  __fctyp.tolower(__s.data(), __s.data() + __s.size());
275 	  return this->transform(__s.data(), __s.data() + __s.size());
276 	}
277 
278       /**
279        * @brief Gets a collation element by name.
280        *
281        * @param __first beginning of the collation element name.
282        * @param __last  one-past-the-end of the collation element name.
283        *
284        * @returns a sequence of one or more characters that represents the
285        * collating element consisting of the character sequence designated by
286        * the iterator range [__first, __last). Returns an empty string if the
287        * character sequence is not a valid collating element.
288        */
289       template<typename _Fwd_iter>
290 	string_type
291 	lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
292 
293       /**
294        * @brief Maps one or more characters to a named character
295        *        classification.
296        *
297        * @param __first beginning of the character sequence.
298        * @param __last  one-past-the-end of the character sequence.
299        * @param __icase ignores the case of the classification name.
300        *
301        * @returns an unspecified value that represents the character
302        * classification named by the character sequence designated by
303        * the iterator range [__first, __last). If @p icase is true,
304        * the returned mask identifies the classification regardless of
305        * the case of the characters to be matched (for example,
306        * [[:lower:]] is the same as [[:alpha:]]), otherwise a
307        * case-dependent classification is returned.  The value
308        * returned shall be independent of the case of the characters
309        * in the character sequence. If the name is not recognized then
310        * returns a value that compares equal to 0.
311        *
312        * At least the following names (or their wide-character equivalent) are
313        * supported.
314        * - d
315        * - w
316        * - s
317        * - alnum
318        * - alpha
319        * - blank
320        * - cntrl
321        * - digit
322        * - graph
323        * - lower
324        * - print
325        * - punct
326        * - space
327        * - upper
328        * - xdigit
329        */
330       template<typename _Fwd_iter>
331 	char_class_type
332 	lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
333 			 bool __icase = false) const;
334 
335       /**
336        * @brief Determines if @p c is a member of an identified class.
337        *
338        * @param __c a character.
339        * @param __f a class type (as returned from lookup_classname).
340        *
341        * @returns true if the character @p __c is a member of the classification
342        * represented by @p __f, false otherwise.
343        *
344        * @throws std::bad_cast if the current locale does not have a ctype
345        *         facet.
346        */
347       bool
348       isctype(_Ch_type __c, char_class_type __f) const;
349 
350       /**
351        * @brief Converts a digit to an int.
352        *
353        * @param __ch    a character representing a digit.
354        * @param __radix the radix if the numeric conversion (limited to 8, 10,
355        *              or 16).
356        *
357        * @returns the value represented by the digit __ch in base radix if the
358        * character __ch is a valid digit in base radix; otherwise returns -1.
359        */
360       int
361       value(_Ch_type __ch, int __radix) const;
362 
363       /**
364        * @brief Imbues the regex_traits object with a copy of a new locale.
365        *
366        * @param __loc A locale.
367        *
368        * @returns a copy of the previous locale in use by the regex_traits
369        *          object.
370        *
371        * @note Calling imbue with a different locale than the one currently in
372        *       use invalidates all cached data held by *this.
373        */
374       locale_type
375       imbue(locale_type __loc)
376       {
377 	std::swap(_M_locale, __loc);
378 	return __loc;
379       }
380 
381       /**
382        * @brief Gets a copy of the current locale in use by the regex_traits
383        * object.
384        */
385       locale_type
386       getloc() const
387       { return _M_locale; }
388 
389     protected:
390       locale_type _M_locale;
391     };
392 
393   // [7.8] Class basic_regex
394   /**
395    * Objects of specializations of this class represent regular expressions
396    * constructed from sequences of character type @p _Ch_type.
397    *
398    * Storage for the regular expression is allocated and deallocated as
399    * necessary by the member functions of this class.
400    */
401   template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
402     class basic_regex
403     {
404     public:
405       static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
406 		    "regex traits class must have the same char_type");
407 
408       // types:
409       typedef _Ch_type				  value_type;
410       typedef _Rx_traits			  traits_type;
411       typedef typename traits_type::string_type   string_type;
412       typedef regex_constants::syntax_option_type flag_type;
413       typedef typename traits_type::locale_type   locale_type;
414 
415       /**
416        * @name Constants
417        * std [28.8.1](1)
418        */
419       ///@{
420       static constexpr flag_type icase = regex_constants::icase;
421       static constexpr flag_type nosubs = regex_constants::nosubs;
422       static constexpr flag_type optimize = regex_constants::optimize;
423       static constexpr flag_type collate = regex_constants::collate;
424       static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
425       static constexpr flag_type basic = regex_constants::basic;
426       static constexpr flag_type extended = regex_constants::extended;
427       static constexpr flag_type awk = regex_constants::awk;
428       static constexpr flag_type grep = regex_constants::grep;
429       static constexpr flag_type egrep = regex_constants::egrep;
430 #if __cplusplus >= 201703L || !defined __STRICT_ANSI__
431       static constexpr flag_type multiline = regex_constants::multiline;
432 #endif
433       ///@}
434 
435       // [7.8.2] construct/copy/destroy
436       /**
437        * Constructs a basic regular expression that does not match any
438        * character sequence.
439        */
440       basic_regex() noexcept
441       : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
442       { }
443 
444       /**
445        * @brief Constructs a basic regular expression from the
446        * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
447        * interpreted according to the flags in @p __f.
448        *
449        * @param __p A pointer to the start of a C-style null-terminated string
450        *          containing a regular expression.
451        * @param __f Flags indicating the syntax rules and options.
452        *
453        * @throws regex_error if @p __p is not a valid regular expression.
454        */
455       explicit
456       basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
457       { _M_compile(__p, __p + _Rx_traits::length(__p), __f); }
458 
459       /**
460        * @brief Constructs a basic regular expression from the sequence
461        * [p, p + len) interpreted according to the flags in @p f.
462        *
463        * @param __p   A pointer to the start of a string containing a regular
464        *              expression.
465        * @param __len The length of the string containing the regular
466        *              expression.
467        * @param __f   Flags indicating the syntax rules and options.
468        *
469        * @throws regex_error if @p __p is not a valid regular expression.
470        */
471       basic_regex(const _Ch_type* __p, std::size_t __len,
472 		  flag_type __f = ECMAScript)
473       {
474 	__glibcxx_requires_string_len(__p, __len);
475 	_M_compile(__p, __p + __len, __f);
476       }
477 
478       /**
479        * @brief Copy-constructs a basic regular expression.
480        *
481        * @param __rhs A @p regex object.
482        */
483       basic_regex(const basic_regex& __rhs) = default;
484 
485       /**
486        * @brief Move-constructs a basic regular expression.
487        *
488        * @param __rhs A @p regex object.
489        */
490       basic_regex(basic_regex&& __rhs) noexcept = default;
491 
492       /**
493        * @brief Constructs a basic regular expression from the string
494        * @p s interpreted according to the flags in @p f.
495        *
496        * @param __s A string containing a regular expression.
497        * @param __f Flags indicating the syntax rules and options.
498        *
499        * @throws regex_error if @p __s is not a valid regular expression.
500        */
501       template<typename _Ch_traits, typename _Ch_alloc>
502 	explicit
503 	basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
504 					    _Ch_alloc>& __s,
505 		    flag_type __f = ECMAScript)
506 	{ _M_compile(__s.data(), __s.data() + __s.size(), __f); }
507 
508       /**
509        * @brief Constructs a basic regular expression from the range
510        * [first, last) interpreted according to the flags in @p f.
511        *
512        * @param __first The start of a range containing a valid regular
513        *                expression.
514        * @param __last  The end of a range containing a valid regular
515        *                expression.
516        * @param __f     The format flags of the regular expression.
517        *
518        * @throws regex_error if @p [__first, __last) is not a valid regular
519        *         expression.
520        */
521       template<typename _FwdIter>
522 	basic_regex(_FwdIter __first, _FwdIter __last,
523 		    flag_type __f = ECMAScript)
524 	{ this->assign(__first, __last, __f); }
525 
526       /**
527        * @brief Constructs a basic regular expression from an initializer list.
528        *
529        * @param __l  The initializer list.
530        * @param __f  The format flags of the regular expression.
531        *
532        * @throws regex_error if @p __l is not a valid regular expression.
533        */
534       basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
535       { _M_compile(__l.begin(), __l.end(), __f); }
536 
537       /**
538        * @brief Destroys a basic regular expression.
539        */
540       ~basic_regex()
541       { }
542 
543       /**
544        * @brief Assigns one regular expression to another.
545        */
546       basic_regex&
547       operator=(const basic_regex&) = default;
548 
549       /**
550        * @brief Move-assigns one regular expression to another.
551        */
552       basic_regex&
553       operator=(basic_regex&&) = default;
554 
555       /**
556        * @brief Replaces a regular expression with a new one constructed from
557        * a C-style null-terminated string.
558        *
559        * @param __p A pointer to the start of a null-terminated C-style string
560        *        containing a regular expression.
561        */
562       basic_regex&
563       operator=(const _Ch_type* __p)
564       { return this->assign(__p); }
565 
566       /**
567        * @brief Replaces a regular expression with a new one constructed from
568        * an initializer list.
569        *
570        * @param __l  The initializer list.
571        *
572        * @throws regex_error if @p __l is not a valid regular expression.
573        */
574       basic_regex&
575       operator=(initializer_list<_Ch_type> __l)
576       { return this->assign(__l); }
577 
578       /**
579        * @brief Replaces a regular expression with a new one constructed from
580        * a string.
581        *
582        * @param __s A pointer to a string containing a regular expression.
583        */
584       template<typename _Ch_traits, typename _Alloc>
585 	basic_regex&
586 	operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
587 	{ return this->assign(__s); }
588 
589       // [7.8.3] assign
590       /**
591        * @brief Assigns one regular expression to another.
592        *
593        * @param __rhs Another regular expression object.
594        */
595       basic_regex&
596       assign(const basic_regex& __rhs) noexcept
597       { return *this = __rhs; }
598 
599       /**
600        * @brief Move-assigns one regular expression to another.
601        *
602        * @param __rhs Another regular expression object.
603        */
604       basic_regex&
605       assign(basic_regex&& __rhs) noexcept
606       { return *this = std::move(__rhs); }
607 
608       /**
609        * @brief Assigns a new regular expression to a regex object from a
610        * C-style null-terminated string containing a regular expression
611        * pattern.
612        *
613        * @param __p     A pointer to a C-style null-terminated string containing
614        *              a regular expression pattern.
615        * @param __flags Syntax option flags.
616        *
617        * @throws regex_error if __p does not contain a valid regular
618        * expression pattern interpreted according to @p __flags.  If
619        * regex_error is thrown, *this remains unchanged.
620        */
621       basic_regex&
622       assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
623       {
624 	_M_compile(__p, __p + _Rx_traits::length(__p), __flags);
625 	return *this;
626       }
627 
628       /**
629        * @brief Assigns a new regular expression to a regex object from a
630        * C-style string containing a regular expression pattern.
631        *
632        * @param __p     A pointer to a C-style string containing a
633        *                regular expression pattern.
634        * @param __len   The length of the regular expression pattern string.
635        * @param __flags Syntax option flags.
636        *
637        * @throws regex_error if p does not contain a valid regular
638        * expression pattern interpreted according to @p __flags.  If
639        * regex_error is thrown, *this remains unchanged.
640        */
641       // _GLIBCXX_RESOLVE_LIB_DEFECTS
642       // 3296. Inconsistent default argument for basic_regex<>::assign
643       basic_regex&
644       assign(const _Ch_type* __p, size_t __len, flag_type __flags = ECMAScript)
645       {
646 	_M_compile(__p, __p + __len, __flags);
647 	return *this;
648       }
649 
650       /**
651        * @brief Assigns a new regular expression to a regex object from a
652        * string containing a regular expression pattern.
653        *
654        * @param __s     A string containing a regular expression pattern.
655        * @param __flags Syntax option flags.
656        *
657        * @throws regex_error if __s does not contain a valid regular
658        * expression pattern interpreted according to @p __flags.  If
659        * regex_error is thrown, *this remains unchanged.
660        */
661       template<typename _Ch_traits, typename _Alloc>
662 	basic_regex&
663 	assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
664 	       flag_type __flags = ECMAScript)
665 	{
666 	  _M_compile(__s.data(), __s.data() + __s.size(), __flags);
667 	  return *this;
668 	}
669 
670       /**
671        * @brief Assigns a new regular expression to a regex object.
672        *
673        * @param __first The start of a range containing a valid regular
674        *                expression.
675        * @param __last  The end of a range containing a valid regular
676        *                expression.
677        * @param __flags Syntax option flags.
678        *
679        * @throws regex_error if p does not contain a valid regular
680        * expression pattern interpreted according to @p __flags.  If
681        * regex_error is thrown, the object remains unchanged.
682        */
683       template<typename _InputIterator>
684 	basic_regex&
685 	assign(_InputIterator __first, _InputIterator __last,
686 	       flag_type __flags = ECMAScript)
687 	{
688 #if __cpp_if_constexpr >= 201606L
689 	  using _ValT = typename iterator_traits<_InputIterator>::value_type;
690 	  if constexpr (__detail::__is_contiguous_iter<_InputIterator>::value
691 			&& is_same_v<_ValT, value_type>)
692 	    {
693 	      __glibcxx_requires_valid_range(__first, __last);
694 	      if constexpr (is_pointer_v<_InputIterator>)
695 		_M_compile(__first, __last, __flags);
696 	      else // __normal_iterator<_T*, C>
697 		_M_compile(__first.base(), __last.base(), __flags);
698 	    }
699 	  else
700 #endif
701 	  this->assign(string_type(__first, __last), __flags);
702 	  return *this;
703 	}
704 
705       /**
706        * @brief Assigns a new regular expression to a regex object.
707        *
708        * @param __l     An initializer list representing a regular expression.
709        * @param __flags Syntax option flags.
710        *
711        * @throws regex_error if @p __l does not contain a valid
712        * regular expression pattern interpreted according to @p
713        * __flags.  If regex_error is thrown, the object remains
714        * unchanged.
715        */
716       basic_regex&
717       assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
718       {
719 	_M_compile(__l.begin(), __l.end(), __flags);
720 	return *this;
721       }
722 
723       // [7.8.4] const operations
724       /**
725        * @brief Gets the number of marked subexpressions within the regular
726        * expression.
727        */
728       unsigned int
729       mark_count() const noexcept
730       {
731 	if (_M_automaton)
732 	  return _M_automaton->_M_sub_count() - 1;
733 	return 0;
734       }
735 
736       /**
737        * @brief Gets the flags used to construct the regular expression
738        * or in the last call to assign().
739        */
740       flag_type
741       flags() const noexcept
742       { return _M_flags; }
743 
744       // [7.8.5] locale
745       /**
746        * @brief Imbues the regular expression object with the given locale.
747        *
748        * @param __loc A locale.
749        */
750       locale_type
751       imbue(locale_type __loc)
752       {
753 	std::swap(__loc, _M_loc);
754 	_M_automaton.reset();
755 	return __loc;
756       }
757 
758       /**
759        * @brief Gets the locale currently imbued in the regular expression
760        *        object.
761        */
762       locale_type
763       getloc() const noexcept
764       { return _M_loc; }
765 
766       // [7.8.6] swap
767       /**
768        * @brief Swaps the contents of two regular expression objects.
769        *
770        * @param __rhs Another regular expression object.
771        */
772       void
773       swap(basic_regex& __rhs) noexcept
774       {
775 	std::swap(_M_flags, __rhs._M_flags);
776 	std::swap(_M_loc, __rhs._M_loc);
777 	std::swap(_M_automaton, __rhs._M_automaton);
778       }
779 
780 #ifdef _GLIBCXX_DEBUG
781       void
782       _M_dot(std::ostream& __ostr)
783       { _M_automaton->_M_dot(__ostr); }
784 #endif
785 
786     private:
787       typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
788 
789       void
790       _M_compile(const _Ch_type* __first, const _Ch_type* __last,
791 		 flag_type __f)
792       {
793 	__detail::_Compiler<_Rx_traits> __c(__first, __last, _M_loc, __f);
794 	_M_automaton = __c._M_get_nfa();
795 	_M_flags = __f;
796       }
797 
798       template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
799 	friend bool
800 	__detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
801 				    const basic_regex<_Cp, _Rp>&,
802 				    regex_constants::match_flag_type,
803 				    __detail::_RegexExecutorPolicy, bool);
804 
805       template<typename, typename, typename, bool>
806 	friend class __detail::_Executor;
807 
808       flag_type		_M_flags;
809       locale_type	_M_loc;
810       _AutomatonPtr	_M_automaton;
811     };
812 
813 #if ! __cpp_inline_variables
814   template<typename _Ch, typename _Tr>
815     constexpr regex_constants::syntax_option_type
816     basic_regex<_Ch, _Tr>::icase;
817 
818   template<typename _Ch, typename _Tr>
819     constexpr regex_constants::syntax_option_type
820     basic_regex<_Ch, _Tr>::nosubs;
821 
822   template<typename _Ch, typename _Tr>
823     constexpr regex_constants::syntax_option_type
824     basic_regex<_Ch, _Tr>::optimize;
825 
826   template<typename _Ch, typename _Tr>
827     constexpr regex_constants::syntax_option_type
828     basic_regex<_Ch, _Tr>::collate;
829 
830   template<typename _Ch, typename _Tr>
831     constexpr regex_constants::syntax_option_type
832     basic_regex<_Ch, _Tr>::ECMAScript;
833 
834   template<typename _Ch, typename _Tr>
835     constexpr regex_constants::syntax_option_type
836     basic_regex<_Ch, _Tr>::basic;
837 
838   template<typename _Ch, typename _Tr>
839     constexpr regex_constants::syntax_option_type
840     basic_regex<_Ch, _Tr>::extended;
841 
842   template<typename _Ch, typename _Tr>
843     constexpr regex_constants::syntax_option_type
844     basic_regex<_Ch, _Tr>::awk;
845 
846   template<typename _Ch, typename _Tr>
847     constexpr regex_constants::syntax_option_type
848     basic_regex<_Ch, _Tr>::grep;
849 
850   template<typename _Ch, typename _Tr>
851     constexpr regex_constants::syntax_option_type
852     basic_regex<_Ch, _Tr>::egrep;
853 #endif // ! C++17
854 
855 #if __cpp_deduction_guides >= 201606
856   template<typename _ForwardIterator>
857     basic_regex(_ForwardIterator, _ForwardIterator,
858 		regex_constants::syntax_option_type = {})
859       -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
860 #endif
861 
862   /** @brief Standard regular expressions. */
863   typedef basic_regex<char>    regex;
864 
865 #ifdef _GLIBCXX_USE_WCHAR_T
866   /** @brief Standard wide-character regular expressions. */
867   typedef basic_regex<wchar_t> wregex;
868 #endif
869 
870 
871   // [7.8.6] basic_regex swap
872   /**
873    * @brief Swaps the contents of two regular expression objects.
874    * @param __lhs First regular expression.
875    * @param __rhs Second regular expression.
876    * @relates basic_regex
877    */
878   template<typename _Ch_type, typename _Rx_traits>
879     inline void
880     swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
881 	 basic_regex<_Ch_type, _Rx_traits>& __rhs) noexcept
882     { __lhs.swap(__rhs); }
883 
884 
885   // C++11 28.9 [re.submatch] Class template sub_match
886   /**
887    * A sequence of characters matched by a particular marked sub-expression.
888    *
889    * An object of this class is essentially a pair of iterators marking a
890    * matched subexpression within a regular expression pattern match. Such
891    * objects can be converted to and compared with std::basic_string objects
892    * of a similar base character type as the pattern matched by the regular
893    * expression.
894    *
895    * The iterators that make up the pair are the usual half-open interval
896    * referencing the actual original pattern matched.
897    */
898   template<typename _BiIter>
899     class sub_match : public std::pair<_BiIter, _BiIter>
900     {
901       typedef iterator_traits<_BiIter>			__iter_traits;
902 
903     public:
904       typedef typename __iter_traits::value_type      	value_type;
905       typedef typename __iter_traits::difference_type 	difference_type;
906       typedef _BiIter					iterator;
907       typedef basic_string<value_type>			string_type;
908 
909       bool matched;
910 
911       constexpr sub_match() noexcept : matched() { }
912 
913       /// Gets the length of the matching sequence.
914       difference_type
915       length() const noexcept
916       { return this->matched ? std::distance(this->first, this->second) : 0; }
917 
918       /**
919        * @brief Gets the matching sequence as a string.
920        *
921        * @returns the matching sequence as a string.
922        *
923        * This is the implicit conversion operator.  It is identical to the
924        * str() member function except that it will want to pop up in
925        * unexpected places and cause a great deal of confusion and cursing
926        * from the unwary.
927        */
928       operator string_type() const
929       { return str(); }
930 
931       /**
932        * @brief Gets the matching sequence as a string.
933        *
934        * @returns the matching sequence as a string.
935        */
936       string_type
937       str() const
938       {
939 	return this->matched
940 	  ? string_type(this->first, this->second)
941 	  : string_type();
942       }
943 
944       /**
945        * @brief Compares this and another matched sequence.
946        *
947        * @param __s Another matched sequence to compare to this one.
948        *
949        * @retval negative  This matched sequence will collate before `__s`.
950        * @retval zero      This matched sequence is equivalent to `__s`.
951        * @retval positive  This matched sequence will collate after `__s`.
952        */
953       int
954       compare(const sub_match& __s) const
955       { return this->_M_str().compare(__s._M_str()); }
956 
957       /**
958        * @{
959        * @brief Compares this `sub_match` to a string.
960        *
961        * @param __s A string to compare to this `sub_match`.
962        *
963        * @retval negative  This matched sequence will collate before `__s`.
964        * @retval zero      This matched sequence is equivalent to `__s`.
965        * @retval positive  This matched sequence will collate after `__s`.
966        */
967       int
968       compare(const string_type& __s) const
969       { return this->_M_str().compare(__s); }
970 
971       int
972       compare(const value_type* __s) const
973       { return this->_M_str().compare(__s); }
974       /// @}
975 
976       /// @cond undocumented
977       // Non-standard, used by comparison operators
978       int
979       _M_compare(const value_type* __s, size_t __n) const
980       { return this->_M_str().compare({__s, __n}); }
981       /// @endcond
982 
983       // _GLIBCXX_RESOLVE_LIB_DEFECTS
984       // 3204. sub_match::swap only swaps the base class
985       /// Swap the values of two sub_match objects.
986       void
987       swap(sub_match& __s) noexcept(__is_nothrow_swappable<_BiIter>::value)
988       {
989 	this->pair<_BiIter, _BiIter>::swap(__s);
990 	std::swap(matched, __s.matched);
991       }
992 
993     private:
994       // Simplified basic_string_view for C++11
995       struct __string_view
996       {
997 	using traits_type = typename string_type::traits_type;
998 
999 	__string_view() = default;
1000 
1001 	__string_view(const value_type* __s, size_t __n) noexcept
1002 	: _M_data(__s), _M_len(__n) { }
1003 
1004 	__string_view(const value_type* __s) noexcept
1005 	: _M_data(__s), _M_len(traits_type::length(__s)) { }
1006 
1007 	__string_view(const string_type& __s) noexcept
1008 	: _M_data(__s.data()), _M_len(__s.length()) { }
1009 
1010 	int
1011 	compare(__string_view __s) const noexcept
1012 	{
1013 	  if (const size_t __n = std::min(_M_len, __s._M_len))
1014 	    if (int __ret = traits_type::compare(_M_data, __s._M_data, __n))
1015 	      return __ret;
1016 	  using __limits = __gnu_cxx::__int_traits<int>;
1017 	  const difference_type __diff = _M_len - __s._M_len;
1018 	  if (__diff > __limits::__max)
1019 	    return __limits::__max;
1020 	  if (__diff < __limits::__min)
1021 	    return __limits::__min;
1022 	  return static_cast<int>(__diff);
1023 	}
1024 
1025       private:
1026 	const value_type* _M_data = nullptr;
1027 	size_t _M_len = 0;
1028       };
1029 
1030       // Create a __string_view over the iterator range.
1031       template<typename _Iter = _BiIter>
1032 	__enable_if_t<__detail::__is_contiguous_iter<_Iter>::value,
1033 		      __string_view>
1034 	_M_str() const noexcept
1035 	{
1036 	  if (this->matched)
1037 	    if (size_t __len = this->second - this->first)
1038 	      return { std::__addressof(*this->first), __len };
1039 	  return {};
1040 	}
1041 
1042       // Create a temporary string that can be converted to __string_view.
1043       template<typename _Iter = _BiIter>
1044 	__enable_if_t<!__detail::__is_contiguous_iter<_Iter>::value,
1045 		      string_type>
1046 	_M_str() const
1047 	{ return str(); }
1048     };
1049 
1050 
1051   /** @brief Standard regex submatch over a C-style null-terminated string. */
1052   typedef sub_match<const char*>	     csub_match;
1053 
1054   /** @brief Standard regex submatch over a standard string. */
1055   typedef sub_match<string::const_iterator>  ssub_match;
1056 
1057 #ifdef _GLIBCXX_USE_WCHAR_T
1058   /** @brief Regex submatch over a C-style null-terminated wide string. */
1059   typedef sub_match<const wchar_t*>	  wcsub_match;
1060 
1061   /** @brief Regex submatch over a standard wide string. */
1062   typedef sub_match<wstring::const_iterator> wssub_match;
1063 #endif
1064 
1065   // [7.9.2] sub_match non-member operators
1066 
1067   /// @relates sub_match @{
1068 
1069   /**
1070    * @brief Tests the equivalence of two regular expression submatches.
1071    * @param __lhs First regular expression submatch.
1072    * @param __rhs Second regular expression submatch.
1073    * @returns true if @a __lhs  is equivalent to @a __rhs, false otherwise.
1074    */
1075   template<typename _BiIter>
1076     inline bool
1077     operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1078     { return __lhs.compare(__rhs) == 0; }
1079 
1080 #if __cpp_lib_three_way_comparison
1081   /**
1082    * @brief Three-way comparison of two regular expression submatches.
1083    * @param __lhs First regular expression submatch.
1084    * @param __rhs Second regular expression submatch.
1085    * @returns A value indicating whether `__lhs` is less than, equal to,
1086    *	      greater than, or incomparable with `__rhs`.
1087    */
1088   template<typename _BiIter>
1089     inline auto
1090     operator<=>(const sub_match<_BiIter>& __lhs,
1091 		const sub_match<_BiIter>& __rhs)
1092     noexcept(__detail::__is_contiguous_iter<_BiIter>::value)
1093     {
1094       using _Tr = char_traits<typename iterator_traits<_BiIter>::value_type>;
1095       return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
1096     }
1097 #else
1098   /**
1099    * @brief Tests the inequivalence of two regular expression submatches.
1100    * @param __lhs First regular expression submatch.
1101    * @param __rhs Second regular expression submatch.
1102    * @returns true if @a __lhs  is not equivalent to @a __rhs, false otherwise.
1103    */
1104   template<typename _BiIter>
1105     inline bool
1106     operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1107     { return __lhs.compare(__rhs) != 0; }
1108 
1109   /**
1110    * @brief Tests the ordering of two regular expression submatches.
1111    * @param __lhs First regular expression submatch.
1112    * @param __rhs Second regular expression submatch.
1113    * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1114    */
1115   template<typename _BiIter>
1116     inline bool
1117     operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1118     { return __lhs.compare(__rhs) < 0; }
1119 
1120   /**
1121    * @brief Tests the ordering of two regular expression submatches.
1122    * @param __lhs First regular expression submatch.
1123    * @param __rhs Second regular expression submatch.
1124    * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1125    */
1126   template<typename _BiIter>
1127     inline bool
1128     operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1129     { return __lhs.compare(__rhs) <= 0; }
1130 
1131   /**
1132    * @brief Tests the ordering of two regular expression submatches.
1133    * @param __lhs First regular expression submatch.
1134    * @param __rhs Second regular expression submatch.
1135    * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1136    */
1137   template<typename _BiIter>
1138     inline bool
1139     operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1140     { return __lhs.compare(__rhs) >= 0; }
1141 
1142   /**
1143    * @brief Tests the ordering of two regular expression submatches.
1144    * @param __lhs First regular expression submatch.
1145    * @param __rhs Second regular expression submatch.
1146    * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1147    */
1148   template<typename _BiIter>
1149     inline bool
1150     operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1151     { return __lhs.compare(__rhs) > 0; }
1152 #endif // three-way comparison
1153 
1154   /// @cond undocumented
1155 
1156   // Alias for a basic_string that can be compared to a sub_match.
1157   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1158     using __sub_match_string = basic_string<
1159 			      typename iterator_traits<_Bi_iter>::value_type,
1160 			      _Ch_traits, _Ch_alloc>;
1161   /// @endcond
1162 
1163 #if ! __cpp_lib_three_way_comparison
1164   /**
1165    * @brief Tests the equivalence of a string and a regular expression
1166    *        submatch.
1167    * @param __lhs A string.
1168    * @param __rhs A regular expression submatch.
1169    * @returns true if @a __lhs  is equivalent to @a __rhs, false otherwise.
1170    */
1171   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1172     inline bool
1173     operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1174 	       const sub_match<_Bi_iter>& __rhs)
1175     { return __rhs._M_compare(__lhs.data(), __lhs.size()) == 0; }
1176 
1177   /**
1178    * @brief Tests the inequivalence of a string and a regular expression
1179    *        submatch.
1180    * @param __lhs A string.
1181    * @param __rhs A regular expression submatch.
1182    * @returns true if @a __lhs  is not equivalent to @a __rhs, false otherwise.
1183    */
1184   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1185     inline bool
1186     operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1187 	       const sub_match<_Bi_iter>& __rhs)
1188     { return !(__lhs == __rhs); }
1189 
1190   /**
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.
1195    */
1196   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1197     inline bool
1198     operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1199 	      const sub_match<_Bi_iter>& __rhs)
1200     { return __rhs._M_compare(__lhs.data(), __lhs.size()) > 0; }
1201 
1202   /**
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.
1207    */
1208   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1209     inline bool
1210     operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1211 	      const sub_match<_Bi_iter>& __rhs)
1212     { return __rhs < __lhs; }
1213 
1214   /**
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.
1219    */
1220   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1221     inline bool
1222     operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1223 	       const sub_match<_Bi_iter>& __rhs)
1224     { return !(__lhs < __rhs); }
1225 
1226   /**
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.
1231    */
1232   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1233     inline bool
1234     operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1235 	       const sub_match<_Bi_iter>& __rhs)
1236     { return !(__rhs < __lhs); }
1237 #endif // three-way comparison
1238 
1239   /**
1240    * @brief Tests the equivalence of a regular expression submatch and a
1241    *        string.
1242    * @param __lhs A regular expression submatch.
1243    * @param __rhs A string.
1244    * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1245    */
1246   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1247     inline bool
1248     operator==(const sub_match<_Bi_iter>& __lhs,
1249 	       const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1250     { return __lhs._M_compare(__rhs.data(), __rhs.size()) == 0; }
1251 
1252 #if __cpp_lib_three_way_comparison
1253   /**
1254    * @brief Three-way comparison of a regular expression submatch and a string.
1255    * @param __lhs A regular expression submatch.
1256    * @param __rhs A string.
1257    * @returns A value indicating whether `__lhs` is less than, equal to,
1258    *	      greater than, or incomparable with `__rhs`.
1259    */
1260   template<typename _Bi_iter, typename _Ch_traits, typename _Alloc>
1261     inline auto
1262     operator<=>(const sub_match<_Bi_iter>& __lhs,
1263 		const __sub_match_string<_Bi_iter, _Ch_traits, _Alloc>& __rhs)
1264     noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
1265     {
1266       return __detail::__char_traits_cmp_cat<_Ch_traits>(
1267 	  __lhs._M_compare(__rhs.data(), __rhs.size()));
1268     }
1269 #else
1270   /**
1271    * @brief Tests the inequivalence of a regular expression submatch and a
1272    *        string.
1273    * @param __lhs A regular expression submatch.
1274    * @param __rhs A string.
1275    * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1276    */
1277   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1278     inline bool
1279     operator!=(const sub_match<_Bi_iter>& __lhs,
1280 	       const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1281     { return !(__lhs == __rhs); }
1282 
1283   /**
1284    * @brief Tests the ordering of a regular expression submatch and a string.
1285    * @param __lhs A regular expression submatch.
1286    * @param __rhs A string.
1287    * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1288    */
1289   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1290     inline bool
1291     operator<(const sub_match<_Bi_iter>& __lhs,
1292 	      const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1293     { return __lhs._M_compare(__rhs.data(), __rhs.size()) < 0; }
1294 
1295   /**
1296    * @brief Tests the ordering of a regular expression submatch and a string.
1297    * @param __lhs A regular expression submatch.
1298    * @param __rhs A string.
1299    * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1300    */
1301   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1302     inline bool
1303     operator>(const sub_match<_Bi_iter>& __lhs,
1304 	      const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1305     { return __rhs < __lhs; }
1306 
1307   /**
1308    * @brief Tests the ordering of a regular expression submatch and a string.
1309    * @param __lhs A regular expression submatch.
1310    * @param __rhs A string.
1311    * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1312    */
1313   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1314     inline bool
1315     operator>=(const sub_match<_Bi_iter>& __lhs,
1316 	       const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1317     { return !(__lhs < __rhs); }
1318 
1319   /**
1320    * @brief Tests the ordering of a regular expression submatch and a string.
1321    * @param __lhs A regular expression submatch.
1322    * @param __rhs A string.
1323    * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1324    */
1325   template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1326     inline bool
1327     operator<=(const sub_match<_Bi_iter>& __lhs,
1328 	       const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1329     { return !(__rhs < __lhs); }
1330 
1331   /**
1332    * @brief Tests the equivalence of a C string and a regular expression
1333    *        submatch.
1334    * @param __lhs A null-terminated string.
1335    * @param __rhs A regular expression submatch.
1336    * @returns true if @a __lhs  is equivalent to @a __rhs, false otherwise.
1337    */
1338   template<typename _Bi_iter>
1339     inline bool
1340     operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1341 	       const sub_match<_Bi_iter>& __rhs)
1342     { return __rhs.compare(__lhs) == 0; }
1343 
1344   /**
1345    * @brief Tests the inequivalence of a C string and a regular
1346    *        expression submatch.
1347    * @param __lhs A null-terminated string.
1348    * @param __rhs A regular expression submatch.
1349    * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1350    */
1351   template<typename _Bi_iter>
1352     inline bool
1353     operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1354 	       const sub_match<_Bi_iter>& __rhs)
1355     { return !(__lhs == __rhs); }
1356 
1357   /**
1358    * @brief Tests the ordering of a C string and a regular expression submatch.
1359    * @param __lhs A null-terminated string.
1360    * @param __rhs A regular expression submatch.
1361    * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1362    */
1363   template<typename _Bi_iter>
1364     inline bool
1365     operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1366 	      const sub_match<_Bi_iter>& __rhs)
1367     { return __rhs.compare(__lhs) > 0; }
1368 
1369   /**
1370    * @brief Tests the ordering of a C string and a regular expression submatch.
1371    * @param __lhs A null-terminated string.
1372    * @param __rhs A regular expression submatch.
1373    * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1374    */
1375   template<typename _Bi_iter>
1376     inline bool
1377     operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1378 	      const sub_match<_Bi_iter>& __rhs)
1379     { return __rhs < __lhs; }
1380 
1381   /**
1382    * @brief Tests the ordering of a C string and a regular expression submatch.
1383    * @param __lhs A null-terminated string.
1384    * @param __rhs A regular expression submatch.
1385    * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1386    */
1387   template<typename _Bi_iter>
1388     inline bool
1389     operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1390 	       const sub_match<_Bi_iter>& __rhs)
1391     { return !(__lhs < __rhs); }
1392 
1393   /**
1394    * @brief Tests the ordering of a C string and a regular expression submatch.
1395    * @param __lhs A null-terminated string.
1396    * @param __rhs A regular expression submatch.
1397    * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1398    */
1399   template<typename _Bi_iter>
1400     inline bool
1401     operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1402 	       const sub_match<_Bi_iter>& __rhs)
1403     { return !(__rhs < __lhs); }
1404 #endif // three-way comparison
1405 
1406   /**
1407    * @brief Tests the equivalence of a regular expression submatch and a C
1408    *        string.
1409    * @param __lhs A regular expression submatch.
1410    * @param __rhs A null-terminated string.
1411    * @returns true if @a __lhs  is equivalent to @a __rhs, false otherwise.
1412    */
1413   template<typename _Bi_iter>
1414     inline bool
1415     operator==(const sub_match<_Bi_iter>& __lhs,
1416 	       typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1417     { return __lhs.compare(__rhs) == 0; }
1418 
1419 #if __cpp_lib_three_way_comparison
1420   /**
1421    * @brief Three-way comparison of a regular expression submatch and a C
1422    *	    string.
1423    * @param __lhs A regular expression submatch.
1424    * @param __rhs A null-terminated string.
1425    * @returns A value indicating whether `__lhs` is less than, equal to,
1426    *	      greater than, or incomparable with `__rhs`.
1427    */
1428   template<typename _Bi_iter>
1429     inline auto
1430     operator<=>(const sub_match<_Bi_iter>& __lhs,
1431 		typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1432     noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
1433     {
1434       using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
1435       return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
1436     }
1437 #else
1438   /**
1439    * @brief Tests the inequivalence of a regular expression submatch and a
1440    *        string.
1441    * @param __lhs A regular expression submatch.
1442    * @param __rhs A null-terminated string.
1443    * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1444    */
1445   template<typename _Bi_iter>
1446     inline bool
1447     operator!=(const sub_match<_Bi_iter>& __lhs,
1448 	       typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1449     { return !(__lhs == __rhs); }
1450 
1451   /**
1452    * @brief Tests the ordering of a regular expression submatch and a C string.
1453    * @param __lhs A regular expression submatch.
1454    * @param __rhs A null-terminated string.
1455    * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1456    */
1457   template<typename _Bi_iter>
1458     inline bool
1459     operator<(const sub_match<_Bi_iter>& __lhs,
1460 	      typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1461     { return __lhs.compare(__rhs) < 0; }
1462 
1463   /**
1464    * @brief Tests the ordering of a regular expression submatch and a C string.
1465    * @param __lhs A regular expression submatch.
1466    * @param __rhs A null-terminated string.
1467    * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1468    */
1469   template<typename _Bi_iter>
1470     inline bool
1471     operator>(const sub_match<_Bi_iter>& __lhs,
1472 	      typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1473     { return __rhs < __lhs; }
1474 
1475   /**
1476    * @brief Tests the ordering of a regular expression submatch and a C string.
1477    * @param __lhs A regular expression submatch.
1478    * @param __rhs A null-terminated string.
1479    * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1480    */
1481   template<typename _Bi_iter>
1482     inline bool
1483     operator>=(const sub_match<_Bi_iter>& __lhs,
1484 	       typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1485     { return !(__lhs < __rhs); }
1486 
1487   /**
1488    * @brief Tests the ordering of a regular expression submatch and a C string.
1489    * @param __lhs A regular expression submatch.
1490    * @param __rhs A null-terminated string.
1491    * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1492    */
1493   template<typename _Bi_iter>
1494     inline bool
1495     operator<=(const sub_match<_Bi_iter>& __lhs,
1496 	       typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1497     { return !(__rhs < __lhs); }
1498 
1499   /**
1500    * @brief Tests the equivalence of a character and a regular expression
1501    *        submatch.
1502    * @param __lhs A character.
1503    * @param __rhs A regular expression submatch.
1504    * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1505    */
1506   template<typename _Bi_iter>
1507     inline bool
1508     operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1509 	       const sub_match<_Bi_iter>& __rhs)
1510     { return __rhs._M_compare(std::__addressof(__lhs), 1) == 0; }
1511 
1512   /**
1513    * @brief Tests the inequivalence of a character and a regular expression
1514    *        submatch.
1515    * @param __lhs A character.
1516    * @param __rhs A regular expression submatch.
1517    * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1518    */
1519   template<typename _Bi_iter>
1520     inline bool
1521     operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1522 	       const sub_match<_Bi_iter>& __rhs)
1523     { return !(__lhs == __rhs); }
1524 
1525   /**
1526    * @brief Tests the ordering of a character and a regular expression
1527    *        submatch.
1528    * @param __lhs A character.
1529    * @param __rhs A regular expression submatch.
1530    * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1531    */
1532   template<typename _Bi_iter>
1533     inline bool
1534     operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1535 	      const sub_match<_Bi_iter>& __rhs)
1536     { return __rhs._M_compare(std::__addressof(__lhs), 1) > 0; }
1537 
1538   /**
1539    * @brief Tests the ordering of a character and a regular expression
1540    *        submatch.
1541    * @param __lhs A character.
1542    * @param __rhs A regular expression submatch.
1543    * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1544    */
1545   template<typename _Bi_iter>
1546     inline bool
1547     operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1548 	      const sub_match<_Bi_iter>& __rhs)
1549     { return __rhs < __lhs; }
1550 
1551   /**
1552    * @brief Tests the ordering of a character and a regular expression
1553    *        submatch.
1554    * @param __lhs A character.
1555    * @param __rhs A regular expression submatch.
1556    * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1557    */
1558   template<typename _Bi_iter>
1559     inline bool
1560     operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1561 	       const sub_match<_Bi_iter>& __rhs)
1562     { return !(__lhs < __rhs); }
1563 
1564   /**
1565    * @brief Tests the ordering of a character and a regular expression
1566    *        submatch.
1567    * @param __lhs A character.
1568    * @param __rhs A regular expression submatch.
1569    * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1570    */
1571   template<typename _Bi_iter>
1572     inline bool
1573     operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1574 	       const sub_match<_Bi_iter>& __rhs)
1575     { return !(__rhs < __lhs); }
1576 #endif // three-way comparison
1577 
1578   /**
1579    * @brief Tests the equivalence of a regular expression submatch and a
1580    *        character.
1581    * @param __lhs A regular expression submatch.
1582    * @param __rhs A character.
1583    * @returns true if @a __lhs  is equivalent to @a __rhs, false otherwise.
1584    */
1585   template<typename _Bi_iter>
1586     inline bool
1587     operator==(const sub_match<_Bi_iter>& __lhs,
1588 	       typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1589     { return __lhs._M_compare(std::__addressof(__rhs), 1) == 0; }
1590 
1591 #if __cpp_lib_three_way_comparison
1592   /**
1593    * @brief Three-way comparison of a regular expression submatch and a
1594    *	    character.
1595    * @param __lhs A regular expression submatch.
1596    * @param __rhs A character.
1597    * @returns A value indicating whether `__lhs` is less than, equal to,
1598    *	      greater than, or incomparable with `__rhs`.
1599    */
1600 
1601   template<typename _Bi_iter>
1602     inline auto
1603     operator<=>(const sub_match<_Bi_iter>& __lhs,
1604 		typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1605     noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
1606     {
1607       using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
1608       return __detail::__char_traits_cmp_cat<_Tr>(
1609 	  __lhs._M_compare(std::__addressof(__rhs), 1));
1610     }
1611 #else
1612   /**
1613    * @brief Tests the inequivalence of a regular expression submatch and a
1614    *        character.
1615    * @param __lhs A regular expression submatch.
1616    * @param __rhs A character.
1617    * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1618    */
1619   template<typename _Bi_iter>
1620     inline bool
1621     operator!=(const sub_match<_Bi_iter>& __lhs,
1622 	       typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1623     { return !(__lhs == __rhs); }
1624 
1625   /**
1626    * @brief Tests the ordering of a regular expression submatch and a
1627    *        character.
1628    * @param __lhs A regular expression submatch.
1629    * @param __rhs A character.
1630    * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1631    */
1632   template<typename _Bi_iter>
1633     inline bool
1634     operator<(const sub_match<_Bi_iter>& __lhs,
1635 	      typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1636     { return __lhs._M_compare(std::__addressof(__rhs), 1) < 0; }
1637 
1638   /**
1639    * @brief Tests the ordering of a regular expression submatch and a
1640    *        character.
1641    * @param __lhs A regular expression submatch.
1642    * @param __rhs A character.
1643    * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1644    */
1645   template<typename _Bi_iter>
1646     inline bool
1647     operator>(const sub_match<_Bi_iter>& __lhs,
1648 	      typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1649     { return __rhs < __lhs; }
1650 
1651   /**
1652    * @brief Tests the ordering of a regular expression submatch and a
1653    *        character.
1654    * @param __lhs A regular expression submatch.
1655    * @param __rhs A character.
1656    * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1657    */
1658   template<typename _Bi_iter>
1659     inline bool
1660     operator>=(const sub_match<_Bi_iter>& __lhs,
1661 	       typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1662     { return !(__lhs < __rhs); }
1663 
1664   /**
1665    * @brief Tests the ordering of a regular expression submatch and a
1666    *        character.
1667    * @param __lhs A regular expression submatch.
1668    * @param __rhs A character.
1669    * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1670    */
1671   template<typename _Bi_iter>
1672     inline bool
1673     operator<=(const sub_match<_Bi_iter>& __lhs,
1674 	       typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1675     { return !(__rhs < __lhs); }
1676 #endif // three-way comparison
1677 
1678   /**
1679    * @brief Inserts a matched string into an output stream.
1680    *
1681    * @param __os The output stream.
1682    * @param __m  A submatch string.
1683    *
1684    * @returns the output stream with the submatch string inserted.
1685    */
1686   template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1687     inline
1688     basic_ostream<_Ch_type, _Ch_traits>&
1689     operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1690 	       const sub_match<_Bi_iter>& __m)
1691     { return __os << __m.str(); }
1692 
1693   /// @} relates sub_match
1694 
1695   // [7.10] Class template match_results
1696 
1697   /**
1698    * @brief The results of a match or search operation.
1699    *
1700    * A collection of character sequences representing the result of a regular
1701    * expression match.  Storage for the collection is allocated and freed as
1702    * necessary by the member functions of class template match_results.
1703    *
1704    * This class satisfies the Sequence requirements, with the exception that
1705    * only the operations defined for a const-qualified Sequence are supported.
1706    *
1707    * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1708    * the whole match. In this case the %sub_match member matched is always true.
1709    * The sub_match object stored at index n denotes what matched the marked
1710    * sub-expression n within the matched expression. If the sub-expression n
1711    * participated in a regular expression match then the %sub_match member
1712    * matched evaluates to true, and members first and second denote the range
1713    * of characters [first, second) which formed that match. Otherwise matched
1714    * is false, and members first and second point to the end of the sequence
1715    * that was searched.
1716    */
1717   template<typename _Bi_iter,
1718 	   typename _Alloc = allocator<sub_match<_Bi_iter> > >
1719     class match_results
1720     : private std::vector<sub_match<_Bi_iter>, _Alloc>
1721     {
1722     private:
1723       /*
1724        * The vector base is empty if this does not represent a match (!ready());
1725        * Otherwise if it's a match failure, it contains 3 elements:
1726        * [0] unmatched
1727        * [1] prefix
1728        * [2] suffix
1729        * Otherwise it contains n+4 elements where n is the number of marked
1730        * sub-expressions:
1731        * [0] entire match
1732        * [1] 1st marked subexpression
1733        * ...
1734        * [n] nth marked subexpression
1735        * [n+1] unmatched
1736        * [n+2] prefix
1737        * [n+3] suffix
1738        */
1739       typedef std::vector<sub_match<_Bi_iter>, _Alloc>     _Base_type;
1740       // In debug mode _Base_type is the debug vector, this is the unsafe one:
1741       typedef _GLIBCXX_STD_C::vector<sub_match<_Bi_iter>, _Alloc> _Unchecked;
1742       typedef std::iterator_traits<_Bi_iter>   	   	   __iter_traits;
1743       typedef regex_constants::match_flag_type		   match_flag_type;
1744 
1745     public:
1746       /**
1747        * @name 28.10 Public Types
1748        */
1749       ///@{
1750       typedef sub_match<_Bi_iter>			   value_type;
1751       typedef const value_type&				   const_reference;
1752       typedef value_type&				   reference;
1753       typedef typename _Base_type::const_iterator	   const_iterator;
1754       typedef const_iterator				   iterator;
1755       typedef typename __iter_traits::difference_type	   difference_type;
1756       typedef typename allocator_traits<_Alloc>::size_type size_type;
1757       typedef _Alloc					   allocator_type;
1758       typedef typename __iter_traits::value_type 	   char_type;
1759       typedef std::basic_string<char_type>		   string_type;
1760       ///@}
1761 
1762     public:
1763       /**
1764        * @name 28.10.1 Construction, Copying, and Destruction
1765        */
1766       ///@{
1767 
1768       /**
1769        * @brief Constructs a default %match_results container.
1770        * @post size() returns 0 and str() returns an empty string.
1771        */
1772       match_results() : match_results(_Alloc()) { }
1773 
1774       /**
1775        * @brief Constructs a default %match_results container.
1776        * @post size() returns 0 and str() returns an empty string.
1777        */
1778       explicit
1779       match_results(const _Alloc& __a) noexcept
1780       : _Base_type(__a)
1781       { }
1782 
1783       /**
1784        * @brief Copy constructs a %match_results.
1785        */
1786       match_results(const match_results&) = default;
1787 
1788       /**
1789        * @brief Move constructs a %match_results.
1790        */
1791       match_results(match_results&&) noexcept = default;
1792 
1793       /**
1794        * @brief Assigns rhs to *this.
1795        */
1796       match_results&
1797       operator=(const match_results&) = default;
1798 
1799       /**
1800        * @brief Move-assigns rhs to *this.
1801        */
1802       match_results&
1803       operator=(match_results&&) = default;
1804 
1805       /**
1806        * @brief Destroys a %match_results object.
1807        */
1808       ~match_results() = default;
1809 
1810       ///@}
1811 
1812       // 28.10.2, state:
1813       /**
1814        * @brief Indicates if the %match_results is ready.
1815        * @retval true   The object has a fully-established result state.
1816        * @retval false  The object is not ready.
1817        */
1818       bool ready() const noexcept { return !_Unchecked::empty(); }
1819 
1820       /**
1821        * @name 28.10.2 Size
1822        */
1823       ///@{
1824 
1825       /**
1826        * @brief Gets the number of matches and submatches.
1827        *
1828        * The number of matches for a given regular expression will be either 0
1829        * if there was no match or mark_count() + 1 if a match was successful.
1830        * Some matches may be empty.
1831        *
1832        * @returns the number of matches found.
1833        */
1834       size_type
1835       size() const noexcept
1836       { return _Unchecked::empty() ? 0 : _Unchecked::size() - 3; }
1837 
1838       size_type
1839       max_size() const noexcept
1840       { return _Unchecked::max_size() - 3; }
1841 
1842       /**
1843        * @brief Indicates if the %match_results contains no results.
1844        * @retval true The %match_results object is empty.
1845        * @retval false The %match_results object is not empty.
1846        */
1847       _GLIBCXX_NODISCARD bool
1848       empty() const noexcept
1849       { return _Unchecked::size() <= 3; }
1850 
1851       ///@}
1852 
1853       /**
1854        * @name 28.10.4 Element Access
1855        */
1856       ///@{
1857 
1858       /**
1859        * @brief Gets the length of the indicated submatch.
1860        * @param __sub indicates the submatch.
1861        * @pre   ready() == true
1862        *
1863        * This function returns the length of the indicated submatch, or the
1864        * length of the entire match if @p __sub is zero (the default).
1865        */
1866       difference_type
1867       length(size_type __sub = 0) const
1868       { return (*this)[__sub].length(); }
1869 
1870       /**
1871        * @brief Gets the offset of the beginning of the indicated submatch.
1872        * @param __sub indicates the submatch.
1873        * @pre   ready() == true
1874        *
1875        * This function returns the offset from the beginning of the target
1876        * sequence to the beginning of the submatch, unless the value of @p __sub
1877        * is zero (the default), in which case this function returns the offset
1878        * from the beginning of the target sequence to the beginning of the
1879        * match.
1880        */
1881       difference_type
1882       position(size_type __sub = 0) const
1883       { return std::distance(_M_begin, (*this)[__sub].first); }
1884 
1885       /**
1886        * @brief Gets the match or submatch converted to a string type.
1887        * @param __sub indicates the submatch.
1888        * @pre   ready() == true
1889        *
1890        * This function gets the submatch (or match, if @p __sub is
1891        * zero) extracted from the target range and converted to the
1892        * associated string type.
1893        */
1894       string_type
1895       str(size_type __sub = 0) const
1896       { return string_type((*this)[__sub]); }
1897 
1898       /**
1899        * @brief Gets a %sub_match reference for the match or submatch.
1900        * @param __sub indicates the submatch.
1901        * @pre   ready() == true
1902        *
1903        * This function gets a reference to the indicated submatch, or
1904        * the entire match if @p __sub is zero.
1905        *
1906        * If @p __sub >= size() then this function returns a %sub_match with a
1907        * special value indicating no submatch.
1908        */
1909       const_reference
1910       operator[](size_type __sub) const
1911       {
1912 	__glibcxx_assert( ready() );
1913 	return __sub < size()
1914 	       ? _Unchecked::operator[](__sub)
1915 	       : _M_unmatched_sub();
1916       }
1917 
1918       /**
1919        * @brief Gets a %sub_match representing the match prefix.
1920        * @pre   ready() == true
1921        *
1922        * This function gets a reference to a %sub_match object representing the
1923        * part of the target range between the start of the target range and the
1924        * start of the match.
1925        */
1926       const_reference
1927       prefix() const
1928       {
1929 	__glibcxx_assert( ready() );
1930 	return !empty() ? _M_prefix() : _M_unmatched_sub();
1931       }
1932 
1933       /**
1934        * @brief Gets a %sub_match representing the match suffix.
1935        * @pre   ready() == true
1936        *
1937        * This function gets a reference to a %sub_match object representing the
1938        * part of the target range between the end of the match and the end of
1939        * the target range.
1940        */
1941       const_reference
1942       suffix() const
1943       {
1944 	__glibcxx_assert( ready() );
1945 	return !empty() ? _M_suffix() : _M_unmatched_sub();
1946       }
1947 
1948       /**
1949        * @brief Gets an iterator to the start of the %sub_match collection.
1950        */
1951       const_iterator
1952       begin() const noexcept
1953       { return _Base_type::begin(); }
1954 
1955       /**
1956        * @brief Gets an iterator to the start of the %sub_match collection.
1957        */
1958       const_iterator
1959       cbegin() const noexcept
1960       { return this->begin(); }
1961 
1962       /**
1963        * @brief Gets an iterator to one-past-the-end of the collection.
1964        */
1965       const_iterator
1966       end() const noexcept
1967       { return _Base_type::end() - (_Base_type::empty() ? 0 : 3); }
1968 
1969       /**
1970        * @brief Gets an iterator to one-past-the-end of the collection.
1971        */
1972       const_iterator
1973       cend() const noexcept
1974       { return this->end(); }
1975 
1976       ///@}
1977 
1978       /**
1979        * @name 28.10.5 Formatting
1980        *
1981        * These functions perform formatted substitution of the matched
1982        * character sequences into their target.  The format specifiers and
1983        * escape sequences accepted by these functions are determined by
1984        * their @p flags parameter as documented above.
1985        */
1986        ///@{
1987 
1988       /**
1989        * @pre   ready() == true
1990        */
1991       template<typename _Out_iter>
1992 	_Out_iter
1993 	format(_Out_iter __out, const char_type* __fmt_first,
1994 	       const char_type* __fmt_last,
1995 	       match_flag_type __flags = regex_constants::format_default) const;
1996 
1997       /**
1998        * @pre   ready() == true
1999        */
2000       template<typename _Out_iter, typename _St, typename _Sa>
2001 	_Out_iter
2002 	format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
2003 	       match_flag_type __flags = regex_constants::format_default) const
2004 	{
2005 	  return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
2006 			__flags);
2007 	}
2008 
2009       /**
2010        * @pre   ready() == true
2011        */
2012       template<typename _St, typename _Sa>
2013 	basic_string<char_type, _St, _Sa>
2014 	format(const basic_string<char_type, _St, _Sa>& __fmt,
2015 	       match_flag_type __flags = regex_constants::format_default) const
2016 	{
2017 	  basic_string<char_type, _St, _Sa> __result;
2018 	  format(std::back_inserter(__result), __fmt, __flags);
2019 	  return __result;
2020 	}
2021 
2022       /**
2023        * @pre   ready() == true
2024        */
2025       string_type
2026       format(const char_type* __fmt,
2027 	     match_flag_type __flags = regex_constants::format_default) const
2028       {
2029 	string_type __result;
2030 	format(std::back_inserter(__result),
2031 	       __fmt,
2032 	       __fmt + char_traits<char_type>::length(__fmt),
2033 	       __flags);
2034 	return __result;
2035       }
2036 
2037       ///@}
2038 
2039       /**
2040        * @name 28.10.6 Allocator
2041        */
2042       ///@{
2043 
2044       /**
2045        * @brief Gets a copy of the allocator.
2046        */
2047       allocator_type
2048       get_allocator() const noexcept
2049       { return _Base_type::get_allocator(); }
2050 
2051       ///@}
2052 
2053       /**
2054        * @name 28.10.7 Swap
2055        */
2056        ///@{
2057 
2058       /**
2059        * @brief Swaps the contents of two match_results.
2060        */
2061       void
2062       swap(match_results& __that) noexcept
2063       {
2064 	using std::swap;
2065 	_Base_type::swap(__that);
2066 	swap(_M_begin, __that._M_begin);
2067       }
2068       ///@}
2069 
2070     private:
2071       template<typename, typename, typename>
2072 	friend class regex_iterator;
2073 
2074       /// @cond undocumented
2075 
2076       template<typename, typename, typename, bool>
2077 	friend class __detail::_Executor;
2078 
2079       template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
2080 	friend bool
2081 	__detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
2082 				    const basic_regex<_Cp, _Rp>&,
2083 				    regex_constants::match_flag_type,
2084 				    __detail::_RegexExecutorPolicy, bool);
2085 
2086       // Reset contents to __size unmatched sub_match objects
2087       // (plus additional objects for prefix, suffix and unmatched sub).
2088       void
2089       _M_resize(unsigned int __size)
2090       { _Unchecked::assign(__size + 3, sub_match<_Bi_iter>{}); }
2091 
2092       // Set state to a failed match for the given past-the-end iterator.
2093       void
2094       _M_establish_failed_match(_Bi_iter __end)
2095       {
2096 	sub_match<_Bi_iter> __sm;
2097 	__sm.first = __sm.second = __end;
2098 	_Unchecked::assign(3, __sm);
2099       }
2100 
2101       const_reference
2102       _M_unmatched_sub() const
2103       { return _Unchecked::operator[](_Unchecked::size() - 3); }
2104 
2105       sub_match<_Bi_iter>&
2106       _M_unmatched_sub()
2107       { return _Unchecked::operator[](_Unchecked::size() - 3); }
2108 
2109       const_reference
2110       _M_prefix() const
2111       { return _Unchecked::operator[](_Unchecked::size() - 2); }
2112 
2113       sub_match<_Bi_iter>&
2114       _M_prefix()
2115       { return _Unchecked::operator[](_Unchecked::size() - 2); }
2116 
2117       const_reference
2118       _M_suffix() const
2119       { return _Unchecked::operator[](_Unchecked::size() - 1); }
2120 
2121       sub_match<_Bi_iter>&
2122       _M_suffix()
2123       { return _Unchecked::operator[](_Unchecked::size() - 1); }
2124 
2125       _Bi_iter _M_begin {};
2126       /// @endcond
2127     };
2128 
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;
2134 #endif
2135 
2136   // match_results comparisons
2137 
2138   /**
2139    * @brief Compares two match_results for equality.
2140    * @returns true if the two objects refer to the same match,
2141    *          false otherwise.
2142    */
2143   template<typename _Bi_iter, typename _Alloc>
2144     inline bool
2145     operator==(const match_results<_Bi_iter, _Alloc>& __m1,
2146 	       const match_results<_Bi_iter, _Alloc>& __m2)
2147     {
2148       if (__m1.ready() != __m2.ready())
2149 	return false;
2150       if (!__m1.ready())  // both are not ready
2151 	return true;
2152       if (__m1.empty() != __m2.empty())
2153 	return false;
2154       if (__m1.empty())   // both are empty
2155 	return true;
2156       return __m1.prefix() == __m2.prefix()
2157 	&& __m1.size() == __m2.size()
2158 	&& std::equal(__m1.begin(), __m1.end(), __m2.begin())
2159 	&& __m1.suffix() == __m2.suffix();
2160     }
2161 
2162 #if ! __cpp_lib_three_way_comparison
2163   /**
2164    * @brief Compares two match_results for inequality.
2165    * @returns true if the two objects do not refer to the same match,
2166    *          false otherwise.
2167    */
2168   template<typename _Bi_iter, class _Alloc>
2169     inline bool
2170     operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
2171 	       const match_results<_Bi_iter, _Alloc>& __m2)
2172     { return !(__m1 == __m2); }
2173 #endif
2174 
2175   // [7.10.6] match_results swap
2176   /**
2177    * @brief Swaps two match results.
2178    * @param __lhs A match result.
2179    * @param __rhs A match result.
2180    *
2181    * The contents of the two match_results objects are swapped.
2182    */
2183   template<typename _Bi_iter, typename _Alloc>
2184     inline void
2185     swap(match_results<_Bi_iter, _Alloc>& __lhs,
2186 	 match_results<_Bi_iter, _Alloc>& __rhs) noexcept
2187     { __lhs.swap(__rhs); }
2188 
2189 _GLIBCXX_END_NAMESPACE_CXX11
2190 
2191   // [28.11.2] Function template regex_match
2192   /**
2193    * @name Matching, Searching, and Replacing
2194    */
2195   ///@{
2196 
2197   /**
2198    * @brief Determines if there is a match between the regular expression @p e
2199    * and all of the character sequence [first, last).
2200    *
2201    * @param __s     Start of the character sequence to match.
2202    * @param __e     One-past-the-end of the character sequence to match.
2203    * @param __m     The match results.
2204    * @param __re    The regular expression.
2205    * @param __flags Controls how the regular expression is matched.
2206    *
2207    * @retval true  A match exists.
2208    * @retval false Otherwise.
2209    *
2210    * @throws an exception of type regex_error.
2211    */
2212   template<typename _Bi_iter, typename _Alloc,
2213 	   typename _Ch_type, typename _Rx_traits>
2214     inline bool
2215     regex_match(_Bi_iter				 __s,
2216 		_Bi_iter				 __e,
2217 		match_results<_Bi_iter, _Alloc>&	 __m,
2218 		const basic_regex<_Ch_type, _Rx_traits>& __re,
2219 		regex_constants::match_flag_type	 __flags
2220 			       = regex_constants::match_default)
2221     {
2222       return __detail::__regex_algo_impl(__s, __e, __m, __re, __flags,
2223 	__detail::_RegexExecutorPolicy::_S_auto, true);
2224     }
2225 
2226   /**
2227    * @brief Indicates if there is a match between the regular expression @p e
2228    * and all of the character sequence [first, last).
2229    *
2230    * @param __first Beginning of the character sequence to match.
2231    * @param __last  One-past-the-end of the character sequence to match.
2232    * @param __re    The regular expression.
2233    * @param __flags Controls how the regular expression is matched.
2234    *
2235    * @retval true  A match exists.
2236    * @retval false Otherwise.
2237    *
2238    * @throws an exception of type regex_error.
2239    */
2240   template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2241     inline bool
2242     regex_match(_Bi_iter __first, _Bi_iter __last,
2243 		const basic_regex<_Ch_type, _Rx_traits>& __re,
2244 		regex_constants::match_flag_type __flags
2245 		= regex_constants::match_default)
2246     {
2247       match_results<_Bi_iter> __what;
2248       return regex_match(__first, __last, __what, __re, __flags);
2249     }
2250 
2251   /**
2252    * @brief Determines if there is a match between the regular expression @p e
2253    * and a C-style null-terminated string.
2254    *
2255    * @param __s  The C-style null-terminated string to match.
2256    * @param __m  The match results.
2257    * @param __re The regular expression.
2258    * @param __f  Controls how the regular expression is matched.
2259    *
2260    * @retval true  A match exists.
2261    * @retval false Otherwise.
2262    *
2263    * @throws an exception of type regex_error.
2264    */
2265   template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2266     inline bool
2267     regex_match(const _Ch_type* __s,
2268 		match_results<const _Ch_type*, _Alloc>& __m,
2269 		const basic_regex<_Ch_type, _Rx_traits>& __re,
2270 		regex_constants::match_flag_type __f
2271 		= regex_constants::match_default)
2272     { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2273 
2274   /**
2275    * @brief Determines if there is a match between the regular expression @p e
2276    * and a string.
2277    *
2278    * @param __s     The string to match.
2279    * @param __m     The match results.
2280    * @param __re    The regular expression.
2281    * @param __flags Controls how the regular expression is matched.
2282    *
2283    * @retval true  A match exists.
2284    * @retval false Otherwise.
2285    *
2286    * @throws an exception of type regex_error.
2287    */
2288   template<typename _Ch_traits, typename _Ch_alloc,
2289 	   typename _Alloc, typename _Ch_type, typename _Rx_traits>
2290     inline bool
2291     regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2292 		match_results<typename basic_string<_Ch_type,
2293 		_Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2294 		const basic_regex<_Ch_type, _Rx_traits>& __re,
2295 		regex_constants::match_flag_type __flags
2296 		= regex_constants::match_default)
2297     { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2298 
2299   // _GLIBCXX_RESOLVE_LIB_DEFECTS
2300   // 2329. regex_match() with match_results should forbid temporary strings
2301   /// Prevent unsafe attempts to get match_results from a temporary string.
2302   template<typename _Ch_traits, typename _Ch_alloc,
2303 	   typename _Alloc, typename _Ch_type, typename _Rx_traits>
2304     bool
2305     regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2306 		match_results<typename basic_string<_Ch_type,
2307 		_Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2308 		const basic_regex<_Ch_type, _Rx_traits>&,
2309 		regex_constants::match_flag_type
2310 		= regex_constants::match_default) = delete;
2311 
2312   /**
2313    * @brief Indicates if there is a match between the regular expression @p e
2314    * and a C-style null-terminated string.
2315    *
2316    * @param __s  The C-style null-terminated string to match.
2317    * @param __re The regular expression.
2318    * @param __f  Controls how the regular expression is matched.
2319    *
2320    * @retval true  A match exists.
2321    * @retval false Otherwise.
2322    *
2323    * @throws an exception of type regex_error.
2324    */
2325   template<typename _Ch_type, class _Rx_traits>
2326     inline bool
2327     regex_match(const _Ch_type* __s,
2328 		const basic_regex<_Ch_type, _Rx_traits>& __re,
2329 		regex_constants::match_flag_type __f
2330 		= regex_constants::match_default)
2331     { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2332 
2333   /**
2334    * @brief Indicates if there is a match between the regular expression @p e
2335    * and a string.
2336    *
2337    * @param __s     [IN] The string to match.
2338    * @param __re    [IN] The regular expression.
2339    * @param __flags [IN] Controls how the regular expression is matched.
2340    *
2341    * @retval true  A match exists.
2342    * @retval false Otherwise.
2343    *
2344    * @throws an exception of type regex_error.
2345    */
2346   template<typename _Ch_traits, typename _Str_allocator,
2347 	   typename _Ch_type, typename _Rx_traits>
2348     inline bool
2349     regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2350 		const basic_regex<_Ch_type, _Rx_traits>& __re,
2351 		regex_constants::match_flag_type __flags
2352 		= regex_constants::match_default)
2353     { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2354 
2355   // [7.11.3] Function template regex_search
2356   /**
2357    * Searches for a regular expression within a range.
2358    * @param __s     [IN]  The start of the string to search.
2359    * @param __e     [IN]  One-past-the-end of the string to search.
2360    * @param __m     [OUT] The match results.
2361    * @param __re    [IN]  The regular expression to search for.
2362    * @param __flags [IN]  Search policy flags.
2363    * @retval true  A match was found within the string.
2364    * @retval false No match was found within the string, the content of %m is
2365    *               undefined.
2366    *
2367    * @throws an exception of type regex_error.
2368    */
2369   template<typename _Bi_iter, typename _Alloc,
2370 	   typename _Ch_type, typename _Rx_traits>
2371     inline bool
2372     regex_search(_Bi_iter __s, _Bi_iter __e,
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)
2377     {
2378       return __detail::__regex_algo_impl(__s, __e, __m, __re, __flags,
2379 	__detail::_RegexExecutorPolicy::_S_auto, false);
2380     }
2381 
2382   /**
2383    * Searches for a regular expression within a range.
2384    * @param __first [IN]  The start of the string to search.
2385    * @param __last  [IN]  One-past-the-end of the string to search.
2386    * @param __re    [IN]  The regular expression to search for.
2387    * @param __flags [IN]  Search policy flags.
2388    * @retval true  A match was found within the string.
2389    * @retval false No match was found within the string.
2390    *
2391    * @throws an exception of type regex_error.
2392    */
2393   template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2394     inline bool
2395     regex_search(_Bi_iter __first, _Bi_iter __last,
2396 		 const basic_regex<_Ch_type, _Rx_traits>& __re,
2397 		 regex_constants::match_flag_type __flags
2398 		 = regex_constants::match_default)
2399     {
2400       match_results<_Bi_iter> __what;
2401       return regex_search(__first, __last, __what, __re, __flags);
2402     }
2403 
2404   /**
2405    * @brief Searches for a regular expression within a C-string.
2406    * @param __s [IN]  A C-string to search for the regex.
2407    * @param __m [OUT] The set of regex matches.
2408    * @param __e [IN]  The regex to search for in @p s.
2409    * @param __f [IN]  The search flags.
2410    * @retval true  A match was found within the string.
2411    * @retval false No match was found within the string, the content of %m is
2412    *               undefined.
2413    *
2414    * @throws an exception of type regex_error.
2415    */
2416   template<typename _Ch_type, class _Alloc, class _Rx_traits>
2417     inline bool
2418     regex_search(const _Ch_type* __s,
2419 		 match_results<const _Ch_type*, _Alloc>& __m,
2420 		 const basic_regex<_Ch_type, _Rx_traits>& __e,
2421 		 regex_constants::match_flag_type __f
2422 		 = regex_constants::match_default)
2423     { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2424 
2425   /**
2426    * @brief Searches for a regular expression within a C-string.
2427    * @param __s [IN]  The C-string to search.
2428    * @param __e [IN]  The regular expression to search for.
2429    * @param __f [IN]  Search policy flags.
2430    * @retval true  A match was found within the string.
2431    * @retval false No match was found within the string.
2432    *
2433    * @throws an exception of type regex_error.
2434    */
2435   template<typename _Ch_type, typename _Rx_traits>
2436     inline bool
2437     regex_search(const _Ch_type* __s,
2438 		 const basic_regex<_Ch_type, _Rx_traits>& __e,
2439 		 regex_constants::match_flag_type __f
2440 		 = regex_constants::match_default)
2441     { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2442 
2443   /**
2444    * @brief Searches for a regular expression within a string.
2445    * @param __s     [IN]  The string to search.
2446    * @param __e     [IN]  The regular expression to search for.
2447    * @param __flags [IN]  Search policy flags.
2448    * @retval true  A match was found within the string.
2449    * @retval false No match was found within the string.
2450    *
2451    * @throws an exception of type regex_error.
2452    */
2453   template<typename _Ch_traits, typename _String_allocator,
2454 	   typename _Ch_type, typename _Rx_traits>
2455     inline bool
2456     regex_search(const basic_string<_Ch_type, _Ch_traits,
2457 		 _String_allocator>& __s,
2458 		 const basic_regex<_Ch_type, _Rx_traits>& __e,
2459 		 regex_constants::match_flag_type __flags
2460 		 = regex_constants::match_default)
2461     { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2462 
2463   /**
2464    * @brief Searches for a regular expression within a string.
2465    * @param __s [IN]  A C++ string to search for the regex.
2466    * @param __m [OUT] The set of regex matches.
2467    * @param __e [IN]  The regex to search for in @p s.
2468    * @param __f [IN]  The search flags.
2469    * @retval true  A match was found within the string.
2470    * @retval false No match was found within the string, the content of %m is
2471    *               undefined.
2472    *
2473    * @throws an exception of type regex_error.
2474    */
2475   template<typename _Ch_traits, typename _Ch_alloc,
2476 	   typename _Alloc, typename _Ch_type,
2477 	   typename _Rx_traits>
2478     inline bool
2479     regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2480 		 match_results<typename basic_string<_Ch_type,
2481 		 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2482 		 const basic_regex<_Ch_type, _Rx_traits>& __e,
2483 		 regex_constants::match_flag_type __f
2484 		 = regex_constants::match_default)
2485     { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2486 
2487   // _GLIBCXX_RESOLVE_LIB_DEFECTS
2488   // 2329. regex_search() with match_results should forbid temporary strings
2489   /// Prevent unsafe attempts to get match_results from a temporary string.
2490   template<typename _Ch_traits, typename _Ch_alloc,
2491 	   typename _Alloc, typename _Ch_type,
2492 	   typename _Rx_traits>
2493     bool
2494     regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2495 		 match_results<typename basic_string<_Ch_type,
2496 		 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2497 		 const basic_regex<_Ch_type, _Rx_traits>&,
2498 		 regex_constants::match_flag_type
2499 		 = regex_constants::match_default) = delete;
2500 
2501   // std [28.11.4] Function template regex_replace
2502 
2503   template<typename _Out_iter, typename _Bi_iter,
2504 	   typename _Rx_traits, typename _Ch_type>
2505     _Out_iter
2506     __regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2507 		    const basic_regex<_Ch_type, _Rx_traits>& __e,
2508 		    const _Ch_type* __fmt, size_t __len,
2509 		    regex_constants::match_flag_type __flags);
2510 
2511   /**
2512    * @brief Search for a regular expression within a range for multiple times,
2513    and replace the matched parts through filling a format string.
2514    * @param __out   [OUT] The output iterator.
2515    * @param __first [IN]  The start of the string to search.
2516    * @param __last  [IN]  One-past-the-end of the string to search.
2517    * @param __e     [IN]  The regular expression to search for.
2518    * @param __fmt   [IN]  The format string.
2519    * @param __flags [IN]  Search and replace policy flags.
2520    *
2521    * @returns __out
2522    * @throws an exception of type regex_error.
2523    */
2524   template<typename _Out_iter, typename _Bi_iter,
2525 	   typename _Rx_traits, typename _Ch_type,
2526 	   typename _St, typename _Sa>
2527     inline _Out_iter
2528     regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2529 		  const basic_regex<_Ch_type, _Rx_traits>& __e,
2530 		  const basic_string<_Ch_type, _St, _Sa>& __fmt,
2531 		  regex_constants::match_flag_type __flags
2532 		  = regex_constants::match_default)
2533     {
2534       return std::__regex_replace(__out, __first, __last, __e, __fmt.c_str(),
2535 				  __fmt.length(), __flags);
2536     }
2537 
2538   /**
2539    * @brief Search for a regular expression within a range for multiple times,
2540    and replace the matched parts through filling a format C-string.
2541    * @param __out   [OUT] The output iterator.
2542    * @param __first [IN]  The start of the string to search.
2543    * @param __last  [IN]  One-past-the-end of the string to search.
2544    * @param __e     [IN]  The regular expression to search for.
2545    * @param __fmt   [IN]  The format C-string.
2546    * @param __flags [IN]  Search and replace policy flags.
2547    *
2548    * @returns __out
2549    * @throws an exception of type regex_error.
2550    */
2551   template<typename _Out_iter, typename _Bi_iter,
2552 	   typename _Rx_traits, typename _Ch_type>
2553     _Out_iter
2554     regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2555 		  const basic_regex<_Ch_type, _Rx_traits>& __e,
2556 		  const _Ch_type* __fmt,
2557 		  regex_constants::match_flag_type __flags
2558 		  = regex_constants::match_default)
2559     {
2560       return std::__regex_replace(__out, __first, __last, __e, __fmt,
2561 				  char_traits<_Ch_type>::length(__fmt),
2562 				  __flags);
2563     }
2564 
2565 
2566   /**
2567    * @brief Search for a regular expression within a string for multiple times,
2568    and replace the matched parts through filling a format string.
2569    * @param __s     [IN] The string to search and replace.
2570    * @param __e     [IN] The regular expression to search for.
2571    * @param __fmt   [IN] The format string.
2572    * @param __flags [IN] Search and replace policy flags.
2573    *
2574    * @returns The string after replacing.
2575    * @throws an exception of type regex_error.
2576    */
2577   template<typename _Rx_traits, typename _Ch_type,
2578 	   typename _St, typename _Sa, typename _Fst, typename _Fsa>
2579     inline basic_string<_Ch_type, _St, _Sa>
2580     regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2581 		  const basic_regex<_Ch_type, _Rx_traits>& __e,
2582 		  const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2583 		  regex_constants::match_flag_type __flags
2584 		  = regex_constants::match_default)
2585     {
2586       basic_string<_Ch_type, _St, _Sa> __result;
2587       regex_replace(std::back_inserter(__result),
2588 		    __s.begin(), __s.end(), __e, __fmt, __flags);
2589       return __result;
2590     }
2591 
2592   /**
2593    * @brief Search for a regular expression within a string for multiple times,
2594    and replace the matched parts through filling a format C-string.
2595    * @param __s     [IN] The string to search and replace.
2596    * @param __e     [IN] The regular expression to search for.
2597    * @param __fmt   [IN] The format C-string.
2598    * @param __flags [IN] Search and replace policy flags.
2599    *
2600    * @returns The string after replacing.
2601    * @throws an exception of type regex_error.
2602    */
2603   template<typename _Rx_traits, typename _Ch_type,
2604 	   typename _St, typename _Sa>
2605     inline basic_string<_Ch_type, _St, _Sa>
2606     regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2607 		  const basic_regex<_Ch_type, _Rx_traits>& __e,
2608 		  const _Ch_type* __fmt,
2609 		  regex_constants::match_flag_type __flags
2610 		  = regex_constants::match_default)
2611     {
2612       basic_string<_Ch_type, _St, _Sa> __result;
2613       regex_replace(std::back_inserter(__result),
2614 		    __s.begin(), __s.end(), __e, __fmt, __flags);
2615       return __result;
2616     }
2617 
2618   /**
2619    * @brief Search for a regular expression within a C-string for multiple
2620    times, and replace the matched parts through filling a format string.
2621    * @param __s     [IN] The C-string to search and replace.
2622    * @param __e     [IN] The regular expression to search for.
2623    * @param __fmt   [IN] The format string.
2624    * @param __flags [IN] Search and replace policy flags.
2625    *
2626    * @returns The string after replacing.
2627    * @throws an exception of type regex_error.
2628    */
2629   template<typename _Rx_traits, typename _Ch_type,
2630 	   typename _St, typename _Sa>
2631     inline basic_string<_Ch_type>
2632     regex_replace(const _Ch_type* __s,
2633 		  const basic_regex<_Ch_type, _Rx_traits>& __e,
2634 		  const basic_string<_Ch_type, _St, _Sa>& __fmt,
2635 		  regex_constants::match_flag_type __flags
2636 		  = regex_constants::match_default)
2637     {
2638       basic_string<_Ch_type> __result;
2639       regex_replace(std::back_inserter(__result), __s,
2640 		    __s + char_traits<_Ch_type>::length(__s),
2641 		    __e, __fmt, __flags);
2642       return __result;
2643     }
2644 
2645   /**
2646    * @brief Search for a regular expression within a C-string for multiple
2647    times, and replace the matched parts through filling a format C-string.
2648    * @param __s     [IN] The C-string to search and replace.
2649    * @param __e     [IN] The regular expression to search for.
2650    * @param __fmt   [IN] The format C-string.
2651    * @param __flags [IN] Search and replace policy flags.
2652    *
2653    * @returns The string after replacing.
2654    * @throws an exception of type regex_error.
2655    */
2656   template<typename _Rx_traits, typename _Ch_type>
2657     inline basic_string<_Ch_type>
2658     regex_replace(const _Ch_type* __s,
2659 		  const basic_regex<_Ch_type, _Rx_traits>& __e,
2660 		  const _Ch_type* __fmt,
2661 		  regex_constants::match_flag_type __flags
2662 		  = regex_constants::match_default)
2663     {
2664       basic_string<_Ch_type> __result;
2665       regex_replace(std::back_inserter(__result), __s,
2666 		    __s + char_traits<_Ch_type>::length(__s),
2667 		    __e, __fmt, __flags);
2668       return __result;
2669     }
2670 
2671   ///@}
2672 
2673 _GLIBCXX_BEGIN_NAMESPACE_CXX11
2674 
2675   // std [28.12] Class template regex_iterator
2676   /**
2677    * An iterator adaptor that will provide repeated calls of regex_search over
2678    * a range until no more matches remain.
2679    */
2680   template<typename _Bi_iter,
2681 	   typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2682 	   typename _Rx_traits = regex_traits<_Ch_type> >
2683     class regex_iterator
2684     {
2685     public:
2686       typedef basic_regex<_Ch_type, _Rx_traits>  regex_type;
2687       typedef match_results<_Bi_iter>	    value_type;
2688       typedef std::ptrdiff_t		     difference_type;
2689       typedef const value_type*		  pointer;
2690       typedef const value_type&		  reference;
2691       typedef std::forward_iterator_tag	  iterator_category;
2692 
2693       /**
2694        * @brief Provides a singular iterator, useful for indicating
2695        * one-past-the-end of a range.
2696        */
2697       regex_iterator() = default;
2698 
2699       /**
2700        * Constructs a %regex_iterator...
2701        * @param __a  [IN] The start of a text range to search.
2702        * @param __b  [IN] One-past-the-end of the text range to search.
2703        * @param __re [IN] The regular expression to match.
2704        * @param __m  [IN] Policy flags for match rules.
2705        */
2706       regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2707 		     regex_constants::match_flag_type __m
2708 		     = regex_constants::match_default)
2709       : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2710       {
2711 	if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2712 	  *this = regex_iterator();
2713       }
2714 
2715       // _GLIBCXX_RESOLVE_LIB_DEFECTS
2716       // 2332. regex_iterator should forbid temporary regexes
2717       regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2718 		     regex_constants::match_flag_type
2719 		     = regex_constants::match_default) = delete;
2720 
2721       /// Copy constructs a %regex_iterator.
2722       regex_iterator(const regex_iterator&) = default;
2723 
2724       /// Copy assigns one %regex_iterator to another.
2725       regex_iterator&
2726       operator=(const regex_iterator&) = default;
2727 
2728       ~regex_iterator() = default;
2729 
2730       /**
2731        * @brief Tests the equivalence of two regex iterators.
2732        */
2733       bool
2734       operator==(const regex_iterator&) const noexcept;
2735 
2736 #if __cplusplus >= 202002L
2737       // _GLIBCXX_RESOLVE_LIB_DEFECTS
2738       // 3719. Directory iterators should be usable with default sentinel
2739       bool operator==(default_sentinel_t) const noexcept
2740       { return _M_pregex == nullptr; }
2741 #endif
2742 
2743 #if __cpp_impl_three_way_comparison < 201907L
2744       /**
2745        * @brief Tests the inequivalence of two regex iterators.
2746        */
2747       bool
2748       operator!=(const regex_iterator& __rhs) const noexcept
2749       { return !(*this == __rhs); }
2750 #endif
2751 
2752       /**
2753        * @brief Dereferences a %regex_iterator.
2754        */
2755       const value_type&
2756       operator*() const noexcept
2757       { return _M_match; }
2758 
2759       /**
2760        * @brief Selects a %regex_iterator member.
2761        */
2762       const value_type*
2763       operator->() const noexcept
2764       { return &_M_match; }
2765 
2766       /**
2767        * @brief Increments a %regex_iterator.
2768        */
2769       regex_iterator&
2770       operator++();
2771 
2772       /**
2773        * @brief Postincrements a %regex_iterator.
2774        */
2775       regex_iterator
2776       operator++(int)
2777       {
2778 	auto __tmp = *this;
2779 	++(*this);
2780 	return __tmp;
2781       }
2782 
2783     private:
2784       _Bi_iter				_M_begin {};
2785       _Bi_iter				_M_end {};
2786       const regex_type*			_M_pregex = nullptr;
2787       regex_constants::match_flag_type	_M_flags {};
2788       match_results<_Bi_iter>		_M_match;
2789     };
2790 
2791   typedef regex_iterator<const char*>			cregex_iterator;
2792   typedef regex_iterator<string::const_iterator>	sregex_iterator;
2793 #ifdef _GLIBCXX_USE_WCHAR_T
2794   typedef regex_iterator<const wchar_t*>		wcregex_iterator;
2795   typedef regex_iterator<wstring::const_iterator>	wsregex_iterator;
2796 #endif
2797 
2798   // [7.12.2] Class template regex_token_iterator
2799   /**
2800    * Iterates over submatches in a range (or @a splits a text string).
2801    *
2802    * The purpose of this iterator is to enumerate all, or all specified,
2803    * matches of a regular expression within a text range.  The dereferenced
2804    * value of an iterator of this class is a std::sub_match object.
2805    */
2806   template<typename _Bi_iter,
2807 	   typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2808 	   typename _Rx_traits = regex_traits<_Ch_type> >
2809     class regex_token_iterator
2810     {
2811     public:
2812       typedef basic_regex<_Ch_type, _Rx_traits>	regex_type;
2813       typedef sub_match<_Bi_iter>		value_type;
2814       typedef std::ptrdiff_t			difference_type;
2815       typedef const value_type*			pointer;
2816       typedef const value_type&			reference;
2817       typedef std::forward_iterator_tag		iterator_category;
2818 
2819     public:
2820       /**
2821        * @brief Default constructs a %regex_token_iterator.
2822        *
2823        * A default-constructed %regex_token_iterator is a singular iterator
2824        * that will compare equal to the one-past-the-end value for any
2825        * iterator of the same type.
2826        */
2827       regex_token_iterator()
2828       : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2829       _M_has_m1(false)
2830       { }
2831 
2832       /**
2833        * Constructs a %regex_token_iterator...
2834        * @param __a          [IN] The start of the text to search.
2835        * @param __b          [IN] One-past-the-end of the text to search.
2836        * @param __re         [IN] The regular expression to search for.
2837        * @param __submatch   [IN] Which submatch to return.  There are some
2838        *                        special values for this parameter:
2839        *                        - -1 each enumerated subexpression does NOT
2840        *                          match the regular expression (aka field
2841        *                          splitting)
2842        *                        - 0 the entire string matching the
2843        *                          subexpression is returned for each match
2844        *                          within the text.
2845        *                        - >0 enumerates only the indicated
2846        *                          subexpression from a match within the text.
2847        * @param __m          [IN] Policy flags for match rules.
2848        */
2849       regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2850 			   int __submatch = 0,
2851 			   regex_constants::match_flag_type __m
2852 			   = regex_constants::match_default)
2853       : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2854       { _M_init(__a, __b); }
2855 
2856       /**
2857        * Constructs a %regex_token_iterator...
2858        * @param __a          [IN] The start of the text to search.
2859        * @param __b          [IN] One-past-the-end of the text to search.
2860        * @param __re         [IN] The regular expression to search for.
2861        * @param __submatches [IN] A list of subexpressions to return for each
2862        *                          regular expression match within the text.
2863        * @param __m          [IN] Policy flags for match rules.
2864        */
2865       regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2866 			   const regex_type& __re,
2867 			   const std::vector<int>& __submatches,
2868 			   regex_constants::match_flag_type __m
2869 			     = regex_constants::match_default)
2870       : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2871       { _M_init(__a, __b); }
2872 
2873       /**
2874        * Constructs a %regex_token_iterator...
2875        * @param __a          [IN] The start of the text to search.
2876        * @param __b          [IN] One-past-the-end of the text to search.
2877        * @param __re         [IN] The regular expression to search for.
2878        * @param __submatches [IN] A list of subexpressions to return for each
2879        *                          regular expression match within the text.
2880        * @param __m          [IN] Policy flags for match rules.
2881        */
2882       regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2883 			   const regex_type& __re,
2884 			   initializer_list<int> __submatches,
2885 			   regex_constants::match_flag_type __m
2886 			     = regex_constants::match_default)
2887       : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2888       { _M_init(__a, __b); }
2889 
2890       /**
2891        * Constructs a %regex_token_iterator...
2892        * @param __a          [IN] The start of the text to search.
2893        * @param __b          [IN] One-past-the-end of the text to search.
2894        * @param __re         [IN] The regular expression to search for.
2895        * @param __submatches [IN] A list of subexpressions to return for each
2896        *                          regular expression match within the text.
2897        * @param __m          [IN] Policy flags for match rules.
2898        */
2899       template<std::size_t _Nm>
2900 	regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2901 			     const regex_type& __re,
2902 			     const int (&__submatches)[_Nm],
2903 			     regex_constants::match_flag_type __m
2904 			     = regex_constants::match_default)
2905       : _M_position(__a, __b, __re, __m),
2906       _M_subs(__submatches, __submatches + _Nm), _M_n(0)
2907       { _M_init(__a, __b); }
2908 
2909       // _GLIBCXX_RESOLVE_LIB_DEFECTS
2910       // 2332. regex_token_iterator should forbid temporary regexes
2911       regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
2912 			   regex_constants::match_flag_type =
2913 			   regex_constants::match_default) = delete;
2914       regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2915 			   const std::vector<int>&,
2916 			   regex_constants::match_flag_type =
2917 			   regex_constants::match_default) = delete;
2918       regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2919 			   initializer_list<int>,
2920 			   regex_constants::match_flag_type =
2921 			   regex_constants::match_default) = delete;
2922       template <std::size_t _Nm>
2923 	regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2924 			     const int (&)[_Nm],
2925 			     regex_constants::match_flag_type =
2926 			     regex_constants::match_default) = delete;
2927 
2928       /**
2929        * @brief Copy constructs a %regex_token_iterator.
2930        * @param __rhs [IN] A %regex_token_iterator to copy.
2931        */
2932       regex_token_iterator(const regex_token_iterator& __rhs)
2933       : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2934       _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_has_m1(__rhs._M_has_m1)
2935       { _M_normalize_result(); }
2936 
2937       /**
2938        * @brief Assigns a %regex_token_iterator to another.
2939        * @param __rhs [IN] A %regex_token_iterator to copy.
2940        */
2941       regex_token_iterator&
2942       operator=(const regex_token_iterator& __rhs);
2943 
2944       /**
2945        * @brief Compares a %regex_token_iterator to another for equality.
2946        */
2947       bool
2948       operator==(const regex_token_iterator& __rhs) const;
2949 
2950 #if __cplusplus >= 202002L
2951       // _GLIBCXX_RESOLVE_LIB_DEFECTS
2952       // 3719. Directory iterators should be usable with default sentinel
2953       bool operator==(default_sentinel_t) const noexcept
2954       { return _M_end_of_seq(); }
2955 #endif
2956 
2957 #if __cpp_impl_three_way_comparison < 201907L
2958       /**
2959        * @brief Compares a %regex_token_iterator to another for inequality.
2960        */
2961       bool
2962       operator!=(const regex_token_iterator& __rhs) const
2963       { return !(*this == __rhs); }
2964 #endif
2965 
2966       /**
2967        * @brief Dereferences a %regex_token_iterator.
2968        */
2969       const value_type&
2970       operator*() const
2971       { return *_M_result; }
2972 
2973       /**
2974        * @brief Selects a %regex_token_iterator member.
2975        */
2976       const value_type*
2977       operator->() const
2978       { return _M_result; }
2979 
2980       /**
2981        * @brief Increments a %regex_token_iterator.
2982        */
2983       regex_token_iterator&
2984       operator++();
2985 
2986       /**
2987        * @brief Postincrements a %regex_token_iterator.
2988        */
2989       regex_token_iterator
2990       operator++(int)
2991       {
2992 	auto __tmp = *this;
2993 	++(*this);
2994 	return __tmp;
2995       }
2996 
2997     private:
2998       typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2999 
3000       void
3001       _M_init(_Bi_iter __a, _Bi_iter __b);
3002 
3003       const value_type&
3004       _M_current_match() const
3005       {
3006 	if (_M_subs[_M_n] == -1)
3007 	  return (*_M_position).prefix();
3008 	else
3009 	  return (*_M_position)[_M_subs[_M_n]];
3010       }
3011 
3012       constexpr bool
3013       _M_end_of_seq() const noexcept
3014       { return _M_result == nullptr; }
3015 
3016       // [28.12.2.2.4]
3017       void
3018       _M_normalize_result()
3019       {
3020 	if (_M_position != _Position())
3021 	  _M_result = &_M_current_match();
3022 	else if (_M_has_m1)
3023 	  _M_result = &_M_suffix;
3024 	else
3025 	  _M_result = nullptr;
3026       }
3027 
3028       _Position		_M_position;
3029       std::vector<int>	_M_subs;
3030       value_type	_M_suffix;
3031       std::size_t	_M_n;
3032       const value_type*	_M_result;
3033 
3034       // Show whether _M_subs contains -1
3035       bool		_M_has_m1;
3036     };
3037 
3038   /** @brief Token iterator for C-style NULL-terminated strings. */
3039   typedef regex_token_iterator<const char*>		cregex_token_iterator;
3040 
3041   /** @brief Token iterator for standard strings. */
3042   typedef regex_token_iterator<string::const_iterator>	sregex_token_iterator;
3043 
3044 #ifdef _GLIBCXX_USE_WCHAR_T
3045   /** @brief Token iterator for C-style NULL-terminated wide strings. */
3046   typedef regex_token_iterator<const wchar_t*>		wcregex_token_iterator;
3047 
3048   /** @brief Token iterator for standard wide-character strings. */
3049   typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
3050 #endif
3051 
3052   ///@} // group regex
3053 
3054 _GLIBCXX_END_NAMESPACE_CXX11
3055 _GLIBCXX_END_NAMESPACE_VERSION
3056 } // namespace
3057 
3058 #include <bits/regex.tcc>
3059