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