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