OSDN Git Service

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