Debian Codegraph @

libstdc++-v3/include/tr1/regex

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