1 // class template regex -*- C++ -*-
3 // Copyright (C) 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * @file tr1_impl/regex
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
38 _GLIBCXX_BEGIN_NAMESPACE_TR1
41 * @addtogroup tr1_regex Regular Expressions
42 * A facility for performing regular expression pattern matching.
46 namespace regex_constants
48 // [7.5.1] Bitmask Type syntax_option_type
65 * @brief This is a bitmask type indicating how to interpret the regex.
67 * The @c syntax_option_type is implementation defined but it is valid to
68 * perform bitwise operations on these values and expect the right thing to
71 * A valid value of type syntax_option_type shall have exactly one of the
72 * elements @c ECMAScript, @c basic, @c extended, @c awk, @c grep, @c egrep
75 typedef unsigned int syntax_option_type;
77 /// Specifies that the matching of regular expressions against a character
78 /// sequence shall be performed without regard to case.
79 static const syntax_option_type icase = 1 << _S_icase;
81 /// Specifies that when a regular expression is matched against a character
82 /// container sequence, no sub-expression matches are to be stored in the
83 /// supplied match_results structure.
84 static const syntax_option_type nosubs = 1 << _S_nosubs;
86 /// Specifies that the regular expression engine should pay more attention to
87 /// the speed with which regular expressions are matched, and less to the
88 /// speed with which regular expression objects are constructed. Otherwise
89 /// it has no detectable effect on the program output.
90 static const syntax_option_type optimize = 1 << _S_optimize;
92 /// Specifies that character ranges of the form [a-b] should be locale
94 static const syntax_option_type collate = 1 << _S_collate;
96 /// Specifies that the grammar recognized by the regular expression engine is
97 /// that used by ECMAScript in ECMA-262 [Ecma International, ECMAScript
98 /// Language Specification, Standard Ecma-262, third edition, 1999], as
99 /// modified in tr1 section [7.13]. This grammar is similar to that defined
100 /// in the PERL scripting language but extended with elements found in the
101 /// POSIX regular expression grammar.
102 static const syntax_option_type ECMAScript = 1 << _S_ECMAScript;
104 /// Specifies that the grammar recognized by the regular expression engine is
105 /// that used by POSIX basic regular expressions in IEEE Std 1003.1-2001,
106 /// Portable Operating System Interface (POSIX), Base Definitions and
107 /// Headers, Section 9, Regular Expressions [IEEE, Information Technology --
108 /// Portable Operating System Interface (POSIX), IEEE Standard 1003.1-2001].
109 static const syntax_option_type basic = 1 << _S_basic;
111 /// Specifies that the grammar recognized by the regular expression engine is
112 /// that used by POSIX extended regular expressions in IEEE Std 1003.1-2001,
113 /// Portable Operating System Interface (POSIX), Base Definitions and Headers,
114 /// Section 9, Regular Expressions.
115 static const syntax_option_type extended = 1 << _S_extended;
117 /// Specifies that the grammar recognized by the regular expression engine is
118 /// that used by POSIX utility awk in IEEE Std 1003.1-2001. This option is
119 /// identical to syntax_option_type extended, except that C-style escape
120 /// sequences are supported. These sequences are, explicitly, '\\', '\a',
121 /// '\b', '\f', '\n', '\r', '\t' , '\v', '\"', '\\', and '\ddd' (where ddd is
122 /// one, two, or three octal digits).
123 static const syntax_option_type awk = 1 << _S_awk;
125 /// Specifies that the grammar recognized by the regular expression engine is
126 /// that used by POSIX utility grep in IEEE Std 1003.1-2001. This option is
127 /// identical to syntax_option_type basic, except that newlines are treated
129 static const syntax_option_type grep = 1 << _S_grep;
131 /// Specifies that the grammar recognized by the regular expression engine is
132 /// that used by POSIX utility grep when given the -E option in
133 /// IEEE Std 1003.1-2001. This option is identical to syntax_option_type
134 /// extended, except that newlines are treated as whitespace.
135 static const syntax_option_type egrep = 1 << _S_egrep;
138 // [7.5.2] Bitmask Type match_flag_type
156 * @brief This is a bitmask type indicating regex matching rules.
158 * Matching a regular expression against a sequence of characters [first,
159 * last) proceeds according to the rules of the grammar specified for the
160 * regular expression object, modified according to the effects listed
161 * below for any bitmask elements set.
163 * The @c match_flag_type is implementation defined but it is valid to
164 * perform bitwise operations on these values and expect the right thing to
167 typedef std::bitset<_S_match_flag_last> match_flag_type;
169 static const match_flag_type match_default = 0;
171 /// The first character in the sequence [first, last) is treated as though it
172 /// is not at the beginning of a line, so the character "^" in the regular
173 /// expression shall not match [first, first).
174 static const match_flag_type match_not_bol = 1 << _S_not_bol;
176 /// The last character in the sequence [first, last) is treated as though it
177 /// is not at the end of a line, so the character "$" in the regular
178 /// expression shall not match [last, last).
179 static const match_flag_type match_not_eol = 1 << _S_not_eol;
181 /// The expression "\b" is not matched against the sub-sequence
183 static const match_flag_type match_not_bow = 1 << _S_not_bow;
185 /// The expression "\b" should not be matched against the sub-sequence
187 static const match_flag_type match_not_eow = 1 << _S_not_eow;
189 /// If more than one match is possible then any match is an acceptable
191 static const match_flag_type match_any = 1 << _S_any;
193 /// The expression does not match an empty sequence.
194 static const match_flag_type match_not_null = 1 << _S_not_null;
196 /// The expression only matches a sub-sequence that begins at first .
197 static const match_flag_type match_continuous = 1 << _S_continuous;
199 /// --first is a valid iterator position. When this flag is set then the
200 /// flags match_not_bol and match_not_bow are ignored by the regular
201 /// expression algorithms 7.11 and iterators 7.12.
202 static const match_flag_type match_prev_avail = 1 << _S_prev_avail;
204 /// When a regular expression match is to be replaced by a new string, the
205 /// new string is constructed using the rules used by the ECMAScript replace
206 /// function in ECMA- 262 [Ecma International, ECMAScript Language
207 /// Specification, Standard Ecma-262, third edition, 1999], part 15.5.4.11
208 /// String.prototype.replace. In addition, during search and replace
209 /// operations all non-overlapping occurrences of the regular expression
210 /// are located and replaced, and sections of the input that did not match
211 /// the expression are copied unchanged to the output string.
213 /// Format strings (from ECMA-262 [15.5.4.11]):
215 /// $& The matched substring.
216 /// $` The portion of <em>string</em> that precedes the matched substring.
217 /// $' The portion of <em>string</em> that follows the matched substring.
218 /// $n The nth capture, where n is in [1,9] and $n is not followed by a
219 /// decimal digit. If n <= m and the nth capture is undefined, use the
221 /// instead. If n > m, the result is implementation-defined.
222 /// $nn The nnth capture, where nn is a two-digit decimal number on [01, 99].
223 /// If nn <= m and the nth capture is undefined, use the empty string
224 /// instead. If nn > m, the result is implementation-defined.
226 static const match_flag_type format_default = 0;
228 /// When a regular expression match is to be replaced by a new string, the
229 /// new string is constructed using the rules used by the POSIX sed utility
230 /// in IEEE Std 1003.1- 2001 [IEEE, Information Technology -- Portable
231 /// Operating System Interface (POSIX), IEEE Standard 1003.1-2001].
232 static const match_flag_type format_sed = 1 << _S_sed;
234 /// During a search and replace operation, sections of the character
235 /// container sequence being searched that do not match the regular
236 /// expression shall not be copied to the output string.
237 static const match_flag_type format_no_copy = 1 << _S_no_copy;
239 /// When specified during a search and replace operation, only the first
240 /// occurrence of the regular expression shall be replaced.
241 static const match_flag_type format_first_only = 1 << _S_first_only;
244 /// [7.5.3] implementation-defined error type
263 /// The expression contained an invalid collating element name.
264 static const error_type error_collate(_S_error_collate);
266 /// The expression contained an invalid character class name.
267 static const error_type error_ctype(_S_error_ctype);
269 /// The expression contained an invalid escaped character, or a trailing
271 static const error_type error_escape(_S_error_escape);
273 /// The expression contained an invalid back reference.
274 static const error_type error_backref(_S_error_backref);
276 /// The expression contained mismatched [ and ].
277 static const error_type error_brack(_S_error_brack);
279 /// The expression contained mismatched ( and ).
280 static const error_type error_paren(_S_error_paren);
282 /// The expression contained mismatched { and }
283 static const error_type error_brace(_S_error_brace);
285 /// The expression contained an invalid range in a {} expression.
286 static const error_type error_badbrace(_S_error_badbrace);
288 /// The expression contained an invalid character range,
289 /// such as [b-a] in most encodings.
290 static const error_type error_range(_S_error_range);
292 /// There was insufficient memory to convert the expression into a
293 /// finite state machine.
294 static const error_type error_space(_S_error_space);
296 /// One of *?+{ was not preceded by a valid regular expression.
297 static const error_type error_badrepeat(_S_error_badrepeat);
299 /// The complexity of an attempted match against a regular expression
300 /// exceeded a pre-set level.
301 static const error_type error_complexity(_S_error_complexity);
303 /// There was insufficient memory to determine whether the
304 /// regular expression could match the specified character sequence.
305 static const error_type error_stack(_S_error_stack);
309 // [7.8] Class regex_error
311 * Defines the exception objects thrown report errors from the
312 * regular expression library.
315 : public std::runtime_error
319 * @brief constructs a regex_error object.
321 * @param ecode the regex error code.
324 regex_error(regex_constants::error_type __ecode)
325 : std::runtime_error("regex_error"), _M_code(__ecode)
329 * @brief gets the regex error code.
331 * @returns the regex error code.
333 regex_constants::error_type
338 regex_constants::error_type _M_code;
342 // [7.7] Class regex_traits
344 * A regular expression traits class that satisfies the requirements of tr1
347 * The class %regex is parameterized around a set of related types and
348 * functions used to complete the definition of its semantics. This class
349 * satisfies the requirements of such a traits class.
351 template<typename _Ch_type>
355 typedef _Ch_type char_type;
356 typedef std::basic_string<char_type> string_type;
357 typedef std::locale locale_type;
358 typedef std::ctype_base::mask char_class_type;
362 * @brief Constructs a default traits object.
368 * @brief Gives the length of a C-style string starting at @p __p.
370 * @param __p a pointer to the start of a character sequence.
372 * @returns the number of characters between @p *__p and the first
373 * default-initialized value of type @p char_type. In other words, uses
374 * the C-string algorithm for determining the length of a sequence of
378 length(const char_type* __p)
379 { return string_type::traits_type::length(__p); }
382 * @brief Performs the identity translation.
384 * @param c A character to the locale-specific character set.
389 translate(char_type __c) const
393 * @brief Translates a character into a case-insensitive equivalent.
395 * @param c A character to the locale-specific character set.
397 * @returns the locale-specific lower-case equivalent of c.
398 * @throws std::bad_cast if the imbued locale does not support the ctype
402 translate_nocase(char_type __c) const
405 using std::use_facet;
406 return use_facet<ctype<char_type> >(_M_locale).tolower(__c);
410 * @brief Gets a sort key for a character sequence.
412 * @param first beginning of the character sequence.
413 * @param last one-past-the-end of the character sequence.
415 * Returns a sort key for the character sequence designated by the
416 * iterator range [F1, F2) such that if the character sequence [G1, G2)
417 * sorts before the character sequence [H1, H2) then
418 * v.transform(G1, G2) < v.transform(H1, H2).
420 * What this really does is provide a more efficient way to compare a
421 * string to multiple other strings in locales with fancy collation
422 * rules and equivalence classes.
424 * @returns a locale-specific sort key equivalent to the input range.
426 * @throws std::bad_cast if the current locale does not have a collate
429 template<typename _Fwd_iter>
431 transform(_Fwd_iter __first, _Fwd_iter __last) const
434 using std::use_facet;
435 const collate<_Ch_type>& __c(use_facet<
436 collate<_Ch_type> >(_M_locale));
437 string_type __s(__first, __last);
438 return __c.transform(__s.data(), __s.data() + __s.size());
444 * @param first beginning of the character sequence.
445 * @param last one-past-the-end of the character sequence.
447 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
448 * typeid(collate_byname<_Ch_type>) and the form of the sort key
449 * returned by collate_byname<_Ch_type>::transform(first, last) is known
450 * and can be converted into a primary sort key then returns that key,
451 * otherwise returns an empty string. WTF??
453 * @todo Implement this function.
455 template<typename _Fwd_iter>
457 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
458 { return string_type(); }
461 * @brief Gets a collation element by name.
463 * @param first beginning of the collation element name.
464 * @param last one-past-the-end of the collation element name.
466 * @returns a sequence of one or more characters that represents the
467 * collating element consisting of the character sequence designated by
468 * the iterator range [first, last). Returns an empty string if the
469 * character sequence is not a valid collating element.
471 * @todo Implement this function.
473 template<typename _Fwd_iter>
475 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
476 { return string_type(); }
479 * @brief Maps one or mire characters to a named character
482 * @param first beginning of the character sequence.
483 * @param last one-past-the-end of the character sequence.
485 * @returns an unspecified value that represents the character
486 * classification named by the character sequence designated by the
487 * iterator range [first, last). The value returned shall be independent
488 * of the case of the characters in the character sequence. If the name
489 * is not recognized then returns a value that compares equal to 0.
491 * At least the following names (or their wide-character equivalent) are
509 * @todo Implement this function.
511 template<typename _Fwd_iter>
513 lookup_classname(_Fwd_iter __first, _Fwd_iter __last) const
517 * @brief Determines if @p c is a member of an identified class.
519 * @param c a character.
520 * @param f a class type (as returned from lookup_classname).
522 * @returns true if the character @p c is a member of the classification
523 * represented by @p f, false otherwise.
525 * @throws std::bad_cast if the current locale does not have a ctype
529 isctype(_Ch_type __c, char_class_type __f) const
532 using std::use_facet;
533 const ctype<_Ch_type>& __ctype(use_facet<
534 ctype<_Ch_type> >(_M_locale));
536 if (__ctype.is(__c, __f))
539 // special case of underscore in [[:w:]]
540 if (__c == __ctype.widen('_'))
542 const char* const __wb[] = "w";
543 char_class_type __wt = this->lookup_classname(__wb,
544 __wb + sizeof(__wb));
549 // special case of [[:space:]] in [[:blank:]]
550 if (__c == __ctype.isspace(__c))
552 const char* const __bb[] = "blank";
553 char_class_type __bt = this->lookup_classname(__bb,
554 __bb + sizeof(__bb));
563 * @brief Converts a digit to an int.
565 * @param ch a character representing a digit.
566 * @param radix the radix if the numeric conversion (limited to 8, 10,
569 * @returns the value represented by the digit ch in base radix if the
570 * character ch is a valid digit in base radix; otherwise returns -1.
572 * @todo Implement this function.
575 value(_Ch_type __ch, int __radix) const;
578 * @brief Imbues the regex_traits object with a copy of a new locale.
580 * @param loc A locale.
582 * @returns a copy of the previous locale in use by the regex_traits
585 * @note Calling imbue with a different locale than the one currently in
586 * use invalidates all cached data held by *this.
589 imbue(locale_type __loc)
591 std::swap(_M_locale, __loc);
596 * @brief Gets a copy of the current locale in use by the regex_traits
601 { return _M_locale; }
604 locale_type _M_locale;
608 // [7.8] Class basic_regex
610 * Objects of specializations of this class represent regular expressions
611 * constructed from sequences of character type @p _Ch_type.
613 * Storage for the regular expression is allocated and deallocated as
614 * necessary by the member functions of this class.
616 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type> >
621 typedef _Ch_type value_type;
622 typedef regex_constants::syntax_option_type flag_type;
623 typedef typename _Rx_traits::locale_type locale_type;
624 typedef typename _Rx_traits::string_type string_type;
627 static const regex_constants::syntax_option_type icase
628 = regex_constants::icase;
629 static const regex_constants::syntax_option_type nosubs
630 = regex_constants::nosubs;
631 static const regex_constants::syntax_option_type optimize
632 = regex_constants::optimize;
633 static const regex_constants::syntax_option_type collate
634 = regex_constants::collate;
635 static const regex_constants::syntax_option_type ECMAScript
636 = regex_constants::ECMAScript;
637 static const regex_constants::syntax_option_type basic
638 = regex_constants::basic;
639 static const regex_constants::syntax_option_type extended
640 = regex_constants::extended;
641 static const regex_constants::syntax_option_type awk
642 = regex_constants::awk;
643 static const regex_constants::syntax_option_type grep
644 = regex_constants::grep;
645 static const regex_constants::syntax_option_type egrep
646 = regex_constants::egrep;
648 // [7.8.2] construct/copy/destroy
650 * Constructs a basic regular expression that does not match any
651 * character sequence.
654 : _M_flags(regex_constants::ECMAScript), _M_pattern(), _M_mark_count(0)
658 * @brief Constructs a basic regular expression from the sequence
659 * [p, p + char_traits<_Ch_type>::length(p)) interpreted according to the
662 * @param p A pointer to the start of a C-style null-terminated string
663 * containing a regular expression.
664 * @param f Flags indicating the syntax rules and options.
666 * @throws regex_error if @p p is not a valid regular expression.
669 basic_regex(const _Ch_type* __p,
670 flag_type __f = regex_constants::ECMAScript)
671 : _M_flags(__f), _M_pattern(__p), _M_mark_count(0)
675 * @brief Constructs a basic regular expression from the sequence
676 * [p, p + len) interpreted according to the flags in @p f.
678 * @param p A pointer to the start of a string containing a regular
680 * @param len The length of the string containing the regular expression.
681 * @param f Flags indicating the syntax rules and options.
683 * @throws regex_error if @p p is not a valid regular expression.
685 basic_regex(const _Ch_type* __p, std::size_t __len, flag_type __f)
686 : _M_flags(__f) , _M_pattern(__p, __len), _M_mark_count(0)
690 * @brief Copy-constructs a basic regular expression.
692 * @param rhs A @p regex object.
694 basic_regex(const basic_regex& __rhs)
695 : _M_flags(__rhs._M_flags), _M_pattern(__rhs._M_pattern),
696 _M_mark_count(__rhs._M_mark_count)
700 * @brief Constructs a basic regular expression from the string
701 * @p interpreted according to the flags in @p f.
703 * @param p A string containing a regular expression.
704 * @param f Flags indicating the syntax rules and options.
706 * @throws regex_error if @p p is not a valid regular expression.
708 template<typename _Ch_traits, typename _Ch_alloc>
710 basic_regex(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
711 flag_type __f = regex_constants::ECMAScript)
712 : _M_flags(__f), _M_pattern(__s), _M_mark_count(0)
716 * @brief Constructs a basic regular expression from the range
717 * [first, last) interpreted according to the flags in @p f.
719 * @param first The start of a range containing a valid regular
721 * @param last The end of a range containing a valid regular
723 * @param f The format flags of the regular expression.
725 * @throws regex_error if @p p is not a valid regular expression.
727 template<typename _InputIterator>
728 basic_regex(_InputIterator __first, _InputIterator __last,
729 flag_type __f = regex_constants::ECMAScript)
730 : _M_flags(__f), _M_pattern(__first, __last), _M_mark_count(0)
734 * @brief Destroys a basic regular expression.
740 * @brief Assigns one regular expression to another.
743 operator=(const basic_regex& __rhs)
744 { return this->assign(__rhs); }
747 * @brief Replaces a regular expression with a new one constructed from
748 * a C-style null-terminated string.
750 * @param A pointer to the start of a null-terminated C-style string
751 * containing a regular expression.
754 operator=(const _Ch_type* __p)
755 { return this->assign(__p, flags()); }
758 * @brief Replaces a regular expression with a new one constructed from
761 * @param A pointer to a string containing a regular expression.
763 template<typename _Ch_typeraits, typename _Allocator>
765 operator=(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s)
766 { return this->assign(__s, flags()); }
770 * @brief the real assignment operator.
772 * @param that Another regular expression object.
775 assign(const basic_regex& __that)
777 basic_regex __tmp(__that);
783 * @brief Assigns a new regular expression to a regex object from a
784 * C-style null-terminated string containing a regular expression
787 * @param p A pointer to a C-style null-terminated string containing
788 * a regular expression pattern.
789 * @param flags Syntax option flags.
791 * @throws regex_error if p does not contain a valid regular expression
792 * pattern interpreted according to @p flags. If regex_error is thrown,
793 * *this remains unchanged.
796 assign(const _Ch_type* __p,
797 flag_type __flags = regex_constants::ECMAScript)
798 { return this->assign(string_type(__p), __flags); }
801 * @brief Assigns a new regular expression to a regex object from a
802 * C-style string containing a regular expression pattern.
804 * @param p A pointer to a C-style string containing a
805 * regular expression pattern.
806 * @param len The length of the regular expression pattern string.
807 * @param flags Syntax option flags.
809 * @throws regex_error if p does not contain a valid regular expression
810 * pattern interpreted according to @p flags. If regex_error is thrown,
811 * *this remains unchanged.
814 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
815 { return this->assign(string_type(__p, __len), __flags); }
818 * @brief Assigns a new regular expression to a regex object from a
819 * string containing a regular expression pattern.
821 * @param s A string containing a regular expression pattern.
822 * @param flags Syntax option flags.
824 * @throws regex_error if p does not contain a valid regular expression
825 * pattern interpreted according to @p flags. If regex_error is thrown,
826 * *this remains unchanged.
828 template<typename _Ch_typeraits, typename _Allocator>
830 assign(const basic_string<_Ch_type, _Ch_typeraits, _Allocator>& __s,
831 flag_type __f = regex_constants::ECMAScript)
833 basic_regex __tmp(__s, __f);
839 * @brief Assigns a new regular expression to a regex object.
841 * @param first The start of a range containing a valid regular
843 * @param last The end of a range containing a valid regular
845 * @param flags Syntax option flags.
847 * @throws regex_error if p does not contain a valid regular expression
848 * pattern interpreted according to @p flags. If regex_error is thrown,
849 * *this remains unchanged.
851 template<typename _InputIterator>
853 assign(_InputIterator __first, _InputIterator __last,
854 flag_type __flags = regex_constants::ECMAScript)
855 { return this->assign(string_type(__first, __last), __flags); }
857 // [7.8.4] const operations
859 * @brief Gets the number of marked subexpressions within the regular
864 { return _M_mark_count; }
867 * @brief Gets the flags used to construct the regular expression
868 * or in the last call to assign().
876 * @brief Imbues the regular expression object with the given locale.
878 * @param loc A locale.
881 imbue(locale_type __loc)
882 { return _M_traits.imbue(__loc); }
885 * @brief Gets the locale currently imbued in the regular expression
890 { return _M_traits.getloc(); }
894 * @brief Swaps the contents of two regular expression objects.
896 * @param rhs Another regular expression object.
899 swap(basic_regex& __rhs)
901 std::swap(_M_flags, __rhs._M_flags);
902 std::swap(_M_pattern, __rhs._M_pattern);
903 std::swap(_M_mark_count, __rhs._M_mark_count);
904 std::swap(_M_traits, __rhs._M_traits);
909 * @brief Compiles a regular expression pattern into a NFA.
910 * @todo Implement this function.
917 string_type _M_pattern;
918 unsigned int _M_mark_count;
919 _Rx_traits _M_traits;
922 typedef basic_regex<char> regex;
923 #ifdef _GLIBCXX_USE_WCHAR_T
924 typedef basic_regex<wchar_t> wregex;
928 // [7.8.6] basic_regex swap
930 * @brief Swaps the contents of two regular expression objects.
931 * @param lhs First regular expression.
932 * @param rhs Second regular expression.
934 template<typename _Ch_type, typename _Rx_traits>
936 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
937 basic_regex<_Ch_type, _Rx_traits>& __rhs)
938 { return __lhs.swap(__rhs); }
941 // [7.9] Class template sub_match
943 * A sequence of characters matched by a particular marked sub-expression.
945 * An object of this class is essentially a pair of iterators marking a
946 * matched subexpression within a regular expression pattern match. Such
947 * objects can be converted to and compared with std::basic_string objects
948 * of a similar base character type as the pattern matched by the regular
951 * The iterators that make up the pair are the usual half-open interval
952 * referencing the actual original pattern matched.
954 template<typename _BiIter>
955 class sub_match : public std::pair<_BiIter, _BiIter>
958 typedef typename iterator_traits<_BiIter>::value_type value_type;
959 typedef typename iterator_traits<_BiIter>::difference_type
961 typedef _BiIter iterator;
967 * Gets the length of the matching sequence.
971 { return this->matched ? std::distance(this->first, this->second) : 0; }
974 * @brief Gets the matching sequence as a string.
976 * @returns the matching sequence as a string.
978 * This is the implicit conversion operator. It is identical to the
979 * str() member function except that it will want to pop up in
980 * unexpected places and cause a great deal of confusion and cursing
983 operator basic_string<value_type>() const
986 ? std::basic_string<value_type>(this->first, this->second)
987 : std::basic_string<value_type>();
991 * @brief Gets the matching sequence as a string.
993 * @returns the matching sequence as a string.
995 basic_string<value_type>
999 ? std::basic_string<value_type>(this->first, this->second)
1000 : std::basic_string<value_type>();
1004 * @brief Compares this and another matched sequence.
1006 * @param s Another matched sequence to compare to this one.
1008 * @retval <0 this matched sequence will collate before @p s.
1009 * @retval =0 this matched sequence is equivalent to @p s.
1010 * @retval <0 this matched sequence will collate after @p s.
1013 compare(const sub_match& __s) const
1014 { return this->str().compare(__s.str()); }
1017 * @brief Compares this sub_match to a string.
1019 * @param s A string to compare to this sub_match.
1021 * @retval <0 this matched sequence will collate before @p s.
1022 * @retval =0 this matched sequence is equivalent to @p s.
1023 * @retval <0 this matched sequence will collate after @p s.
1026 compare(const basic_string<value_type>& __s) const
1027 { return this->str().compare(__s); }
1030 * @brief Compares this sub_match to a C-style string.
1032 * @param s A C-style string to compare to this sub_match.
1034 * @retval <0 this matched sequence will collate before @p s.
1035 * @retval =0 this matched sequence is equivalent to @p s.
1036 * @retval <0 this matched sequence will collate after @p s.
1039 compare(const value_type* __s) const
1040 { return this->str().compare(__s); }
1044 typedef sub_match<const char*> csub_match;
1045 typedef sub_match<string::const_iterator> ssub_match;
1046 #ifdef _GLIBCXX_USE_WCHAR_T
1047 typedef sub_match<const wchar_t*> wcsub_match;
1048 typedef sub_match<wstring::const_iterator> wssub_match;
1051 // [7.9.2] sub_match non-member operators
1054 * @brief Tests the equivalence of two regular expression submatches.
1055 * @param lhs First regular expression submatch.
1056 * @param rhs Second regular expression submatch.
1057 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1059 template<typename _BiIter>
1061 operator==(const sub_match<_BiIter>& __lhs,
1062 const sub_match<_BiIter>& __rhs)
1063 { return __lhs.compare(__rhs) == 0; }
1066 * @brief Tests the inequivalence of two regular expression submatches.
1067 * @param lhs First regular expression submatch.
1068 * @param rhs Second regular expression submatch.
1069 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1071 template<typename _BiIter>
1073 operator!=(const sub_match<_BiIter>& __lhs,
1074 const sub_match<_BiIter>& __rhs)
1075 { return __lhs.compare(__rhs) != 0; }
1078 * @brief Tests the ordering of two regular expression submatches.
1079 * @param lhs First regular expression submatch.
1080 * @param rhs Second regular expression submatch.
1081 * @returns true if @a lhs precedes @a rhs, false otherwise.
1083 template<typename _BiIter>
1085 operator<(const sub_match<_BiIter>& __lhs,
1086 const sub_match<_BiIter>& __rhs)
1087 { return __lhs.compare(__rhs) < 0; }
1090 * @brief Tests the ordering of two regular expression submatches.
1091 * @param lhs First regular expression submatch.
1092 * @param rhs Second regular expression submatch.
1093 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1095 template<typename _BiIter>
1097 operator<=(const sub_match<_BiIter>& __lhs,
1098 const sub_match<_BiIter>& __rhs)
1099 { return __lhs.compare(__rhs) <= 0; }
1102 * @brief Tests the ordering of two regular expression submatches.
1103 * @param lhs First regular expression submatch.
1104 * @param rhs Second regular expression submatch.
1105 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1107 template<typename _BiIter>
1109 operator>=(const sub_match<_BiIter>& __lhs,
1110 const sub_match<_BiIter>& __rhs)
1111 { return __lhs.compare(__rhs) >= 0; }
1114 * @brief Tests the ordering of two regular expression submatches.
1115 * @param lhs First regular expression submatch.
1116 * @param rhs Second regular expression submatch.
1117 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1119 template<typename _BiIter>
1121 operator>(const sub_match<_BiIter>& __lhs,
1122 const sub_match<_BiIter>& __rhs)
1123 { return __lhs.compare(__rhs) > 0; }
1126 * @brief Tests the equivalence of a string and a regular expression
1128 * @param lhs A string.
1129 * @param rhs A regular expression submatch.
1130 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1132 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1134 operator==(const basic_string<
1135 typename iterator_traits<_Bi_iter>::value_type,
1136 _Ch_traits, _Ch_alloc>& __lhs,
1137 const sub_match<_Bi_iter>& __rhs)
1138 { return __lhs == __rhs.str(); }
1141 * @brief Tests the inequivalence of a string and a regular expression
1143 * @param lhs A string.
1144 * @param rhs A regular expression submatch.
1145 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1147 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1149 operator!=(const basic_string<
1150 typename iterator_traits<_Bi_iter>::value_type,
1151 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1152 { return __lhs != __rhs.str(); }
1155 * @brief Tests the ordering of a string and a regular expression submatch.
1156 * @param lhs A string.
1157 * @param rhs A regular expression submatch.
1158 * @returns true if @a lhs precedes @a rhs, false otherwise.
1160 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1162 operator<(const basic_string<
1163 typename iterator_traits<_Bi_iter>::value_type,
1164 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1165 { return __lhs < __rhs.str(); }
1168 * @brief Tests the ordering of a string and a regular expression submatch.
1169 * @param lhs A string.
1170 * @param rhs A regular expression submatch.
1171 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1173 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1175 operator>(const basic_string<
1176 typename iterator_traits<_Bi_iter>::value_type,
1177 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1178 { return __lhs > __rhs.str(); }
1181 * @brief Tests the ordering of a string and a regular expression submatch.
1182 * @param lhs A string.
1183 * @param rhs A regular expression submatch.
1184 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1186 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1188 operator>=(const basic_string<
1189 typename iterator_traits<_Bi_iter>::value_type,
1190 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1191 { return __lhs >= __rhs.str(); }
1194 * @brief Tests the ordering of a string and a regular expression submatch.
1195 * @param lhs A string.
1196 * @param rhs A regular expression submatch.
1197 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1199 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1201 operator<=(const basic_string<
1202 typename iterator_traits<_Bi_iter>::value_type,
1203 _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs)
1204 { return __lhs <= __rhs.str(); }
1207 * @brief Tests the equivalence of a regular expression submatch and a
1209 * @param lhs A regular expression submatch.
1210 * @param rhs A string.
1211 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1213 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1215 operator==(const sub_match<_Bi_iter>& __lhs,
1217 typename iterator_traits<_Bi_iter>::value_type,
1218 _Ch_traits, _Ch_alloc>& __rhs)
1219 { return __lhs.str() == __rhs; }
1222 * @brief Tests the inequivalence of a regular expression submatch and a
1224 * @param lhs A regular expression submatch.
1225 * @param rhs A string.
1226 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1228 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1230 operator!=(const sub_match<_Bi_iter>& __lhs,
1232 typename iterator_traits<_Bi_iter>::value_type,
1233 _Ch_traits, _Ch_alloc>& __rhs)
1234 { return __lhs.str() != __rhs; }
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 precedes @a rhs, false otherwise.
1242 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1244 operator<(const sub_match<_Bi_iter>& __lhs,
1246 typename iterator_traits<_Bi_iter>::value_type,
1247 _Ch_traits, _Ch_alloc>& __rhs)
1248 { return __lhs.str() < __rhs; }
1251 * @brief Tests the ordering of a regular expression submatch and a string.
1252 * @param lhs A regular expression submatch.
1253 * @param rhs A string.
1254 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1256 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1258 operator>(const sub_match<_Bi_iter>& __lhs,
1260 typename iterator_traits<_Bi_iter>::value_type,
1261 _Ch_traits, _Ch_alloc>& __rhs)
1262 { return __lhs.str() > __rhs; }
1265 * @brief Tests the ordering of a regular expression submatch and a string.
1266 * @param lhs A regular expression submatch.
1267 * @param rhs A string.
1268 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1270 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1272 operator>=(const sub_match<_Bi_iter>& __lhs,
1274 typename iterator_traits<_Bi_iter>::value_type,
1275 _Ch_traits, _Ch_alloc>& __rhs)
1276 { return __lhs.str() >= __rhs; }
1279 * @brief Tests the ordering of a regular expression submatch and a string.
1280 * @param lhs A regular expression submatch.
1281 * @param rhs A string.
1282 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1284 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1286 operator<=(const sub_match<_Bi_iter>& __lhs,
1288 typename iterator_traits<_Bi_iter>::value_type,
1289 _Ch_traits, _Ch_alloc>& __rhs)
1290 { return __lhs.str() <= __rhs; }
1293 * @brief Tests the equivalence of a C string and a regular expression
1295 * @param lhs A C string.
1296 * @param rhs A regular expression submatch.
1297 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1299 template<typename _Bi_iter>
1301 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1302 const sub_match<_Bi_iter>& __rhs)
1303 { return __lhs == __rhs.str(); }
1306 * @brief Tests the inequivalence of an iterator value and a regular
1307 * expression submatch.
1308 * @param lhs A regular expression submatch.
1309 * @param rhs A string.
1310 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1312 template<typename _Bi_iter>
1314 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1315 const sub_match<_Bi_iter>& __rhs)
1316 { return __lhs != __rhs.str(); }
1319 * @brief Tests the ordering of a string and a regular expression submatch.
1320 * @param lhs A string.
1321 * @param rhs A regular expression submatch.
1322 * @returns true if @a lhs precedes @a rhs, false otherwise.
1324 template<typename _Bi_iter>
1326 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1327 const sub_match<_Bi_iter>& __rhs)
1328 { return __lhs < __rhs.str(); }
1331 * @brief Tests the ordering of a string and a regular expression submatch.
1332 * @param lhs A string.
1333 * @param rhs A regular expression submatch.
1334 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1336 template<typename _Bi_iter>
1338 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1339 const sub_match<_Bi_iter>& __rhs)
1340 { return __lhs > __rhs.str(); }
1343 * @brief Tests the ordering of a string and a regular expression submatch.
1344 * @param lhs A string.
1345 * @param rhs A regular expression submatch.
1346 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1348 template<typename _Bi_iter>
1350 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1351 const sub_match<_Bi_iter>& __rhs)
1352 { return __lhs >= __rhs.str(); }
1355 * @brief Tests the ordering of a string and a regular expression submatch.
1356 * @param lhs A string.
1357 * @param rhs A regular expression submatch.
1358 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1360 template<typename _Bi_iter>
1362 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1363 const sub_match<_Bi_iter>& __rhs)
1364 { return __lhs <= __rhs.str(); }
1367 * @brief Tests the equivalence of a regular expression submatch and a
1369 * @param lhs A regular expression submatch.
1370 * @param rhs A pointer to a string?
1371 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1373 template<typename _Bi_iter>
1375 operator==(const sub_match<_Bi_iter>& __lhs,
1376 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1377 { return __lhs.str() == __rhs; }
1380 * @brief Tests the inequivalence of a regular expression submatch and a
1382 * @param lhs A regular expression submatch.
1383 * @param rhs A pointer to a string.
1384 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1386 template<typename _Bi_iter>
1388 operator!=(const sub_match<_Bi_iter>& __lhs,
1389 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1390 { return __lhs.str() != __rhs; }
1393 * @brief Tests the ordering of a regular expression submatch and a string.
1394 * @param lhs A regular expression submatch.
1395 * @param rhs A string.
1396 * @returns true if @a lhs precedes @a rhs, false otherwise.
1398 template<typename _Bi_iter>
1400 operator<(const sub_match<_Bi_iter>& __lhs,
1401 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1402 { return __lhs.str() < __rhs; }
1405 * @brief Tests the ordering of a regular expression submatch and a string.
1406 * @param lhs A regular expression submatch.
1407 * @param rhs A string.
1408 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1410 template<typename _Bi_iter>
1412 operator>(const sub_match<_Bi_iter>& __lhs,
1413 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1414 { return __lhs.str() > __rhs; }
1417 * @brief Tests the ordering of a regular expression submatch and a string.
1418 * @param lhs A regular expression submatch.
1419 * @param rhs A string.
1420 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1422 template<typename _Bi_iter>
1424 operator>=(const sub_match<_Bi_iter>& __lhs,
1425 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1426 { return __lhs.str() >= __rhs; }
1429 * @brief Tests the ordering of a regular expression submatch and a string.
1430 * @param lhs A regular expression submatch.
1431 * @param rhs A string.
1432 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1434 template<typename _Bi_iter>
1436 operator<=(const sub_match<_Bi_iter>& __lhs,
1437 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1438 { return __lhs.str() <= __rhs; }
1441 * @brief Tests the equivalence of a string and a regular expression
1443 * @param lhs A string.
1444 * @param rhs A regular expression submatch.
1445 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1447 template<typename _Bi_iter>
1449 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1450 const sub_match<_Bi_iter>& __rhs)
1451 { return __lhs == __rhs.str(); }
1454 * @brief Tests the inequivalence of a string and a regular expression
1456 * @param lhs A string.
1457 * @param rhs A regular expression submatch.
1458 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1460 template<typename _Bi_iter>
1462 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1463 const sub_match<_Bi_iter>& __rhs)
1464 { return __lhs != __rhs.str(); }
1467 * @brief Tests the ordering of a string and a regular expression submatch.
1468 * @param lhs A string.
1469 * @param rhs A regular expression submatch.
1470 * @returns true if @a lhs precedes @a rhs, false otherwise.
1472 template<typename _Bi_iter>
1474 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1475 const sub_match<_Bi_iter>& __rhs)
1476 { return __lhs < __rhs.str(); }
1479 * @brief Tests the ordering of a string and a regular expression submatch.
1480 * @param lhs A string.
1481 * @param rhs A regular expression submatch.
1482 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1484 template<typename _Bi_iter>
1486 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1487 const sub_match<_Bi_iter>& __rhs)
1488 { return __lhs > __rhs.str(); }
1491 * @brief Tests the ordering of a string and a regular expression submatch.
1492 * @param lhs A string.
1493 * @param rhs A regular expression submatch.
1494 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1496 template<typename _Bi_iter>
1498 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1499 const sub_match<_Bi_iter>& __rhs)
1500 { return __lhs >= __rhs.str(); }
1503 * @brief Tests the ordering of a string and a regular expression submatch.
1504 * @param lhs A string.
1505 * @param rhs A regular expression submatch.
1506 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1508 template<typename _Bi_iter>
1510 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1511 const sub_match<_Bi_iter>& __rhs)
1512 { return __lhs <= __rhs.str(); }
1515 * @brief Tests the equivalence of a regular expression submatch and a
1517 * @param lhs A regular expression submatch.
1518 * @param rhs A const string reference.
1519 * @returns true if @a lhs is equivalent to @a rhs, false otherwise.
1521 template<typename _Bi_iter>
1523 operator==(const sub_match<_Bi_iter>& __lhs,
1524 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1525 { return __lhs.str() == __rhs; }
1528 * @brief Tests the inequivalence of a regular expression submatch and a
1530 * @param lhs A regular expression submatch.
1531 * @param rhs A const string reference.
1532 * @returns true if @a lhs is not equivalent to @a rhs, false otherwise.
1534 template<typename _Bi_iter>
1536 operator!=(const sub_match<_Bi_iter>& __lhs,
1537 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1538 { return __lhs.str() != __rhs; }
1541 * @brief Tests the ordering of a regular expression submatch and a string.
1542 * @param lhs A regular expression submatch.
1543 * @param rhs A const string reference.
1544 * @returns true if @a lhs precedes @a rhs, false otherwise.
1546 template<typename _Bi_iter>
1548 operator<(const sub_match<_Bi_iter>& __lhs,
1549 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1550 { return __lhs.str() < __rhs; }
1553 * @brief Tests the ordering of a regular expression submatch and a string.
1554 * @param lhs A regular expression submatch.
1555 * @param rhs A const string reference.
1556 * @returns true if @a lhs succeeds @a rhs, false otherwise.
1558 template<typename _Bi_iter>
1560 operator>(const sub_match<_Bi_iter>& __lhs,
1561 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1562 { return __lhs.str() > __rhs; }
1565 * @brief Tests the ordering of a regular expression submatch and a string.
1566 * @param lhs A regular expression submatch.
1567 * @param rhs A const string reference.
1568 * @returns true if @a lhs does not precede @a rhs, false otherwise.
1570 template<typename _Bi_iter>
1572 operator>=(const sub_match<_Bi_iter>& __lhs,
1573 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1574 { return __lhs.str() >= __rhs; }
1577 * @brief Tests the ordering of a regular expression submatch and a string.
1578 * @param lhs A regular expression submatch.
1579 * @param rhs A const string reference.
1580 * @returns true if @a lhs does not succeed @a rhs, false otherwise.
1582 template<typename _Bi_iter>
1584 operator<=(const sub_match<_Bi_iter>& __lhs,
1585 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1586 { return __lhs.str() <= __rhs; }
1589 * @brief Inserts a matched string into an output stream.
1591 * @param os The output stream.
1592 * @param m A submatch string.
1594 * @returns the output stream with the submatch string inserted.
1596 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1598 basic_ostream<_Ch_type, _Ch_traits>&
1599 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1600 const sub_match<_Bi_iter>& __m)
1601 { return __os << __m.str(); }
1603 // [7.10] Class template match_results
1605 * A collection of character sequences representing the result of a regular
1606 * expression match. Storage for the collection is allocated and freed as
1607 * necessary by the member functions of class template match_results.
1609 * This class satisfies the Sequence requirements, with the exception that
1610 * only the operations defined for a const-qualified Sequence are supported.
1612 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1613 * the whole match. In this case the sub_match member matched is always true.
1614 * The sub_match object stored at index n denotes what matched the marked
1615 * sub-expression n within the matched expression. If the sub-expression n
1616 * participated in a regular expression match then the sub_match member
1617 * matched evaluates to true, and members first and second denote the range
1618 * of characters [first, second) which formed that match. Otherwise matched
1619 * is false, and members first and second point to the end of the sequence
1620 * that was searched.
1622 template<typename _Bi_iter,
1623 typename _Allocator = allocator<sub_match<_Bi_iter> > >
1625 : private std::vector<std::_GLIBCXX_TR1 sub_match<_Bi_iter>, _Allocator>
1628 typedef std::vector<std::_GLIBCXX_TR1 sub_match<_Bi_iter>, _Allocator>
1632 typedef sub_match<_Bi_iter> value_type;
1633 typedef typename _Allocator::const_reference const_reference;
1634 typedef const_reference reference;
1635 typedef typename _Base_type::const_iterator const_iterator;
1636 typedef const_iterator iterator;
1637 typedef typename iterator_traits<_Bi_iter>::difference_type
1639 typedef typename _Allocator::size_type size_type;
1640 typedef _Allocator allocator_type;
1641 typedef typename iterator_traits<_Bi_iter>::value_type char_type;
1642 typedef basic_string<char_type> string_type;
1645 // [7.10.1] construct/copy/destroy
1647 * @brief Constructs a default match_results container.
1650 match_results(const _Allocator& __a = _Allocator())
1651 : _Base_type(__a), _M_matched(false)
1655 * @brief Copy constructs a match_result.
1657 match_results(const match_results& __rhs)
1658 : _Base_type(__rhs), _M_matched(__rhs._M_matched),
1659 _M_prefix(__rhs._M_prefix), _M_suffix(__rhs._M_suffix)
1663 * @brief Assigns rhs to *this.
1666 operator=(const match_results& __rhs)
1668 match_results __tmp(__rhs);
1673 * @todo Implement this function.
1680 * @todo Document this function.
1684 { return _M_matched ? _Base_type::size() + 1 : 0; }
1687 * @todo Implement this function.
1691 using _Base_type::max_size;
1694 * @todo Document this function.
1698 { return size() == 0; }
1700 // [7.10.3] element access
1702 * @brief Gets the length of the indicated submatch.
1703 * @param sub indicates the submatch.
1706 length(size_type __sub = 0) const
1707 { return _M_matched ? this->str(__sub).length() : 0; }
1710 * @todo Document this function.
1713 position(size_type __sub = 0) const
1715 return _M_matched ? std::distance(this->prefix().first,
1716 (*this)[__sub].first) : 0;
1720 * @todo Document this function.
1723 str(size_type __sub = 0) const
1724 { return _M_matched ? (*this)[__sub].str() : string_type(); }
1727 * @todo Document this function.
1730 operator[](size_type __n) const
1731 { return _Base_type::operator[](__n); }
1734 * @todo Document this function.
1738 { return _M_prefix; }
1741 * @todo Document this function.
1745 { return _M_suffix; }
1748 * @todo Document this function.
1752 { return _Base_type::begin(); }
1755 * @todo Document this function.
1759 { return _Base_type::end(); }
1763 * @todo Implement this function.
1765 template<typename _Out_iter>
1767 format(_Out_iter __out, const string_type& __fmt,
1768 regex_constants::match_flag_type __flags
1769 = regex_constants::format_default) const
1773 * @todo Implement this function.
1776 format(const string_type& __fmt,
1777 regex_constants::match_flag_type __flags
1778 = regex_constants::format_default) const;
1780 // [7.10.5] allocator
1782 * @todo Document this function.
1785 //get_allocator() const;
1786 using _Base_type::get_allocator;
1790 * @todo Document this function.
1793 swap(match_results& __that)
1795 _Base_type::swap(__that);
1796 std::swap(_M_matched, __that._M_matched);
1797 std::swap(_M_prefix, __that._M_prefix);
1798 std::swap(_M_suffix, __that._M_suffix);
1803 value_type _M_prefix;
1804 value_type _M_suffix;
1807 typedef match_results<const char*> cmatch;
1808 typedef match_results<string::const_iterator> smatch;
1809 #ifdef _GLIBCXX_USE_WCHAR_T
1810 typedef match_results<const wchar_t*> wcmatch;
1811 typedef match_results<wstring::const_iterator> wsmatch;
1814 // match_results comparisons
1816 * @todo Implement this function.
1818 template<typename _Bi_iter, typename _Allocator>
1820 operator==(const match_results<_Bi_iter, _Allocator>& __m1,
1821 const match_results<_Bi_iter, _Allocator>& __m2);
1824 * @todo Implement this function.
1826 template<typename _Bi_iter, class _Allocator>
1828 operator!=(const match_results<_Bi_iter, _Allocator>& __m1,
1829 const match_results<_Bi_iter, _Allocator>& __m2);
1831 // [7.10.6] match_results swap
1833 * @brief Swaps two match results.
1834 * @param lhs A match result.
1835 * @param rhs A match result.
1837 * The contents of the two match_results objects are swapped.
1839 template<typename _Bi_iter, typename _Allocator>
1841 swap(match_results<_Bi_iter, _Allocator>& __lhs,
1842 match_results<_Bi_iter, _Allocator>& __rhs)
1843 { return __lhs.swap(__rhs); }
1845 // [7.11.2] Function template regex_match
1847 * @brief Determines if there is a match between the regular expression @p e
1848 * and all of the character sequence [first, last).
1850 * @param first Beginning of the character sequence to match.
1851 * @param last One-past-the-end of the character sequence to match.
1852 * @param m The match results.
1853 * @param re The regular expression.
1854 * @param flags Controls how the regular expression is matched.
1856 * @retval true A match exists.
1857 * @retval false Otherwise.
1859 * @todo Implement this function.
1861 template<typename _Bi_iter, typename _Allocator,
1862 typename _Ch_type, typename _Rx_traits>
1864 regex_match(_Bi_iter __first, _Bi_iter __last,
1865 match_results<_Bi_iter, _Allocator>& __m,
1866 const basic_regex<_Ch_type, _Rx_traits>& __re,
1867 regex_constants::match_flag_type __flags
1868 = regex_constants::match_default)
1872 * @brief Indicates if there is a match between the regular expression @p e
1873 * and all of the character sequence [first, last).
1875 * @param first Beginning of the character sequence to match.
1876 * @param last One-past-the-end of the character sequence to match.
1877 * @param re The regular expression.
1878 * @param flags Controls how the regular expression is matched.
1880 * @retval true A match exists.
1881 * @retval false Otherwise.
1883 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1885 regex_match(_Bi_iter __first, _Bi_iter __last,
1886 const basic_regex<_Ch_type, _Rx_traits>& __re,
1887 regex_constants::match_flag_type __flags
1888 = regex_constants::match_default)
1890 match_results<_Bi_iter> __what;
1891 return regex_match(__first, __last, __what, __re, __flags);
1895 * @brief Determines if there is a match between the regular expression @p e
1896 * and a C-style null-terminated string.
1898 * @param s The C-style null-terminated string to match.
1899 * @param m The match results.
1900 * @param re The regular expression.
1901 * @param f Controls how the regular expression is matched.
1903 * @retval true A match exists.
1904 * @retval false Otherwise.
1906 template<typename _Ch_type, typename _Allocator, typename _Rx_traits>
1908 regex_match(const _Ch_type* __s,
1909 match_results<const _Ch_type*, _Allocator>& __m,
1910 const basic_regex<_Ch_type, _Rx_traits>& __re,
1911 regex_constants::match_flag_type __f
1912 = regex_constants::match_default)
1913 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
1916 * @brief Determines if there is a match between the regular expression @p e
1919 * @param s The string to match.
1920 * @param m The match results.
1921 * @param re The regular expression.
1922 * @param flags Controls how the regular expression is matched.
1924 * @retval true A match exists.
1925 * @retval false Otherwise.
1927 template<typename _Ch_traits, typename _Ch_alloc,
1928 typename _Allocator, typename _Ch_type, typename _Rx_traits>
1930 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
1931 match_results<typename basic_string<_Ch_type,
1932 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
1933 const basic_regex<_Ch_type, _Rx_traits>& __re,
1934 regex_constants::match_flag_type __flags
1935 = regex_constants::match_default)
1936 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
1939 * @brief Indicates if there is a match between the regular expression @p e
1940 * and a C-style null-terminated string.
1942 * @param s The C-style null-terminated string to match.
1943 * @param re The regular expression.
1944 * @param f Controls how the regular expression is matched.
1946 * @retval true A match exists.
1947 * @retval false Otherwise.
1949 template<typename _Ch_type, class _Rx_traits>
1951 regex_match(const _Ch_type* __s,
1952 const basic_regex<_Ch_type, _Rx_traits>& __re,
1953 regex_constants::match_flag_type __f
1954 = regex_constants::match_default)
1955 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
1958 * @brief Indicates if there is a match between the regular expression @p e
1961 * @param s [IN] The string to match.
1962 * @param re [IN] The regular expression.
1963 * @param flags [IN] Controls how the regular expression is matched.
1965 * @retval true A match exists.
1966 * @retval false Otherwise.
1968 template<typename _Ch_traits, typename _Str_allocator,
1969 typename _Ch_type, typename _Rx_traits>
1971 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
1972 const basic_regex<_Ch_type, _Rx_traits>& __re,
1973 regex_constants::match_flag_type __flags
1974 = regex_constants::match_default)
1975 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
1977 // [7.11.3] Function template regex_search
1979 * Searches for a regular expression within a range.
1980 * @param first [IN] The start of the string to search.
1981 * @param last [IN] One-past-the-end of the string to search.
1982 * @param m [OUT] The match results.
1983 * @param re [IN] The regular expression to search for.
1984 * @param flags [IN] Search policy flags.
1985 * @retval true A match was found within the string.
1986 * @retval false No match was found within the string, the content of %m is
1988 * @todo Implement this function.
1990 template<typename _Bi_iter, typename _Allocator,
1991 typename _Ch_type, typename _Rx_traits>
1993 regex_search(_Bi_iter __first, _Bi_iter __last,
1994 match_results<_Bi_iter, _Allocator>& __m,
1995 const basic_regex<_Ch_type, _Rx_traits>& __re,
1996 regex_constants::match_flag_type __flags
1997 = regex_constants::match_default)
2001 * Searches for a regular expression within a range.
2002 * @param first [IN] The start of the string to search.
2003 * @param last [IN] One-past-the-end of the string to search.
2004 * @param re [IN] The regular expression to search for.
2005 * @param flags [IN] Search policy flags.
2006 * @retval true A match was found within the string.
2007 * @retval false No match was found within the string.
2008 * @todo Document me.
2010 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2012 regex_search(_Bi_iter __first, _Bi_iter __last,
2013 const basic_regex<_Ch_type, _Rx_traits>& __re,
2014 regex_constants::match_flag_type __flags
2015 = regex_constants::match_default)
2017 match_results<_Bi_iter> __what;
2018 return regex_search(__first, __last, __what, __re, __flags);
2022 * @brief Searches for a regular expression within a C-string.
2023 * @param s [IN] A C-string to search for the regex.
2024 * @param m [OUT] The set of regex matches.
2025 * @param e [IN] The regex to search for in @p s.
2026 * @param f [IN] The search flags.
2027 * @retval true A match was found within the string.
2028 * @retval false No match was found within the string, the content of %m is
2030 * @todo Document me.
2032 template<typename _Ch_type, class _Allocator, class _Rx_traits>
2034 regex_search(const _Ch_type* __s,
2035 match_results<const _Ch_type*, _Allocator>& __m,
2036 const basic_regex<_Ch_type, _Rx_traits>& __e,
2037 regex_constants::match_flag_type __f
2038 = regex_constants::match_default)
2039 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2042 * @brief Searches for a regular expression within a C-string.
2043 * @param s [IN] The C-string to search.
2044 * @param e [IN] The regular expression to search for.
2045 * @param f [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 * @todo Document me.
2050 template<typename _Ch_type, typename _Rx_traits>
2052 regex_search(const _Ch_type* __s,
2053 const basic_regex<_Ch_type, _Rx_traits>& __e,
2054 regex_constants::match_flag_type __f
2055 = regex_constants::match_default)
2056 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2059 * @brief Searches for a regular expression within a string.
2060 * @param s [IN] The string to search.
2061 * @param e [IN] The regular expression to search for.
2062 * @param flags [IN] Search policy flags.
2063 * @retval true A match was found within the string.
2064 * @retval false No match was found within the string.
2065 * @todo Document me.
2067 template<typename _Ch_traits, typename _String_allocator,
2068 typename _Ch_type, typename _Rx_traits>
2070 regex_search(const basic_string<_Ch_type, _Ch_traits,
2071 _String_allocator>& __s,
2072 const basic_regex<_Ch_type, _Rx_traits>& __e,
2073 regex_constants::match_flag_type __flags
2074 = regex_constants::match_default)
2075 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2078 * @brief Searches for a regular expression within a string.
2079 * @param s [IN] A C++ string to search for the regex.
2080 * @param m [OUT] The set of regex matches.
2081 * @param e [IN] The regex to search for in @p s.
2082 * @param f [IN] The search flags.
2083 * @retval true A match was found within the string.
2084 * @retval false No match was found within the string, the content of %m is
2087 template<typename _Ch_traits, typename _Ch_alloc,
2088 typename _Allocator, typename _Ch_type,
2089 typename _Rx_traits>
2091 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2092 match_results<typename basic_string<_Ch_type,
2093 _Ch_traits, _Ch_alloc>::const_iterator, _Allocator>& __m,
2094 const basic_regex<_Ch_type, _Rx_traits>& __e,
2095 regex_constants::match_flag_type __f
2096 = regex_constants::match_default)
2097 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2099 // [7.11.4] Function template regex_replace
2101 * @todo Implement this function.
2102 * @todo Document this function.
2104 template<typename _Out_iter, typename _Bi_iter,
2105 typename _Rx_traits, typename _Ch_type>
2107 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2108 const basic_regex<_Ch_type, _Rx_traits>& __e,
2109 const basic_string<_Ch_type>& __fmt,
2110 regex_constants::match_flag_type __flags
2111 = regex_constants::match_default)
2115 * @todo Document me.
2117 template<typename _Rx_traits, typename _Ch_type>
2118 inline basic_string<_Ch_type>
2119 regex_replace(const basic_string<_Ch_type>& __s,
2120 const basic_regex<_Ch_type, _Rx_traits>& __e,
2121 const basic_string<_Ch_type>& __fmt,
2122 regex_constants::match_flag_type __flags
2123 = regex_constants::match_default)
2125 std::string __result;
2126 regex_replace(std::back_inserter(__result),
2127 __s.begin(), __s.end(), __e, __fmt, __flags);
2131 // [7.12.1] Class template regex_iterator
2133 * An iterator adaptor that will provide repeated calls of regex_search over
2134 * a range until no more matches remain.
2136 template<typename _Bi_iter,
2137 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2138 typename _Rx_traits = regex_traits<_Ch_type> >
2139 class regex_iterator
2142 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2143 typedef match_results<_Bi_iter> value_type;
2144 typedef std::ptrdiff_t difference_type;
2145 typedef const value_type* pointer;
2146 typedef const value_type& reference;
2147 typedef std::forward_iterator_tag iterator_category;
2151 * @brief Provides a singular iterator, useful for indicating
2152 * one-past-the-end of a range.
2153 * @todo Implement this function.
2154 * @todo Document this function.
2159 * Constructs a %regex_iterator...
2160 * @param a [IN] The start of a text range to search.
2161 * @param b [IN] One-past-the-end of the text range to search.
2162 * @param re [IN] The regular expression to match.
2163 * @param m [IN] Policy flags for match rules.
2164 * @todo Implement this function.
2165 * @todo Document this function.
2167 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2168 regex_constants::match_flag_type __m
2169 = regex_constants::match_default);
2172 * Copy constructs a %regex_iterator.
2173 * @todo Implement this function.
2174 * @todo Document this function.
2176 regex_iterator(const regex_iterator& __rhs);
2179 * @todo Implement this function.
2180 * @todo Document this function.
2183 operator=(const regex_iterator& __rhs);
2186 * @todo Implement this function.
2187 * @todo Document this function.
2190 operator==(const regex_iterator& __rhs);
2193 * @todo Implement this function.
2194 * @todo Document this function.
2197 operator!=(const regex_iterator& __rhs);
2200 * @todo Implement this function.
2201 * @todo Document this function.
2207 * @todo Implement this function.
2208 * @todo Document this function.
2214 * @todo Implement this function.
2215 * @todo Document this function.
2221 * @todo Implement this function.
2222 * @todo Document this function.
2228 // these members are shown for exposition only:
2231 const regex_type* pregex;
2232 regex_constants::match_flag_type flags;
2233 match_results<_Bi_iter> match;
2236 typedef regex_iterator<const char*> cregex_iterator;
2237 typedef regex_iterator<string::const_iterator> sregex_iterator;
2238 #ifdef _GLIBCXX_USE_WCHAR_T
2239 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2240 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2243 // [7.12.2] Class template regex_token_iterator
2245 * Iterates over submatches in a range (or "splits" a text string).
2247 * The purpose of this iterator is to enumerate all, or all specified,
2248 * matches of a regular expression within a text range. The dereferenced
2249 * value of an iterator of this class is a std::tr1::sub_match object.
2251 template<typename _Bi_iter,
2252 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2253 typename _Rx_traits = regex_traits<_Ch_type> >
2254 class regex_token_iterator
2257 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2258 typedef sub_match<_Bi_iter> value_type;
2259 typedef std::ptrdiff_t difference_type;
2260 typedef const value_type* pointer;
2261 typedef const value_type& reference;
2262 typedef std::forward_iterator_tag iterator_category;
2266 * @brief Default constructs a %regex_token_iterator.
2267 * @todo Implement this function.
2269 * A default-constructed %regex_token_iterator is a singular iterator
2270 * that will compare equal to the one-past-the-end value for any
2271 * iterator of the same type.
2273 regex_token_iterator();
2276 * Constructs a %regex_token_iterator...
2277 * @param a [IN] The start of the text to search.
2278 * @param b [IN] One-past-the-end of the text to search.
2279 * @param re [IN] The regular expression to search for.
2280 * @param submatch [IN] Which submatch to return. There are some
2281 * special values for this parameter:
2282 * - -1 each enumerated subexpression does NOT
2283 * match the regular expression (aka field
2285 * - 0 the entire string matching the
2286 * subexpression is returned for each match
2288 * - >0 enumerates only the indicated
2289 * subexpression from a match within the text.
2290 * @param m [IN] Policy flags for match rules.
2292 * @todo Implement this function.
2293 * @todo Document this function.
2295 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2297 regex_constants::match_flag_type __m
2298 = regex_constants::match_default);
2301 * Constructs a %regex_token_iterator...
2302 * @param a [IN] The start of the text to search.
2303 * @param b [IN] One-past-the-end of the text to search.
2304 * @param re [IN] The regular expression to search for.
2305 * @param submatches [IN] A list of subexpressions to return for each
2306 * regular expression match within the text.
2307 * @param m [IN] Policy flags for match rules.
2309 * @todo Implement this function.
2310 * @todo Document this function.
2312 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2313 const regex_type& __re,
2314 const std::vector<int>& __submatches,
2315 regex_constants::match_flag_type __m
2316 = regex_constants::match_default);
2319 * Constructs a %regex_token_iterator...
2320 * @param a [IN] The start of the text to search.
2321 * @param b [IN] One-past-the-end of the text to search.
2322 * @param re [IN] The regular expression to search for.
2323 * @param submatches [IN] A list of subexpressions to return for each
2324 * regular expression match within the text.
2325 * @param m [IN] Policy flags for match rules.
2327 * @todo Implement this function.
2328 * @todo Document this function.
2330 template<std::size_t _Nm>
2331 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2332 const regex_type& __re,
2333 const int (&__submatches)[_Nm],
2334 regex_constants::match_flag_type __m
2335 = regex_constants::match_default);
2338 * @brief Copy constructs a %regex_token_iterator.
2339 * @param rhs [IN] A %regex_token_iterator to copy.
2340 * @todo Implement this function.
2342 regex_token_iterator(const regex_token_iterator& __rhs);
2345 * @brief Assigns a %regex_token_iterator to another.
2346 * @param rhs [IN] A %regex_token_iterator to copy.
2347 * @todo Implement this function.
2349 regex_token_iterator&
2350 operator=(const regex_token_iterator& __rhs);
2353 * @brief Compares a %regex_token_iterator to another for equality.
2354 * @todo Implement this function.
2357 operator==(const regex_token_iterator& __rhs);
2360 * @brief Compares a %regex_token_iterator to another for inequality.
2361 * @todo Implement this function.
2364 operator!=(const regex_token_iterator& __rhs);
2367 * @brief Dereferences a %regex_token_iterator.
2368 * @todo Implement this function.
2374 * @brief Selects a %regex_token_iterator member.
2375 * @todo Implement this function.
2381 * @brief Increments a %regex_token_iterator.
2382 * @todo Implement this function.
2384 regex_token_iterator&
2388 * @brief Postincrements a %regex_token_iterator.
2389 * @todo Implement this function.
2391 regex_token_iterator
2394 private: // data members for exposition only:
2395 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> position_iterator;
2397 position_iterator __position;
2398 const value_type* __result;
2399 value_type __suffix;
2401 std::vector<int> __subs;
2404 typedef regex_token_iterator<const char*> cregex_token_iterator;
2405 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2406 #ifdef _GLIBCXX_USE_WCHAR_T
2407 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2408 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2411 /** @} */ // group tr1_regex
2413 _GLIBCXX_END_NAMESPACE_TR1