OSDN Git Service

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