18cd882041414dc9213b3ac006d842a1888d623b
[platform/upstream/gcc.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),
1762                __fmt + char_traits<char_type>::length(__fmt),
1763                __flags);
1764         return __result;
1765       }
1766
1767       //@} 
1768
1769       /**
1770        * @name 10.5 Allocator
1771        */
1772       //@{ 
1773
1774       /**
1775        * @brief Gets a copy of the allocator.
1776        */
1777       allocator_type
1778       get_allocator() const
1779       { return _Base_type::get_allocator(); }
1780       
1781       //@} 
1782
1783       /**
1784        * @name 10.6 Swap
1785        */
1786        //@{ 
1787
1788       /**
1789        * @brief Swaps the contents of two match_results.
1790        */
1791       void
1792       swap(match_results& __that)
1793       { _Base_type::swap(__that); }
1794       //@} 
1795       
1796     private:
1797       friend class __regex::_SpecializedResults<_Bi_iter, _Allocator>;
1798     };
1799   
1800   typedef match_results<const char*>             cmatch;
1801   typedef match_results<string::const_iterator>  smatch;
1802 #ifdef _GLIBCXX_USE_WCHAR_T
1803   typedef match_results<const wchar_t*>          wcmatch;
1804   typedef match_results<wstring::const_iterator> wsmatch;
1805 #endif
1806
1807   // match_results comparisons
1808   /**
1809    * @brief Compares two match_results for equality.
1810    * @returns true if the two objects refer to the same match,
1811    * false otherwise.
1812    */
1813   template<typename _Bi_iter, typename _Allocator>
1814     inline bool
1815     operator==(const match_results<_Bi_iter, _Allocator>& __m1,
1816                const match_results<_Bi_iter, _Allocator>& __m2)
1817     {
1818       if (__m1.ready() != __m2.ready())
1819         return false;
1820       if (!__m1.ready())  // both are not ready
1821         return true;
1822       if (__m1.empty() != __m2.empty())
1823         return false;
1824       if (__m1.empty())   // both are empty
1825         return true;
1826       return __m1.prefix() == __m2.prefix()
1827         && __m1.size() == __m2.size()
1828         && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1829         && __m1.suffix() == __m2.suffix();
1830     }
1831
1832   /**
1833    * @brief Compares two match_results for inequality.
1834    * @returns true if the two objects do not refer to the same match,
1835    * false otherwise.
1836    */
1837   template<typename _Bi_iter, class _Allocator>
1838     inline bool
1839     operator!=(const match_results<_Bi_iter, _Allocator>& __m1,
1840                const match_results<_Bi_iter, _Allocator>& __m2)
1841     { return !(__m1 == __m2); }
1842
1843   // [7.10.6] match_results swap
1844   /**
1845    * @brief Swaps two match results.
1846    * @param lhs A match result.
1847    * @param rhs A match result.
1848    *
1849    * The contents of the two match_results objects are swapped.
1850    */
1851   template<typename _Bi_iter, typename _Allocator>
1852     inline void
1853     swap(match_results<_Bi_iter, _Allocator>& __lhs,
1854          match_results<_Bi_iter, _Allocator>& __rhs)
1855     { __lhs.swap(__rhs); }
1856
1857   // [7.11.2] Function template regex_match
1858   /**
1859    * @name Matching, Searching, and Replacing
1860    */
1861   //@{
1862
1863   /**
1864    * @brief Determines if there is a match between the regular expression @p e
1865    * and all of the character sequence [first, last).
1866    *
1867    * @param s     Start of the character sequence to match.
1868    * @param e     One-past-the-end of the character sequence to match.
1869    * @param m     The match results.
1870    * @param re    The regular expression.
1871    * @param flags Controls how the regular expression is matched.
1872    *
1873    * @retval true  A match exists.
1874    * @retval false Otherwise.
1875    *
1876    * @throws an exception of type regex_error.
1877    *
1878    * @todo Implement this function.
1879    */
1880   template<typename _Bi_iter, typename _Allocator,
1881            typename _Ch_type, typename _Rx_traits>
1882     bool
1883     regex_match(_Bi_iter                                 __s,
1884                 _Bi_iter                                 __e,
1885                 match_results<_Bi_iter, _Allocator>&     __m,
1886                 const basic_regex<_Ch_type, _Rx_traits>& __re,
1887                 regex_constants::match_flag_type         __flags
1888                                = regex_constants::match_default)
1889     {
1890       __regex::_AutomatonPtr __a = __re._M_get_automaton();
1891       __regex::_Automaton::_SizeT __sz = __a->_M_sub_count();
1892       __regex::_SpecializedCursor<_Bi_iter> __cs(__s, __e);
1893       __regex::_SpecializedResults<_Bi_iter, _Allocator> __r(__sz, __cs, __m);
1894       __regex::_Grep_matcher __matcher(__cs, __r, __a, __flags);
1895       return __m[0].matched;
1896     }
1897
1898   /**
1899    * @brief Indicates if there is a match between the regular expression @p e
1900    * and all of the character sequence [first, last).
1901    *
1902    * @param first Beginning of the character sequence to match.
1903    * @param last  One-past-the-end of the character sequence to match.
1904    * @param re    The regular expression.
1905    * @param flags Controls how the regular expression is matched.
1906    *
1907    * @retval true  A match exists.
1908    * @retval false Otherwise.
1909    *
1910    * @throws an exception of type regex_error.
1911    */
1912   template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1913     bool
1914     regex_match(_Bi_iter __first, _Bi_iter __last,
1915                 const basic_regex<_Ch_type, _Rx_traits>& __re,
1916                 regex_constants::match_flag_type __flags
1917                 = regex_constants::match_default)
1918     { 
1919       match_results<_Bi_iter> __what;
1920       return regex_match(__first, __last, __what, __re, __flags);
1921     }
1922
1923   /**
1924    * @brief Determines if there is a match between the regular expression @p e
1925    * and a C-style null-terminated string.
1926    *
1927    * @param s  The C-style null-terminated string to match.
1928    * @param m  The match results.
1929    * @param re The regular expression.
1930    * @param f  Controls how the regular expression is matched.
1931    *
1932    * @retval true  A match exists.
1933    * @retval false Otherwise.
1934    *
1935    * @throws an exception of type regex_error.
1936    */
1937   template<typename _Ch_type, typename _Allocator, typename _Rx_traits>
1938     inline bool
1939     regex_match(const _Ch_type* __s,
1940                 match_results<const _Ch_type*, _Allocator>& __m,
1941                 const basic_regex<_Ch_type, _Rx_traits>& __re,
1942                 regex_constants::match_flag_type __f
1943                 = regex_constants::match_default)
1944     { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
1945
1946   /**
1947    * @brief Determines if there is a match between the regular expression @p e
1948    * and a string.
1949    *
1950    * @param s     The string to match.
1951    * @param m     The match results.
1952    * @param re    The regular expression.
1953    * @param flags Controls how the regular expression is matched.
1954    *
1955    * @retval true  A match exists.
1956    * @retval false Otherwise.
1957    *
1958    * @throws an exception of type regex_error.
1959    */
1960   template<typename _Ch_traits, typename _Ch_alloc,
1961            typename _Allocator, typename _Ch_type, typename _Rx_traits>
1962     inline bool
1963     regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
1964                 match_results<typename basic_string<_Ch_type, 
1965                 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
1966                 const basic_regex<_Ch_type, _Rx_traits>& __re,
1967                 regex_constants::match_flag_type __flags
1968                 = regex_constants::match_default)
1969     { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
1970
1971   /**
1972    * @brief Indicates if there is a match between the regular expression @p e
1973    * and a C-style null-terminated string.
1974    *
1975    * @param s  The C-style null-terminated string to match.
1976    * @param re The regular expression.
1977    * @param f  Controls how the regular expression is matched.
1978    *
1979    * @retval true  A match exists.
1980    * @retval false Otherwise.
1981    *
1982    * @throws an exception of type regex_error.
1983    */
1984   template<typename _Ch_type, class _Rx_traits>
1985     inline bool
1986     regex_match(const _Ch_type* __s,
1987                 const basic_regex<_Ch_type, _Rx_traits>& __re,
1988                 regex_constants::match_flag_type __f
1989                 = regex_constants::match_default)
1990     { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
1991
1992   /**
1993    * @brief Indicates if there is a match between the regular expression @p e
1994    * and a string.
1995    *
1996    * @param s     [IN] The string to match.
1997    * @param re    [IN] The regular expression.
1998    * @param flags [IN] Controls how the regular expression is matched.
1999    *
2000    * @retval true  A match exists.
2001    * @retval false Otherwise.
2002    *
2003    * @throws an exception of type regex_error.
2004    */
2005   template<typename _Ch_traits, typename _Str_allocator,
2006            typename _Ch_type, typename _Rx_traits>
2007     inline bool
2008     regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2009                 const basic_regex<_Ch_type, _Rx_traits>& __re,
2010                 regex_constants::match_flag_type __flags
2011                 = regex_constants::match_default)
2012     { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2013
2014   // [7.11.3] Function template regex_search
2015   /**
2016    * Searches for a regular expression within a range.
2017    * @param first [IN]  The start of the string to search.
2018    * @param last  [IN]  One-past-the-end of the string to search.
2019    * @param m     [OUT] The match results.
2020    * @param re    [IN]  The regular expression to search for.
2021    * @param flags [IN]  Search policy flags.
2022    * @retval true  A match was found within the string.
2023    * @retval false No match was found within the string, the content of %m is
2024    *               undefined.
2025    *
2026    * @throws an exception of type regex_error.
2027    *
2028    * @todo Implement this function.
2029    */
2030   template<typename _Bi_iter, typename _Allocator,
2031            typename _Ch_type, typename _Rx_traits>
2032     inline bool
2033     regex_search(_Bi_iter __first, _Bi_iter __last,
2034                  match_results<_Bi_iter, _Allocator>& __m,
2035                  const basic_regex<_Ch_type, _Rx_traits>& __re,
2036                  regex_constants::match_flag_type __flags
2037                  = regex_constants::match_default)
2038     { return false; }
2039
2040   /**
2041    * Searches for a regular expression within a range.
2042    * @param first [IN]  The start of the string to search.
2043    * @param last  [IN]  One-past-the-end of the string to search.
2044    * @param re    [IN]  The regular expression to search for.
2045    * @param flags [IN]  Search policy flags.
2046    * @retval true  A match was found within the string.
2047    * @retval false No match was found within the string.
2048    * @doctodo
2049    *
2050    * @throws an exception of type regex_error.
2051    */
2052   template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2053     inline bool
2054     regex_search(_Bi_iter __first, _Bi_iter __last,
2055                  const basic_regex<_Ch_type, _Rx_traits>& __re,
2056                  regex_constants::match_flag_type __flags
2057                  = regex_constants::match_default)
2058     {
2059       match_results<_Bi_iter> __what;
2060       return regex_search(__first, __last, __what, __re, __flags);
2061     }
2062
2063   /**
2064    * @brief Searches for a regular expression within a C-string.
2065    * @param s [IN]  A C-string to search for the regex.
2066    * @param m [OUT] The set of regex matches.
2067    * @param e [IN]  The regex to search for in @p s.
2068    * @param f [IN]  The search flags.
2069    * @retval true  A match was found within the string.
2070    * @retval false No match was found within the string, the content of %m is
2071    *               undefined.
2072    * @doctodo
2073    *
2074    * @throws an exception of type regex_error.
2075    */
2076   template<typename _Ch_type, class _Allocator, class _Rx_traits>
2077     inline bool
2078     regex_search(const _Ch_type* __s,
2079                  match_results<const _Ch_type*, _Allocator>& __m,
2080                  const basic_regex<_Ch_type, _Rx_traits>& __e,
2081                  regex_constants::match_flag_type __f
2082                  = regex_constants::match_default)
2083     { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2084
2085   /**
2086    * @brief Searches for a regular expression within a C-string.
2087    * @param s [IN]  The C-string to search.
2088    * @param e [IN]  The regular expression to search for.
2089    * @param f [IN]  Search policy flags.
2090    * @retval true  A match was found within the string.
2091    * @retval false No match was found within the string.
2092    * @doctodo
2093    *
2094    * @throws an exception of type regex_error.
2095    */
2096   template<typename _Ch_type, typename _Rx_traits>
2097     inline bool
2098     regex_search(const _Ch_type* __s,
2099                  const basic_regex<_Ch_type, _Rx_traits>& __e,
2100                  regex_constants::match_flag_type __f
2101                  = regex_constants::match_default)
2102     { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2103
2104   /**
2105    * @brief Searches for a regular expression within a string.
2106    * @param s     [IN]  The string to search.
2107    * @param e     [IN]  The regular expression to search for.
2108    * @param flags [IN]  Search policy flags.
2109    * @retval true  A match was found within the string.
2110    * @retval false No match was found within the string.
2111    * @doctodo
2112    *
2113    * @throws an exception of type regex_error.
2114    */
2115   template<typename _Ch_traits, typename _String_allocator,
2116            typename _Ch_type, typename _Rx_traits>
2117     inline bool
2118     regex_search(const basic_string<_Ch_type, _Ch_traits,
2119                  _String_allocator>& __s,
2120                  const basic_regex<_Ch_type, _Rx_traits>& __e,
2121                  regex_constants::match_flag_type __flags
2122                  = regex_constants::match_default)
2123     { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2124
2125   /**
2126    * @brief Searches for a regular expression within a string.
2127    * @param s [IN]  A C++ string to search for the regex.
2128    * @param m [OUT] The set of regex matches.
2129    * @param e [IN]  The regex to search for in @p s.
2130    * @param f [IN]  The search flags.
2131    * @retval true  A match was found within the string.
2132    * @retval false No match was found within the string, the content of %m is
2133    *               undefined.
2134    *
2135    * @throws an exception of type regex_error.
2136    */
2137   template<typename _Ch_traits, typename _Ch_alloc,
2138            typename _Allocator, typename _Ch_type,
2139            typename _Rx_traits>
2140     inline bool
2141     regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2142                  match_results<typename basic_string<_Ch_type,
2143                  _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
2144                  const basic_regex<_Ch_type, _Rx_traits>& __e,
2145                  regex_constants::match_flag_type __f
2146                  = regex_constants::match_default)
2147     { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2148
2149   // std [28.11.4] Function template regex_replace
2150   /**
2151    * @doctodo
2152    * @param out
2153    * @param first
2154    * @param last
2155    * @param e
2156    * @param fmt
2157    * @param flags
2158    *
2159    * @returns out
2160    * @throws an exception of type regex_error.
2161    *
2162    * @todo Implement this function.
2163    */
2164   template<typename _Out_iter, typename _Bi_iter,
2165            typename _Rx_traits, typename _Ch_type>
2166     inline _Out_iter
2167     regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2168                   const basic_regex<_Ch_type, _Rx_traits>& __e,
2169                   const basic_string<_Ch_type>& __fmt,
2170                   regex_constants::match_flag_type __flags
2171                   = regex_constants::match_default)
2172     { return __out; }
2173
2174   /**
2175    * @doctodo
2176    * @param s
2177    * @param e
2178    * @param fmt
2179    * @param flags
2180    *
2181    * @returns a copy of string @p s with replacements.
2182    *
2183    * @throws an exception of type regex_error.
2184    */
2185   template<typename _Rx_traits, typename _Ch_type>
2186     inline basic_string<_Ch_type>
2187     regex_replace(const basic_string<_Ch_type>& __s,
2188                   const basic_regex<_Ch_type, _Rx_traits>& __e,
2189                   const basic_string<_Ch_type>& __fmt,
2190                   regex_constants::match_flag_type __flags
2191                   = regex_constants::match_default)
2192     {
2193       std::string __result;
2194       regex_replace(std::back_inserter(__result),
2195                     __s.begin(), __s.end(), __e, __fmt, __flags);
2196       return __result;
2197     }
2198
2199   //@}
2200
2201   // std [28.12] Class template regex_iterator
2202   /**
2203    * An iterator adaptor that will provide repeated calls of regex_search over 
2204    * a range until no more matches remain.
2205    */
2206   template<typename _Bi_iter,
2207            typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2208            typename _Rx_traits = regex_traits<_Ch_type> >
2209     class regex_iterator
2210     {
2211     public:
2212       typedef basic_regex<_Ch_type, _Rx_traits>  regex_type;
2213       typedef match_results<_Bi_iter>            value_type;
2214       typedef std::ptrdiff_t                     difference_type;
2215       typedef const value_type*                  pointer;
2216       typedef const value_type&                  reference;
2217       typedef std::forward_iterator_tag          iterator_category;
2218
2219     public:
2220       /**
2221        * @brief Provides a singular iterator, useful for indicating
2222        * one-past-the-end of a range.
2223        * @todo Implement this function.
2224        * @doctodo
2225        */
2226       regex_iterator();
2227       
2228       /**
2229        * Constructs a %regex_iterator...
2230        * @param a  [IN] The start of a text range to search.
2231        * @param b  [IN] One-past-the-end of the text range to search.
2232        * @param re [IN] The regular expression to match.
2233        * @param m  [IN] Policy flags for match rules.
2234        * @todo Implement this function.
2235        * @doctodo
2236        */
2237       regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2238                      regex_constants::match_flag_type __m
2239                      = regex_constants::match_default);
2240
2241       /**
2242        * Copy constructs a %regex_iterator.
2243        * @todo Implement this function.
2244        * @doctodo
2245        */
2246       regex_iterator(const regex_iterator& __rhs);
2247       
2248       /**
2249        * @todo Implement this function.
2250        * @doctodo
2251        */
2252       regex_iterator&
2253       operator=(const regex_iterator& __rhs);
2254       
2255       /**
2256        * @todo Implement this function.
2257        * @doctodo
2258        */
2259       bool
2260       operator==(const regex_iterator& __rhs);
2261       
2262       /**
2263        * @todo Implement this function.
2264        * @doctodo
2265        */
2266       bool
2267       operator!=(const regex_iterator& __rhs);
2268       
2269       /**
2270        * @todo Implement this function.
2271        * @doctodo
2272        */
2273       const value_type&
2274       operator*();
2275       
2276       /**
2277        * @todo Implement this function.
2278        * @doctodo
2279        */
2280       const value_type*
2281       operator->();
2282       
2283       /**
2284        * @todo Implement this function.
2285        * @doctodo
2286        */
2287       regex_iterator&
2288       operator++();
2289       
2290       /**
2291        * @todo Implement this function.
2292        * @doctodo
2293        */
2294       regex_iterator
2295       operator++(int);
2296       
2297     private:
2298       // these members are shown for exposition only:
2299       _Bi_iter                         begin;
2300       _Bi_iter                         end;
2301       const regex_type*                pregex;
2302       regex_constants::match_flag_type flags;
2303       match_results<_Bi_iter>          match;
2304     };
2305   
2306   typedef regex_iterator<const char*>             cregex_iterator;
2307   typedef regex_iterator<string::const_iterator>  sregex_iterator;
2308 #ifdef _GLIBCXX_USE_WCHAR_T
2309   typedef regex_iterator<const wchar_t*>          wcregex_iterator;
2310   typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2311 #endif
2312
2313   // [7.12.2] Class template regex_token_iterator
2314   /**
2315    * Iterates over submatches in a range (or @a splits a text string).
2316    *
2317    * The purpose of this iterator is to enumerate all, or all specified,
2318    * matches of a regular expression within a text range.  The dereferenced
2319    * value of an iterator of this class is a std::sub_match object.
2320    */
2321   template<typename _Bi_iter,
2322            typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2323            typename _Rx_traits = regex_traits<_Ch_type> >
2324     class regex_token_iterator
2325     {
2326     public:
2327       typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2328       typedef sub_match<_Bi_iter>               value_type;
2329       typedef std::ptrdiff_t                    difference_type;
2330       typedef const value_type*                 pointer;
2331       typedef const value_type&                 reference;
2332       typedef std::forward_iterator_tag         iterator_category;
2333       
2334     public:
2335       /**
2336        * @brief Default constructs a %regex_token_iterator.
2337        * @todo Implement this function.
2338        * 
2339        * A default-constructed %regex_token_iterator is a singular iterator
2340        * that will compare equal to the one-past-the-end value for any
2341        * iterator of the same type.
2342        */
2343       regex_token_iterator();
2344       
2345       /**
2346        * Constructs a %regex_token_iterator...
2347        * @param a          [IN] The start of the text to search.
2348        * @param b          [IN] One-past-the-end of the text to search.
2349        * @param re         [IN] The regular expression to search for.
2350        * @param submatch   [IN] Which submatch to return.  There are some
2351        *                        special values for this parameter:
2352        *                        - -1 each enumerated subexpression does NOT
2353        *                          match the regular expression (aka field
2354        *                          splitting)
2355        *                        - 0 the entire string matching the
2356        *                          subexpression is returned for each match
2357        *                          within the text.
2358        *                        - >0 enumerates only the indicated
2359        *                          subexpression from a match within the text.
2360        * @param m          [IN] Policy flags for match rules.
2361        *
2362        * @todo Implement this function.
2363        * @doctodo
2364        */
2365       regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2366                            int __submatch = 0,
2367                            regex_constants::match_flag_type __m
2368                            = regex_constants::match_default);
2369
2370       /**
2371        * Constructs a %regex_token_iterator...
2372        * @param a          [IN] The start of the text to search.
2373        * @param b          [IN] One-past-the-end of the text to search.
2374        * @param re         [IN] The regular expression to search for.
2375        * @param submatches [IN] A list of subexpressions to return for each
2376        *                        regular expression match within the text.
2377        * @param m          [IN] Policy flags for match rules.
2378        *
2379        * @todo Implement this function.
2380        * @doctodo
2381        */
2382       regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2383                            const regex_type& __re,
2384                            const std::vector<int>& __submatches,
2385                            regex_constants::match_flag_type __m
2386                              = regex_constants::match_default);
2387
2388       /**
2389        * Constructs a %regex_token_iterator...
2390        * @param a          [IN] The start of the text to search.
2391        * @param b          [IN] One-past-the-end of the text to search.
2392        * @param re         [IN] The regular expression to search for.
2393        * @param submatches [IN] A list of subexpressions to return for each
2394        *                        regular expression match within the text.
2395        * @param m          [IN] Policy flags for match rules.
2396        
2397        * @todo Implement this function.
2398        * @doctodo
2399        */
2400       template<std::size_t _Nm>
2401         regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2402                              const regex_type& __re,
2403                              const int (&__submatches)[_Nm],
2404                              regex_constants::match_flag_type __m
2405                              = regex_constants::match_default);
2406
2407       /**
2408        * @brief Copy constructs a %regex_token_iterator.
2409        * @param rhs [IN] A %regex_token_iterator to copy.
2410        * @todo Implement this function.
2411        */
2412       regex_token_iterator(const regex_token_iterator& __rhs);
2413       
2414       /**
2415        * @brief Assigns a %regex_token_iterator to another.
2416        * @param rhs [IN] A %regex_token_iterator to copy.
2417        * @todo Implement this function.
2418        */
2419       regex_token_iterator&
2420       operator=(const regex_token_iterator& __rhs);
2421       
2422       /**
2423        * @brief Compares a %regex_token_iterator to another for equality.
2424        * @todo Implement this function.
2425        */
2426       bool
2427       operator==(const regex_token_iterator& __rhs);
2428       
2429       /**
2430        * @brief Compares a %regex_token_iterator to another for inequality.
2431        * @todo Implement this function.
2432        */
2433       bool
2434       operator!=(const regex_token_iterator& __rhs);
2435       
2436       /**
2437        * @brief Dereferences a %regex_token_iterator.
2438        * @todo Implement this function.
2439        */
2440       const value_type&
2441       operator*();
2442       
2443       /**
2444        * @brief Selects a %regex_token_iterator member.
2445        * @todo Implement this function.
2446        */
2447       const value_type*
2448       operator->();
2449       
2450       /**
2451        * @brief Increments a %regex_token_iterator.
2452        * @todo Implement this function.
2453        */
2454       regex_token_iterator&
2455       operator++();
2456       
2457       /**
2458        * @brief Postincrements a %regex_token_iterator.
2459        * @todo Implement this function.
2460        */
2461       regex_token_iterator
2462       operator++(int);
2463       
2464     private: // data members for exposition only:
2465       typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> position_iterator;
2466
2467       position_iterator __position;
2468       const value_type* __result;
2469       value_type        __suffix;
2470       std::size_t       __n;
2471       std::vector<int>  __subs;
2472     };
2473
2474   /** @brief Token iterator for C-style NULL-terminated strings. */
2475   typedef regex_token_iterator<const char*>             cregex_token_iterator;
2476   /** @brief Token iterator for standard strings. */
2477   typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
2478 #ifdef _GLIBCXX_USE_WCHAR_T
2479   /** @brief Token iterator for C-style NULL-terminated wide strings. */
2480   typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
2481   /** @brief Token iterator for standard wide-character strings. */
2482   typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2483 #endif
2484   
2485   //@} // group regex
2486 _GLIBCXX_END_NAMESPACE_VERSION
2487 } // namespace
2488