2013-08-16 Tim Shen <timshen91@gmail.com>
[official-gcc.git] / libstdc++-v3 / include / bits / regex.h
blob8fa979fa375967dd1de1e9c847910bcc38dd0c3a
1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2013 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.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // 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.
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/>.
25 /**
26 * @file bits/regex.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
31 namespace std _GLIBCXX_VISIBILITY(default)
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
35 /**
36 * @addtogroup regex
37 * @{
40 /**
41 * @brief Class regex_traits. Describes aspects of a regular expression.
43 * A regular expression traits class that satisfies the requirements of
44 * section [28.7].
46 * The class %regex is parameterized around a set of related types and
47 * functions used to complete the definition of its semantics. This class
48 * satisfies the requirements of such a traits class.
50 template<typename _Ch_type>
51 struct regex_traits
53 public:
54 typedef _Ch_type char_type;
55 typedef std::basic_string<char_type> string_type;
56 typedef std::locale locale_type;
57 private:
58 struct _RegexMask
60 typedef typename std::ctype<char_type>::mask _BaseType;
61 _BaseType _M_base;
62 unsigned char _M_extended;
63 static constexpr unsigned char _S_under = 1 << 0;
64 // FIXME: _S_blank should be removed in the future,
65 // when locale's complete.
66 static constexpr unsigned char _S_blank = 1 << 1;
67 static constexpr unsigned char _S_valid_mask = 0x3;
69 constexpr _RegexMask(_BaseType __base = 0,
70 unsigned char __extended = 0)
71 : _M_base(__base), _M_extended(__extended)
72 { }
74 constexpr _RegexMask
75 operator&(_RegexMask __other) const
77 return _RegexMask(_M_base & __other._M_base,
78 _M_extended & __other._M_extended);
81 constexpr _RegexMask
82 operator|(_RegexMask __other) const
84 return _RegexMask(_M_base | __other._M_base,
85 _M_extended | __other._M_extended);
88 constexpr _RegexMask
89 operator^(_RegexMask __other) const
91 return _RegexMask(_M_base ^ __other._M_base,
92 _M_extended ^ __other._M_extended);
95 constexpr _RegexMask
96 operator~() const
97 { return _RegexMask(~_M_base, ~_M_extended); }
99 _RegexMask&
100 operator&=(_RegexMask __other)
101 { return *this = (*this) & __other; }
103 _RegexMask&
104 operator|=(_RegexMask __other)
105 { return *this = (*this) | __other; }
107 _RegexMask&
108 operator^=(_RegexMask __other)
109 { return *this = (*this) ^ __other; }
111 constexpr bool
112 operator==(_RegexMask __other) const
114 return (_M_extended & _S_valid_mask)
115 == (__other._M_extended & _S_valid_mask)
116 && _M_base == __other._M_base;
119 constexpr bool
120 operator!=(_RegexMask __other) const
121 { return !((*this) == __other); }
124 public:
125 typedef _RegexMask char_class_type;
127 public:
129 * @brief Constructs a default traits object.
131 regex_traits() { }
134 * @brief Gives the length of a C-style string starting at @p __p.
136 * @param __p a pointer to the start of a character sequence.
138 * @returns the number of characters between @p *__p and the first
139 * default-initialized value of type @p char_type. In other words, uses
140 * the C-string algorithm for determining the length of a sequence of
141 * characters.
143 static std::size_t
144 length(const char_type* __p)
145 { return string_type::traits_type::length(__p); }
148 * @brief Performs the identity translation.
150 * @param __c A character to the locale-specific character set.
152 * @returns __c.
154 char_type
155 translate(char_type __c) const
156 { return __c; }
159 * @brief Translates a character into a case-insensitive equivalent.
161 * @param __c A character to the locale-specific character set.
163 * @returns the locale-specific lower-case equivalent of __c.
164 * @throws std::bad_cast if the imbued locale does not support the ctype
165 * facet.
167 char_type
168 translate_nocase(char_type __c) const
170 typedef std::ctype<char_type> __ctype_type;
171 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
172 return __fctyp.tolower(__c);
176 * @brief Gets a sort key for a character sequence.
178 * @param __first beginning of the character sequence.
179 * @param __last one-past-the-end of the character sequence.
181 * Returns a sort key for the character sequence designated by the
182 * iterator range [F1, F2) such that if the character sequence [G1, G2)
183 * sorts before the character sequence [H1, H2) then
184 * v.transform(G1, G2) < v.transform(H1, H2).
186 * What this really does is provide a more efficient way to compare a
187 * string to multiple other strings in locales with fancy collation
188 * rules and equivalence classes.
190 * @returns a locale-specific sort key equivalent to the input range.
192 * @throws std::bad_cast if the current locale does not have a collate
193 * facet.
195 template<typename _Fwd_iter>
196 string_type
197 transform(_Fwd_iter __first, _Fwd_iter __last) const
199 typedef std::collate<char_type> __collate_type;
200 const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
201 string_type __s(__first, __last);
202 return __fclt.transform(__s.data(), __s.data() + __s.size());
206 * @brief Gets a sort key for a character sequence, independent of case.
208 * @param __first beginning of the character sequence.
209 * @param __last one-past-the-end of the character sequence.
211 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
212 * typeid(collate_byname<_Ch_type>) and the form of the sort key
213 * returned by collate_byname<_Ch_type>::transform(__first, __last)
214 * is known and can be converted into a primary sort key
215 * then returns that key, otherwise returns an empty string.
217 * @todo Implement this function.
219 template<typename _Fwd_iter>
220 string_type
221 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
223 typedef std::ctype<char_type> __ctype_type;
224 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
225 std::vector<char_type> __s(__first, __last);
226 // FIXME : this is not entirely correct
227 __fctyp.tolower(__s.data(), __s.data() + __s.size());
228 return this->transform(__s.data(), __s.data() + __s.size());
232 * @brief Gets a collation element by name.
234 * @param __first beginning of the collation element name.
235 * @param __last one-past-the-end of the collation element name.
237 * @returns a sequence of one or more characters that represents the
238 * collating element consisting of the character sequence designated by
239 * the iterator range [__first, __last). Returns an empty string if the
240 * character sequence is not a valid collating element.
242 template<typename _Fwd_iter>
243 string_type
244 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
247 * @brief Maps one or more characters to a named character
248 * classification.
250 * @param __first beginning of the character sequence.
251 * @param __last one-past-the-end of the character sequence.
252 * @param __icase ignores the case of the classification name.
254 * @returns an unspecified value that represents the character
255 * classification named by the character sequence designated by
256 * the iterator range [__first, __last). If @p icase is true,
257 * the returned mask identifies the classification regardless of
258 * the case of the characters to be matched (for example,
259 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
260 * case-dependent classification is returned. The value
261 * returned shall be independent of the case of the characters
262 * in the character sequence. If the name is not recognized then
263 * returns a value that compares equal to 0.
265 * At least the following names (or their wide-character equivalent) are
266 * supported.
267 * - d
268 * - w
269 * - s
270 * - alnum
271 * - alpha
272 * - blank
273 * - cntrl
274 * - digit
275 * - graph
276 * - lower
277 * - print
278 * - punct
279 * - space
280 * - upper
281 * - xdigit
283 template<typename _Fwd_iter>
284 char_class_type
285 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
286 bool __icase = false) const;
289 * @brief Determines if @p c is a member of an identified class.
291 * @param __c a character.
292 * @param __f a class type (as returned from lookup_classname).
294 * @returns true if the character @p __c is a member of the classification
295 * represented by @p __f, false otherwise.
297 * @throws std::bad_cast if the current locale does not have a ctype
298 * facet.
300 bool
301 isctype(_Ch_type __c, char_class_type __f) const;
304 * @brief Converts a digit to an int.
306 * @param __ch a character representing a digit.
307 * @param __radix the radix if the numeric conversion (limited to 8, 10,
308 * or 16).
310 * @returns the value represented by the digit __ch in base radix if the
311 * character __ch is a valid digit in base radix; otherwise returns -1.
314 value(_Ch_type __ch, int __radix) const;
317 * @brief Imbues the regex_traits object with a copy of a new locale.
319 * @param __loc A locale.
321 * @returns a copy of the previous locale in use by the regex_traits
322 * object.
324 * @note Calling imbue with a different locale than the one currently in
325 * use invalidates all cached data held by *this.
327 locale_type
328 imbue(locale_type __loc)
330 std::swap(_M_locale, __loc);
331 return __loc;
335 * @brief Gets a copy of the current locale in use by the regex_traits
336 * object.
338 locale_type
339 getloc() const
340 { return _M_locale; }
342 protected:
343 locale_type _M_locale;
346 template<typename _Ch_type>
347 template<typename _Fwd_iter>
348 typename regex_traits<_Ch_type>::string_type
349 regex_traits<_Ch_type>::
350 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
352 typedef std::ctype<char_type> __ctype_type;
353 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
355 static const char* __collatenames[] =
357 "NUL",
358 "SOH",
359 "STX",
360 "ETX",
361 "EOT",
362 "ENQ",
363 "ACK",
364 "alert",
365 "backspace",
366 "tab",
367 "newline",
368 "vertical-tab",
369 "form-feed",
370 "carriage-return",
371 "SO",
372 "SI",
373 "DLE",
374 "DC1",
375 "DC2",
376 "DC3",
377 "DC4",
378 "NAK",
379 "SYN",
380 "ETB",
381 "CAN",
382 "EM",
383 "SUB",
384 "ESC",
385 "IS4",
386 "IS3",
387 "IS2",
388 "IS1",
389 "space",
390 "exclamation-mark",
391 "quotation-mark",
392 "number-sign",
393 "dollar-sign",
394 "percent-sign",
395 "ampersand",
396 "apostrophe",
397 "left-parenthesis",
398 "right-parenthesis",
399 "asterisk",
400 "plus-sign",
401 "comma",
402 "hyphen",
403 "period",
404 "slash",
405 "zero",
406 "one",
407 "two",
408 "three",
409 "four",
410 "five",
411 "six",
412 "seven",
413 "eight",
414 "nine",
415 "colon",
416 "semicolon",
417 "less-than-sign",
418 "equals-sign",
419 "greater-than-sign",
420 "question-mark",
421 "commercial-at",
422 "A",
423 "B",
424 "C",
425 "D",
426 "E",
427 "F",
428 "G",
429 "H",
430 "I",
431 "J",
432 "K",
433 "L",
434 "M",
435 "N",
436 "O",
437 "P",
438 "Q",
439 "R",
440 "S",
441 "T",
442 "U",
443 "V",
444 "W",
445 "X",
446 "Y",
447 "Z",
448 "left-square-bracket",
449 "backslash",
450 "right-square-bracket",
451 "circumflex",
452 "underscore",
453 "grave-accent",
454 "a",
455 "b",
456 "c",
457 "d",
458 "e",
459 "f",
460 "g",
461 "h",
462 "i",
463 "j",
464 "k",
465 "l",
466 "m",
467 "n",
468 "o",
469 "p",
470 "q",
471 "r",
472 "s",
473 "t",
474 "u",
475 "v",
476 "w",
477 "x",
478 "y",
479 "z",
480 "left-curly-bracket",
481 "vertical-line",
482 "right-curly-bracket",
483 "tilde",
484 "DEL",
488 // same as boost
489 static const char* __digraphs[] =
491 "ae",
492 "Ae",
493 "AE",
494 "ch",
495 "Ch",
496 "CH",
497 "ll",
498 "Ll",
499 "LL",
500 "ss",
501 "Ss",
502 "SS",
503 "nj",
504 "Nj",
505 "NJ",
506 "dz",
507 "Dz",
508 "DZ",
509 "lj",
510 "Lj",
511 "LJ",
515 std::string __s(__last - __first, '?');
516 __fctyp.narrow(__first, __last, '?', &*__s.begin());
518 for (unsigned int __i = 0; *__collatenames[__i]; __i++)
519 if (__s == __collatenames[__i])
520 return string_type(1, __fctyp.widen((char)__i));
522 for (unsigned int __i = 0; *__digraphs[__i]; __i++)
524 const char* __now = __digraphs[__i];
525 if (__s == __now)
527 string_type ret(__s.size(), __fctyp.widen('?'));
528 __fctyp.widen(__now, __now + 2/* ouch */, &*ret.begin());
529 return ret;
532 return string_type();
535 template<typename _Ch_type>
536 template<typename _Fwd_iter>
537 typename regex_traits<_Ch_type>::char_class_type
538 regex_traits<_Ch_type>::
539 lookup_classname(_Fwd_iter __first, _Fwd_iter __last, bool __icase) const
541 typedef std::ctype<char_type> __ctype_type;
542 typedef std::ctype<char> __cctype_type;
543 typedef const pair<const char*, char_class_type> _ClassnameEntry;
544 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
545 const __cctype_type& __cctyp(use_facet<__cctype_type>(_M_locale));
547 static _ClassnameEntry __classnames[] =
549 {"d", ctype_base::digit},
550 {"w", {ctype_base::alnum, _RegexMask::_S_under}},
551 {"s", ctype_base::space},
552 {"alnum", ctype_base::alnum},
553 {"alpha", ctype_base::alpha},
554 {"blank", {0, _RegexMask::_S_blank}},
555 {"cntrl", ctype_base::cntrl},
556 {"digit", ctype_base::digit},
557 {"graph", ctype_base::graph},
558 {"lower", ctype_base::lower},
559 {"print", ctype_base::print},
560 {"punct", ctype_base::punct},
561 {"space", ctype_base::space},
562 {"upper", ctype_base::upper},
563 {"xdigit", ctype_base::xdigit},
566 std::string __s(__last - __first, '?');
567 __fctyp.narrow(__first, __last, '?', &__s[0]);
568 __cctyp.tolower(&*__s.begin(), &*__s.begin() + __s.size());
569 for (_ClassnameEntry* __it = __classnames;
570 __it < *(&__classnames + 1);
571 ++__it)
573 if (__s == __it->first)
575 if (__icase
576 && ((__it->second
577 & (ctype_base::lower | ctype_base::upper)) != 0))
578 return ctype_base::alpha;
579 return __it->second;
582 return 0;
585 template<typename _Ch_type>
586 bool
587 regex_traits<_Ch_type>::
588 isctype(_Ch_type __c, char_class_type __f) const
590 typedef std::ctype<char_type> __ctype_type;
591 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
593 return __fctyp.is(__f._M_base, __c)
594 // [[:w:]]
595 || ((__f._M_extended & _RegexMask::_S_under)
596 && __c == __fctyp.widen('_'))
597 // [[:blank:]]
598 || ((__f._M_extended & _RegexMask::_S_blank)
599 && (__c == __fctyp.widen(' ')
600 || __c == __fctyp.widen('\t')));
603 template<typename _Ch_type>
605 regex_traits<_Ch_type>::
606 value(_Ch_type __ch, int __radix) const
608 std::basic_istringstream<char_type> __is(string_type(1, __ch));
609 int __v;
610 if (__radix == 8)
611 __is >> std::oct;
612 else if (__radix == 16)
613 __is >> std::hex;
614 __is >> __v;
615 return __is.fail() ? -1 : __v;
618 // [7.8] Class basic_regex
620 * Objects of specializations of this class represent regular expressions
621 * constructed from sequences of character type @p _Ch_type.
623 * Storage for the regular expression is allocated and deallocated as
624 * necessary by the member functions of this class.
626 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type> >
627 class basic_regex
629 public:
630 // types:
631 typedef _Ch_type value_type;
632 typedef _Rx_traits traits_type;
633 typedef typename traits_type::string_type string_type;
634 typedef regex_constants::syntax_option_type flag_type;
635 typedef typename traits_type::locale_type locale_type;
638 * @name Constants
639 * std [28.8.1](1)
641 //@{
642 static constexpr flag_type icase = regex_constants::icase;
643 static constexpr flag_type nosubs = regex_constants::nosubs;
644 static constexpr flag_type optimize = regex_constants::optimize;
645 static constexpr flag_type collate = regex_constants::collate;
646 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
647 static constexpr flag_type basic = regex_constants::basic;
648 static constexpr flag_type extended = regex_constants::extended;
649 static constexpr flag_type awk = regex_constants::awk;
650 static constexpr flag_type grep = regex_constants::grep;
651 static constexpr flag_type egrep = regex_constants::egrep;
652 //@}
654 // [7.8.2] construct/copy/destroy
656 * Constructs a basic regular expression that does not match any
657 * character sequence.
659 basic_regex()
660 : _M_flags(ECMAScript), _M_automaton(nullptr)
664 * @brief Constructs a basic regular expression from the
665 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
666 * interpreted according to the flags in @p __f.
668 * @param __p A pointer to the start of a C-style null-terminated string
669 * containing a regular expression.
670 * @param __f Flags indicating the syntax rules and options.
672 * @throws regex_error if @p __p is not a valid regular expression.
674 explicit
675 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
676 : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
680 * @brief Constructs a basic regular expression from the sequence
681 * [p, p + len) interpreted according to the flags in @p f.
683 * @param __p A pointer to the start of a string containing a regular
684 * expression.
685 * @param __len The length of the string containing the regular
686 * expression.
687 * @param __f Flags indicating the syntax rules and options.
689 * @throws regex_error if @p __p is not a valid regular expression.
691 basic_regex(const _Ch_type* __p,
692 std::size_t __len, flag_type __f = ECMAScript)
693 : basic_regex(__p, __p + __len, __f)
697 * @brief Copy-constructs a basic regular expression.
699 * @param __rhs A @p regex object.
701 basic_regex(const basic_regex& __rhs) = default;
704 * @brief Move-constructs a basic regular expression.
706 * @param __rhs A @p regex object.
708 basic_regex(const basic_regex&& __rhs) noexcept
709 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
710 _M_automaton(std::move(__rhs._M_automaton))
714 * @brief Constructs a basic regular expression from the string
715 * @p s interpreted according to the flags in @p f.
717 * @param __s A string containing a regular expression.
718 * @param __f Flags indicating the syntax rules and options.
720 * @throws regex_error if @p __s is not a valid regular expression.
722 template<typename _Ch_traits, typename _Ch_alloc>
723 explicit
724 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
725 _Ch_alloc>& __s,
726 flag_type __f = ECMAScript)
727 : basic_regex(__s.begin(), __s.end(), __f)
731 * @brief Constructs a basic regular expression from the range
732 * [first, last) interpreted according to the flags in @p f.
734 * @param __first The start of a range containing a valid regular
735 * expression.
736 * @param __last The end of a range containing a valid regular
737 * expression.
738 * @param __f The format flags of the regular expression.
740 * @throws regex_error if @p [__first, __last) is not a valid regular
741 * expression.
743 template<typename _InputIterator>
744 basic_regex(_InputIterator __first, _InputIterator __last,
745 flag_type __f = ECMAScript)
746 : _M_flags(__f),
747 _M_automaton(__detail::_Compiler<_InputIterator, _Ch_type, _Rx_traits>
748 (__first, __last, _M_traits, _M_flags)._M_get_nfa())
752 * @brief Constructs a basic regular expression from an initializer list.
754 * @param __l The initializer list.
755 * @param __f The format flags of the regular expression.
757 * @throws regex_error if @p __l is not a valid regular expression.
759 basic_regex(initializer_list<_Ch_type> __l,
760 flag_type __f = ECMAScript)
761 : basic_regex(__l.begin(), __l.end(), __f)
765 * @brief Destroys a basic regular expression.
767 ~basic_regex()
771 * @brief Assigns one regular expression to another.
773 basic_regex&
774 operator=(const basic_regex& __rhs)
775 { return this->assign(__rhs); }
778 * @brief Move-assigns one regular expression to another.
780 basic_regex&
781 operator=(basic_regex&& __rhs) noexcept
782 { return this->assign(std::move(__rhs)); }
785 * @brief Replaces a regular expression with a new one constructed from
786 * a C-style null-terminated string.
788 * @param __p A pointer to the start of a null-terminated C-style string
789 * containing a regular expression.
791 basic_regex&
792 operator=(const _Ch_type* __p)
793 { return this->assign(__p, flags()); }
796 * @brief Replaces a regular expression with a new one constructed from
797 * a string.
799 * @param __s A pointer to a string containing a regular expression.
801 template<typename _Ch_typeraits, typename _Alloc>
802 basic_regex&
803 operator=(const basic_string<_Ch_type, _Ch_typeraits, _Alloc>& __s)
804 { return this->assign(__s, flags()); }
806 // [7.8.3] assign
808 * @brief the real assignment operator.
810 * @param __rhs Another regular expression object.
812 basic_regex&
813 assign(const basic_regex& __rhs)
815 basic_regex __tmp(__rhs);
816 this->swap(__tmp);
817 return *this;
821 * @brief The move-assignment operator.
823 * @param __rhs Another regular expression object.
825 basic_regex&
826 assign(basic_regex&& __rhs) noexcept
828 basic_regex __tmp(std::move(__rhs));
829 this->swap(__tmp);
830 return *this;
834 * @brief Assigns a new regular expression to a regex object from a
835 * C-style null-terminated string containing a regular expression
836 * pattern.
838 * @param __p A pointer to a C-style null-terminated string containing
839 * a regular expression pattern.
840 * @param __flags Syntax option flags.
842 * @throws regex_error if __p does not contain a valid regular
843 * expression pattern interpreted according to @p __flags. If
844 * regex_error is thrown, *this remains unchanged.
846 basic_regex&
847 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
848 { return this->assign(string_type(__p), __flags); }
851 * @brief Assigns a new regular expression to a regex object from a
852 * C-style string containing a regular expression pattern.
854 * @param __p A pointer to a C-style string containing a
855 * regular expression pattern.
856 * @param __len The length of the regular expression pattern string.
857 * @param __flags Syntax option flags.
859 * @throws regex_error if p does not contain a valid regular
860 * expression pattern interpreted according to @p __flags. If
861 * regex_error is thrown, *this remains unchanged.
863 basic_regex&
864 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
865 { return this->assign(string_type(__p, __len), __flags); }
868 * @brief Assigns a new regular expression to a regex object from a
869 * string containing a regular expression pattern.
871 * @param __s A string containing a regular expression pattern.
872 * @param __flags Syntax option flags.
874 * @throws regex_error if __s does not contain a valid regular
875 * expression pattern interpreted according to @p __flags. If
876 * regex_error is thrown, *this remains unchanged.
878 template<typename _Ch_typeraits, typename _Alloc>
879 basic_regex&
880 assign(const basic_string<_Ch_type, _Ch_typeraits, _Alloc>& __s,
881 flag_type __flags = ECMAScript)
883 basic_regex __tmp(__s, __flags);
884 this->swap(__tmp);
885 return *this;
889 * @brief Assigns a new regular expression to a regex object.
891 * @param __first The start of a range containing a valid regular
892 * expression.
893 * @param __last The end of a range containing a valid regular
894 * expression.
895 * @param __flags Syntax option flags.
897 * @throws regex_error if p does not contain a valid regular
898 * expression pattern interpreted according to @p __flags. If
899 * regex_error is thrown, the object remains unchanged.
901 template<typename _InputIterator>
902 basic_regex&
903 assign(_InputIterator __first, _InputIterator __last,
904 flag_type __flags = ECMAScript)
905 { return this->assign(string_type(__first, __last), __flags); }
908 * @brief Assigns a new regular expression to a regex object.
910 * @param __l An initializer list representing a regular expression.
911 * @param __flags Syntax option flags.
913 * @throws regex_error if @p __l does not contain a valid
914 * regular expression pattern interpreted according to @p
915 * __flags. If regex_error is thrown, the object remains
916 * unchanged.
918 basic_regex&
919 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
920 { return this->assign(__l.begin(), __l.end(), __flags); }
922 // [7.8.4] const operations
924 * @brief Gets the number of marked subexpressions within the regular
925 * expression.
927 unsigned int
928 mark_count() const
929 { return _M_automaton->_M_sub_count() - 1; }
932 * @brief Gets the flags used to construct the regular expression
933 * or in the last call to assign().
935 flag_type
936 flags() const
937 { return _M_flags; }
939 // [7.8.5] locale
941 * @brief Imbues the regular expression object with the given locale.
943 * @param __loc A locale.
945 locale_type
946 imbue(locale_type __loc)
947 { return _M_traits.imbue(__loc); }
950 * @brief Gets the locale currently imbued in the regular expression
951 * object.
953 locale_type
954 getloc() const
955 { return _M_traits.getloc(); }
957 // [7.8.6] swap
959 * @brief Swaps the contents of two regular expression objects.
961 * @param __rhs Another regular expression object.
963 void
964 swap(basic_regex& __rhs)
966 std::swap(_M_flags, __rhs._M_flags);
967 std::swap(_M_traits, __rhs._M_traits);
968 std::swap(_M_automaton, __rhs._M_automaton);
971 #ifdef _GLIBCXX_DEBUG
972 void
973 _M_dot(std::ostream& __ostr)
974 { _M_automaton->_M_dot(__ostr); }
975 #endif
977 protected:
978 typedef std::shared_ptr<__detail::_Automaton<_Ch_type, _Rx_traits>>
979 _AutomatonPtr;
981 template<typename _BiIter, typename _Alloc,
982 typename _CharT, typename _TraitsT>
983 friend std::unique_ptr<
984 __detail::_Executor<_BiIter, _Alloc, _CharT, _TraitsT>>
985 __detail::__get_executor(_BiIter,
986 _BiIter,
987 match_results<_BiIter, _Alloc>&,
988 const basic_regex<_CharT, _TraitsT>&,
989 regex_constants::match_flag_type);
991 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
992 friend bool
993 regex_match(_Bp, _Bp,
994 match_results<_Bp, _Ap>&,
995 const basic_regex<_Cp, _Rp>&,
996 regex_constants::match_flag_type);
998 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
999 friend bool
1000 regex_search(_Bp, _Bp,
1001 match_results<_Bp, _Ap>&,
1002 const basic_regex<_Cp, _Rp>&,
1003 regex_constants::match_flag_type);
1005 flag_type _M_flags;
1006 _Rx_traits _M_traits;
1007 _AutomatonPtr _M_automaton;
1010 /** @brief Standard regular expressions. */
1011 typedef basic_regex<char> regex;
1013 #ifdef _GLIBCXX_USE_WCHAR_T
1014 /** @brief Standard wide-character regular expressions. */
1015 typedef basic_regex<wchar_t> wregex;
1016 #endif
1019 // [7.8.6] basic_regex swap
1021 * @brief Swaps the contents of two regular expression objects.
1022 * @param __lhs First regular expression.
1023 * @param __rhs Second regular expression.
1025 template<typename _Ch_type, typename _Rx_traits>
1026 inline void
1027 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
1028 basic_regex<_Ch_type, _Rx_traits>& __rhs)
1029 { __lhs.swap(__rhs); }
1032 // [7.9] Class template sub_match
1034 * A sequence of characters matched by a particular marked sub-expression.
1036 * An object of this class is essentially a pair of iterators marking a
1037 * matched subexpression within a regular expression pattern match. Such
1038 * objects can be converted to and compared with std::basic_string objects
1039 * of a similar base character type as the pattern matched by the regular
1040 * expression.
1042 * The iterators that make up the pair are the usual half-open interval
1043 * referencing the actual original pattern matched.
1045 template<typename _BiIter>
1046 class sub_match : public std::pair<_BiIter, _BiIter>
1048 typedef iterator_traits<_BiIter> __iter_traits;
1050 public:
1051 typedef typename __iter_traits::value_type value_type;
1052 typedef typename __iter_traits::difference_type difference_type;
1053 typedef _BiIter iterator;
1054 typedef std::basic_string<value_type> string_type;
1056 bool matched;
1058 constexpr sub_match() : matched() { }
1061 * Gets the length of the matching sequence.
1063 difference_type
1064 length() const
1065 { return this->matched ? std::distance(this->first, this->second) : 0; }
1068 * @brief Gets the matching sequence as a string.
1070 * @returns the matching sequence as a string.
1072 * This is the implicit conversion operator. It is identical to the
1073 * str() member function except that it will want to pop up in
1074 * unexpected places and cause a great deal of confusion and cursing
1075 * from the unwary.
1077 operator string_type() const
1079 return this->matched
1080 ? string_type(this->first, this->second)
1081 : string_type();
1085 * @brief Gets the matching sequence as a string.
1087 * @returns the matching sequence as a string.
1089 string_type
1090 str() const
1092 return this->matched
1093 ? string_type(this->first, this->second)
1094 : string_type();
1098 * @brief Compares this and another matched sequence.
1100 * @param __s Another matched sequence to compare to this one.
1102 * @retval <0 this matched sequence will collate before @p __s.
1103 * @retval =0 this matched sequence is equivalent to @p __s.
1104 * @retval <0 this matched sequence will collate after @p __s.
1107 compare(const sub_match& __s) const
1108 { return this->str().compare(__s.str()); }
1111 * @brief Compares this sub_match to a string.
1113 * @param __s A string to compare to this sub_match.
1115 * @retval <0 this matched sequence will collate before @p __s.
1116 * @retval =0 this matched sequence is equivalent to @p __s.
1117 * @retval <0 this matched sequence will collate after @p __s.
1120 compare(const string_type& __s) const
1121 { return this->str().compare(__s); }
1124 * @brief Compares this sub_match to a C-style string.
1126 * @param __s A C-style string to compare to this sub_match.
1128 * @retval <0 this matched sequence will collate before @p __s.
1129 * @retval =0 this matched sequence is equivalent to @p __s.
1130 * @retval <0 this matched sequence will collate after @p __s.
1133 compare(const value_type* __s) const
1134 { return this->str().compare(__s); }
1138 /** @brief Standard regex submatch over a C-style null-terminated string. */
1139 typedef sub_match<const char*> csub_match;
1141 /** @brief Standard regex submatch over a standard string. */
1142 typedef sub_match<string::const_iterator> ssub_match;
1144 #ifdef _GLIBCXX_USE_WCHAR_T
1145 /** @brief Regex submatch over a C-style null-terminated wide string. */
1146 typedef sub_match<const wchar_t*> wcsub_match;
1148 /** @brief Regex submatch over a standard wide string. */
1149 typedef sub_match<wstring::const_iterator> wssub_match;
1150 #endif
1152 // [7.9.2] sub_match non-member operators
1155 * @brief Tests the equivalence of two regular expression submatches.
1156 * @param __lhs First regular expression submatch.
1157 * @param __rhs Second regular expression submatch.
1158 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1160 template<typename _BiIter>
1161 inline bool
1162 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1163 { return __lhs.compare(__rhs) == 0; }
1166 * @brief Tests the inequivalence of two regular expression submatches.
1167 * @param __lhs First regular expression submatch.
1168 * @param __rhs Second regular expression submatch.
1169 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1171 template<typename _BiIter>
1172 inline bool
1173 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1174 { return __lhs.compare(__rhs) != 0; }
1177 * @brief Tests the ordering of two regular expression submatches.
1178 * @param __lhs First regular expression submatch.
1179 * @param __rhs Second regular expression submatch.
1180 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1182 template<typename _BiIter>
1183 inline bool
1184 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1185 { return __lhs.compare(__rhs) < 0; }
1188 * @brief Tests the ordering of two regular expression submatches.
1189 * @param __lhs First regular expression submatch.
1190 * @param __rhs Second regular expression submatch.
1191 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1193 template<typename _BiIter>
1194 inline bool
1195 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1196 { return __lhs.compare(__rhs) <= 0; }
1199 * @brief Tests the ordering of two regular expression submatches.
1200 * @param __lhs First regular expression submatch.
1201 * @param __rhs Second regular expression submatch.
1202 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1204 template<typename _BiIter>
1205 inline bool
1206 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1207 { return __lhs.compare(__rhs) >= 0; }
1210 * @brief Tests the ordering of two regular expression submatches.
1211 * @param __lhs First regular expression submatch.
1212 * @param __rhs Second regular expression submatch.
1213 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1215 template<typename _BiIter>
1216 inline bool
1217 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1218 { return __lhs.compare(__rhs) > 0; }
1220 // Alias for sub_match'd string.
1221 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1222 using __sub_match_string = basic_string<
1223 typename iterator_traits<_Bi_iter>::value_type,
1224 _Ch_traits, _Ch_alloc>;
1227 * @brief Tests the equivalence of a string and a regular expression
1228 * submatch.
1229 * @param __lhs A string.
1230 * @param __rhs A regular expression submatch.
1231 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1233 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1234 inline bool
1235 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1236 const sub_match<_Bi_iter>& __rhs)
1237 { return __rhs.compare(__lhs.c_str()) == 0; }
1240 * @brief Tests the inequivalence of a string and a regular expression
1241 * submatch.
1242 * @param __lhs A string.
1243 * @param __rhs A regular expression submatch.
1244 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1246 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1247 inline bool
1248 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1249 const sub_match<_Bi_iter>& __rhs)
1250 { return !(__lhs == __rhs); }
1253 * @brief Tests the ordering of a string and a regular expression submatch.
1254 * @param __lhs A string.
1255 * @param __rhs A regular expression submatch.
1256 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1258 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1259 inline bool
1260 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1261 const sub_match<_Bi_iter>& __rhs)
1262 { return __rhs.compare(__lhs.c_str()) > 0; }
1265 * @brief Tests the ordering of a string and a regular expression submatch.
1266 * @param __lhs A string.
1267 * @param __rhs A regular expression submatch.
1268 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1270 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1271 inline bool
1272 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1273 const sub_match<_Bi_iter>& __rhs)
1274 { return __rhs < __lhs; }
1277 * @brief Tests the ordering of a string and a regular expression submatch.
1278 * @param __lhs A string.
1279 * @param __rhs A regular expression submatch.
1280 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1282 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1283 inline bool
1284 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1285 const sub_match<_Bi_iter>& __rhs)
1286 { return !(__lhs < __rhs); }
1289 * @brief Tests the ordering of a string and a regular expression submatch.
1290 * @param __lhs A string.
1291 * @param __rhs A regular expression submatch.
1292 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1294 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1295 inline bool
1296 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1297 const sub_match<_Bi_iter>& __rhs)
1298 { return !(__rhs < __lhs); }
1301 * @brief Tests the equivalence of a regular expression submatch and a
1302 * string.
1303 * @param __lhs A regular expression submatch.
1304 * @param __rhs A string.
1305 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1307 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1308 inline bool
1309 operator==(const sub_match<_Bi_iter>& __lhs,
1310 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1311 { return __lhs.compare(__rhs.c_str()) == 0; }
1314 * @brief Tests the inequivalence of a regular expression submatch and a
1315 * string.
1316 * @param __lhs A regular expression submatch.
1317 * @param __rhs A string.
1318 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1320 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1321 inline bool
1322 operator!=(const sub_match<_Bi_iter>& __lhs,
1323 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1324 { return !(__lhs == __rhs); }
1327 * @brief Tests the ordering of a regular expression submatch and a string.
1328 * @param __lhs A regular expression submatch.
1329 * @param __rhs A string.
1330 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1332 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1333 inline bool
1334 operator<(const sub_match<_Bi_iter>& __lhs,
1335 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1336 { return __lhs.compare(__rhs.c_str()) < 0; }
1339 * @brief Tests the ordering of a regular expression submatch and a string.
1340 * @param __lhs A regular expression submatch.
1341 * @param __rhs A string.
1342 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1344 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1345 inline bool
1346 operator>(const sub_match<_Bi_iter>& __lhs,
1347 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1348 { return __rhs < __lhs; }
1351 * @brief Tests the ordering of a regular expression submatch and a string.
1352 * @param __lhs A regular expression submatch.
1353 * @param __rhs A string.
1354 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1356 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1357 inline bool
1358 operator>=(const sub_match<_Bi_iter>& __lhs,
1359 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1360 { return !(__lhs < __rhs); }
1363 * @brief Tests the ordering of a regular expression submatch and a string.
1364 * @param __lhs A regular expression submatch.
1365 * @param __rhs A string.
1366 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1368 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1369 inline bool
1370 operator<=(const sub_match<_Bi_iter>& __lhs,
1371 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1372 { return !(__rhs < __lhs); }
1375 * @brief Tests the equivalence of a C string and a regular expression
1376 * submatch.
1377 * @param __lhs A C string.
1378 * @param __rhs A regular expression submatch.
1379 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1381 template<typename _Bi_iter>
1382 inline bool
1383 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1384 const sub_match<_Bi_iter>& __rhs)
1385 { return __rhs.compare(__lhs) == 0; }
1388 * @brief Tests the inequivalence of an iterator value and a regular
1389 * expression submatch.
1390 * @param __lhs A regular expression submatch.
1391 * @param __rhs A string.
1392 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1394 template<typename _Bi_iter>
1395 inline bool
1396 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1397 const sub_match<_Bi_iter>& __rhs)
1398 { return !(__lhs == __rhs); }
1401 * @brief Tests the ordering of a string and a regular expression submatch.
1402 * @param __lhs A string.
1403 * @param __rhs A regular expression submatch.
1404 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1406 template<typename _Bi_iter>
1407 inline bool
1408 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1409 const sub_match<_Bi_iter>& __rhs)
1410 { return __rhs.compare(__lhs) > 0; }
1413 * @brief Tests the ordering of a string and a regular expression submatch.
1414 * @param __lhs A string.
1415 * @param __rhs A regular expression submatch.
1416 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1418 template<typename _Bi_iter>
1419 inline bool
1420 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1421 const sub_match<_Bi_iter>& __rhs)
1422 { return __rhs < __lhs; }
1425 * @brief Tests the ordering of a string and a regular expression submatch.
1426 * @param __lhs A string.
1427 * @param __rhs A regular expression submatch.
1428 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1430 template<typename _Bi_iter>
1431 inline bool
1432 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1433 const sub_match<_Bi_iter>& __rhs)
1434 { return !(__lhs < __rhs); }
1437 * @brief Tests the ordering of a string and a regular expression submatch.
1438 * @param __lhs A string.
1439 * @param __rhs A regular expression submatch.
1440 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1442 template<typename _Bi_iter>
1443 inline bool
1444 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1445 const sub_match<_Bi_iter>& __rhs)
1446 { return !(__rhs < __lhs); }
1449 * @brief Tests the equivalence of a regular expression submatch and a
1450 * string.
1451 * @param __lhs A regular expression submatch.
1452 * @param __rhs A pointer to a string?
1453 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1455 template<typename _Bi_iter>
1456 inline bool
1457 operator==(const sub_match<_Bi_iter>& __lhs,
1458 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1459 { return __lhs.compare(__rhs) == 0; }
1462 * @brief Tests the inequivalence of a regular expression submatch and a
1463 * string.
1464 * @param __lhs A regular expression submatch.
1465 * @param __rhs A pointer to a string.
1466 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1468 template<typename _Bi_iter>
1469 inline bool
1470 operator!=(const sub_match<_Bi_iter>& __lhs,
1471 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1472 { return !(__lhs == __rhs); }
1475 * @brief Tests the ordering of a regular expression submatch and a string.
1476 * @param __lhs A regular expression submatch.
1477 * @param __rhs A string.
1478 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1480 template<typename _Bi_iter>
1481 inline bool
1482 operator<(const sub_match<_Bi_iter>& __lhs,
1483 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1484 { return __lhs.compare(__rhs) < 0; }
1487 * @brief Tests the ordering of a regular expression submatch and a string.
1488 * @param __lhs A regular expression submatch.
1489 * @param __rhs A string.
1490 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1492 template<typename _Bi_iter>
1493 inline bool
1494 operator>(const sub_match<_Bi_iter>& __lhs,
1495 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1496 { return __rhs < __lhs; }
1499 * @brief Tests the ordering of a regular expression submatch and a string.
1500 * @param __lhs A regular expression submatch.
1501 * @param __rhs A string.
1502 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1504 template<typename _Bi_iter>
1505 inline bool
1506 operator>=(const sub_match<_Bi_iter>& __lhs,
1507 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1508 { return !(__lhs < __rhs); }
1511 * @brief Tests the ordering of a regular expression submatch and a string.
1512 * @param __lhs A regular expression submatch.
1513 * @param __rhs A string.
1514 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1516 template<typename _Bi_iter>
1517 inline bool
1518 operator<=(const sub_match<_Bi_iter>& __lhs,
1519 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1520 { return !(__rhs < __lhs); }
1523 * @brief Tests the equivalence of a string and a regular expression
1524 * submatch.
1525 * @param __lhs A string.
1526 * @param __rhs A regular expression submatch.
1527 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1529 template<typename _Bi_iter>
1530 inline bool
1531 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1532 const sub_match<_Bi_iter>& __rhs)
1534 typedef typename sub_match<_Bi_iter>::string_type string_type;
1535 return __rhs.compare(string_type(1, __lhs)) == 0;
1539 * @brief Tests the inequivalence of a string and a regular expression
1540 * submatch.
1541 * @param __lhs A string.
1542 * @param __rhs A regular expression submatch.
1543 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1545 template<typename _Bi_iter>
1546 inline bool
1547 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1548 const sub_match<_Bi_iter>& __rhs)
1549 { return !(__lhs == __rhs); }
1552 * @brief Tests the ordering of a string and a regular expression submatch.
1553 * @param __lhs A string.
1554 * @param __rhs A regular expression submatch.
1555 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1557 template<typename _Bi_iter>
1558 inline bool
1559 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1560 const sub_match<_Bi_iter>& __rhs)
1562 typedef typename sub_match<_Bi_iter>::string_type string_type;
1563 return __rhs.compare(string_type(1, __lhs)) > 0;
1567 * @brief Tests the ordering of a string and a regular expression submatch.
1568 * @param __lhs A string.
1569 * @param __rhs A regular expression submatch.
1570 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1572 template<typename _Bi_iter>
1573 inline bool
1574 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1575 const sub_match<_Bi_iter>& __rhs)
1576 { return __rhs < __lhs; }
1579 * @brief Tests the ordering of a string and a regular expression submatch.
1580 * @param __lhs A string.
1581 * @param __rhs A regular expression submatch.
1582 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
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); }
1591 * @brief Tests the ordering of a string and a regular expression submatch.
1592 * @param __lhs A string.
1593 * @param __rhs A regular expression submatch.
1594 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1596 template<typename _Bi_iter>
1597 inline bool
1598 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1599 const sub_match<_Bi_iter>& __rhs)
1600 { return !(__rhs < __lhs); }
1603 * @brief Tests the equivalence of a regular expression submatch and a
1604 * string.
1605 * @param __lhs A regular expression submatch.
1606 * @param __rhs A const string reference.
1607 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1609 template<typename _Bi_iter>
1610 inline bool
1611 operator==(const sub_match<_Bi_iter>& __lhs,
1612 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1614 typedef typename sub_match<_Bi_iter>::string_type string_type;
1615 return __lhs.compare(string_type(1, __rhs)) == 0;
1619 * @brief Tests the inequivalence of a regular expression submatch and a
1620 * string.
1621 * @param __lhs A regular expression submatch.
1622 * @param __rhs A const string reference.
1623 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1625 template<typename _Bi_iter>
1626 inline bool
1627 operator!=(const sub_match<_Bi_iter>& __lhs,
1628 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1629 { return !(__lhs == __rhs); }
1632 * @brief Tests the ordering of a regular expression submatch and a string.
1633 * @param __lhs A regular expression submatch.
1634 * @param __rhs A const string reference.
1635 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1637 template<typename _Bi_iter>
1638 inline bool
1639 operator<(const sub_match<_Bi_iter>& __lhs,
1640 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1642 typedef typename sub_match<_Bi_iter>::string_type string_type;
1643 return __lhs.compare(string_type(1, __rhs)) < 0;
1647 * @brief Tests the ordering of a regular expression submatch and a string.
1648 * @param __lhs A regular expression submatch.
1649 * @param __rhs A const string reference.
1650 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1652 template<typename _Bi_iter>
1653 inline bool
1654 operator>(const sub_match<_Bi_iter>& __lhs,
1655 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1656 { return __rhs < __lhs; }
1659 * @brief Tests the ordering of a regular expression submatch and a string.
1660 * @param __lhs A regular expression submatch.
1661 * @param __rhs A const string reference.
1662 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1664 template<typename _Bi_iter>
1665 inline bool
1666 operator>=(const sub_match<_Bi_iter>& __lhs,
1667 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1668 { return !(__lhs < __rhs); }
1671 * @brief Tests the ordering of a regular expression submatch and a string.
1672 * @param __lhs A regular expression submatch.
1673 * @param __rhs A const string reference.
1674 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
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 !(__rhs < __lhs); }
1683 * @brief Inserts a matched string into an output stream.
1685 * @param __os The output stream.
1686 * @param __m A submatch string.
1688 * @returns the output stream with the submatch string inserted.
1690 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1691 inline
1692 basic_ostream<_Ch_type, _Ch_traits>&
1693 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1694 const sub_match<_Bi_iter>& __m)
1695 { return __os << __m.str(); }
1697 // [7.10] Class template match_results
1700 * Special sub_match object representing an unmatched sub-expression.
1702 template<typename _Bi_iter>
1703 inline const sub_match<_Bi_iter>&
1704 __unmatched_sub()
1706 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1707 return __unmatched;
1711 * @brief The results of a match or search operation.
1713 * A collection of character sequences representing the result of a regular
1714 * expression match. Storage for the collection is allocated and freed as
1715 * necessary by the member functions of class template match_results.
1717 * This class satisfies the Sequence requirements, with the exception that
1718 * only the operations defined for a const-qualified Sequence are supported.
1720 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1721 * the whole match. In this case the %sub_match member matched is always true.
1722 * The sub_match object stored at index n denotes what matched the marked
1723 * sub-expression n within the matched expression. If the sub-expression n
1724 * participated in a regular expression match then the %sub_match member
1725 * matched evaluates to true, and members first and second denote the range
1726 * of characters [first, second) which formed that match. Otherwise matched
1727 * is false, and members first and second point to the end of the sequence
1728 * that was searched.
1730 * @nosubgrouping
1732 template<typename _Bi_iter,
1733 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1734 class match_results
1735 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1737 private:
1739 * The vector base is empty if this does not represent a successful match.
1740 * Otherwise it contains n+3 elements where n is the number of marked
1741 * sub-expressions:
1742 * [0] entire match
1743 * [1] 1st marked subexpression
1744 * ...
1745 * [n] nth marked subexpression
1746 * [n+1] prefix
1747 * [n+2] suffix
1749 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1750 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1751 typedef regex_constants::match_flag_type match_flag_type;
1753 public:
1755 * @name 10.? Public Types
1757 //@{
1758 typedef _Alloc allocator_type;
1759 typedef sub_match<_Bi_iter> value_type;
1760 typedef const value_type& const_reference;
1761 typedef const_reference reference;
1762 typedef typename _Base_type::const_iterator const_iterator;
1763 typedef const_iterator iterator;
1764 typedef typename __iter_traits::difference_type difference_type;
1765 typedef typename __iter_traits::value_type char_type;
1766 typedef typename allocator_traits<_Alloc>::size_type size_type;
1769 typedef std::basic_string<char_type> string_type;
1770 //@}
1772 public:
1774 * @name 28.10.1 Construction, Copying, and Destruction
1776 //@{
1779 * @brief Constructs a default %match_results container.
1780 * @post size() returns 0 and str() returns an empty string.
1782 explicit
1783 match_results(const _Alloc& __a = _Alloc())
1784 : _Base_type(__a)
1788 * @brief Copy constructs a %match_results.
1790 match_results(const match_results& __rhs)
1791 : _Base_type(__rhs)
1795 * @brief Move constructs a %match_results.
1797 match_results(match_results&& __rhs) noexcept
1798 : _Base_type(std::move(__rhs))
1802 * @brief Assigns rhs to *this.
1804 match_results&
1805 operator=(const match_results& __rhs)
1807 match_results(__rhs).swap(*this);
1808 return *this;
1812 * @brief Move-assigns rhs to *this.
1814 match_results&
1815 operator=(match_results&& __rhs)
1817 match_results(std::move(__rhs)).swap(*this);
1818 return *this;
1822 * @brief Destroys a %match_results object.
1824 ~match_results()
1827 //@}
1829 // 28.10.2, state:
1831 * @brief Indicates if the %match_results is ready.
1832 * @retval true The object has a fully-established result state.
1833 * @retval false The object is not ready.
1835 bool ready() const { return !_Base_type::empty(); }
1838 * @name 28.10.2 Size
1840 //@{
1843 * @brief Gets the number of matches and submatches.
1845 * The number of matches for a given regular expression will be either 0
1846 * if there was no match or mark_count() + 1 if a match was successful.
1847 * Some matches may be empty.
1849 * @returns the number of matches found.
1851 size_type
1852 size() const
1854 size_type __size = _Base_type::size();
1855 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1858 size_type
1859 max_size() const
1860 { return _Base_type::max_size(); }
1863 * @brief Indicates if the %match_results contains no results.
1864 * @retval true The %match_results object is empty.
1865 * @retval false The %match_results object is not empty.
1867 bool
1868 empty() const
1869 { return size() == 0; }
1871 //@}
1874 * @name 10.3 Element Access
1876 //@{
1879 * @brief Gets the length of the indicated submatch.
1880 * @param __sub indicates the submatch.
1881 * @pre ready() == true
1883 * This function returns the length of the indicated submatch, or the
1884 * length of the entire match if @p __sub is zero (the default).
1886 difference_type
1887 length(size_type __sub = 0) const
1888 { return (*this)[__sub].length(); }
1891 * @brief Gets the offset of the beginning of the indicated submatch.
1892 * @param __sub indicates the submatch.
1893 * @pre ready() == true
1895 * This function returns the offset from the beginning of the target
1896 * sequence to the beginning of the submatch, unless the value of @p __sub
1897 * is zero (the default), in which case this function returns the offset
1898 * from the beginning of the target sequence to the beginning of the
1899 * match.
1901 * Returns -1 if @p __sub is out of range.
1903 difference_type
1904 position(size_type __sub = 0) const
1906 return __sub < size() ? std::distance(this->prefix().first,
1907 (*this)[__sub].first) : -1;
1911 * @brief Gets the match or submatch converted to a string type.
1912 * @param __sub indicates the submatch.
1913 * @pre ready() == true
1915 * This function gets the submatch (or match, if @p __sub is
1916 * zero) extracted from the target range and converted to the
1917 * associated string type.
1919 string_type
1920 str(size_type __sub = 0) const
1921 { return (*this)[__sub].str(); }
1924 * @brief Gets a %sub_match reference for the match or submatch.
1925 * @param __sub indicates the submatch.
1926 * @pre ready() == true
1928 * This function gets a reference to the indicated submatch, or
1929 * the entire match if @p __sub is zero.
1931 * If @p __sub >= size() then this function returns a %sub_match with a
1932 * special value indicating no submatch.
1934 const_reference
1935 operator[](size_type __sub) const
1937 _GLIBCXX_DEBUG_ASSERT( ready() );
1938 return __sub < size()
1939 ? _Base_type::operator[](__sub)
1940 : __unmatched_sub<_Bi_iter>();
1944 * @brief Gets a %sub_match representing the match prefix.
1945 * @pre ready() == true
1947 * This function gets a reference to a %sub_match object representing the
1948 * part of the target range between the start of the target range and the
1949 * start of the match.
1951 const_reference
1952 prefix() const
1954 _GLIBCXX_DEBUG_ASSERT( ready() );
1955 return !empty()
1956 ? _Base_type::operator[](_Base_type::size() - 2)
1957 : __unmatched_sub<_Bi_iter>();
1961 * @brief Gets a %sub_match representing the match suffix.
1962 * @pre ready() == true
1964 * This function gets a reference to a %sub_match object representing the
1965 * part of the target range between the end of the match and the end of
1966 * the target range.
1968 const_reference
1969 suffix() const
1971 _GLIBCXX_DEBUG_ASSERT( ready() );
1972 return !empty()
1973 ? _Base_type::operator[](_Base_type::size() - 1)
1974 : __unmatched_sub<_Bi_iter>();
1978 * @brief Gets an iterator to the start of the %sub_match collection.
1980 const_iterator
1981 begin() const
1982 { return _Base_type::begin(); }
1985 * @brief Gets an iterator to the start of the %sub_match collection.
1987 const_iterator
1988 cbegin() const
1989 { return _Base_type::cbegin(); }
1992 * @brief Gets an iterator to one-past-the-end of the collection.
1994 const_iterator
1995 end() const
1996 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
1999 * @brief Gets an iterator to one-past-the-end of the collection.
2001 const_iterator
2002 cend() const
2003 { return end(); }
2005 //@}
2008 * @name 10.4 Formatting
2010 * These functions perform formatted substitution of the matched
2011 * character sequences into their target. The format specifiers and
2012 * escape sequences accepted by these functions are determined by
2013 * their @p flags parameter as documented above.
2015 //@{
2018 * @pre ready() == true
2019 * @todo Implement this function.
2021 template<typename _Out_iter>
2022 _Out_iter
2023 format(_Out_iter __out, const char_type* __fmt_first,
2024 const char_type* __fmt_last,
2025 match_flag_type __flags = regex_constants::format_default) const
2026 { return __out; }
2029 * @pre ready() == true
2031 template<typename _Out_iter, typename _St, typename _Sa>
2032 _Out_iter
2033 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
2034 match_flag_type __flags = regex_constants::format_default) const
2036 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
2037 __flags);
2041 * @pre ready() == true
2043 template<typename _Out_iter, typename _St, typename _Sa>
2044 basic_string<char_type, _St, _Sa>
2045 format(const basic_string<char_type, _St, _Sa>& __fmt,
2046 match_flag_type __flags = regex_constants::format_default) const
2048 basic_string<char_type, _St, _Sa> __result;
2049 format(std::back_inserter(__result), __fmt, __flags);
2050 return __result;
2054 * @pre ready() == true
2056 string_type
2057 format(const char_type* __fmt,
2058 match_flag_type __flags = regex_constants::format_default) const
2060 string_type __result;
2061 format(std::back_inserter(__result),
2062 __fmt,
2063 __fmt + char_traits<char_type>::length(__fmt),
2064 __flags);
2065 return __result;
2068 //@}
2071 * @name 10.5 Allocator
2073 //@{
2076 * @brief Gets a copy of the allocator.
2078 allocator_type
2079 get_allocator() const
2080 { return _Base_type::get_allocator(); }
2082 //@}
2085 * @name 10.6 Swap
2087 //@{
2090 * @brief Swaps the contents of two match_results.
2092 void
2093 swap(match_results& __that)
2094 { _Base_type::swap(__that); }
2095 //@}
2097 private:
2098 template<typename, typename, typename, typename>
2099 friend class __detail::_Executor;
2101 template<typename, typename, typename, typename>
2102 friend class __detail::_DFSExecutor;
2104 template<typename, typename, typename, typename>
2105 friend class __detail::_BFSExecutor;
2107 template<typename _Bp, typename _Ap, typename _Ch_type, typename _Rx_traits>
2108 friend bool
2109 regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&,
2110 const basic_regex<_Ch_type,
2111 _Rx_traits>&,
2112 regex_constants::match_flag_type);
2114 template<typename _Bp, typename _Ap, typename _Ch_type, typename _Rx_traits>
2115 friend bool
2116 regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&,
2117 const basic_regex<_Ch_type,
2118 _Rx_traits>&,
2119 regex_constants::match_flag_type);
2122 typedef match_results<const char*> cmatch;
2123 typedef match_results<string::const_iterator> smatch;
2124 #ifdef _GLIBCXX_USE_WCHAR_T
2125 typedef match_results<const wchar_t*> wcmatch;
2126 typedef match_results<wstring::const_iterator> wsmatch;
2127 #endif
2129 // match_results comparisons
2131 * @brief Compares two match_results for equality.
2132 * @returns true if the two objects refer to the same match,
2133 * false otherwise.
2135 template<typename _Bi_iter, typename _Alloc>
2136 inline bool
2137 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
2138 const match_results<_Bi_iter, _Alloc>& __m2)
2140 if (__m1.ready() != __m2.ready())
2141 return false;
2142 if (!__m1.ready()) // both are not ready
2143 return true;
2144 if (__m1.empty() != __m2.empty())
2145 return false;
2146 if (__m1.empty()) // both are empty
2147 return true;
2148 return __m1.prefix() == __m2.prefix()
2149 && __m1.size() == __m2.size()
2150 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
2151 && __m1.suffix() == __m2.suffix();
2155 * @brief Compares two match_results for inequality.
2156 * @returns true if the two objects do not refer to the same match,
2157 * false otherwise.
2159 template<typename _Bi_iter, class _Alloc>
2160 inline bool
2161 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
2162 const match_results<_Bi_iter, _Alloc>& __m2)
2163 { return !(__m1 == __m2); }
2165 // [7.10.6] match_results swap
2167 * @brief Swaps two match results.
2168 * @param __lhs A match result.
2169 * @param __rhs A match result.
2171 * The contents of the two match_results objects are swapped.
2173 template<typename _Bi_iter, typename _Alloc>
2174 inline void
2175 swap(match_results<_Bi_iter, _Alloc>& __lhs,
2176 match_results<_Bi_iter, _Alloc>& __rhs)
2177 { __lhs.swap(__rhs); }
2179 // [7.11.2] Function template regex_match
2181 * @name Matching, Searching, and Replacing
2183 //@{
2186 * @brief Determines if there is a match between the regular expression @p e
2187 * and all of the character sequence [first, last).
2189 * @param __s Start of the character sequence to match.
2190 * @param __e One-past-the-end of the character sequence to match.
2191 * @param __m The match results.
2192 * @param __re The regular expression.
2193 * @param __flags Controls how the regular expression is matched.
2195 * @retval true A match exists.
2196 * @retval false Otherwise.
2198 * @throws an exception of type regex_error.
2200 * @todo Implement this function.
2202 template<typename _Bi_iter, typename _Alloc,
2203 typename _Ch_type, typename _Rx_traits>
2204 bool
2205 regex_match(_Bi_iter __s,
2206 _Bi_iter __e,
2207 match_results<_Bi_iter, _Alloc>& __m,
2208 const basic_regex<_Ch_type, _Rx_traits>& __re,
2209 regex_constants::match_flag_type __flags
2210 = regex_constants::match_default)
2212 if (__re._M_automaton == nullptr)
2213 return false;
2214 if (__detail::__get_executor(__s, __e, __m, __re, __flags)->_M_match())
2216 for (auto __it : __m)
2217 if (!__it.matched)
2218 __it.first = __it.second = __e;
2219 __m.at(__m.size()).matched = false;
2220 __m.at(__m.size()).first = __s;
2221 __m.at(__m.size()).second = __s;
2222 __m.at(__m.size()+1).matched = false;
2223 __m.at(__m.size()+1).first = __e;
2224 __m.at(__m.size()+1).second = __e;
2225 return true;
2227 return false;
2231 * @brief Indicates if there is a match between the regular expression @p e
2232 * and all of the character sequence [first, last).
2234 * @param __first Beginning of the character sequence to match.
2235 * @param __last One-past-the-end of the character sequence to match.
2236 * @param __re The regular expression.
2237 * @param __flags Controls how the regular expression is matched.
2239 * @retval true A match exists.
2240 * @retval false Otherwise.
2242 * @throws an exception of type regex_error.
2244 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2245 bool
2246 regex_match(_Bi_iter __first, _Bi_iter __last,
2247 const basic_regex<_Ch_type, _Rx_traits>& __re,
2248 regex_constants::match_flag_type __flags
2249 = regex_constants::match_default)
2251 match_results<_Bi_iter> __what;
2252 return regex_match(__first, __last, __what, __re, __flags);
2256 * @brief Determines if there is a match between the regular expression @p e
2257 * and a C-style null-terminated string.
2259 * @param __s The C-style null-terminated string to match.
2260 * @param __m The match results.
2261 * @param __re The regular expression.
2262 * @param __f Controls how the regular expression is matched.
2264 * @retval true A match exists.
2265 * @retval false Otherwise.
2267 * @throws an exception of type regex_error.
2269 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2270 inline bool
2271 regex_match(const _Ch_type* __s,
2272 match_results<const _Ch_type*, _Alloc>& __m,
2273 const basic_regex<_Ch_type, _Rx_traits>& __re,
2274 regex_constants::match_flag_type __f
2275 = regex_constants::match_default)
2276 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2279 * @brief Determines if there is a match between the regular expression @p e
2280 * and a string.
2282 * @param __s The string to match.
2283 * @param __m The match results.
2284 * @param __re The regular expression.
2285 * @param __flags Controls how the regular expression is matched.
2287 * @retval true A match exists.
2288 * @retval false Otherwise.
2290 * @throws an exception of type regex_error.
2292 template<typename _Ch_traits, typename _Ch_alloc,
2293 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2294 inline bool
2295 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2296 match_results<typename basic_string<_Ch_type,
2297 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2298 const basic_regex<_Ch_type, _Rx_traits>& __re,
2299 regex_constants::match_flag_type __flags
2300 = regex_constants::match_default)
2301 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2304 * @brief Indicates if there is a match between the regular expression @p e
2305 * and a C-style null-terminated string.
2307 * @param __s The C-style null-terminated string to match.
2308 * @param __re The regular expression.
2309 * @param __f Controls how the regular expression is matched.
2311 * @retval true A match exists.
2312 * @retval false Otherwise.
2314 * @throws an exception of type regex_error.
2316 template<typename _Ch_type, class _Rx_traits>
2317 inline bool
2318 regex_match(const _Ch_type* __s,
2319 const basic_regex<_Ch_type, _Rx_traits>& __re,
2320 regex_constants::match_flag_type __f
2321 = regex_constants::match_default)
2322 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2325 * @brief Indicates if there is a match between the regular expression @p e
2326 * and a string.
2328 * @param __s [IN] The string to match.
2329 * @param __re [IN] The regular expression.
2330 * @param __flags [IN] Controls how the regular expression is matched.
2332 * @retval true A match exists.
2333 * @retval false Otherwise.
2335 * @throws an exception of type regex_error.
2337 template<typename _Ch_traits, typename _Str_allocator,
2338 typename _Ch_type, typename _Rx_traits>
2339 inline bool
2340 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2341 const basic_regex<_Ch_type, _Rx_traits>& __re,
2342 regex_constants::match_flag_type __flags
2343 = regex_constants::match_default)
2344 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2346 // [7.11.3] Function template regex_search
2348 * Searches for a regular expression within a range.
2349 * @param __first [IN] The start of the string to search.
2350 * @param __last [IN] One-past-the-end of the string to search.
2351 * @param __m [OUT] The match results.
2352 * @param __re [IN] The regular expression to search for.
2353 * @param __flags [IN] Search policy 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.
2358 * @throws an exception of type regex_error.
2360 * @todo Implement this function.
2362 template<typename _Bi_iter, typename _Alloc,
2363 typename _Ch_type, typename _Rx_traits>
2364 inline bool
2365 regex_search(_Bi_iter __first, _Bi_iter __last,
2366 match_results<_Bi_iter, _Alloc>& __m,
2367 const basic_regex<_Ch_type, _Rx_traits>& __re,
2368 regex_constants::match_flag_type __flags
2369 = regex_constants::match_default)
2371 if (__re._M_automaton == nullptr)
2372 return false;
2373 for (auto __cur = __first; __cur != __last; ++__cur) // Any KMP-like algo?
2374 if (__detail::__get_executor(__cur, __last, __m, __re, __flags)
2375 ->_M_search_from_first())
2377 for (auto __it : __m)
2378 if (!__it.matched)
2379 __it.first = __it.second = __last;
2380 __m.at(__m.size()).first = __first;
2381 __m.at(__m.size()).second = __m[0].first;
2382 __m.at(__m.size()+1).first = __m[0].second;
2383 __m.at(__m.size()+1).second = __last;
2384 __m.at(__m.size()).matched =
2385 (__m.prefix().first != __m.prefix().second);
2386 __m.at(__m.size()+1).matched =
2387 (__m.suffix().first != __m.suffix().second);
2388 return true;
2390 return false;
2395 * Searches for a regular expression within a range.
2396 * @param __first [IN] The start of the string to search.
2397 * @param __last [IN] One-past-the-end of the string to search.
2398 * @param __re [IN] The regular expression to search for.
2399 * @param __flags [IN] Search policy flags.
2400 * @retval true A match was found within the string.
2401 * @retval false No match was found within the string.
2403 * @throws an exception of type regex_error.
2405 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2406 inline bool
2407 regex_search(_Bi_iter __first, _Bi_iter __last,
2408 const basic_regex<_Ch_type, _Rx_traits>& __re,
2409 regex_constants::match_flag_type __flags
2410 = regex_constants::match_default)
2412 match_results<_Bi_iter> __what;
2413 return regex_search(__first, __last, __what, __re, __flags);
2417 * @brief Searches for a regular expression within a C-string.
2418 * @param __s [IN] A C-string to search for the regex.
2419 * @param __m [OUT] The set of regex matches.
2420 * @param __e [IN] The regex to search for in @p s.
2421 * @param __f [IN] The search flags.
2422 * @retval true A match was found within the string.
2423 * @retval false No match was found within the string, the content of %m is
2424 * undefined.
2426 * @throws an exception of type regex_error.
2428 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2429 inline bool
2430 regex_search(const _Ch_type* __s,
2431 match_results<const _Ch_type*, _Alloc>& __m,
2432 const basic_regex<_Ch_type, _Rx_traits>& __e,
2433 regex_constants::match_flag_type __f
2434 = regex_constants::match_default)
2435 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2438 * @brief Searches for a regular expression within a C-string.
2439 * @param __s [IN] The C-string to search.
2440 * @param __e [IN] The regular expression to search for.
2441 * @param __f [IN] Search policy flags.
2442 * @retval true A match was found within the string.
2443 * @retval false No match was found within the string.
2445 * @throws an exception of type regex_error.
2447 template<typename _Ch_type, typename _Rx_traits>
2448 inline bool
2449 regex_search(const _Ch_type* __s,
2450 const basic_regex<_Ch_type, _Rx_traits>& __e,
2451 regex_constants::match_flag_type __f
2452 = regex_constants::match_default)
2453 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2456 * @brief Searches for a regular expression within a string.
2457 * @param __s [IN] The string to search.
2458 * @param __e [IN] The regular expression to search for.
2459 * @param __flags [IN] Search policy flags.
2460 * @retval true A match was found within the string.
2461 * @retval false No match was found within the string.
2463 * @throws an exception of type regex_error.
2465 template<typename _Ch_traits, typename _String_allocator,
2466 typename _Ch_type, typename _Rx_traits>
2467 inline bool
2468 regex_search(const basic_string<_Ch_type, _Ch_traits,
2469 _String_allocator>& __s,
2470 const basic_regex<_Ch_type, _Rx_traits>& __e,
2471 regex_constants::match_flag_type __flags
2472 = regex_constants::match_default)
2473 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2476 * @brief Searches for a regular expression within a string.
2477 * @param __s [IN] A C++ string to search for the regex.
2478 * @param __m [OUT] The set of regex matches.
2479 * @param __e [IN] The regex to search for in @p s.
2480 * @param __f [IN] The search flags.
2481 * @retval true A match was found within the string.
2482 * @retval false No match was found within the string, the content of %m is
2483 * undefined.
2485 * @throws an exception of type regex_error.
2487 template<typename _Ch_traits, typename _Ch_alloc,
2488 typename _Alloc, typename _Ch_type,
2489 typename _Rx_traits>
2490 inline bool
2491 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2492 match_results<typename basic_string<_Ch_type,
2493 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2494 const basic_regex<_Ch_type, _Rx_traits>& __e,
2495 regex_constants::match_flag_type __f
2496 = regex_constants::match_default)
2497 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2499 // std [28.11.4] Function template regex_replace
2501 * @doctodo
2502 * @param __out
2503 * @param __first
2504 * @param __last
2505 * @param __e
2506 * @param __fmt
2507 * @param __flags
2509 * @returns out
2510 * @throws an exception of type regex_error.
2512 * @todo Implement this function.
2514 template<typename _Out_iter, typename _Bi_iter,
2515 typename _Rx_traits, typename _Ch_type>
2516 inline _Out_iter
2517 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2518 const basic_regex<_Ch_type, _Rx_traits>& __e,
2519 const basic_string<_Ch_type>& __fmt,
2520 regex_constants::match_flag_type __flags
2521 = regex_constants::match_default)
2522 { return __out; }
2525 * @doctodo
2526 * @param __s
2527 * @param __e
2528 * @param __fmt
2529 * @param __flags
2531 * @returns a copy of string @p s with replacements.
2533 * @throws an exception of type regex_error.
2535 template<typename _Rx_traits, typename _Ch_type>
2536 inline basic_string<_Ch_type>
2537 regex_replace(const basic_string<_Ch_type>& __s,
2538 const basic_regex<_Ch_type, _Rx_traits>& __e,
2539 const basic_string<_Ch_type>& __fmt,
2540 regex_constants::match_flag_type __flags
2541 = regex_constants::match_default)
2543 basic_string<_Ch_type> __result;
2544 regex_replace(std::back_inserter(__result),
2545 __s.begin(), __s.end(), __e, __fmt, __flags);
2546 return __result;
2549 //@}
2551 // std [28.12] Class template regex_iterator
2553 * An iterator adaptor that will provide repeated calls of regex_search over
2554 * a range until no more matches remain.
2556 template<typename _Bi_iter,
2557 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2558 typename _Rx_traits = regex_traits<_Ch_type> >
2559 class regex_iterator
2561 public:
2562 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2563 typedef match_results<_Bi_iter> value_type;
2564 typedef std::ptrdiff_t difference_type;
2565 typedef const value_type* pointer;
2566 typedef const value_type& reference;
2567 typedef std::forward_iterator_tag iterator_category;
2570 * @brief Provides a singular iterator, useful for indicating
2571 * one-past-the-end of a range.
2573 regex_iterator()
2574 : _M_match()
2578 * Constructs a %regex_iterator...
2579 * @param __a [IN] The start of a text range to search.
2580 * @param __b [IN] One-past-the-end of the text range to search.
2581 * @param __re [IN] The regular expression to match.
2582 * @param __m [IN] Policy flags for match rules.
2584 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2585 regex_constants::match_flag_type __m
2586 = regex_constants::match_default)
2587 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2588 { regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags); }
2591 * Copy constructs a %regex_iterator.
2593 regex_iterator(const regex_iterator& __rhs) = default;
2596 * @brief Assigns one %regex_iterator to another.
2598 regex_iterator&
2599 operator=(const regex_iterator& __rhs) = default;
2602 * @brief Tests the equivalence of two regex iterators.
2604 bool
2605 operator==(const regex_iterator& __rhs) const;
2608 * @brief Tests the inequivalence of two regex iterators.
2610 bool
2611 operator!=(const regex_iterator& __rhs) const
2612 { return !(*this == __rhs); }
2615 * @brief Dereferences a %regex_iterator.
2617 const value_type&
2618 operator*() const
2619 { return _M_match; }
2622 * @brief Selects a %regex_iterator member.
2624 const value_type*
2625 operator->() const
2626 { return &_M_match; }
2629 * @brief Increments a %regex_iterator.
2631 regex_iterator&
2632 operator++();
2635 * @brief Postincrements a %regex_iterator.
2637 regex_iterator
2638 operator++(int)
2640 auto __tmp = *this;
2641 ++(*this);
2642 return __tmp;
2645 private:
2646 _Bi_iter _M_begin;
2647 _Bi_iter _M_end;
2648 const regex_type* _M_pregex;
2649 regex_constants::match_flag_type _M_flags;
2650 match_results<_Bi_iter> _M_match;
2653 template<typename _Bi_iter,
2654 typename _Ch_type,
2655 typename _Rx_traits>
2656 bool
2657 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2658 operator==(const regex_iterator& __rhs) const
2660 return (_M_match.empty() && __rhs._M_match.empty())
2661 || (_M_begin == __rhs._M_begin
2662 && _M_end == __rhs._M_end
2663 && _M_pregex == __rhs._M_pregex
2664 && _M_flags == __rhs._M_flags
2665 && _M_match[0] == __rhs._M_match[0]);
2668 template<typename _Bi_iter,
2669 typename _Ch_type,
2670 typename _Rx_traits>
2671 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
2672 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2673 operator++()
2675 // FIXME: In all cases in which the call to regex_search returns true,
2676 // match.prefix().first shall be equal to the previous value of
2677 // match[0].second, and for each index i in the half-open range
2678 // [0, match.size()) for which match[i].matched is true,
2679 // match[i].position() shall return distance(begin, match[i].first).
2680 // [28.12.1.4.5]
2681 if (_M_match[0].matched)
2683 auto __start = _M_match[0].second;
2684 if (_M_match[0].first == _M_match[0].second)
2685 if (__start == _M_end)
2687 _M_match = value_type();
2688 return *this;
2690 else
2692 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags
2693 | regex_constants::match_not_null
2694 | regex_constants::match_continuous))
2695 return *this;
2696 else
2697 ++__start;
2699 _M_flags |= regex_constants::match_prev_avail;
2700 if (!regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags))
2701 _M_match = value_type();
2703 return *this;
2706 typedef regex_iterator<const char*> cregex_iterator;
2707 typedef regex_iterator<string::const_iterator> sregex_iterator;
2708 #ifdef _GLIBCXX_USE_WCHAR_T
2709 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2710 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2711 #endif
2713 // [7.12.2] Class template regex_token_iterator
2715 * Iterates over submatches in a range (or @a splits a text string).
2717 * The purpose of this iterator is to enumerate all, or all specified,
2718 * matches of a regular expression within a text range. The dereferenced
2719 * value of an iterator of this class is a std::sub_match object.
2721 template<typename _Bi_iter,
2722 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2723 typename _Rx_traits = regex_traits<_Ch_type> >
2724 class regex_token_iterator
2726 public:
2727 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2728 typedef sub_match<_Bi_iter> value_type;
2729 typedef std::ptrdiff_t difference_type;
2730 typedef const value_type* pointer;
2731 typedef const value_type& reference;
2732 typedef std::forward_iterator_tag iterator_category;
2734 public:
2736 * @brief Default constructs a %regex_token_iterator.
2738 * A default-constructed %regex_token_iterator is a singular iterator
2739 * that will compare equal to the one-past-the-end value for any
2740 * iterator of the same type.
2742 regex_token_iterator()
2743 : _M_position(), _M_result(nullptr), _M_suffix(), _M_n(0), _M_subs()
2747 * Constructs a %regex_token_iterator...
2748 * @param __a [IN] The start of the text to search.
2749 * @param __b [IN] One-past-the-end of the text to search.
2750 * @param __re [IN] The regular expression to search for.
2751 * @param __submatch [IN] Which submatch to return. There are some
2752 * special values for this parameter:
2753 * - -1 each enumerated subexpression does NOT
2754 * match the regular expression (aka field
2755 * splitting)
2756 * - 0 the entire string matching the
2757 * subexpression is returned for each match
2758 * within the text.
2759 * - >0 enumerates only the indicated
2760 * subexpression from a match within the text.
2761 * @param __m [IN] Policy flags for match rules.
2763 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2764 int __submatch = 0,
2765 regex_constants::match_flag_type __m
2766 = regex_constants::match_default)
2767 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2768 { _M_init(__a, __b); }
2771 * Constructs a %regex_token_iterator...
2772 * @param __a [IN] The start of the text to search.
2773 * @param __b [IN] One-past-the-end of the text to search.
2774 * @param __re [IN] The regular expression to search for.
2775 * @param __submatches [IN] A list of subexpressions to return for each
2776 * regular expression match within the text.
2777 * @param __m [IN] Policy flags for match rules.
2779 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2780 const regex_type& __re,
2781 const std::vector<int>& __submatches,
2782 regex_constants::match_flag_type __m
2783 = regex_constants::match_default)
2784 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2785 { _M_init(__a, __b); }
2788 * Constructs a %regex_token_iterator...
2789 * @param __a [IN] The start of the text to search.
2790 * @param __b [IN] One-past-the-end of the text to search.
2791 * @param __re [IN] The regular expression to search for.
2792 * @param __submatches [IN] A list of subexpressions to return for each
2793 * regular expression match within the text.
2794 * @param __m [IN] Policy flags for match rules.
2796 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2797 const regex_type& __re,
2798 initializer_list<int> __submatches,
2799 regex_constants::match_flag_type __m
2800 = regex_constants::match_default)
2801 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2802 { _M_init(__a, __b); }
2805 * Constructs a %regex_token_iterator...
2806 * @param __a [IN] The start of the text to search.
2807 * @param __b [IN] One-past-the-end of the text to search.
2808 * @param __re [IN] The regular expression to search for.
2809 * @param __submatches [IN] A list of subexpressions to return for each
2810 * regular expression match within the text.
2811 * @param __m [IN] Policy flags for match rules.
2813 template<std::size_t _Nm>
2814 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2815 const regex_type& __re,
2816 const int (&__submatches)[_Nm],
2817 regex_constants::match_flag_type __m
2818 = regex_constants::match_default)
2819 : _M_position(__a, __b, __re, __m),
2820 _M_subs(__submatches, *(&__submatches+1)), _M_n(0)
2821 { _M_init(__a, __b); }
2824 * @brief Copy constructs a %regex_token_iterator.
2825 * @param __rhs [IN] A %regex_token_iterator to copy.
2827 regex_token_iterator(const regex_token_iterator& __rhs)
2828 : _M_position(__rhs.position), _M_subs(__rhs.subs), _M_n(__rhs.N),
2829 _M_result(__rhs.result), _M_suffix(__rhs.suffix),
2830 _M_has_m1(__rhs._M_has_m1)
2832 if (__rhs._M_result == &__rhs._M_suffix)
2833 _M_result = &_M_suffix;
2837 * @brief Assigns a %regex_token_iterator to another.
2838 * @param __rhs [IN] A %regex_token_iterator to copy.
2840 regex_token_iterator&
2841 operator=(const regex_token_iterator& __rhs);
2844 * @brief Compares a %regex_token_iterator to another for equality.
2846 bool
2847 operator==(const regex_token_iterator& __rhs) const;
2850 * @brief Compares a %regex_token_iterator to another for inequality.
2852 bool
2853 operator!=(const regex_token_iterator& __rhs) const
2854 { return !(*this == __rhs); }
2857 * @brief Dereferences a %regex_token_iterator.
2859 const value_type&
2860 operator*() const
2861 { return *_M_result; }
2864 * @brief Selects a %regex_token_iterator member.
2866 const value_type*
2867 operator->() const
2868 { return _M_result; }
2871 * @brief Increments a %regex_token_iterator.
2873 regex_token_iterator&
2874 operator++();
2877 * @brief Postincrements a %regex_token_iterator.
2879 regex_token_iterator
2880 operator++(int)
2882 auto __tmp = *this;
2883 ++(*this);
2884 return __tmp;
2887 private:
2888 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2890 void
2891 _M_init(_Bi_iter __a, _Bi_iter __b);
2893 const value_type&
2894 _M_current_match() const
2896 if (_M_subs[_M_n] == -1)
2897 return (*_M_position).prefix();
2898 else
2899 return (*_M_position)[_M_subs[_M_n]];
2902 bool
2903 _M_end_of_seq() const
2904 { return _M_result != nullptr; }
2906 _Position _M_position;
2907 const value_type* _M_result;
2908 value_type _M_suffix;
2909 std::size_t _M_n;
2910 std::vector<int> _M_subs;
2912 // Show whether _M_subs contains -1
2913 bool _M_has_m1;
2916 template<typename _Bi_iter,
2917 typename _Ch_type,
2918 typename _Rx_traits>
2919 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
2920 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2921 operator=(const regex_token_iterator& __rhs)
2923 _M_position = __rhs._M_position;
2924 _M_subs = __rhs._M_subs;
2925 _M_n = __rhs._M_n;
2926 _M_result = __rhs._M_result;
2927 _M_suffix = __rhs._M_suffix;
2928 _M_has_m1 = __rhs._M_has_m1;
2929 if (__rhs._M_result == &__rhs._M_suffix)
2930 _M_result = &_M_suffix;
2933 template<typename _Bi_iter,
2934 typename _Ch_type,
2935 typename _Rx_traits>
2936 bool
2937 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2938 operator==(const regex_token_iterator& __rhs) const
2940 if (_M_end_of_seq() && __rhs._M_end_of_seq())
2941 return true;
2942 if (_M_suffix.matched && __rhs._M_suffix.matched
2943 && _M_suffix == __rhs._M_suffix)
2944 return true;
2945 if (_M_end_of_seq() || _M_suffix.matched
2946 || __rhs._M_end_of_seq() || __rhs._M_suffix.matched)
2947 return false;
2948 return _M_position == __rhs._M_position
2949 && _M_n == __rhs._M_n
2950 && _M_subs == __rhs._M_subs;
2953 template<typename _Bi_iter,
2954 typename _Ch_type,
2955 typename _Rx_traits>
2956 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
2957 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2958 operator++()
2960 _Position __prev = _M_position;
2961 if (_M_suffix.matched)
2962 *this = regex_token_iterator();
2963 else if (_M_n + 1 < _M_subs.size())
2965 _M_n++;
2966 _M_result = &_M_current_match();
2968 else
2970 _M_n = 0;
2971 ++_M_position;
2972 if (_M_position != _Position())
2973 _M_result = &_M_current_match();
2974 else if (_M_has_m1 && __prev->suffix().length() != 0)
2976 _M_suffix.matched = true;
2977 _M_suffix.first = __prev->suffix().first;
2978 _M_suffix.second = __prev->suffix().second;
2979 _M_result = &_M_suffix;
2981 else
2982 *this = regex_token_iterator();
2984 return *this;
2987 template<typename _Bi_iter,
2988 typename _Ch_type,
2989 typename _Rx_traits>
2990 void
2991 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2992 _M_init(_Bi_iter __a, _Bi_iter __b)
2994 _M_has_m1 = false;
2995 for (auto __it : _M_subs)
2996 if (__it == -1)
2998 _M_has_m1 = true;
2999 break;
3001 if (_M_position != _Position())
3002 _M_result = &_M_current_match();
3003 else if (_M_has_m1)
3005 _M_suffix.matched = true;
3006 _M_suffix.first = __a;
3007 _M_suffix.second = __b;
3008 _M_result = &_M_suffix;
3010 else
3011 _M_result = nullptr;
3014 /** @brief Token iterator for C-style NULL-terminated strings. */
3015 typedef regex_token_iterator<const char*> cregex_token_iterator;
3017 /** @brief Token iterator for standard strings. */
3018 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
3020 #ifdef _GLIBCXX_USE_WCHAR_T
3021 /** @brief Token iterator for C-style NULL-terminated wide strings. */
3022 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
3024 /** @brief Token iterator for standard wide-character strings. */
3025 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
3026 #endif
3028 //@} // group regex
3029 _GLIBCXX_END_NAMESPACE_VERSION
3030 } // namespace