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