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