regex.h: Add friend classes.
[gcc.git] / libstdc++-v3 / include / bits / regex.h
1 // class template regex -*- C++ -*-
2
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.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /**
26 * @file bits/regex.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
29 */
30
31 namespace std _GLIBCXX_VISIBILITY(default)
32 {
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
34
35 /**
36 * @addtogroup regex
37 * @{
38 */
39
40 /**
41 * @brief Class regex_traits. Describes aspects of a regular expression.
42 *
43 * A regular expression traits class that satisfies the requirements of
44 * section [28.7].
45 *
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.
49 */
50 template<typename _Ch_type>
51 struct regex_traits
52 {
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
59 {
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;
68
69 constexpr _RegexMask(_BaseType __base = 0,
70 unsigned char __extended = 0)
71 : _M_base(__base), _M_extended(__extended)
72 { }
73
74 constexpr _RegexMask
75 operator&(_RegexMask __other) const
76 {
77 return _RegexMask(_M_base & __other._M_base,
78 _M_extended & __other._M_extended);
79 }
80
81 constexpr _RegexMask
82 operator|(_RegexMask __other) const
83 {
84 return _RegexMask(_M_base | __other._M_base,
85 _M_extended | __other._M_extended);
86 }
87
88 constexpr _RegexMask
89 operator^(_RegexMask __other) const
90 {
91 return _RegexMask(_M_base ^ __other._M_base,
92 _M_extended ^ __other._M_extended);
93 }
94
95 constexpr _RegexMask
96 operator~() const
97 { return _RegexMask(~_M_base, ~_M_extended); }
98
99 _RegexMask&
100 operator&=(_RegexMask __other)
101 { return *this = (*this) & __other; }
102
103 _RegexMask&
104 operator|=(_RegexMask __other)
105 { return *this = (*this) | __other; }
106
107 _RegexMask&
108 operator^=(_RegexMask __other)
109 { return *this = (*this) ^ __other; }
110
111 constexpr bool
112 operator==(_RegexMask __other) const
113 {
114 return (_M_extended & _S_valid_mask)
115 == (__other._M_extended & _S_valid_mask)
116 && _M_base == __other._M_base;
117 }
118
119 constexpr bool
120 operator!=(_RegexMask __other) const
121 { return !((*this) == __other); }
122
123 };
124 public:
125 typedef _RegexMask char_class_type;
126
127 public:
128 /**
129 * @brief Constructs a default traits object.
130 */
131 regex_traits() { }
132
133 /**
134 * @brief Gives the length of a C-style string starting at @p __p.
135 *
136 * @param __p a pointer to the start of a character sequence.
137 *
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.
142 */
143 static std::size_t
144 length(const char_type* __p)
145 { return string_type::traits_type::length(__p); }
146
147 /**
148 * @brief Performs the identity translation.
149 *
150 * @param __c A character to the locale-specific character set.
151 *
152 * @returns __c.
153 */
154 char_type
155 translate(char_type __c) const
156 { return __c; }
157
158 /**
159 * @brief Translates a character into a case-insensitive equivalent.
160 *
161 * @param __c A character to the locale-specific character set.
162 *
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.
166 */
167 char_type
168 translate_nocase(char_type __c) const
169 {
170 typedef std::ctype<char_type> __ctype_type;
171 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
172 return __fctyp.tolower(__c);
173 }
174
175 /**
176 * @brief Gets a sort key for a character sequence.
177 *
178 * @param __first beginning of the character sequence.
179 * @param __last one-past-the-end of the character sequence.
180 *
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).
185 *
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.
189 *
190 * @returns a locale-specific sort key equivalent to the input range.
191 *
192 * @throws std::bad_cast if the current locale does not have a collate
193 * facet.
194 */
195 template<typename _Fwd_iter>
196 string_type
197 transform(_Fwd_iter __first, _Fwd_iter __last) const
198 {
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());
203 }
204
205 /**
206 * @brief Gets a sort key for a character sequence, independent of case.
207 *
208 * @param __first beginning of the character sequence.
209 * @param __last one-past-the-end of the character sequence.
210 *
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.
216 *
217 * @todo Implement this function.
218 */
219 template<typename _Fwd_iter>
220 string_type
221 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
222 {
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());
229 }
230
231 /**
232 * @brief Gets a collation element by name.
233 *
234 * @param __first beginning of the collation element name.
235 * @param __last one-past-the-end of the collation element name.
236 *
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.
241 */
242 template<typename _Fwd_iter>
243 string_type
244 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
245
246 /**
247 * @brief Maps one or more characters to a named character
248 * classification.
249 *
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.
253 *
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.
264 *
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
282 */
283 template<typename _Fwd_iter>
284 char_class_type
285 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
286 bool __icase = false) const;
287
288 /**
289 * @brief Determines if @p c is a member of an identified class.
290 *
291 * @param __c a character.
292 * @param __f a class type (as returned from lookup_classname).
293 *
294 * @returns true if the character @p __c is a member of the classification
295 * represented by @p __f, false otherwise.
296 *
297 * @throws std::bad_cast if the current locale does not have a ctype
298 * facet.
299 */
300 bool
301 isctype(_Ch_type __c, char_class_type __f) const;
302
303 /**
304 * @brief Converts a digit to an int.
305 *
306 * @param __ch a character representing a digit.
307 * @param __radix the radix if the numeric conversion (limited to 8, 10,
308 * or 16).
309 *
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.
312 */
313 int
314 value(_Ch_type __ch, int __radix) const;
315
316 /**
317 * @brief Imbues the regex_traits object with a copy of a new locale.
318 *
319 * @param __loc A locale.
320 *
321 * @returns a copy of the previous locale in use by the regex_traits
322 * object.
323 *
324 * @note Calling imbue with a different locale than the one currently in
325 * use invalidates all cached data held by *this.
326 */
327 locale_type
328 imbue(locale_type __loc)
329 {
330 std::swap(_M_locale, __loc);
331 return __loc;
332 }
333
334 /**
335 * @brief Gets a copy of the current locale in use by the regex_traits
336 * object.
337 */
338 locale_type
339 getloc() const
340 { return _M_locale; }
341
342 protected:
343 locale_type _M_locale;
344 };
345
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
351 {
352 typedef std::ctype<char_type> __ctype_type;
353 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
354
355 static const char* __collatenames[] =
356 {
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",
485 ""
486 };
487
488 // same as boost
489 static const char* __digraphs[] =
490 {
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",
512 ""
513 };
514
515 std::string __s(__last - __first, '?');
516 __fctyp.narrow(__first, __last, '?', &*__s.begin());
517
518 for (unsigned int __i = 0; *__collatenames[__i]; __i++)
519 if (__s == __collatenames[__i])
520 return string_type(1, __fctyp.widen((char)__i));
521
522 for (unsigned int __i = 0; *__digraphs[__i]; __i++)
523 {
524 const char* __now = __digraphs[__i];
525 if (__s == __now)
526 {
527 string_type ret(__s.size(), __fctyp.widen('?'));
528 __fctyp.widen(__now, __now + 2/* ouch */, &*ret.begin());
529 return ret;
530 }
531 }
532 return string_type();
533 }
534
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
540 {
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));
546
547 static _ClassnameEntry __classnames[] =
548 {
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},
564 };
565
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)
572 {
573 if (__s == __it->first)
574 {
575 if (__icase
576 && ((__it->second
577 & (ctype_base::lower | ctype_base::upper)) != 0))
578 return ctype_base::alpha;
579 return __it->second;
580 }
581 }
582 return 0;
583 }
584
585 template<typename _Ch_type>
586 bool
587 regex_traits<_Ch_type>::
588 isctype(_Ch_type __c, char_class_type __f) const
589 {
590 typedef std::ctype<char_type> __ctype_type;
591 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
592
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')));
601 }
602
603 template<typename _Ch_type>
604 int
605 regex_traits<_Ch_type>::
606 value(_Ch_type __ch, int __radix) const
607 {
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;
616 }
617
618 // [7.8] Class basic_regex
619 /**
620 * Objects of specializations of this class represent regular expressions
621 * constructed from sequences of character type @p _Ch_type.
622 *
623 * Storage for the regular expression is allocated and deallocated as
624 * necessary by the member functions of this class.
625 */
626 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type> >
627 class basic_regex
628 {
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;
636
637 /**
638 * @name Constants
639 * std [28.8.1](1)
640 */
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 //@}
653
654 // [7.8.2] construct/copy/destroy
655 /**
656 * Constructs a basic regular expression that does not match any
657 * character sequence.
658 */
659 basic_regex()
660 : _M_flags(ECMAScript), _M_automaton(nullptr)
661 { }
662
663 /**
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.
667 *
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.
671 *
672 * @throws regex_error if @p __p is not a valid regular expression.
673 */
674 explicit
675 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
676 : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
677 { }
678
679 /**
680 * @brief Constructs a basic regular expression from the sequence
681 * [p, p + len) interpreted according to the flags in @p f.
682 *
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.
688 *
689 * @throws regex_error if @p __p is not a valid regular expression.
690 */
691 basic_regex(const _Ch_type* __p,
692 std::size_t __len, flag_type __f = ECMAScript)
693 : basic_regex(__p, __p + __len, __f)
694 { }
695
696 /**
697 * @brief Copy-constructs a basic regular expression.
698 *
699 * @param __rhs A @p regex object.
700 */
701 basic_regex(const basic_regex& __rhs) = default;
702
703 /**
704 * @brief Move-constructs a basic regular expression.
705 *
706 * @param __rhs A @p regex object.
707 */
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))
711 { }
712
713 /**
714 * @brief Constructs a basic regular expression from the string
715 * @p s interpreted according to the flags in @p f.
716 *
717 * @param __s A string containing a regular expression.
718 * @param __f Flags indicating the syntax rules and options.
719 *
720 * @throws regex_error if @p __s is not a valid regular expression.
721 */
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)
728 { }
729
730 /**
731 * @brief Constructs a basic regular expression from the range
732 * [first, last) interpreted according to the flags in @p f.
733 *
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.
739 *
740 * @throws regex_error if @p [__first, __last) is not a valid regular
741 * expression.
742 */
743 template<typename _FwdIter>
744 basic_regex(_FwdIter __first, _FwdIter __last,
745 flag_type __f = ECMAScript)
746 : _M_flags(__f),
747 _M_automaton(__detail::_Compiler<_FwdIter, _Ch_type, _Rx_traits>
748 (__first, __last, _M_traits, _M_flags)._M_get_nfa())
749 { }
750
751 /**
752 * @brief Constructs a basic regular expression from an initializer list.
753 *
754 * @param __l The initializer list.
755 * @param __f The format flags of the regular expression.
756 *
757 * @throws regex_error if @p __l is not a valid regular expression.
758 */
759 basic_regex(initializer_list<_Ch_type> __l,
760 flag_type __f = ECMAScript)
761 : basic_regex(__l.begin(), __l.end(), __f)
762 { }
763
764 /**
765 * @brief Destroys a basic regular expression.
766 */
767 ~basic_regex()
768 { }
769
770 /**
771 * @brief Assigns one regular expression to another.
772 */
773 basic_regex&
774 operator=(const basic_regex& __rhs)
775 { return this->assign(__rhs); }
776
777 /**
778 * @brief Move-assigns one regular expression to another.
779 */
780 basic_regex&
781 operator=(basic_regex&& __rhs) noexcept
782 { return this->assign(std::move(__rhs)); }
783
784 /**
785 * @brief Replaces a regular expression with a new one constructed from
786 * a C-style null-terminated string.
787 *
788 * @param __p A pointer to the start of a null-terminated C-style string
789 * containing a regular expression.
790 */
791 basic_regex&
792 operator=(const _Ch_type* __p)
793 { return this->assign(__p, flags()); }
794
795 /**
796 * @brief Replaces a regular expression with a new one constructed from
797 * a string.
798 *
799 * @param __s A pointer to a string containing a regular expression.
800 */
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()); }
805
806 // [7.8.3] assign
807 /**
808 * @brief the real assignment operator.
809 *
810 * @param __rhs Another regular expression object.
811 */
812 basic_regex&
813 assign(const basic_regex& __rhs)
814 {
815 basic_regex __tmp(__rhs);
816 this->swap(__tmp);
817 return *this;
818 }
819
820 /**
821 * @brief The move-assignment operator.
822 *
823 * @param __rhs Another regular expression object.
824 */
825 basic_regex&
826 assign(basic_regex&& __rhs) noexcept
827 {
828 basic_regex __tmp(std::move(__rhs));
829 this->swap(__tmp);
830 return *this;
831 }
832
833 /**
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.
837 *
838 * @param __p A pointer to a C-style null-terminated string containing
839 * a regular expression pattern.
840 * @param __flags Syntax option flags.
841 *
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.
845 */
846 basic_regex&
847 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
848 { return this->assign(string_type(__p), __flags); }
849
850 /**
851 * @brief Assigns a new regular expression to a regex object from a
852 * C-style string containing a regular expression pattern.
853 *
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.
858 *
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.
862 */
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); }
866
867 /**
868 * @brief Assigns a new regular expression to a regex object from a
869 * string containing a regular expression pattern.
870 *
871 * @param __s A string containing a regular expression pattern.
872 * @param __flags Syntax option flags.
873 *
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.
877 */
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)
882 {
883 _M_flags = __flags;
884 _M_automaton =
885 __detail::_Compiler<decltype(__s.begin()), _Ch_type, _Rx_traits>
886 (__s.begin(), __s.end(), _M_traits, _M_flags)._M_get_nfa();
887 return *this;
888 }
889
890 /**
891 * @brief Assigns a new regular expression to a regex object.
892 *
893 * @param __first The start of a range containing a valid regular
894 * expression.
895 * @param __last The end of a range containing a valid regular
896 * expression.
897 * @param __flags Syntax option flags.
898 *
899 * @throws regex_error if p does not contain a valid regular
900 * expression pattern interpreted according to @p __flags. If
901 * regex_error is thrown, the object remains unchanged.
902 */
903 template<typename _InputIterator>
904 basic_regex&
905 assign(_InputIterator __first, _InputIterator __last,
906 flag_type __flags = ECMAScript)
907 { return this->assign(string_type(__first, __last), __flags); }
908
909 /**
910 * @brief Assigns a new regular expression to a regex object.
911 *
912 * @param __l An initializer list representing a regular expression.
913 * @param __flags Syntax option flags.
914 *
915 * @throws regex_error if @p __l does not contain a valid
916 * regular expression pattern interpreted according to @p
917 * __flags. If regex_error is thrown, the object remains
918 * unchanged.
919 */
920 basic_regex&
921 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
922 { return this->assign(__l.begin(), __l.end(), __flags); }
923
924 // [7.8.4] const operations
925 /**
926 * @brief Gets the number of marked subexpressions within the regular
927 * expression.
928 */
929 unsigned int
930 mark_count() const
931 { return _M_automaton->_M_sub_count() - 1; }
932
933 /**
934 * @brief Gets the flags used to construct the regular expression
935 * or in the last call to assign().
936 */
937 flag_type
938 flags() const
939 { return _M_flags; }
940
941 // [7.8.5] locale
942 /**
943 * @brief Imbues the regular expression object with the given locale.
944 *
945 * @param __loc A locale.
946 */
947 locale_type
948 imbue(locale_type __loc)
949 { return _M_traits.imbue(__loc); }
950
951 /**
952 * @brief Gets the locale currently imbued in the regular expression
953 * object.
954 */
955 locale_type
956 getloc() const
957 { return _M_traits.getloc(); }
958
959 // [7.8.6] swap
960 /**
961 * @brief Swaps the contents of two regular expression objects.
962 *
963 * @param __rhs Another regular expression object.
964 */
965 void
966 swap(basic_regex& __rhs)
967 {
968 std::swap(_M_flags, __rhs._M_flags);
969 std::swap(_M_traits, __rhs._M_traits);
970 std::swap(_M_automaton, __rhs._M_automaton);
971 }
972
973 #ifdef _GLIBCXX_DEBUG
974 void
975 _M_dot(std::ostream& __ostr)
976 { _M_automaton->_M_dot(__ostr); }
977 #endif
978
979 protected:
980 typedef std::shared_ptr<__detail::_Automaton<_Ch_type, _Rx_traits>>
981 _AutomatonPtr;
982
983 template<typename _BiIter, typename _Alloc,
984 typename _CharT, typename _TraitsT>
985 friend std::unique_ptr<
986 __detail::_Executor<_BiIter, _Alloc, _CharT, _TraitsT>>
987 __detail::__get_executor(_BiIter,
988 _BiIter,
989 match_results<_BiIter, _Alloc>&,
990 const basic_regex<_CharT, _TraitsT>&,
991 regex_constants::match_flag_type);
992
993 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
994 friend bool
995 regex_match(_Bp, _Bp,
996 match_results<_Bp, _Ap>&,
997 const basic_regex<_Cp, _Rp>&,
998 regex_constants::match_flag_type);
999
1000 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp>
1001 friend bool
1002 regex_search(_Bp, _Bp,
1003 match_results<_Bp, _Ap>&,
1004 const basic_regex<_Cp, _Rp>&,
1005 regex_constants::match_flag_type);
1006
1007 template<typename, typename, typename, typename>
1008 friend class __detail::_Executor;
1009
1010 template<typename, typename, typename, typename>
1011 friend class __detail::_DFSExecutor;
1012
1013 template<typename, typename, typename, typename>
1014 friend class __detail::_BFSExecutor;
1015
1016 flag_type _M_flags;
1017 _Rx_traits _M_traits;
1018 _AutomatonPtr _M_automaton;
1019 };
1020
1021 /** @brief Standard regular expressions. */
1022 typedef basic_regex<char> regex;
1023
1024 #ifdef _GLIBCXX_USE_WCHAR_T
1025 /** @brief Standard wide-character regular expressions. */
1026 typedef basic_regex<wchar_t> wregex;
1027 #endif
1028
1029
1030 // [7.8.6] basic_regex swap
1031 /**
1032 * @brief Swaps the contents of two regular expression objects.
1033 * @param __lhs First regular expression.
1034 * @param __rhs Second regular expression.
1035 */
1036 template<typename _Ch_type, typename _Rx_traits>
1037 inline void
1038 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
1039 basic_regex<_Ch_type, _Rx_traits>& __rhs)
1040 { __lhs.swap(__rhs); }
1041
1042
1043 // [7.9] Class template sub_match
1044 /**
1045 * A sequence of characters matched by a particular marked sub-expression.
1046 *
1047 * An object of this class is essentially a pair of iterators marking a
1048 * matched subexpression within a regular expression pattern match. Such
1049 * objects can be converted to and compared with std::basic_string objects
1050 * of a similar base character type as the pattern matched by the regular
1051 * expression.
1052 *
1053 * The iterators that make up the pair are the usual half-open interval
1054 * referencing the actual original pattern matched.
1055 */
1056 template<typename _BiIter>
1057 class sub_match : public std::pair<_BiIter, _BiIter>
1058 {
1059 typedef iterator_traits<_BiIter> __iter_traits;
1060
1061 public:
1062 typedef typename __iter_traits::value_type value_type;
1063 typedef typename __iter_traits::difference_type difference_type;
1064 typedef _BiIter iterator;
1065 typedef std::basic_string<value_type> string_type;
1066
1067 bool matched;
1068
1069 constexpr sub_match() : matched() { }
1070
1071 /**
1072 * Gets the length of the matching sequence.
1073 */
1074 difference_type
1075 length() const
1076 { return this->matched ? std::distance(this->first, this->second) : 0; }
1077
1078 /**
1079 * @brief Gets the matching sequence as a string.
1080 *
1081 * @returns the matching sequence as a string.
1082 *
1083 * This is the implicit conversion operator. It is identical to the
1084 * str() member function except that it will want to pop up in
1085 * unexpected places and cause a great deal of confusion and cursing
1086 * from the unwary.
1087 */
1088 operator string_type() const
1089 {
1090 return this->matched
1091 ? string_type(this->first, this->second)
1092 : string_type();
1093 }
1094
1095 /**
1096 * @brief Gets the matching sequence as a string.
1097 *
1098 * @returns the matching sequence as a string.
1099 */
1100 string_type
1101 str() const
1102 {
1103 return this->matched
1104 ? string_type(this->first, this->second)
1105 : string_type();
1106 }
1107
1108 /**
1109 * @brief Compares this and another matched sequence.
1110 *
1111 * @param __s Another matched sequence to compare to this one.
1112 *
1113 * @retval <0 this matched sequence will collate before @p __s.
1114 * @retval =0 this matched sequence is equivalent to @p __s.
1115 * @retval <0 this matched sequence will collate after @p __s.
1116 */
1117 int
1118 compare(const sub_match& __s) const
1119 { return this->str().compare(__s.str()); }
1120
1121 /**
1122 * @brief Compares this sub_match to a string.
1123 *
1124 * @param __s A string to compare to this sub_match.
1125 *
1126 * @retval <0 this matched sequence will collate before @p __s.
1127 * @retval =0 this matched sequence is equivalent to @p __s.
1128 * @retval <0 this matched sequence will collate after @p __s.
1129 */
1130 int
1131 compare(const string_type& __s) const
1132 { return this->str().compare(__s); }
1133
1134 /**
1135 * @brief Compares this sub_match to a C-style string.
1136 *
1137 * @param __s A C-style string to compare to this sub_match.
1138 *
1139 * @retval <0 this matched sequence will collate before @p __s.
1140 * @retval =0 this matched sequence is equivalent to @p __s.
1141 * @retval <0 this matched sequence will collate after @p __s.
1142 */
1143 int
1144 compare(const value_type* __s) const
1145 { return this->str().compare(__s); }
1146 };
1147
1148
1149 /** @brief Standard regex submatch over a C-style null-terminated string. */
1150 typedef sub_match<const char*> csub_match;
1151
1152 /** @brief Standard regex submatch over a standard string. */
1153 typedef sub_match<string::const_iterator> ssub_match;
1154
1155 #ifdef _GLIBCXX_USE_WCHAR_T
1156 /** @brief Regex submatch over a C-style null-terminated wide string. */
1157 typedef sub_match<const wchar_t*> wcsub_match;
1158
1159 /** @brief Regex submatch over a standard wide string. */
1160 typedef sub_match<wstring::const_iterator> wssub_match;
1161 #endif
1162
1163 // [7.9.2] sub_match non-member operators
1164
1165 /**
1166 * @brief Tests the equivalence 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 equivalent to @a __rhs, false otherwise.
1170 */
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; }
1175
1176 /**
1177 * @brief Tests the inequivalence 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 is not equivalent to @a __rhs, false otherwise.
1181 */
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; }
1186
1187 /**
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 precedes @a __rhs, false otherwise.
1192 */
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; }
1197
1198 /**
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 succeed @a __rhs, false otherwise.
1203 */
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; }
1208
1209 /**
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 does not precede @a __rhs, false otherwise.
1214 */
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; }
1219
1220 /**
1221 * @brief Tests the ordering of two regular expression submatches.
1222 * @param __lhs First regular expression submatch.
1223 * @param __rhs Second regular expression submatch.
1224 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1225 */
1226 template<typename _BiIter>
1227 inline bool
1228 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1229 { return __lhs.compare(__rhs) > 0; }
1230
1231 // Alias for sub_match'd string.
1232 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1233 using __sub_match_string = basic_string<
1234 typename iterator_traits<_Bi_iter>::value_type,
1235 _Ch_traits, _Ch_alloc>;
1236
1237 /**
1238 * @brief Tests the equivalence of a string and a regular expression
1239 * submatch.
1240 * @param __lhs A string.
1241 * @param __rhs A regular expression submatch.
1242 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1243 */
1244 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1245 inline bool
1246 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1247 const sub_match<_Bi_iter>& __rhs)
1248 { return __rhs.compare(__lhs.c_str()) == 0; }
1249
1250 /**
1251 * @brief Tests the inequivalence of a string and a regular expression
1252 * submatch.
1253 * @param __lhs A string.
1254 * @param __rhs A regular expression submatch.
1255 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1256 */
1257 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1258 inline bool
1259 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1260 const sub_match<_Bi_iter>& __rhs)
1261 { return !(__lhs == __rhs); }
1262
1263 /**
1264 * @brief Tests the ordering of a string and a regular expression submatch.
1265 * @param __lhs A string.
1266 * @param __rhs A regular expression submatch.
1267 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1268 */
1269 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1270 inline bool
1271 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1272 const sub_match<_Bi_iter>& __rhs)
1273 { return __rhs.compare(__lhs.c_str()) > 0; }
1274
1275 /**
1276 * @brief Tests the ordering of a string and a regular expression submatch.
1277 * @param __lhs A string.
1278 * @param __rhs A regular expression submatch.
1279 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1280 */
1281 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1282 inline bool
1283 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1284 const sub_match<_Bi_iter>& __rhs)
1285 { return __rhs < __lhs; }
1286
1287 /**
1288 * @brief Tests the ordering of a string and a regular expression submatch.
1289 * @param __lhs A string.
1290 * @param __rhs A regular expression submatch.
1291 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1292 */
1293 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1294 inline bool
1295 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1296 const sub_match<_Bi_iter>& __rhs)
1297 { return !(__lhs < __rhs); }
1298
1299 /**
1300 * @brief Tests the ordering of a string and a regular expression submatch.
1301 * @param __lhs A string.
1302 * @param __rhs A regular expression submatch.
1303 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1304 */
1305 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1306 inline bool
1307 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1308 const sub_match<_Bi_iter>& __rhs)
1309 { return !(__rhs < __lhs); }
1310
1311 /**
1312 * @brief Tests the equivalence of a regular expression submatch and a
1313 * string.
1314 * @param __lhs A regular expression submatch.
1315 * @param __rhs A string.
1316 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1317 */
1318 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1319 inline bool
1320 operator==(const sub_match<_Bi_iter>& __lhs,
1321 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1322 { return __lhs.compare(__rhs.c_str()) == 0; }
1323
1324 /**
1325 * @brief Tests the inequivalence of a regular expression submatch and a
1326 * string.
1327 * @param __lhs A regular expression submatch.
1328 * @param __rhs A string.
1329 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1330 */
1331 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1332 inline bool
1333 operator!=(const sub_match<_Bi_iter>& __lhs,
1334 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1335 { return !(__lhs == __rhs); }
1336
1337 /**
1338 * @brief Tests the ordering of a regular expression submatch and a string.
1339 * @param __lhs A regular expression submatch.
1340 * @param __rhs A string.
1341 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1342 */
1343 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1344 inline bool
1345 operator<(const sub_match<_Bi_iter>& __lhs,
1346 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1347 { return __lhs.compare(__rhs.c_str()) < 0; }
1348
1349 /**
1350 * @brief Tests the ordering of a regular expression submatch and a string.
1351 * @param __lhs A regular expression submatch.
1352 * @param __rhs A string.
1353 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1354 */
1355 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1356 inline bool
1357 operator>(const sub_match<_Bi_iter>& __lhs,
1358 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1359 { return __rhs < __lhs; }
1360
1361 /**
1362 * @brief Tests the ordering of a regular expression submatch and a string.
1363 * @param __lhs A regular expression submatch.
1364 * @param __rhs A string.
1365 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1366 */
1367 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1368 inline bool
1369 operator>=(const sub_match<_Bi_iter>& __lhs,
1370 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1371 { return !(__lhs < __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 does not succeed @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 __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1383 { return !(__rhs < __lhs); }
1384
1385 /**
1386 * @brief Tests the equivalence of a C string and a regular expression
1387 * submatch.
1388 * @param __lhs A C string.
1389 * @param __rhs A regular expression submatch.
1390 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1391 */
1392 template<typename _Bi_iter>
1393 inline bool
1394 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1395 const sub_match<_Bi_iter>& __rhs)
1396 { return __rhs.compare(__lhs) == 0; }
1397
1398 /**
1399 * @brief Tests the inequivalence of an iterator value and a regular
1400 * expression submatch.
1401 * @param __lhs A regular expression submatch.
1402 * @param __rhs A string.
1403 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1404 */
1405 template<typename _Bi_iter>
1406 inline bool
1407 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1408 const sub_match<_Bi_iter>& __rhs)
1409 { return !(__lhs == __rhs); }
1410
1411 /**
1412 * @brief Tests the ordering of a string and a regular expression submatch.
1413 * @param __lhs A string.
1414 * @param __rhs A regular expression submatch.
1415 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1416 */
1417 template<typename _Bi_iter>
1418 inline bool
1419 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1420 const sub_match<_Bi_iter>& __rhs)
1421 { return __rhs.compare(__lhs) > 0; }
1422
1423 /**
1424 * @brief Tests the ordering of a string and a regular expression submatch.
1425 * @param __lhs A string.
1426 * @param __rhs A regular expression submatch.
1427 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1428 */
1429 template<typename _Bi_iter>
1430 inline bool
1431 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1432 const sub_match<_Bi_iter>& __rhs)
1433 { return __rhs < __lhs; }
1434
1435 /**
1436 * @brief Tests the ordering of a string and a regular expression submatch.
1437 * @param __lhs A string.
1438 * @param __rhs A regular expression submatch.
1439 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1440 */
1441 template<typename _Bi_iter>
1442 inline bool
1443 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1444 const sub_match<_Bi_iter>& __rhs)
1445 { return !(__lhs < __rhs); }
1446
1447 /**
1448 * @brief Tests the ordering of a string and a regular expression submatch.
1449 * @param __lhs A string.
1450 * @param __rhs A regular expression submatch.
1451 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1452 */
1453 template<typename _Bi_iter>
1454 inline bool
1455 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1456 const sub_match<_Bi_iter>& __rhs)
1457 { return !(__rhs < __lhs); }
1458
1459 /**
1460 * @brief Tests the equivalence of a regular expression submatch and a
1461 * string.
1462 * @param __lhs A regular expression submatch.
1463 * @param __rhs A pointer to a string?
1464 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1465 */
1466 template<typename _Bi_iter>
1467 inline bool
1468 operator==(const sub_match<_Bi_iter>& __lhs,
1469 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1470 { return __lhs.compare(__rhs) == 0; }
1471
1472 /**
1473 * @brief Tests the inequivalence of a regular expression submatch and a
1474 * string.
1475 * @param __lhs A regular expression submatch.
1476 * @param __rhs A pointer to a string.
1477 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1478 */
1479 template<typename _Bi_iter>
1480 inline bool
1481 operator!=(const sub_match<_Bi_iter>& __lhs,
1482 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1483 { return !(__lhs == __rhs); }
1484
1485 /**
1486 * @brief Tests the ordering of a regular expression submatch and a string.
1487 * @param __lhs A regular expression submatch.
1488 * @param __rhs A string.
1489 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1490 */
1491 template<typename _Bi_iter>
1492 inline bool
1493 operator<(const sub_match<_Bi_iter>& __lhs,
1494 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1495 { return __lhs.compare(__rhs) < 0; }
1496
1497 /**
1498 * @brief Tests the ordering of a regular expression submatch and a string.
1499 * @param __lhs A regular expression submatch.
1500 * @param __rhs A string.
1501 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1502 */
1503 template<typename _Bi_iter>
1504 inline bool
1505 operator>(const sub_match<_Bi_iter>& __lhs,
1506 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1507 { return __rhs < __lhs; }
1508
1509 /**
1510 * @brief Tests the ordering of a regular expression submatch and a string.
1511 * @param __lhs A regular expression submatch.
1512 * @param __rhs A string.
1513 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1514 */
1515 template<typename _Bi_iter>
1516 inline bool
1517 operator>=(const sub_match<_Bi_iter>& __lhs,
1518 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1519 { return !(__lhs < __rhs); }
1520
1521 /**
1522 * @brief Tests the ordering of a regular expression submatch and a string.
1523 * @param __lhs A regular expression submatch.
1524 * @param __rhs A string.
1525 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1526 */
1527 template<typename _Bi_iter>
1528 inline bool
1529 operator<=(const sub_match<_Bi_iter>& __lhs,
1530 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1531 { return !(__rhs < __lhs); }
1532
1533 /**
1534 * @brief Tests the equivalence of a string and a regular expression
1535 * submatch.
1536 * @param __lhs A string.
1537 * @param __rhs A regular expression submatch.
1538 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1539 */
1540 template<typename _Bi_iter>
1541 inline bool
1542 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1543 const sub_match<_Bi_iter>& __rhs)
1544 {
1545 typedef typename sub_match<_Bi_iter>::string_type string_type;
1546 return __rhs.compare(string_type(1, __lhs)) == 0;
1547 }
1548
1549 /**
1550 * @brief Tests the inequivalence of a string and a regular expression
1551 * submatch.
1552 * @param __lhs A string.
1553 * @param __rhs A regular expression submatch.
1554 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1555 */
1556 template<typename _Bi_iter>
1557 inline bool
1558 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1559 const sub_match<_Bi_iter>& __rhs)
1560 { return !(__lhs == __rhs); }
1561
1562 /**
1563 * @brief Tests the ordering of a string and a regular expression submatch.
1564 * @param __lhs A string.
1565 * @param __rhs A regular expression submatch.
1566 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1567 */
1568 template<typename _Bi_iter>
1569 inline bool
1570 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1571 const sub_match<_Bi_iter>& __rhs)
1572 {
1573 typedef typename sub_match<_Bi_iter>::string_type string_type;
1574 return __rhs.compare(string_type(1, __lhs)) > 0;
1575 }
1576
1577 /**
1578 * @brief Tests the ordering of a string and a regular expression submatch.
1579 * @param __lhs A string.
1580 * @param __rhs A regular expression submatch.
1581 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1582 */
1583 template<typename _Bi_iter>
1584 inline bool
1585 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1586 const sub_match<_Bi_iter>& __rhs)
1587 { return __rhs < __lhs; }
1588
1589 /**
1590 * @brief Tests the ordering of a string and a regular expression submatch.
1591 * @param __lhs A string.
1592 * @param __rhs A regular expression submatch.
1593 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1594 */
1595 template<typename _Bi_iter>
1596 inline bool
1597 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1598 const sub_match<_Bi_iter>& __rhs)
1599 { return !(__lhs < __rhs); }
1600
1601 /**
1602 * @brief Tests the ordering of a string and a regular expression submatch.
1603 * @param __lhs A string.
1604 * @param __rhs A regular expression submatch.
1605 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1606 */
1607 template<typename _Bi_iter>
1608 inline bool
1609 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1610 const sub_match<_Bi_iter>& __rhs)
1611 { return !(__rhs < __lhs); }
1612
1613 /**
1614 * @brief Tests the equivalence of a regular expression submatch and a
1615 * string.
1616 * @param __lhs A regular expression submatch.
1617 * @param __rhs A const string reference.
1618 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1619 */
1620 template<typename _Bi_iter>
1621 inline bool
1622 operator==(const sub_match<_Bi_iter>& __lhs,
1623 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1624 {
1625 typedef typename sub_match<_Bi_iter>::string_type string_type;
1626 return __lhs.compare(string_type(1, __rhs)) == 0;
1627 }
1628
1629 /**
1630 * @brief Tests the inequivalence of a regular expression submatch and a
1631 * string.
1632 * @param __lhs A regular expression submatch.
1633 * @param __rhs A const string reference.
1634 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1635 */
1636 template<typename _Bi_iter>
1637 inline bool
1638 operator!=(const sub_match<_Bi_iter>& __lhs,
1639 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1640 { return !(__lhs == __rhs); }
1641
1642 /**
1643 * @brief Tests the ordering of a regular expression submatch and a string.
1644 * @param __lhs A regular expression submatch.
1645 * @param __rhs A const string reference.
1646 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1647 */
1648 template<typename _Bi_iter>
1649 inline bool
1650 operator<(const sub_match<_Bi_iter>& __lhs,
1651 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1652 {
1653 typedef typename sub_match<_Bi_iter>::string_type string_type;
1654 return __lhs.compare(string_type(1, __rhs)) < 0;
1655 }
1656
1657 /**
1658 * @brief Tests the ordering of a regular expression submatch and a string.
1659 * @param __lhs A regular expression submatch.
1660 * @param __rhs A const string reference.
1661 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1662 */
1663 template<typename _Bi_iter>
1664 inline bool
1665 operator>(const sub_match<_Bi_iter>& __lhs,
1666 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1667 { return __rhs < __lhs; }
1668
1669 /**
1670 * @brief Tests the ordering of a regular expression submatch and a string.
1671 * @param __lhs A regular expression submatch.
1672 * @param __rhs A const string reference.
1673 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1674 */
1675 template<typename _Bi_iter>
1676 inline bool
1677 operator>=(const sub_match<_Bi_iter>& __lhs,
1678 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1679 { return !(__lhs < __rhs); }
1680
1681 /**
1682 * @brief Tests the ordering of a regular expression submatch and a string.
1683 * @param __lhs A regular expression submatch.
1684 * @param __rhs A const string reference.
1685 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1686 */
1687 template<typename _Bi_iter>
1688 inline bool
1689 operator<=(const sub_match<_Bi_iter>& __lhs,
1690 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1691 { return !(__rhs < __lhs); }
1692
1693 /**
1694 * @brief Inserts a matched string into an output stream.
1695 *
1696 * @param __os The output stream.
1697 * @param __m A submatch string.
1698 *
1699 * @returns the output stream with the submatch string inserted.
1700 */
1701 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1702 inline
1703 basic_ostream<_Ch_type, _Ch_traits>&
1704 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1705 const sub_match<_Bi_iter>& __m)
1706 { return __os << __m.str(); }
1707
1708 // [7.10] Class template match_results
1709
1710 /*
1711 * Special sub_match object representing an unmatched sub-expression.
1712 */
1713 template<typename _Bi_iter>
1714 inline const sub_match<_Bi_iter>&
1715 __unmatched_sub()
1716 {
1717 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1718 return __unmatched;
1719 }
1720
1721 /**
1722 * @brief The results of a match or search operation.
1723 *
1724 * A collection of character sequences representing the result of a regular
1725 * expression match. Storage for the collection is allocated and freed as
1726 * necessary by the member functions of class template match_results.
1727 *
1728 * This class satisfies the Sequence requirements, with the exception that
1729 * only the operations defined for a const-qualified Sequence are supported.
1730 *
1731 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1732 * the whole match. In this case the %sub_match member matched is always true.
1733 * The sub_match object stored at index n denotes what matched the marked
1734 * sub-expression n within the matched expression. If the sub-expression n
1735 * participated in a regular expression match then the %sub_match member
1736 * matched evaluates to true, and members first and second denote the range
1737 * of characters [first, second) which formed that match. Otherwise matched
1738 * is false, and members first and second point to the end of the sequence
1739 * that was searched.
1740 *
1741 * @nosubgrouping
1742 */
1743 template<typename _Bi_iter,
1744 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1745 class match_results
1746 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1747 {
1748 private:
1749 /*
1750 * The vector base is empty if this does not represent a successful match.
1751 * Otherwise it contains n+3 elements where n is the number of marked
1752 * sub-expressions:
1753 * [0] entire match
1754 * [1] 1st marked subexpression
1755 * ...
1756 * [n] nth marked subexpression
1757 * [n+1] prefix
1758 * [n+2] suffix
1759 */
1760 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1761 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1762 typedef regex_constants::match_flag_type match_flag_type;
1763
1764 public:
1765 /**
1766 * @name 10.? Public Types
1767 */
1768 //@{
1769 typedef _Alloc allocator_type;
1770 typedef sub_match<_Bi_iter> value_type;
1771 typedef const value_type& const_reference;
1772 typedef const_reference reference;
1773 typedef typename _Base_type::const_iterator const_iterator;
1774 typedef const_iterator iterator;
1775 typedef typename __iter_traits::difference_type difference_type;
1776 typedef typename __iter_traits::value_type char_type;
1777 typedef typename allocator_traits<_Alloc>::size_type size_type;
1778
1779
1780 typedef std::basic_string<char_type> string_type;
1781 //@}
1782
1783 public:
1784 /**
1785 * @name 28.10.1 Construction, Copying, and Destruction
1786 */
1787 //@{
1788
1789 /**
1790 * @brief Constructs a default %match_results container.
1791 * @post size() returns 0 and str() returns an empty string.
1792 */
1793 explicit
1794 match_results(const _Alloc& __a = _Alloc())
1795 : _Base_type(__a), _M_in_iterator(false)
1796 { }
1797
1798 /**
1799 * @brief Copy constructs a %match_results.
1800 */
1801 match_results(const match_results& __rhs)
1802 : _Base_type(__rhs), _M_in_iterator(false)
1803 { }
1804
1805 /**
1806 * @brief Move constructs a %match_results.
1807 */
1808 match_results(match_results&& __rhs) noexcept
1809 : _Base_type(std::move(__rhs)), _M_in_iterator(false)
1810 { }
1811
1812 /**
1813 * @brief Assigns rhs to *this.
1814 */
1815 match_results&
1816 operator=(const match_results& __rhs)
1817 {
1818 match_results(__rhs).swap(*this);
1819 return *this;
1820 }
1821
1822 /**
1823 * @brief Move-assigns rhs to *this.
1824 */
1825 match_results&
1826 operator=(match_results&& __rhs)
1827 {
1828 match_results(std::move(__rhs)).swap(*this);
1829 return *this;
1830 }
1831
1832 /**
1833 * @brief Destroys a %match_results object.
1834 */
1835 ~match_results()
1836 { }
1837
1838 //@}
1839
1840 // 28.10.2, state:
1841 /**
1842 * @brief Indicates if the %match_results is ready.
1843 * @retval true The object has a fully-established result state.
1844 * @retval false The object is not ready.
1845 */
1846 bool ready() const { return !_Base_type::empty(); }
1847
1848 /**
1849 * @name 28.10.2 Size
1850 */
1851 //@{
1852
1853 /**
1854 * @brief Gets the number of matches and submatches.
1855 *
1856 * The number of matches for a given regular expression will be either 0
1857 * if there was no match or mark_count() + 1 if a match was successful.
1858 * Some matches may be empty.
1859 *
1860 * @returns the number of matches found.
1861 */
1862 size_type
1863 size() const
1864 {
1865 size_type __size = _Base_type::size();
1866 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1867 }
1868
1869 size_type
1870 max_size() const
1871 { return _Base_type::max_size(); }
1872
1873 /**
1874 * @brief Indicates if the %match_results contains no results.
1875 * @retval true The %match_results object is empty.
1876 * @retval false The %match_results object is not empty.
1877 */
1878 bool
1879 empty() const
1880 { return size() == 0; }
1881
1882 //@}
1883
1884 /**
1885 * @name 10.3 Element Access
1886 */
1887 //@{
1888
1889 /**
1890 * @brief Gets the length of the indicated submatch.
1891 * @param __sub indicates the submatch.
1892 * @pre ready() == true
1893 *
1894 * This function returns the length of the indicated submatch, or the
1895 * length of the entire match if @p __sub is zero (the default).
1896 */
1897 difference_type
1898 length(size_type __sub = 0) const
1899 { return (*this)[__sub].length(); }
1900
1901 /**
1902 * @brief Gets the offset of the beginning of the indicated submatch.
1903 * @param __sub indicates the submatch.
1904 * @pre ready() == true
1905 *
1906 * This function returns the offset from the beginning of the target
1907 * sequence to the beginning of the submatch, unless the value of @p __sub
1908 * is zero (the default), in which case this function returns the offset
1909 * from the beginning of the target sequence to the beginning of the
1910 * match.
1911 *
1912 * Returns -1 if @p __sub is out of range.
1913 */
1914 difference_type
1915 position(size_type __sub = 0) const
1916 {
1917 // [28.12.1.4.5]
1918 if (_M_in_iterator)
1919 return __sub < size() ? std::distance(_M_begin,
1920 (*this)[__sub].first) : -1;
1921 else
1922 return __sub < size() ? std::distance(this->prefix().first,
1923 (*this)[__sub].first) : -1;
1924 }
1925
1926 /**
1927 * @brief Gets the match or submatch converted to a string type.
1928 * @param __sub indicates the submatch.
1929 * @pre ready() == true
1930 *
1931 * This function gets the submatch (or match, if @p __sub is
1932 * zero) extracted from the target range and converted to the
1933 * associated string type.
1934 */
1935 string_type
1936 str(size_type __sub = 0) const
1937 { return (*this)[__sub].str(); }
1938
1939 /**
1940 * @brief Gets a %sub_match reference for the match or submatch.
1941 * @param __sub indicates the submatch.
1942 * @pre ready() == true
1943 *
1944 * This function gets a reference to the indicated submatch, or
1945 * the entire match if @p __sub is zero.
1946 *
1947 * If @p __sub >= size() then this function returns a %sub_match with a
1948 * special value indicating no submatch.
1949 */
1950 const_reference
1951 operator[](size_type __sub) const
1952 {
1953 _GLIBCXX_DEBUG_ASSERT( ready() );
1954 return __sub < size()
1955 ? _Base_type::operator[](__sub)
1956 : __unmatched_sub<_Bi_iter>();
1957 }
1958
1959 /**
1960 * @brief Gets a %sub_match representing the match prefix.
1961 * @pre ready() == true
1962 *
1963 * This function gets a reference to a %sub_match object representing the
1964 * part of the target range between the start of the target range and the
1965 * start of the match.
1966 */
1967 const_reference
1968 prefix() const
1969 {
1970 _GLIBCXX_DEBUG_ASSERT( ready() );
1971 return !empty()
1972 ? _Base_type::operator[](_Base_type::size() - 2)
1973 : __unmatched_sub<_Bi_iter>();
1974 }
1975
1976 /**
1977 * @brief Gets a %sub_match representing the match suffix.
1978 * @pre ready() == true
1979 *
1980 * This function gets a reference to a %sub_match object representing the
1981 * part of the target range between the end of the match and the end of
1982 * the target range.
1983 */
1984 const_reference
1985 suffix() const
1986 {
1987 _GLIBCXX_DEBUG_ASSERT( ready() );
1988 return !empty()
1989 ? _Base_type::operator[](_Base_type::size() - 1)
1990 : __unmatched_sub<_Bi_iter>();
1991 }
1992
1993 /**
1994 * @brief Gets an iterator to the start of the %sub_match collection.
1995 */
1996 const_iterator
1997 begin() const
1998 { return _Base_type::begin(); }
1999
2000 /**
2001 * @brief Gets an iterator to the start of the %sub_match collection.
2002 */
2003 const_iterator
2004 cbegin() const
2005 { return _Base_type::cbegin(); }
2006
2007 /**
2008 * @brief Gets an iterator to one-past-the-end of the collection.
2009 */
2010 const_iterator
2011 end() const
2012 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
2013
2014 /**
2015 * @brief Gets an iterator to one-past-the-end of the collection.
2016 */
2017 const_iterator
2018 cend() const
2019 { return end(); }
2020
2021 //@}
2022
2023 /**
2024 * @name 10.4 Formatting
2025 *
2026 * These functions perform formatted substitution of the matched
2027 * character sequences into their target. The format specifiers and
2028 * escape sequences accepted by these functions are determined by
2029 * their @p flags parameter as documented above.
2030 */
2031 //@{
2032
2033 /**
2034 * @pre ready() == true
2035 * @todo Implement this function.
2036 */
2037 template<typename _Out_iter>
2038 _Out_iter
2039 format(_Out_iter __out, const char_type* __fmt_first,
2040 const char_type* __fmt_last,
2041 match_flag_type __flags = regex_constants::format_default) const
2042 { return __out; }
2043
2044 /**
2045 * @pre ready() == true
2046 */
2047 template<typename _Out_iter, typename _St, typename _Sa>
2048 _Out_iter
2049 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
2050 match_flag_type __flags = regex_constants::format_default) const
2051 {
2052 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
2053 __flags);
2054 }
2055
2056 /**
2057 * @pre ready() == true
2058 */
2059 template<typename _Out_iter, typename _St, typename _Sa>
2060 basic_string<char_type, _St, _Sa>
2061 format(const basic_string<char_type, _St, _Sa>& __fmt,
2062 match_flag_type __flags = regex_constants::format_default) const
2063 {
2064 basic_string<char_type, _St, _Sa> __result;
2065 format(std::back_inserter(__result), __fmt, __flags);
2066 return __result;
2067 }
2068
2069 /**
2070 * @pre ready() == true
2071 */
2072 string_type
2073 format(const char_type* __fmt,
2074 match_flag_type __flags = regex_constants::format_default) const
2075 {
2076 string_type __result;
2077 format(std::back_inserter(__result),
2078 __fmt,
2079 __fmt + char_traits<char_type>::length(__fmt),
2080 __flags);
2081 return __result;
2082 }
2083
2084 //@}
2085
2086 /**
2087 * @name 10.5 Allocator
2088 */
2089 //@{
2090
2091 /**
2092 * @brief Gets a copy of the allocator.
2093 */
2094 allocator_type
2095 get_allocator() const
2096 { return _Base_type::get_allocator(); }
2097
2098 //@}
2099
2100 /**
2101 * @name 10.6 Swap
2102 */
2103 //@{
2104
2105 /**
2106 * @brief Swaps the contents of two match_results.
2107 */
2108 void
2109 swap(match_results& __that)
2110 { _Base_type::swap(__that); }
2111 //@}
2112
2113 private:
2114 template<typename, typename, typename, typename>
2115 friend class __detail::_Executor;
2116
2117 template<typename, typename, typename, typename>
2118 friend class __detail::_DFSExecutor;
2119
2120 template<typename, typename, typename, typename>
2121 friend class __detail::_BFSExecutor;
2122
2123 template<typename, typename, typename>
2124 friend class regex_iterator;
2125
2126 template<typename _Bp, typename _Ap,
2127 typename _Ch_type, typename _Rx_traits>
2128 friend bool
2129 regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&,
2130 const basic_regex<_Ch_type,
2131 _Rx_traits>&,
2132 regex_constants::match_flag_type);
2133
2134 template<typename _Bp, typename _Ap,
2135 typename _Ch_type, typename _Rx_traits>
2136 friend bool
2137 regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&,
2138 const basic_regex<_Ch_type,
2139 _Rx_traits>&,
2140 regex_constants::match_flag_type);
2141
2142 _Bi_iter _M_begin;
2143 bool _M_in_iterator;
2144 };
2145
2146 typedef match_results<const char*> cmatch;
2147 typedef match_results<string::const_iterator> smatch;
2148 #ifdef _GLIBCXX_USE_WCHAR_T
2149 typedef match_results<const wchar_t*> wcmatch;
2150 typedef match_results<wstring::const_iterator> wsmatch;
2151 #endif
2152
2153 // match_results comparisons
2154 /**
2155 * @brief Compares two match_results for equality.
2156 * @returns true if the two objects refer to the same match,
2157 * false otherwise.
2158 */
2159 template<typename _Bi_iter, typename _Alloc>
2160 inline bool
2161 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
2162 const match_results<_Bi_iter, _Alloc>& __m2)
2163 {
2164 if (__m1.ready() != __m2.ready())
2165 return false;
2166 if (!__m1.ready()) // both are not ready
2167 return true;
2168 if (__m1.empty() != __m2.empty())
2169 return false;
2170 if (__m1.empty()) // both are empty
2171 return true;
2172 return __m1.prefix() == __m2.prefix()
2173 && __m1.size() == __m2.size()
2174 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
2175 && __m1.suffix() == __m2.suffix();
2176 }
2177
2178 /**
2179 * @brief Compares two match_results for inequality.
2180 * @returns true if the two objects do not refer to the same match,
2181 * false otherwise.
2182 */
2183 template<typename _Bi_iter, class _Alloc>
2184 inline bool
2185 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
2186 const match_results<_Bi_iter, _Alloc>& __m2)
2187 { return !(__m1 == __m2); }
2188
2189 // [7.10.6] match_results swap
2190 /**
2191 * @brief Swaps two match results.
2192 * @param __lhs A match result.
2193 * @param __rhs A match result.
2194 *
2195 * The contents of the two match_results objects are swapped.
2196 */
2197 template<typename _Bi_iter, typename _Alloc>
2198 inline void
2199 swap(match_results<_Bi_iter, _Alloc>& __lhs,
2200 match_results<_Bi_iter, _Alloc>& __rhs)
2201 { __lhs.swap(__rhs); }
2202
2203 // [7.11.2] Function template regex_match
2204 /**
2205 * @name Matching, Searching, and Replacing
2206 */
2207 //@{
2208
2209 /**
2210 * @brief Determines if there is a match between the regular expression @p e
2211 * and all of the character sequence [first, last).
2212 *
2213 * @param __s Start of the character sequence to match.
2214 * @param __e One-past-the-end of the character sequence to match.
2215 * @param __m The match results.
2216 * @param __re The regular expression.
2217 * @param __flags Controls how the regular expression is matched.
2218 *
2219 * @retval true A match exists.
2220 * @retval false Otherwise.
2221 *
2222 * @throws an exception of type regex_error.
2223 */
2224 template<typename _Bi_iter, typename _Alloc,
2225 typename _Ch_type, typename _Rx_traits>
2226 bool
2227 regex_match(_Bi_iter __s,
2228 _Bi_iter __e,
2229 match_results<_Bi_iter, _Alloc>& __m,
2230 const basic_regex<_Ch_type, _Rx_traits>& __re,
2231 regex_constants::match_flag_type __flags
2232 = regex_constants::match_default)
2233 {
2234 if (__re._M_automaton == nullptr)
2235 return false;
2236
2237 auto __size = __re._M_automaton->_M_sub_count();
2238 __size += 2;
2239 __m.resize(__size);
2240 for (decltype(__size) __i = 0; __i < __size; ++__i)
2241 __m.at(__i).matched = false;
2242
2243 if (__detail::__get_executor(__s, __e, __m, __re, __flags)->_M_match())
2244 {
2245 for (auto __it : __m)
2246 if (!__it.matched)
2247 __it.first = __it.second = __e;
2248 __m.at(__m.size()).matched = false;
2249 __m.at(__m.size()).first = __s;
2250 __m.at(__m.size()).second = __s;
2251 __m.at(__m.size()+1).matched = false;
2252 __m.at(__m.size()+1).first = __e;
2253 __m.at(__m.size()+1).second = __e;
2254 return true;
2255 }
2256 return false;
2257 }
2258
2259 /**
2260 * @brief Indicates if there is a match between the regular expression @p e
2261 * and all of the character sequence [first, last).
2262 *
2263 * @param __first Beginning of the character sequence to match.
2264 * @param __last One-past-the-end of the character sequence to match.
2265 * @param __re The regular expression.
2266 * @param __flags Controls how the regular expression is matched.
2267 *
2268 * @retval true A match exists.
2269 * @retval false Otherwise.
2270 *
2271 * @throws an exception of type regex_error.
2272 */
2273 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2274 bool
2275 regex_match(_Bi_iter __first, _Bi_iter __last,
2276 const basic_regex<_Ch_type, _Rx_traits>& __re,
2277 regex_constants::match_flag_type __flags
2278 = regex_constants::match_default)
2279 {
2280 match_results<_Bi_iter> __what;
2281 return regex_match(__first, __last, __what, __re, __flags);
2282 }
2283
2284 /**
2285 * @brief Determines if there is a match between the regular expression @p e
2286 * and a C-style null-terminated string.
2287 *
2288 * @param __s The C-style null-terminated string to match.
2289 * @param __m The match results.
2290 * @param __re The regular expression.
2291 * @param __f Controls how the regular expression is matched.
2292 *
2293 * @retval true A match exists.
2294 * @retval false Otherwise.
2295 *
2296 * @throws an exception of type regex_error.
2297 */
2298 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2299 inline bool
2300 regex_match(const _Ch_type* __s,
2301 match_results<const _Ch_type*, _Alloc>& __m,
2302 const basic_regex<_Ch_type, _Rx_traits>& __re,
2303 regex_constants::match_flag_type __f
2304 = regex_constants::match_default)
2305 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2306
2307 /**
2308 * @brief Determines if there is a match between the regular expression @p e
2309 * and a string.
2310 *
2311 * @param __s The string to match.
2312 * @param __m The match results.
2313 * @param __re The regular expression.
2314 * @param __flags Controls how the regular expression is matched.
2315 *
2316 * @retval true A match exists.
2317 * @retval false Otherwise.
2318 *
2319 * @throws an exception of type regex_error.
2320 */
2321 template<typename _Ch_traits, typename _Ch_alloc,
2322 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2323 inline bool
2324 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2325 match_results<typename basic_string<_Ch_type,
2326 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2327 const basic_regex<_Ch_type, _Rx_traits>& __re,
2328 regex_constants::match_flag_type __flags
2329 = regex_constants::match_default)
2330 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2331
2332 /**
2333 * @brief Indicates if there is a match between the regular expression @p e
2334 * and a C-style null-terminated string.
2335 *
2336 * @param __s The C-style null-terminated string to match.
2337 * @param __re The regular expression.
2338 * @param __f Controls how the regular expression is matched.
2339 *
2340 * @retval true A match exists.
2341 * @retval false Otherwise.
2342 *
2343 * @throws an exception of type regex_error.
2344 */
2345 template<typename _Ch_type, class _Rx_traits>
2346 inline bool
2347 regex_match(const _Ch_type* __s,
2348 const basic_regex<_Ch_type, _Rx_traits>& __re,
2349 regex_constants::match_flag_type __f
2350 = regex_constants::match_default)
2351 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2352
2353 /**
2354 * @brief Indicates if there is a match between the regular expression @p e
2355 * and a string.
2356 *
2357 * @param __s [IN] The string to match.
2358 * @param __re [IN] The regular expression.
2359 * @param __flags [IN] Controls how the regular expression is matched.
2360 *
2361 * @retval true A match exists.
2362 * @retval false Otherwise.
2363 *
2364 * @throws an exception of type regex_error.
2365 */
2366 template<typename _Ch_traits, typename _Str_allocator,
2367 typename _Ch_type, typename _Rx_traits>
2368 inline bool
2369 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2370 const basic_regex<_Ch_type, _Rx_traits>& __re,
2371 regex_constants::match_flag_type __flags
2372 = regex_constants::match_default)
2373 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2374
2375 // [7.11.3] Function template regex_search
2376 /**
2377 * Searches for a regular expression within a range.
2378 * @param __first [IN] The start of the string to search.
2379 * @param __last [IN] One-past-the-end of the string to search.
2380 * @param __m [OUT] The match results.
2381 * @param __re [IN] The regular expression to search for.
2382 * @param __flags [IN] Search policy flags.
2383 * @retval true A match was found within the string.
2384 * @retval false No match was found within the string, the content of %m is
2385 * undefined.
2386 *
2387 * @throws an exception of type regex_error.
2388 */
2389 template<typename _Bi_iter, typename _Alloc,
2390 typename _Ch_type, typename _Rx_traits>
2391 inline bool
2392 regex_search(_Bi_iter __first, _Bi_iter __last,
2393 match_results<_Bi_iter, _Alloc>& __m,
2394 const basic_regex<_Ch_type, _Rx_traits>& __re,
2395 regex_constants::match_flag_type __flags
2396 = regex_constants::match_default)
2397 {
2398 if (__re._M_automaton == nullptr)
2399 return false;
2400
2401 auto __size = __re._M_automaton->_M_sub_count();
2402 __size += 2;
2403 __m.resize(__size);
2404 for (decltype(__size) __i = 0; __i < __size; ++__i)
2405 __m.at(__i).matched = false;
2406
2407 if (__detail::__get_executor(__first, __last, __m, __re, __flags)
2408 ->_M_search())
2409 {
2410 for (auto __it : __m)
2411 if (!__it.matched)
2412 __it.first = __it.second = __last;
2413 __m.at(__m.size()).first = __first;
2414 __m.at(__m.size()).second = __m[0].first;
2415 __m.at(__m.size()+1).first = __m[0].second;
2416 __m.at(__m.size()+1).second = __last;
2417 __m.at(__m.size()).matched =
2418 (__m.prefix().first != __m.prefix().second);
2419 __m.at(__m.size()+1).matched =
2420 (__m.suffix().first != __m.suffix().second);
2421 return true;
2422 }
2423 return false;
2424 }
2425
2426 /**
2427 * Searches for a regular expression within a range.
2428 * @param __first [IN] The start of the string to search.
2429 * @param __last [IN] One-past-the-end of the string to search.
2430 * @param __re [IN] The regular expression to search for.
2431 * @param __flags [IN] Search policy flags.
2432 * @retval true A match was found within the string.
2433 * @retval false No match was found within the string.
2434 *
2435 * @throws an exception of type regex_error.
2436 */
2437 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2438 inline bool
2439 regex_search(_Bi_iter __first, _Bi_iter __last,
2440 const basic_regex<_Ch_type, _Rx_traits>& __re,
2441 regex_constants::match_flag_type __flags
2442 = regex_constants::match_default)
2443 {
2444 match_results<_Bi_iter> __what;
2445 return regex_search(__first, __last, __what, __re, __flags);
2446 }
2447
2448 /**
2449 * @brief Searches for a regular expression within a C-string.
2450 * @param __s [IN] A C-string to search for the regex.
2451 * @param __m [OUT] The set of regex matches.
2452 * @param __e [IN] The regex to search for in @p s.
2453 * @param __f [IN] The search flags.
2454 * @retval true A match was found within the string.
2455 * @retval false No match was found within the string, the content of %m is
2456 * undefined.
2457 *
2458 * @throws an exception of type regex_error.
2459 */
2460 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2461 inline bool
2462 regex_search(const _Ch_type* __s,
2463 match_results<const _Ch_type*, _Alloc>& __m,
2464 const basic_regex<_Ch_type, _Rx_traits>& __e,
2465 regex_constants::match_flag_type __f
2466 = regex_constants::match_default)
2467 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2468
2469 /**
2470 * @brief Searches for a regular expression within a C-string.
2471 * @param __s [IN] The C-string to search.
2472 * @param __e [IN] The regular expression to search for.
2473 * @param __f [IN] Search policy flags.
2474 * @retval true A match was found within the string.
2475 * @retval false No match was found within the string.
2476 *
2477 * @throws an exception of type regex_error.
2478 */
2479 template<typename _Ch_type, typename _Rx_traits>
2480 inline bool
2481 regex_search(const _Ch_type* __s,
2482 const basic_regex<_Ch_type, _Rx_traits>& __e,
2483 regex_constants::match_flag_type __f
2484 = regex_constants::match_default)
2485 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2486
2487 /**
2488 * @brief Searches for a regular expression within a string.
2489 * @param __s [IN] The string to search.
2490 * @param __e [IN] The regular expression to search for.
2491 * @param __flags [IN] Search policy flags.
2492 * @retval true A match was found within the string.
2493 * @retval false No match was found within the string.
2494 *
2495 * @throws an exception of type regex_error.
2496 */
2497 template<typename _Ch_traits, typename _String_allocator,
2498 typename _Ch_type, typename _Rx_traits>
2499 inline bool
2500 regex_search(const basic_string<_Ch_type, _Ch_traits,
2501 _String_allocator>& __s,
2502 const basic_regex<_Ch_type, _Rx_traits>& __e,
2503 regex_constants::match_flag_type __flags
2504 = regex_constants::match_default)
2505 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2506
2507 /**
2508 * @brief Searches for a regular expression within a string.
2509 * @param __s [IN] A C++ string to search for the regex.
2510 * @param __m [OUT] The set of regex matches.
2511 * @param __e [IN] The regex to search for in @p s.
2512 * @param __f [IN] The search flags.
2513 * @retval true A match was found within the string.
2514 * @retval false No match was found within the string, the content of %m is
2515 * undefined.
2516 *
2517 * @throws an exception of type regex_error.
2518 */
2519 template<typename _Ch_traits, typename _Ch_alloc,
2520 typename _Alloc, typename _Ch_type,
2521 typename _Rx_traits>
2522 inline bool
2523 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2524 match_results<typename basic_string<_Ch_type,
2525 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2526 const basic_regex<_Ch_type, _Rx_traits>& __e,
2527 regex_constants::match_flag_type __f
2528 = regex_constants::match_default)
2529 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2530
2531 // std [28.11.4] Function template regex_replace
2532 /**
2533 * @doctodo
2534 * @param __out
2535 * @param __first
2536 * @param __last
2537 * @param __e
2538 * @param __fmt
2539 * @param __flags
2540 *
2541 * @returns out
2542 * @throws an exception of type regex_error.
2543 *
2544 * @todo Implement this function.
2545 */
2546 template<typename _Out_iter, typename _Bi_iter,
2547 typename _Rx_traits, typename _Ch_type>
2548 inline _Out_iter
2549 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2550 const basic_regex<_Ch_type, _Rx_traits>& __e,
2551 const basic_string<_Ch_type>& __fmt,
2552 regex_constants::match_flag_type __flags
2553 = regex_constants::match_default)
2554 { return __out; }
2555
2556 /**
2557 * @doctodo
2558 * @param __s
2559 * @param __e
2560 * @param __fmt
2561 * @param __flags
2562 *
2563 * @returns a copy of string @p s with replacements.
2564 *
2565 * @throws an exception of type regex_error.
2566 */
2567 template<typename _Rx_traits, typename _Ch_type>
2568 inline basic_string<_Ch_type>
2569 regex_replace(const basic_string<_Ch_type>& __s,
2570 const basic_regex<_Ch_type, _Rx_traits>& __e,
2571 const basic_string<_Ch_type>& __fmt,
2572 regex_constants::match_flag_type __flags
2573 = regex_constants::match_default)
2574 {
2575 basic_string<_Ch_type> __result;
2576 regex_replace(std::back_inserter(__result),
2577 __s.begin(), __s.end(), __e, __fmt, __flags);
2578 return __result;
2579 }
2580
2581 //@}
2582
2583 // std [28.12] Class template regex_iterator
2584 /**
2585 * An iterator adaptor that will provide repeated calls of regex_search over
2586 * a range until no more matches remain.
2587 */
2588 template<typename _Bi_iter,
2589 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2590 typename _Rx_traits = regex_traits<_Ch_type> >
2591 class regex_iterator
2592 {
2593 public:
2594 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2595 typedef match_results<_Bi_iter> value_type;
2596 typedef std::ptrdiff_t difference_type;
2597 typedef const value_type* pointer;
2598 typedef const value_type& reference;
2599 typedef std::forward_iterator_tag iterator_category;
2600
2601 /**
2602 * @brief Provides a singular iterator, useful for indicating
2603 * one-past-the-end of a range.
2604 */
2605 regex_iterator()
2606 : _M_match()
2607 { }
2608
2609 /**
2610 * Constructs a %regex_iterator...
2611 * @param __a [IN] The start of a text range to search.
2612 * @param __b [IN] One-past-the-end of the text range to search.
2613 * @param __re [IN] The regular expression to match.
2614 * @param __m [IN] Policy flags for match rules.
2615 */
2616 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2617 regex_constants::match_flag_type __m
2618 = regex_constants::match_default)
2619 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2620 {
2621 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2622 *this = regex_iterator();
2623 }
2624
2625 /**
2626 * Copy constructs a %regex_iterator.
2627 */
2628 regex_iterator(const regex_iterator& __rhs) = default;
2629
2630 /**
2631 * @brief Assigns one %regex_iterator to another.
2632 */
2633 regex_iterator&
2634 operator=(const regex_iterator& __rhs) = default;
2635
2636 /**
2637 * @brief Tests the equivalence of two regex iterators.
2638 */
2639 bool
2640 operator==(const regex_iterator& __rhs) const;
2641
2642 /**
2643 * @brief Tests the inequivalence of two regex iterators.
2644 */
2645 bool
2646 operator!=(const regex_iterator& __rhs) const
2647 { return !(*this == __rhs); }
2648
2649 /**
2650 * @brief Dereferences a %regex_iterator.
2651 */
2652 const value_type&
2653 operator*() const
2654 { return _M_match; }
2655
2656 /**
2657 * @brief Selects a %regex_iterator member.
2658 */
2659 const value_type*
2660 operator->() const
2661 { return &_M_match; }
2662
2663 /**
2664 * @brief Increments a %regex_iterator.
2665 */
2666 regex_iterator&
2667 operator++();
2668
2669 /**
2670 * @brief Postincrements a %regex_iterator.
2671 */
2672 regex_iterator
2673 operator++(int)
2674 {
2675 auto __tmp = *this;
2676 ++(*this);
2677 return __tmp;
2678 }
2679
2680 private:
2681 _Bi_iter _M_begin;
2682 _Bi_iter _M_end;
2683 const regex_type* _M_pregex;
2684 regex_constants::match_flag_type _M_flags;
2685 match_results<_Bi_iter> _M_match;
2686 };
2687
2688 template<typename _Bi_iter,
2689 typename _Ch_type,
2690 typename _Rx_traits>
2691 bool
2692 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2693 operator==(const regex_iterator& __rhs) const
2694 {
2695 return (_M_match.empty() && __rhs._M_match.empty())
2696 || (_M_begin == __rhs._M_begin
2697 && _M_end == __rhs._M_end
2698 && _M_pregex == __rhs._M_pregex
2699 && _M_flags == __rhs._M_flags
2700 && _M_match[0] == __rhs._M_match[0]);
2701 }
2702
2703 template<typename _Bi_iter,
2704 typename _Ch_type,
2705 typename _Rx_traits>
2706 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
2707 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2708 operator++()
2709 {
2710 // In all cases in which the call to regex_search returns true,
2711 // match.prefix().first shall be equal to the previous value of
2712 // match[0].second, and for each index i in the half-open range
2713 // [0, match.size()) for which match[i].matched is true,
2714 // match[i].position() shall return distance(begin, match[i].first).
2715 // [28.12.1.4.5]
2716 if (_M_match[0].matched)
2717 {
2718 auto __start = _M_match[0].second;
2719 if (_M_match[0].first == _M_match[0].second)
2720 if (__start == _M_end)
2721 {
2722 _M_match = value_type();
2723 return *this;
2724 }
2725 else
2726 {
2727 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags
2728 | regex_constants::match_not_null
2729 | regex_constants::match_continuous))
2730 {
2731 _M_match._M_in_iterator = true;
2732 _M_match._M_begin = _M_begin;
2733 return *this;
2734 }
2735 else
2736 ++__start;
2737 }
2738 _M_flags |= regex_constants::match_prev_avail;
2739 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags))
2740 {
2741 _M_match._M_in_iterator = true;
2742 _M_match._M_begin = _M_begin;
2743 }
2744 else
2745 _M_match = value_type();
2746 }
2747 return *this;
2748 }
2749
2750 typedef regex_iterator<const char*> cregex_iterator;
2751 typedef regex_iterator<string::const_iterator> sregex_iterator;
2752 #ifdef _GLIBCXX_USE_WCHAR_T
2753 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2754 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2755 #endif
2756
2757 // [7.12.2] Class template regex_token_iterator
2758 /**
2759 * Iterates over submatches in a range (or @a splits a text string).
2760 *
2761 * The purpose of this iterator is to enumerate all, or all specified,
2762 * matches of a regular expression within a text range. The dereferenced
2763 * value of an iterator of this class is a std::sub_match object.
2764 */
2765 template<typename _Bi_iter,
2766 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2767 typename _Rx_traits = regex_traits<_Ch_type> >
2768 class regex_token_iterator
2769 {
2770 public:
2771 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2772 typedef sub_match<_Bi_iter> value_type;
2773 typedef std::ptrdiff_t difference_type;
2774 typedef const value_type* pointer;
2775 typedef const value_type& reference;
2776 typedef std::forward_iterator_tag iterator_category;
2777
2778 public:
2779 /**
2780 * @brief Default constructs a %regex_token_iterator.
2781 *
2782 * A default-constructed %regex_token_iterator is a singular iterator
2783 * that will compare equal to the one-past-the-end value for any
2784 * iterator of the same type.
2785 */
2786 regex_token_iterator()
2787 : _M_position(), _M_result(nullptr), _M_suffix(), _M_n(0), _M_subs()
2788 { }
2789
2790 /**
2791 * Constructs a %regex_token_iterator...
2792 * @param __a [IN] The start of the text to search.
2793 * @param __b [IN] One-past-the-end of the text to search.
2794 * @param __re [IN] The regular expression to search for.
2795 * @param __submatch [IN] Which submatch to return. There are some
2796 * special values for this parameter:
2797 * - -1 each enumerated subexpression does NOT
2798 * match the regular expression (aka field
2799 * splitting)
2800 * - 0 the entire string matching the
2801 * subexpression is returned for each match
2802 * within the text.
2803 * - >0 enumerates only the indicated
2804 * subexpression from a match within the text.
2805 * @param __m [IN] Policy flags for match rules.
2806 */
2807 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2808 int __submatch = 0,
2809 regex_constants::match_flag_type __m
2810 = regex_constants::match_default)
2811 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2812 { _M_init(__a, __b); }
2813
2814 /**
2815 * Constructs a %regex_token_iterator...
2816 * @param __a [IN] The start of the text to search.
2817 * @param __b [IN] One-past-the-end of the text to search.
2818 * @param __re [IN] The regular expression to search for.
2819 * @param __submatches [IN] A list of subexpressions to return for each
2820 * regular expression match within the text.
2821 * @param __m [IN] Policy flags for match rules.
2822 */
2823 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2824 const regex_type& __re,
2825 const std::vector<int>& __submatches,
2826 regex_constants::match_flag_type __m
2827 = regex_constants::match_default)
2828 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2829 { _M_init(__a, __b); }
2830
2831 /**
2832 * Constructs a %regex_token_iterator...
2833 * @param __a [IN] The start of the text to search.
2834 * @param __b [IN] One-past-the-end of the text to search.
2835 * @param __re [IN] The regular expression to search for.
2836 * @param __submatches [IN] A list of subexpressions to return for each
2837 * regular expression match within the text.
2838 * @param __m [IN] Policy flags for match rules.
2839 */
2840 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2841 const regex_type& __re,
2842 initializer_list<int> __submatches,
2843 regex_constants::match_flag_type __m
2844 = regex_constants::match_default)
2845 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2846 { _M_init(__a, __b); }
2847
2848 /**
2849 * Constructs a %regex_token_iterator...
2850 * @param __a [IN] The start of the text to search.
2851 * @param __b [IN] One-past-the-end of the text to search.
2852 * @param __re [IN] The regular expression to search for.
2853 * @param __submatches [IN] A list of subexpressions to return for each
2854 * regular expression match within the text.
2855 * @param __m [IN] Policy flags for match rules.
2856 */
2857 template<std::size_t _Nm>
2858 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2859 const regex_type& __re,
2860 const int (&__submatches)[_Nm],
2861 regex_constants::match_flag_type __m
2862 = regex_constants::match_default)
2863 : _M_position(__a, __b, __re, __m),
2864 _M_subs(__submatches, *(&__submatches+1)), _M_n(0)
2865 { _M_init(__a, __b); }
2866
2867 /**
2868 * @brief Copy constructs a %regex_token_iterator.
2869 * @param __rhs [IN] A %regex_token_iterator to copy.
2870 */
2871 regex_token_iterator(const regex_token_iterator& __rhs)
2872 : _M_position(__rhs.position), _M_subs(__rhs.subs), _M_n(__rhs.N),
2873 _M_result(__rhs.result), _M_suffix(__rhs.suffix),
2874 _M_has_m1(__rhs._M_has_m1)
2875 {
2876 if (__rhs._M_result == &__rhs._M_suffix)
2877 _M_result = &_M_suffix;
2878 }
2879
2880 /**
2881 * @brief Assigns a %regex_token_iterator to another.
2882 * @param __rhs [IN] A %regex_token_iterator to copy.
2883 */
2884 regex_token_iterator&
2885 operator=(const regex_token_iterator& __rhs);
2886
2887 /**
2888 * @brief Compares a %regex_token_iterator to another for equality.
2889 */
2890 bool
2891 operator==(const regex_token_iterator& __rhs) const;
2892
2893 /**
2894 * @brief Compares a %regex_token_iterator to another for inequality.
2895 */
2896 bool
2897 operator!=(const regex_token_iterator& __rhs) const
2898 { return !(*this == __rhs); }
2899
2900 /**
2901 * @brief Dereferences a %regex_token_iterator.
2902 */
2903 const value_type&
2904 operator*() const
2905 { return *_M_result; }
2906
2907 /**
2908 * @brief Selects a %regex_token_iterator member.
2909 */
2910 const value_type*
2911 operator->() const
2912 { return _M_result; }
2913
2914 /**
2915 * @brief Increments a %regex_token_iterator.
2916 */
2917 regex_token_iterator&
2918 operator++();
2919
2920 /**
2921 * @brief Postincrements a %regex_token_iterator.
2922 */
2923 regex_token_iterator
2924 operator++(int)
2925 {
2926 auto __tmp = *this;
2927 ++(*this);
2928 return __tmp;
2929 }
2930
2931 private:
2932 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2933
2934 void
2935 _M_init(_Bi_iter __a, _Bi_iter __b);
2936
2937 const value_type&
2938 _M_current_match() const
2939 {
2940 if (_M_subs[_M_n] == -1)
2941 return (*_M_position).prefix();
2942 else
2943 return (*_M_position)[_M_subs[_M_n]];
2944 }
2945
2946 constexpr bool
2947 _M_end_of_seq()
2948 { return _M_result == nullptr; }
2949
2950 _Position _M_position;
2951 const value_type* _M_result;
2952 value_type _M_suffix;
2953 std::size_t _M_n;
2954 std::vector<int> _M_subs;
2955
2956 // Show whether _M_subs contains -1
2957 bool _M_has_m1;
2958 };
2959
2960 template<typename _Bi_iter,
2961 typename _Ch_type,
2962 typename _Rx_traits>
2963 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
2964 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2965 operator=(const regex_token_iterator& __rhs)
2966 {
2967 _M_position = __rhs._M_position;
2968 _M_subs = __rhs._M_subs;
2969 _M_n = __rhs._M_n;
2970 _M_result = __rhs._M_result;
2971 _M_suffix = __rhs._M_suffix;
2972 _M_has_m1 = __rhs._M_has_m1;
2973 if (__rhs._M_result == &__rhs._M_suffix)
2974 _M_result = &_M_suffix;
2975 }
2976
2977 template<typename _Bi_iter,
2978 typename _Ch_type,
2979 typename _Rx_traits>
2980 bool
2981 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2982 operator==(const regex_token_iterator& __rhs) const
2983 {
2984 if (_M_end_of_seq() && __rhs._M_end_of_seq())
2985 return true;
2986 if (_M_suffix.matched && __rhs._M_suffix.matched
2987 && _M_suffix == __rhs._M_suffix)
2988 return true;
2989 if (_M_end_of_seq() || _M_suffix.matched
2990 || __rhs._M_end_of_seq() || __rhs._M_suffix.matched)
2991 return false;
2992 return _M_position == __rhs._M_position
2993 && _M_n == __rhs._M_n
2994 && _M_subs == __rhs._M_subs;
2995 }
2996
2997 template<typename _Bi_iter,
2998 typename _Ch_type,
2999 typename _Rx_traits>
3000 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
3001 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
3002 operator++()
3003 {
3004 _Position __prev = _M_position;
3005 if (_M_suffix.matched)
3006 *this = regex_token_iterator();
3007 else if (_M_n + 1 < _M_subs.size())
3008 {
3009 _M_n++;
3010 _M_result = &_M_current_match();
3011 }
3012 else
3013 {
3014 _M_n = 0;
3015 ++_M_position;
3016 if (_M_position != _Position())
3017 _M_result = &_M_current_match();
3018 else if (_M_has_m1 && __prev->suffix().length() != 0)
3019 {
3020 _M_suffix.matched = true;
3021 _M_suffix.first = __prev->suffix().first;
3022 _M_suffix.second = __prev->suffix().second;
3023 _M_result = &_M_suffix;
3024 }
3025 else
3026 *this = regex_token_iterator();
3027 }
3028 return *this;
3029 }
3030
3031 template<typename _Bi_iter,
3032 typename _Ch_type,
3033 typename _Rx_traits>
3034 void
3035 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
3036 _M_init(_Bi_iter __a, _Bi_iter __b)
3037 {
3038 _M_has_m1 = false;
3039 for (auto __it : _M_subs)
3040 if (__it == -1)
3041 {
3042 _M_has_m1 = true;
3043 break;
3044 }
3045 if (_M_position != _Position())
3046 _M_result = &_M_current_match();
3047 else if (_M_has_m1)
3048 {
3049 _M_suffix.matched = true;
3050 _M_suffix.first = __a;
3051 _M_suffix.second = __b;
3052 _M_result = &_M_suffix;
3053 }
3054 else
3055 _M_result = nullptr;
3056 }
3057
3058 /** @brief Token iterator for C-style NULL-terminated strings. */
3059 typedef regex_token_iterator<const char*> cregex_token_iterator;
3060
3061 /** @brief Token iterator for standard strings. */
3062 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
3063
3064 #ifdef _GLIBCXX_USE_WCHAR_T
3065 /** @brief Token iterator for C-style NULL-terminated wide strings. */
3066 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
3067
3068 /** @brief Token iterator for standard wide-character strings. */
3069 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
3070 #endif
3071
3072 //@} // group regex
3073 _GLIBCXX_END_NAMESPACE_VERSION
3074 } // namespace
3075