Makefile.am: Adjust to new files.
[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 __try
224 {
225 typedef std::ctype<char_type> __ctype_type;
226 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
227 std::vector<char_type> __v(__first, __last);
228 // FIXME : this is not entirely correct
229 __fctyp.tolower(&*__v.begin(), &*__v.end());
230 return this->transform(&*__v.begin(), &*__v.end());
231 }
232 __catch (std::bad_cast)
233 {
234 }
235 return string_type();
236 }
237
238 /**
239 * @brief Gets a collation element by name.
240 *
241 * @param __first beginning of the collation element name.
242 * @param __last one-past-the-end of the collation element name.
243 *
244 * @returns a sequence of one or more characters that represents the
245 * collating element consisting of the character sequence designated by
246 * the iterator range [__first, __last). Returns an empty string if the
247 * character sequence is not a valid collating element.
248 */
249 template<typename _Fwd_iter>
250 string_type
251 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
252
253 /**
254 * @brief Maps one or more characters to a named character
255 * classification.
256 *
257 * @param __first beginning of the character sequence.
258 * @param __last one-past-the-end of the character sequence.
259 * @param __icase ignores the case of the classification name.
260 *
261 * @returns an unspecified value that represents the character
262 * classification named by the character sequence designated by
263 * the iterator range [__first, __last). If @p icase is true,
264 * the returned mask identifies the classification regardless of
265 * the case of the characters to be matched (for example,
266 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
267 * case-dependent classification is returned. The value
268 * returned shall be independent of the case of the characters
269 * in the character sequence. If the name is not recognized then
270 * returns a value that compares equal to 0.
271 *
272 * At least the following names (or their wide-character equivalent) are
273 * supported.
274 * - d
275 * - w
276 * - s
277 * - alnum
278 * - alpha
279 * - blank
280 * - cntrl
281 * - digit
282 * - graph
283 * - lower
284 * - print
285 * - punct
286 * - space
287 * - upper
288 * - xdigit
289 */
290 template<typename _Fwd_iter>
291 char_class_type
292 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
293 bool __icase = false) const;
294
295 /**
296 * @brief Determines if @p c is a member of an identified class.
297 *
298 * @param __c a character.
299 * @param __f a class type (as returned from lookup_classname).
300 *
301 * @returns true if the character @p __c is a member of the classification
302 * represented by @p __f, false otherwise.
303 *
304 * @throws std::bad_cast if the current locale does not have a ctype
305 * facet.
306 */
307 bool
308 isctype(_Ch_type __c, char_class_type __f) const;
309
310 /**
311 * @brief Converts a digit to an int.
312 *
313 * @param __ch a character representing a digit.
314 * @param __radix the radix if the numeric conversion (limited to 8, 10,
315 * or 16).
316 *
317 * @returns the value represented by the digit __ch in base radix if the
318 * character __ch is a valid digit in base radix; otherwise returns -1.
319 */
320 int
321 value(_Ch_type __ch, int __radix) const;
322
323 /**
324 * @brief Imbues the regex_traits object with a copy of a new locale.
325 *
326 * @param __loc A locale.
327 *
328 * @returns a copy of the previous locale in use by the regex_traits
329 * object.
330 *
331 * @note Calling imbue with a different locale than the one currently in
332 * use invalidates all cached data held by *this.
333 */
334 locale_type
335 imbue(locale_type __loc)
336 {
337 std::swap(_M_locale, __loc);
338 return __loc;
339 }
340
341 /**
342 * @brief Gets a copy of the current locale in use by the regex_traits
343 * object.
344 */
345 locale_type
346 getloc() const
347 { return _M_locale; }
348
349 protected:
350 locale_type _M_locale;
351 };
352
353 template<typename _Ch_type>
354 template<typename _Fwd_iter>
355 typename regex_traits<_Ch_type>::string_type
356 regex_traits<_Ch_type>::
357 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
358 {
359 typedef std::ctype<char_type> __ctype_type;
360 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
361
362 static const char* __collatenames[] =
363 {
364 "NUL",
365 "SOH",
366 "STX",
367 "ETX",
368 "EOT",
369 "ENQ",
370 "ACK",
371 "alert",
372 "backspace",
373 "tab",
374 "newline",
375 "vertical-tab",
376 "form-feed",
377 "carriage-return",
378 "SO",
379 "SI",
380 "DLE",
381 "DC1",
382 "DC2",
383 "DC3",
384 "DC4",
385 "NAK",
386 "SYN",
387 "ETB",
388 "CAN",
389 "EM",
390 "SUB",
391 "ESC",
392 "IS4",
393 "IS3",
394 "IS2",
395 "IS1",
396 "space",
397 "exclamation-mark",
398 "quotation-mark",
399 "number-sign",
400 "dollar-sign",
401 "percent-sign",
402 "ampersand",
403 "apostrophe",
404 "left-parenthesis",
405 "right-parenthesis",
406 "asterisk",
407 "plus-sign",
408 "comma",
409 "hyphen",
410 "period",
411 "slash",
412 "zero",
413 "one",
414 "two",
415 "three",
416 "four",
417 "five",
418 "six",
419 "seven",
420 "eight",
421 "nine",
422 "colon",
423 "semicolon",
424 "less-than-sign",
425 "equals-sign",
426 "greater-than-sign",
427 "question-mark",
428 "commercial-at",
429 "A",
430 "B",
431 "C",
432 "D",
433 "E",
434 "F",
435 "G",
436 "H",
437 "I",
438 "J",
439 "K",
440 "L",
441 "M",
442 "N",
443 "O",
444 "P",
445 "Q",
446 "R",
447 "S",
448 "T",
449 "U",
450 "V",
451 "W",
452 "X",
453 "Y",
454 "Z",
455 "left-square-bracket",
456 "backslash",
457 "right-square-bracket",
458 "circumflex",
459 "underscore",
460 "grave-accent",
461 "a",
462 "b",
463 "c",
464 "d",
465 "e",
466 "f",
467 "g",
468 "h",
469 "i",
470 "j",
471 "k",
472 "l",
473 "m",
474 "n",
475 "o",
476 "p",
477 "q",
478 "r",
479 "s",
480 "t",
481 "u",
482 "v",
483 "w",
484 "x",
485 "y",
486 "z",
487 "left-curly-bracket",
488 "vertical-line",
489 "right-curly-bracket",
490 "tilde",
491 "DEL",
492 ""
493 };
494
495 // same as boost
496 static const char* __digraphs[] =
497 {
498 "ae",
499 "Ae",
500 "AE",
501 "ch",
502 "Ch",
503 "CH",
504 "ll",
505 "Ll",
506 "LL",
507 "ss",
508 "Ss",
509 "SS",
510 "nj",
511 "Nj",
512 "NJ",
513 "dz",
514 "Dz",
515 "DZ",
516 "lj",
517 "Lj",
518 "LJ",
519 ""
520 };
521
522 std::string __s(__last - __first, '?');
523 __fctyp.narrow(__first, __last, '?', &*__s.begin());
524
525 for (unsigned int __i = 0; *__collatenames[__i]; __i++)
526 if (__s == __collatenames[__i])
527 return string_type(1, __fctyp.widen((char)__i));
528
529 for (unsigned int __i = 0; *__digraphs[__i]; __i++)
530 {
531 const char* __now = __digraphs[__i];
532 if (__s == __now)
533 {
534 string_type ret(__s.size(), __fctyp.widen('?'));
535 __fctyp.widen(__now, __now + 2/* ouch */, &*ret.begin());
536 return ret;
537 }
538 }
539 return string_type();
540 }
541
542 template<typename _Ch_type>
543 template<typename _Fwd_iter>
544 typename regex_traits<_Ch_type>::char_class_type
545 regex_traits<_Ch_type>::
546 lookup_classname(_Fwd_iter __first, _Fwd_iter __last, bool __icase) const
547 {
548 typedef std::ctype<char_type> __ctype_type;
549 typedef std::ctype<char> __cctype_type;
550 typedef const pair<const char*, char_class_type> _ClassnameEntry;
551 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
552 const __cctype_type& __cctyp(use_facet<__cctype_type>(_M_locale));
553
554 static _ClassnameEntry __classnames[] =
555 {
556 {"d", ctype_base::digit},
557 {"w", {ctype_base::alnum, _RegexMask::_S_under}},
558 {"s", ctype_base::space},
559 {"alnum", ctype_base::alnum},
560 {"alpha", ctype_base::alpha},
561 {"blank", {0, _RegexMask::_S_blank}},
562 {"cntrl", ctype_base::cntrl},
563 {"digit", ctype_base::digit},
564 {"graph", ctype_base::graph},
565 {"lower", ctype_base::lower},
566 {"print", ctype_base::print},
567 {"punct", ctype_base::punct},
568 {"space", ctype_base::space},
569 {"upper", ctype_base::upper},
570 {"xdigit", ctype_base::xdigit},
571 };
572
573 std::string __s(__last - __first, '?');
574 __fctyp.narrow(__first, __last, '?', &__s[0]);
575 __cctyp.tolower(&*__s.begin(), &*__s.end());
576 for (_ClassnameEntry* __it = __classnames;
577 __it < *(&__classnames + 1);
578 ++__it)
579 {
580 if (__s == __it->first)
581 {
582 if (__icase
583 && ((__it->second
584 & (ctype_base::lower | ctype_base::upper)) != 0))
585 return ctype_base::alpha;
586 return __it->second;
587 }
588 }
589 return 0;
590 }
591
592 template<typename _Ch_type>
593 bool
594 regex_traits<_Ch_type>::
595 isctype(_Ch_type __c, char_class_type __f) const
596 {
597 typedef std::ctype<char_type> __ctype_type;
598 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
599
600 return __fctyp.is(__f._M_base, __c)
601 // [[:w:]]
602 || ((__f._M_extended & _RegexMask::_S_under)
603 && __c == __fctyp.widen('_'))
604 // [[:blank:]]
605 || ((__f._M_extended & _RegexMask::_S_blank)
606 && (__c == __fctyp.widen(' ')
607 || __c == __fctyp.widen('\t')));
608 }
609
610 template<typename _Ch_type>
611 int
612 regex_traits<_Ch_type>::
613 value(_Ch_type __ch, int __radix) const
614 {
615 std::basic_istringstream<char_type> __is(string_type(1, __ch));
616 int __v;
617 if (__radix == 8)
618 __is >> std::oct;
619 else if (__radix == 16)
620 __is >> std::hex;
621 __is >> __v;
622 return __is.fail() ? -1 : __v;
623 }
624
625 // [7.8] Class basic_regex
626 /**
627 * Objects of specializations of this class represent regular expressions
628 * constructed from sequences of character type @p _Ch_type.
629 *
630 * Storage for the regular expression is allocated and deallocated as
631 * necessary by the member functions of this class.
632 */
633 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type> >
634 class basic_regex
635 {
636 public:
637 // types:
638 typedef _Ch_type value_type;
639 typedef _Rx_traits traits_type;
640 typedef typename traits_type::string_type string_type;
641 typedef regex_constants::syntax_option_type flag_type;
642 typedef typename traits_type::locale_type locale_type;
643
644 /**
645 * @name Constants
646 * std [28.8.1](1)
647 */
648 //@{
649 static constexpr flag_type icase = regex_constants::icase;
650 static constexpr flag_type nosubs = regex_constants::nosubs;
651 static constexpr flag_type optimize = regex_constants::optimize;
652 static constexpr flag_type collate = regex_constants::collate;
653 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
654 static constexpr flag_type basic = regex_constants::basic;
655 static constexpr flag_type extended = regex_constants::extended;
656 static constexpr flag_type awk = regex_constants::awk;
657 static constexpr flag_type grep = regex_constants::grep;
658 static constexpr flag_type egrep = regex_constants::egrep;
659 //@}
660
661 // [7.8.2] construct/copy/destroy
662 /**
663 * Constructs a basic regular expression that does not match any
664 * character sequence.
665 */
666 basic_regex()
667 : _M_flags(ECMAScript), _M_automaton(nullptr)
668 { }
669
670 /**
671 * @brief Constructs a basic regular expression from the
672 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
673 * interpreted according to the flags in @p __f.
674 *
675 * @param __p A pointer to the start of a C-style null-terminated string
676 * containing a regular expression.
677 * @param __f Flags indicating the syntax rules and options.
678 *
679 * @throws regex_error if @p __p is not a valid regular expression.
680 */
681 explicit
682 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
683 : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
684 { }
685
686 /**
687 * @brief Constructs a basic regular expression from the sequence
688 * [p, p + len) interpreted according to the flags in @p f.
689 *
690 * @param __p A pointer to the start of a string containing a regular
691 * expression.
692 * @param __len The length of the string containing the regular
693 * expression.
694 * @param __f Flags indicating the syntax rules and options.
695 *
696 * @throws regex_error if @p __p is not a valid regular expression.
697 */
698 basic_regex(const _Ch_type* __p,
699 std::size_t __len, flag_type __f = ECMAScript)
700 : basic_regex(__p, __p + __len, __f)
701 { }
702
703 /**
704 * @brief Copy-constructs a basic regular expression.
705 *
706 * @param __rhs A @p regex object.
707 */
708 basic_regex(const basic_regex& __rhs) = default;
709
710 /**
711 * @brief Move-constructs a basic regular expression.
712 *
713 * @param __rhs A @p regex object.
714 */
715 basic_regex(const basic_regex&& __rhs) noexcept
716 : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
717 _M_automaton(std::move(__rhs._M_automaton))
718 { }
719
720 /**
721 * @brief Constructs a basic regular expression from the string
722 * @p s interpreted according to the flags in @p f.
723 *
724 * @param __s A string containing a regular expression.
725 * @param __f Flags indicating the syntax rules and options.
726 *
727 * @throws regex_error if @p __s is not a valid regular expression.
728 */
729 template<typename _Ch_traits, typename _Ch_alloc>
730 explicit
731 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
732 _Ch_alloc>& __s,
733 flag_type __f = ECMAScript)
734 : basic_regex(__s.begin(), __s.end(), __f)
735 { }
736
737 /**
738 * @brief Constructs a basic regular expression from the range
739 * [first, last) interpreted according to the flags in @p f.
740 *
741 * @param __first The start of a range containing a valid regular
742 * expression.
743 * @param __last The end of a range containing a valid regular
744 * expression.
745 * @param __f The format flags of the regular expression.
746 *
747 * @throws regex_error if @p [__first, __last) is not a valid regular
748 * expression.
749 */
750 template<typename _InputIterator>
751 basic_regex(_InputIterator __first, _InputIterator __last,
752 flag_type __f = ECMAScript)
753 : _M_flags(__f),
754 _M_automaton(__detail::_Compiler<_InputIterator, _Ch_type, _Rx_traits>
755 (__first, __last, _M_traits, _M_flags)._M_get_nfa())
756 { }
757
758 /**
759 * @brief Constructs a basic regular expression from an initializer list.
760 *
761 * @param __l The initializer list.
762 * @param __f The format flags of the regular expression.
763 *
764 * @throws regex_error if @p __l is not a valid regular expression.
765 */
766 basic_regex(initializer_list<_Ch_type> __l,
767 flag_type __f = ECMAScript)
768 : basic_regex(__l.begin(), __l.end(), __f)
769 { }
770
771 /**
772 * @brief Destroys a basic regular expression.
773 */
774 ~basic_regex()
775 { }
776
777 /**
778 * @brief Assigns one regular expression to another.
779 */
780 basic_regex&
781 operator=(const basic_regex& __rhs)
782 { return this->assign(__rhs); }
783
784 /**
785 * @brief Move-assigns one regular expression to another.
786 */
787 basic_regex&
788 operator=(basic_regex&& __rhs) noexcept
789 { return this->assign(std::move(__rhs)); }
790
791 /**
792 * @brief Replaces a regular expression with a new one constructed from
793 * a C-style null-terminated string.
794 *
795 * @param __p A pointer to the start of a null-terminated C-style string
796 * containing a regular expression.
797 */
798 basic_regex&
799 operator=(const _Ch_type* __p)
800 { return this->assign(__p, flags()); }
801
802 /**
803 * @brief Replaces a regular expression with a new one constructed from
804 * a string.
805 *
806 * @param __s A pointer to a string containing a regular expression.
807 */
808 template<typename _Ch_typeraits, typename _Alloc>
809 basic_regex&
810 operator=(const basic_string<_Ch_type, _Ch_typeraits, _Alloc>& __s)
811 { return this->assign(__s, flags()); }
812
813 // [7.8.3] assign
814 /**
815 * @brief the real assignment operator.
816 *
817 * @param __rhs Another regular expression object.
818 */
819 basic_regex&
820 assign(const basic_regex& __rhs)
821 {
822 basic_regex __tmp(__rhs);
823 this->swap(__tmp);
824 return *this;
825 }
826
827 /**
828 * @brief The move-assignment operator.
829 *
830 * @param __rhs Another regular expression object.
831 */
832 basic_regex&
833 assign(basic_regex&& __rhs) noexcept
834 {
835 basic_regex __tmp(std::move(__rhs));
836 this->swap(__tmp);
837 return *this;
838 }
839
840 /**
841 * @brief Assigns a new regular expression to a regex object from a
842 * C-style null-terminated string containing a regular expression
843 * pattern.
844 *
845 * @param __p A pointer to a C-style null-terminated string containing
846 * a regular expression pattern.
847 * @param __flags Syntax option flags.
848 *
849 * @throws regex_error if __p does not contain a valid regular
850 * expression pattern interpreted according to @p __flags. If
851 * regex_error is thrown, *this remains unchanged.
852 */
853 basic_regex&
854 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
855 { return this->assign(string_type(__p), __flags); }
856
857 /**
858 * @brief Assigns a new regular expression to a regex object from a
859 * C-style string containing a regular expression pattern.
860 *
861 * @param __p A pointer to a C-style string containing a
862 * regular expression pattern.
863 * @param __len The length of the regular expression pattern string.
864 * @param __flags Syntax option flags.
865 *
866 * @throws regex_error if p does not contain a valid regular
867 * expression pattern interpreted according to @p __flags. If
868 * regex_error is thrown, *this remains unchanged.
869 */
870 basic_regex&
871 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
872 { return this->assign(string_type(__p, __len), __flags); }
873
874 /**
875 * @brief Assigns a new regular expression to a regex object from a
876 * string containing a regular expression pattern.
877 *
878 * @param __s A string containing a regular expression pattern.
879 * @param __flags Syntax option flags.
880 *
881 * @throws regex_error if __s does not contain a valid regular
882 * expression pattern interpreted according to @p __flags. If
883 * regex_error is thrown, *this remains unchanged.
884 */
885 template<typename _Ch_typeraits, typename _Alloc>
886 basic_regex&
887 assign(const basic_string<_Ch_type, _Ch_typeraits, _Alloc>& __s,
888 flag_type __flags = ECMAScript)
889 {
890 basic_regex __tmp(__s, __flags);
891 this->swap(__tmp);
892 return *this;
893 }
894
895 /**
896 * @brief Assigns a new regular expression to a regex object.
897 *
898 * @param __first The start of a range containing a valid regular
899 * expression.
900 * @param __last The end of a range containing a valid regular
901 * expression.
902 * @param __flags Syntax option flags.
903 *
904 * @throws regex_error if p does not contain a valid regular
905 * expression pattern interpreted according to @p __flags. If
906 * regex_error is thrown, the object remains unchanged.
907 */
908 template<typename _InputIterator>
909 basic_regex&
910 assign(_InputIterator __first, _InputIterator __last,
911 flag_type __flags = ECMAScript)
912 { return this->assign(string_type(__first, __last), __flags); }
913
914 /**
915 * @brief Assigns a new regular expression to a regex object.
916 *
917 * @param __l An initializer list representing a regular expression.
918 * @param __flags Syntax option flags.
919 *
920 * @throws regex_error if @p __l does not contain a valid
921 * regular expression pattern interpreted according to @p
922 * __flags. If regex_error is thrown, the object remains
923 * unchanged.
924 */
925 basic_regex&
926 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
927 { return this->assign(__l.begin(), __l.end(), __flags); }
928
929 // [7.8.4] const operations
930 /**
931 * @brief Gets the number of marked subexpressions within the regular
932 * expression.
933 */
934 unsigned int
935 mark_count() const
936 { return _M_automaton->_M_sub_count() - 1; }
937
938 /**
939 * @brief Gets the flags used to construct the regular expression
940 * or in the last call to assign().
941 */
942 flag_type
943 flags() const
944 { return _M_flags; }
945
946 // [7.8.5] locale
947 /**
948 * @brief Imbues the regular expression object with the given locale.
949 *
950 * @param __loc A locale.
951 */
952 locale_type
953 imbue(locale_type __loc)
954 { return _M_traits.imbue(__loc); }
955
956 /**
957 * @brief Gets the locale currently imbued in the regular expression
958 * object.
959 */
960 locale_type
961 getloc() const
962 { return _M_traits.getloc(); }
963
964 // [7.8.6] swap
965 /**
966 * @brief Swaps the contents of two regular expression objects.
967 *
968 * @param __rhs Another regular expression object.
969 */
970 void
971 swap(basic_regex& __rhs)
972 {
973 std::swap(_M_flags, __rhs._M_flags);
974 std::swap(_M_traits, __rhs._M_traits);
975 std::swap(_M_automaton, __rhs._M_automaton);
976 }
977
978 #ifdef _GLIBCXX_DEBUG
979 void
980 _M_dot(std::ostream& __ostr)
981 { _M_automaton->_M_dot(__ostr); }
982 #endif
983
984 protected:
985 typedef std::shared_ptr<__detail::_Automaton<_Ch_type, _Rx_traits>>
986 _AutomatonPtr;
987
988 template<typename _BiIter, typename _Alloc,
989 typename _CharT, typename _TraitsT>
990 friend std::unique_ptr<
991 __detail::_Executor<_BiIter, _Alloc, _CharT, _TraitsT>>
992 __detail::__get_executor(_BiIter,
993 _BiIter,
994 match_results<_BiIter, _Alloc>&,
995 const basic_regex<_CharT, _TraitsT>&,
996 regex_constants::match_flag_type);
997
998 template<typename _B, typename _A, typename _C, typename _R>
999 friend bool
1000 regex_match(_B, _B,
1001 match_results<_B, _A>&,
1002 const basic_regex<_C, _R>&,
1003 regex_constants::match_flag_type);
1004
1005 template<typename _B, typename _A, typename _C, typename _R>
1006 friend bool
1007 regex_search(_B, _B,
1008 match_results<_B, _A>&,
1009 const basic_regex<_C, _R>&,
1010 regex_constants::match_flag_type);
1011
1012 flag_type _M_flags;
1013 _Rx_traits _M_traits;
1014 _AutomatonPtr _M_automaton;
1015 };
1016
1017 /** @brief Standard regular expressions. */
1018 typedef basic_regex<char> regex;
1019
1020 #ifdef _GLIBCXX_USE_WCHAR_T
1021 /** @brief Standard wide-character regular expressions. */
1022 typedef basic_regex<wchar_t> wregex;
1023 #endif
1024
1025
1026 // [7.8.6] basic_regex swap
1027 /**
1028 * @brief Swaps the contents of two regular expression objects.
1029 * @param __lhs First regular expression.
1030 * @param __rhs Second regular expression.
1031 */
1032 template<typename _Ch_type, typename _Rx_traits>
1033 inline void
1034 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
1035 basic_regex<_Ch_type, _Rx_traits>& __rhs)
1036 { __lhs.swap(__rhs); }
1037
1038
1039 // [7.9] Class template sub_match
1040 /**
1041 * A sequence of characters matched by a particular marked sub-expression.
1042 *
1043 * An object of this class is essentially a pair of iterators marking a
1044 * matched subexpression within a regular expression pattern match. Such
1045 * objects can be converted to and compared with std::basic_string objects
1046 * of a similar base character type as the pattern matched by the regular
1047 * expression.
1048 *
1049 * The iterators that make up the pair are the usual half-open interval
1050 * referencing the actual original pattern matched.
1051 */
1052 template<typename _BiIter>
1053 class sub_match : public std::pair<_BiIter, _BiIter>
1054 {
1055 typedef iterator_traits<_BiIter> __iter_traits;
1056
1057 public:
1058 typedef typename __iter_traits::value_type value_type;
1059 typedef typename __iter_traits::difference_type difference_type;
1060 typedef _BiIter iterator;
1061 typedef std::basic_string<value_type> string_type;
1062
1063 bool matched;
1064
1065 constexpr sub_match() : matched() { }
1066
1067 /**
1068 * Gets the length of the matching sequence.
1069 */
1070 difference_type
1071 length() const
1072 { return this->matched ? std::distance(this->first, this->second) : 0; }
1073
1074 /**
1075 * @brief Gets the matching sequence as a string.
1076 *
1077 * @returns the matching sequence as a string.
1078 *
1079 * This is the implicit conversion operator. It is identical to the
1080 * str() member function except that it will want to pop up in
1081 * unexpected places and cause a great deal of confusion and cursing
1082 * from the unwary.
1083 */
1084 operator string_type() const
1085 {
1086 return this->matched
1087 ? string_type(this->first, this->second)
1088 : string_type();
1089 }
1090
1091 /**
1092 * @brief Gets the matching sequence as a string.
1093 *
1094 * @returns the matching sequence as a string.
1095 */
1096 string_type
1097 str() const
1098 {
1099 return this->matched
1100 ? string_type(this->first, this->second)
1101 : string_type();
1102 }
1103
1104 /**
1105 * @brief Compares this and another matched sequence.
1106 *
1107 * @param __s Another matched sequence to compare to this one.
1108 *
1109 * @retval <0 this matched sequence will collate before @p __s.
1110 * @retval =0 this matched sequence is equivalent to @p __s.
1111 * @retval <0 this matched sequence will collate after @p __s.
1112 */
1113 int
1114 compare(const sub_match& __s) const
1115 { return this->str().compare(__s.str()); }
1116
1117 /**
1118 * @brief Compares this sub_match to a string.
1119 *
1120 * @param __s A string to compare to this sub_match.
1121 *
1122 * @retval <0 this matched sequence will collate before @p __s.
1123 * @retval =0 this matched sequence is equivalent to @p __s.
1124 * @retval <0 this matched sequence will collate after @p __s.
1125 */
1126 int
1127 compare(const string_type& __s) const
1128 { return this->str().compare(__s); }
1129
1130 /**
1131 * @brief Compares this sub_match to a C-style string.
1132 *
1133 * @param __s A C-style string to compare to this sub_match.
1134 *
1135 * @retval <0 this matched sequence will collate before @p __s.
1136 * @retval =0 this matched sequence is equivalent to @p __s.
1137 * @retval <0 this matched sequence will collate after @p __s.
1138 */
1139 int
1140 compare(const value_type* __s) const
1141 { return this->str().compare(__s); }
1142 };
1143
1144
1145 /** @brief Standard regex submatch over a C-style null-terminated string. */
1146 typedef sub_match<const char*> csub_match;
1147
1148 /** @brief Standard regex submatch over a standard string. */
1149 typedef sub_match<string::const_iterator> ssub_match;
1150
1151 #ifdef _GLIBCXX_USE_WCHAR_T
1152 /** @brief Regex submatch over a C-style null-terminated wide string. */
1153 typedef sub_match<const wchar_t*> wcsub_match;
1154
1155 /** @brief Regex submatch over a standard wide string. */
1156 typedef sub_match<wstring::const_iterator> wssub_match;
1157 #endif
1158
1159 // [7.9.2] sub_match non-member operators
1160
1161 /**
1162 * @brief Tests the equivalence of two regular expression submatches.
1163 * @param __lhs First regular expression submatch.
1164 * @param __rhs Second regular expression submatch.
1165 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1166 */
1167 template<typename _BiIter>
1168 inline bool
1169 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1170 { return __lhs.compare(__rhs) == 0; }
1171
1172 /**
1173 * @brief Tests the inequivalence of two regular expression submatches.
1174 * @param __lhs First regular expression submatch.
1175 * @param __rhs Second regular expression submatch.
1176 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1177 */
1178 template<typename _BiIter>
1179 inline bool
1180 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1181 { return __lhs.compare(__rhs) != 0; }
1182
1183 /**
1184 * @brief Tests the ordering of two regular expression submatches.
1185 * @param __lhs First regular expression submatch.
1186 * @param __rhs Second regular expression submatch.
1187 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1188 */
1189 template<typename _BiIter>
1190 inline bool
1191 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1192 { return __lhs.compare(__rhs) < 0; }
1193
1194 /**
1195 * @brief Tests the ordering of two regular expression submatches.
1196 * @param __lhs First regular expression submatch.
1197 * @param __rhs Second regular expression submatch.
1198 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1199 */
1200 template<typename _BiIter>
1201 inline bool
1202 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1203 { return __lhs.compare(__rhs) <= 0; }
1204
1205 /**
1206 * @brief Tests the ordering of two regular expression submatches.
1207 * @param __lhs First regular expression submatch.
1208 * @param __rhs Second regular expression submatch.
1209 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1210 */
1211 template<typename _BiIter>
1212 inline bool
1213 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1214 { return __lhs.compare(__rhs) >= 0; }
1215
1216 /**
1217 * @brief Tests the ordering of two regular expression submatches.
1218 * @param __lhs First regular expression submatch.
1219 * @param __rhs Second regular expression submatch.
1220 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1221 */
1222 template<typename _BiIter>
1223 inline bool
1224 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1225 { return __lhs.compare(__rhs) > 0; }
1226
1227 // Alias for sub_match'd string.
1228 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1229 using __sub_match_string = basic_string<
1230 typename iterator_traits<_Bi_iter>::value_type,
1231 _Ch_traits, _Ch_alloc>;
1232
1233 /**
1234 * @brief Tests the equivalence of a string and a regular expression
1235 * submatch.
1236 * @param __lhs A string.
1237 * @param __rhs A regular expression submatch.
1238 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1239 */
1240 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1241 inline bool
1242 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1243 const sub_match<_Bi_iter>& __rhs)
1244 { return __rhs.compare(__lhs.c_str()) == 0; }
1245
1246 /**
1247 * @brief Tests the inequivalence of a string and a regular expression
1248 * submatch.
1249 * @param __lhs A string.
1250 * @param __rhs A regular expression submatch.
1251 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1252 */
1253 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1254 inline bool
1255 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1256 const sub_match<_Bi_iter>& __rhs)
1257 { return !(__lhs == __rhs); }
1258
1259 /**
1260 * @brief Tests the ordering of a string and a regular expression submatch.
1261 * @param __lhs A string.
1262 * @param __rhs A regular expression submatch.
1263 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1264 */
1265 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1266 inline bool
1267 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1268 const sub_match<_Bi_iter>& __rhs)
1269 { return __rhs.compare(__lhs.c_str()) > 0; }
1270
1271 /**
1272 * @brief Tests the ordering of a string and a regular expression submatch.
1273 * @param __lhs A string.
1274 * @param __rhs A regular expression submatch.
1275 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1276 */
1277 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1278 inline bool
1279 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1280 const sub_match<_Bi_iter>& __rhs)
1281 { return __rhs < __lhs; }
1282
1283 /**
1284 * @brief Tests the ordering of a string and a regular expression submatch.
1285 * @param __lhs A string.
1286 * @param __rhs A regular expression submatch.
1287 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1288 */
1289 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1290 inline bool
1291 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1292 const sub_match<_Bi_iter>& __rhs)
1293 { return !(__lhs < __rhs); }
1294
1295 /**
1296 * @brief Tests the ordering of a string and a regular expression submatch.
1297 * @param __lhs A string.
1298 * @param __rhs A regular expression submatch.
1299 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1300 */
1301 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1302 inline bool
1303 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1304 const sub_match<_Bi_iter>& __rhs)
1305 { return !(__rhs < __lhs); }
1306
1307 /**
1308 * @brief Tests the equivalence of a regular expression submatch and a
1309 * string.
1310 * @param __lhs A regular expression submatch.
1311 * @param __rhs A string.
1312 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1313 */
1314 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1315 inline bool
1316 operator==(const sub_match<_Bi_iter>& __lhs,
1317 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1318 { return __lhs.compare(__rhs.c_str()) == 0; }
1319
1320 /**
1321 * @brief Tests the inequivalence of a regular expression submatch and a
1322 * string.
1323 * @param __lhs A regular expression submatch.
1324 * @param __rhs A string.
1325 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1326 */
1327 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1328 inline bool
1329 operator!=(const sub_match<_Bi_iter>& __lhs,
1330 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1331 { return !(__lhs == __rhs); }
1332
1333 /**
1334 * @brief Tests the ordering of a regular expression submatch and a string.
1335 * @param __lhs A regular expression submatch.
1336 * @param __rhs A string.
1337 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1338 */
1339 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1340 inline bool
1341 operator<(const sub_match<_Bi_iter>& __lhs,
1342 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1343 { return __lhs.compare(__rhs.c_str()) < 0; }
1344
1345 /**
1346 * @brief Tests the ordering of a regular expression submatch and a string.
1347 * @param __lhs A regular expression submatch.
1348 * @param __rhs A string.
1349 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1350 */
1351 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1352 inline bool
1353 operator>(const sub_match<_Bi_iter>& __lhs,
1354 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1355 { return __rhs < __lhs; }
1356
1357 /**
1358 * @brief Tests the ordering of a regular expression submatch and a string.
1359 * @param __lhs A regular expression submatch.
1360 * @param __rhs A string.
1361 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1362 */
1363 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1364 inline bool
1365 operator>=(const sub_match<_Bi_iter>& __lhs,
1366 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1367 { return !(__lhs < __rhs); }
1368
1369 /**
1370 * @brief Tests the ordering of a regular expression submatch and a string.
1371 * @param __lhs A regular expression submatch.
1372 * @param __rhs A string.
1373 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1374 */
1375 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1376 inline bool
1377 operator<=(const sub_match<_Bi_iter>& __lhs,
1378 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1379 { return !(__rhs < __lhs); }
1380
1381 /**
1382 * @brief Tests the equivalence of a C string and a regular expression
1383 * submatch.
1384 * @param __lhs A C string.
1385 * @param __rhs A regular expression submatch.
1386 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1387 */
1388 template<typename _Bi_iter>
1389 inline bool
1390 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1391 const sub_match<_Bi_iter>& __rhs)
1392 { return __rhs.compare(__lhs) == 0; }
1393
1394 /**
1395 * @brief Tests the inequivalence of an iterator value and a regular
1396 * expression submatch.
1397 * @param __lhs A regular expression submatch.
1398 * @param __rhs A string.
1399 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1400 */
1401 template<typename _Bi_iter>
1402 inline bool
1403 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1404 const sub_match<_Bi_iter>& __rhs)
1405 { return !(__lhs == __rhs); }
1406
1407 /**
1408 * @brief Tests the ordering of a string and a regular expression submatch.
1409 * @param __lhs A string.
1410 * @param __rhs A regular expression submatch.
1411 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1412 */
1413 template<typename _Bi_iter>
1414 inline bool
1415 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1416 const sub_match<_Bi_iter>& __rhs)
1417 { return __rhs.compare(__lhs) > 0; }
1418
1419 /**
1420 * @brief Tests the ordering of a string and a regular expression submatch.
1421 * @param __lhs A string.
1422 * @param __rhs A regular expression submatch.
1423 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1424 */
1425 template<typename _Bi_iter>
1426 inline bool
1427 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1428 const sub_match<_Bi_iter>& __rhs)
1429 { return __rhs < __lhs; }
1430
1431 /**
1432 * @brief Tests the ordering of a string and a regular expression submatch.
1433 * @param __lhs A string.
1434 * @param __rhs A regular expression submatch.
1435 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1436 */
1437 template<typename _Bi_iter>
1438 inline bool
1439 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1440 const sub_match<_Bi_iter>& __rhs)
1441 { return !(__lhs < __rhs); }
1442
1443 /**
1444 * @brief Tests the ordering of a string and a regular expression submatch.
1445 * @param __lhs A string.
1446 * @param __rhs A regular expression submatch.
1447 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1448 */
1449 template<typename _Bi_iter>
1450 inline bool
1451 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1452 const sub_match<_Bi_iter>& __rhs)
1453 { return !(__rhs < __lhs); }
1454
1455 /**
1456 * @brief Tests the equivalence of a regular expression submatch and a
1457 * string.
1458 * @param __lhs A regular expression submatch.
1459 * @param __rhs A pointer to a string?
1460 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1461 */
1462 template<typename _Bi_iter>
1463 inline bool
1464 operator==(const sub_match<_Bi_iter>& __lhs,
1465 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1466 { return __lhs.compare(__rhs) == 0; }
1467
1468 /**
1469 * @brief Tests the inequivalence of a regular expression submatch and a
1470 * string.
1471 * @param __lhs A regular expression submatch.
1472 * @param __rhs A pointer to a string.
1473 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1474 */
1475 template<typename _Bi_iter>
1476 inline bool
1477 operator!=(const sub_match<_Bi_iter>& __lhs,
1478 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1479 { return !(__lhs == __rhs); }
1480
1481 /**
1482 * @brief Tests the ordering of a regular expression submatch and a string.
1483 * @param __lhs A regular expression submatch.
1484 * @param __rhs A string.
1485 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1486 */
1487 template<typename _Bi_iter>
1488 inline bool
1489 operator<(const sub_match<_Bi_iter>& __lhs,
1490 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1491 { return __lhs.compare(__rhs) < 0; }
1492
1493 /**
1494 * @brief Tests the ordering of a regular expression submatch and a string.
1495 * @param __lhs A regular expression submatch.
1496 * @param __rhs A string.
1497 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1498 */
1499 template<typename _Bi_iter>
1500 inline bool
1501 operator>(const sub_match<_Bi_iter>& __lhs,
1502 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1503 { return __rhs < __lhs; }
1504
1505 /**
1506 * @brief Tests the ordering of a regular expression submatch and a string.
1507 * @param __lhs A regular expression submatch.
1508 * @param __rhs A string.
1509 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1510 */
1511 template<typename _Bi_iter>
1512 inline bool
1513 operator>=(const sub_match<_Bi_iter>& __lhs,
1514 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1515 { return !(__lhs < __rhs); }
1516
1517 /**
1518 * @brief Tests the ordering of a regular expression submatch and a string.
1519 * @param __lhs A regular expression submatch.
1520 * @param __rhs A string.
1521 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1522 */
1523 template<typename _Bi_iter>
1524 inline bool
1525 operator<=(const sub_match<_Bi_iter>& __lhs,
1526 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1527 { return !(__rhs < __lhs); }
1528
1529 /**
1530 * @brief Tests the equivalence of a string and a regular expression
1531 * submatch.
1532 * @param __lhs A string.
1533 * @param __rhs A regular expression submatch.
1534 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1535 */
1536 template<typename _Bi_iter>
1537 inline bool
1538 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1539 const sub_match<_Bi_iter>& __rhs)
1540 {
1541 typedef typename sub_match<_Bi_iter>::string_type string_type;
1542 return __rhs.compare(string_type(1, __lhs)) == 0;
1543 }
1544
1545 /**
1546 * @brief Tests the inequivalence of a string and a regular expression
1547 * submatch.
1548 * @param __lhs A string.
1549 * @param __rhs A regular expression submatch.
1550 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1551 */
1552 template<typename _Bi_iter>
1553 inline bool
1554 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1555 const sub_match<_Bi_iter>& __rhs)
1556 { return !(__lhs == __rhs); }
1557
1558 /**
1559 * @brief Tests the ordering of a string and a regular expression submatch.
1560 * @param __lhs A string.
1561 * @param __rhs A regular expression submatch.
1562 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1563 */
1564 template<typename _Bi_iter>
1565 inline bool
1566 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1567 const sub_match<_Bi_iter>& __rhs)
1568 {
1569 typedef typename sub_match<_Bi_iter>::string_type string_type;
1570 return __rhs.compare(string_type(1, __lhs)) > 0;
1571 }
1572
1573 /**
1574 * @brief Tests the ordering of a string and a regular expression submatch.
1575 * @param __lhs A string.
1576 * @param __rhs A regular expression submatch.
1577 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1578 */
1579 template<typename _Bi_iter>
1580 inline bool
1581 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1582 const sub_match<_Bi_iter>& __rhs)
1583 { return __rhs < __lhs; }
1584
1585 /**
1586 * @brief Tests the ordering of a string and a regular expression submatch.
1587 * @param __lhs A string.
1588 * @param __rhs A regular expression submatch.
1589 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1590 */
1591 template<typename _Bi_iter>
1592 inline bool
1593 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1594 const sub_match<_Bi_iter>& __rhs)
1595 { return !(__lhs < __rhs); }
1596
1597 /**
1598 * @brief Tests the ordering of a string and a regular expression submatch.
1599 * @param __lhs A string.
1600 * @param __rhs A regular expression submatch.
1601 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1602 */
1603 template<typename _Bi_iter>
1604 inline bool
1605 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1606 const sub_match<_Bi_iter>& __rhs)
1607 { return !(__rhs < __lhs); }
1608
1609 /**
1610 * @brief Tests the equivalence of a regular expression submatch and a
1611 * string.
1612 * @param __lhs A regular expression submatch.
1613 * @param __rhs A const string reference.
1614 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1615 */
1616 template<typename _Bi_iter>
1617 inline bool
1618 operator==(const sub_match<_Bi_iter>& __lhs,
1619 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1620 {
1621 typedef typename sub_match<_Bi_iter>::string_type string_type;
1622 return __lhs.compare(string_type(1, __rhs)) == 0;
1623 }
1624
1625 /**
1626 * @brief Tests the inequivalence of a regular expression submatch and a
1627 * string.
1628 * @param __lhs A regular expression submatch.
1629 * @param __rhs A const string reference.
1630 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1631 */
1632 template<typename _Bi_iter>
1633 inline bool
1634 operator!=(const sub_match<_Bi_iter>& __lhs,
1635 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1636 { return !(__lhs == __rhs); }
1637
1638 /**
1639 * @brief Tests the ordering of a regular expression submatch and a string.
1640 * @param __lhs A regular expression submatch.
1641 * @param __rhs A const string reference.
1642 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1643 */
1644 template<typename _Bi_iter>
1645 inline bool
1646 operator<(const sub_match<_Bi_iter>& __lhs,
1647 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1648 {
1649 typedef typename sub_match<_Bi_iter>::string_type string_type;
1650 return __lhs.compare(string_type(1, __rhs)) < 0;
1651 }
1652
1653 /**
1654 * @brief Tests the ordering of a regular expression submatch and a string.
1655 * @param __lhs A regular expression submatch.
1656 * @param __rhs A const string reference.
1657 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1658 */
1659 template<typename _Bi_iter>
1660 inline bool
1661 operator>(const sub_match<_Bi_iter>& __lhs,
1662 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1663 { return __rhs < __lhs; }
1664
1665 /**
1666 * @brief Tests the ordering of a regular expression submatch and a string.
1667 * @param __lhs A regular expression submatch.
1668 * @param __rhs A const string reference.
1669 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1670 */
1671 template<typename _Bi_iter>
1672 inline bool
1673 operator>=(const sub_match<_Bi_iter>& __lhs,
1674 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1675 { return !(__lhs < __rhs); }
1676
1677 /**
1678 * @brief Tests the ordering of a regular expression submatch and a string.
1679 * @param __lhs A regular expression submatch.
1680 * @param __rhs A const string reference.
1681 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1682 */
1683 template<typename _Bi_iter>
1684 inline bool
1685 operator<=(const sub_match<_Bi_iter>& __lhs,
1686 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1687 { return !(__rhs < __lhs); }
1688
1689 /**
1690 * @brief Inserts a matched string into an output stream.
1691 *
1692 * @param __os The output stream.
1693 * @param __m A submatch string.
1694 *
1695 * @returns the output stream with the submatch string inserted.
1696 */
1697 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1698 inline
1699 basic_ostream<_Ch_type, _Ch_traits>&
1700 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1701 const sub_match<_Bi_iter>& __m)
1702 { return __os << __m.str(); }
1703
1704 // [7.10] Class template match_results
1705
1706 /*
1707 * Special sub_match object representing an unmatched sub-expression.
1708 */
1709 template<typename _Bi_iter>
1710 inline const sub_match<_Bi_iter>&
1711 __unmatched_sub()
1712 {
1713 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1714 return __unmatched;
1715 }
1716
1717 /**
1718 * @brief The results of a match or search operation.
1719 *
1720 * A collection of character sequences representing the result of a regular
1721 * expression match. Storage for the collection is allocated and freed as
1722 * necessary by the member functions of class template match_results.
1723 *
1724 * This class satisfies the Sequence requirements, with the exception that
1725 * only the operations defined for a const-qualified Sequence are supported.
1726 *
1727 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1728 * the whole match. In this case the %sub_match member matched is always true.
1729 * The sub_match object stored at index n denotes what matched the marked
1730 * sub-expression n within the matched expression. If the sub-expression n
1731 * participated in a regular expression match then the %sub_match member
1732 * matched evaluates to true, and members first and second denote the range
1733 * of characters [first, second) which formed that match. Otherwise matched
1734 * is false, and members first and second point to the end of the sequence
1735 * that was searched.
1736 *
1737 * @nosubgrouping
1738 */
1739 template<typename _Bi_iter,
1740 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1741 class match_results
1742 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1743 {
1744 private:
1745 /*
1746 * The vector base is empty if this does not represent a successful match.
1747 * Otherwise it contains n+3 elements where n is the number of marked
1748 * sub-expressions:
1749 * [0] entire match
1750 * [1] 1st marked subexpression
1751 * ...
1752 * [n] nth marked subexpression
1753 * [n+1] prefix
1754 * [n+2] suffix
1755 */
1756 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1757 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1758 typedef regex_constants::match_flag_type match_flag_type;
1759
1760 public:
1761 /**
1762 * @name 10.? Public Types
1763 */
1764 //@{
1765 typedef _Alloc allocator_type;
1766 typedef sub_match<_Bi_iter> value_type;
1767 typedef const value_type& const_reference;
1768 typedef const_reference reference;
1769 typedef typename _Base_type::const_iterator const_iterator;
1770 typedef const_iterator iterator;
1771 typedef typename __iter_traits::difference_type difference_type;
1772 typedef typename __iter_traits::value_type char_type;
1773 typedef typename allocator_traits<_Alloc>::size_type size_type;
1774
1775
1776 typedef std::basic_string<char_type> string_type;
1777 //@}
1778
1779 public:
1780 /**
1781 * @name 28.10.1 Construction, Copying, and Destruction
1782 */
1783 //@{
1784
1785 /**
1786 * @brief Constructs a default %match_results container.
1787 * @post size() returns 0 and str() returns an empty string.
1788 */
1789 explicit
1790 match_results(const _Alloc& __a = _Alloc())
1791 : _Base_type(__a)
1792 { }
1793
1794 /**
1795 * @brief Copy constructs a %match_results.
1796 */
1797 match_results(const match_results& __rhs)
1798 : _Base_type(__rhs)
1799 { }
1800
1801 /**
1802 * @brief Move constructs a %match_results.
1803 */
1804 match_results(match_results&& __rhs) noexcept
1805 : _Base_type(std::move(__rhs))
1806 { }
1807
1808 /**
1809 * @brief Assigns rhs to *this.
1810 */
1811 match_results&
1812 operator=(const match_results& __rhs)
1813 {
1814 match_results(__rhs).swap(*this);
1815 return *this;
1816 }
1817
1818 /**
1819 * @brief Move-assigns rhs to *this.
1820 */
1821 match_results&
1822 operator=(match_results&& __rhs)
1823 {
1824 match_results(std::move(__rhs)).swap(*this);
1825 return *this;
1826 }
1827
1828 /**
1829 * @brief Destroys a %match_results object.
1830 */
1831 ~match_results()
1832 { }
1833
1834 //@}
1835
1836 // 28.10.2, state:
1837 /**
1838 * @brief Indicates if the %match_results is ready.
1839 * @retval true The object has a fully-established result state.
1840 * @retval false The object is not ready.
1841 */
1842 bool ready() const { return !_Base_type::empty(); }
1843
1844 /**
1845 * @name 28.10.2 Size
1846 */
1847 //@{
1848
1849 /**
1850 * @brief Gets the number of matches and submatches.
1851 *
1852 * The number of matches for a given regular expression will be either 0
1853 * if there was no match or mark_count() + 1 if a match was successful.
1854 * Some matches may be empty.
1855 *
1856 * @returns the number of matches found.
1857 */
1858 size_type
1859 size() const
1860 {
1861 size_type __size = _Base_type::size();
1862 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1863 }
1864
1865 size_type
1866 max_size() const
1867 { return _Base_type::max_size(); }
1868
1869 /**
1870 * @brief Indicates if the %match_results contains no results.
1871 * @retval true The %match_results object is empty.
1872 * @retval false The %match_results object is not empty.
1873 */
1874 bool
1875 empty() const
1876 { return size() == 0; }
1877
1878 //@}
1879
1880 /**
1881 * @name 10.3 Element Access
1882 */
1883 //@{
1884
1885 /**
1886 * @brief Gets the length of the indicated submatch.
1887 * @param __sub indicates the submatch.
1888 * @pre ready() == true
1889 *
1890 * This function returns the length of the indicated submatch, or the
1891 * length of the entire match if @p __sub is zero (the default).
1892 */
1893 difference_type
1894 length(size_type __sub = 0) const
1895 { return (*this)[__sub].length(); }
1896
1897 /**
1898 * @brief Gets the offset of the beginning of the indicated submatch.
1899 * @param __sub indicates the submatch.
1900 * @pre ready() == true
1901 *
1902 * This function returns the offset from the beginning of the target
1903 * sequence to the beginning of the submatch, unless the value of @p __sub
1904 * is zero (the default), in which case this function returns the offset
1905 * from the beginning of the target sequence to the beginning of the
1906 * match.
1907 *
1908 * Returns -1 if @p __sub is out of range.
1909 */
1910 difference_type
1911 position(size_type __sub = 0) const
1912 {
1913 return __sub < size() ? std::distance(this->prefix().first,
1914 (*this)[__sub].first) : -1;
1915 }
1916
1917 /**
1918 * @brief Gets the match or submatch converted to a string type.
1919 * @param __sub indicates the submatch.
1920 * @pre ready() == true
1921 *
1922 * This function gets the submatch (or match, if @p __sub is
1923 * zero) extracted from the target range and converted to the
1924 * associated string type.
1925 */
1926 string_type
1927 str(size_type __sub = 0) const
1928 { return (*this)[__sub].str(); }
1929
1930 /**
1931 * @brief Gets a %sub_match reference for the match or submatch.
1932 * @param __sub indicates the submatch.
1933 * @pre ready() == true
1934 *
1935 * This function gets a reference to the indicated submatch, or
1936 * the entire match if @p __sub is zero.
1937 *
1938 * If @p __sub >= size() then this function returns a %sub_match with a
1939 * special value indicating no submatch.
1940 */
1941 const_reference
1942 operator[](size_type __sub) const
1943 {
1944 _GLIBCXX_DEBUG_ASSERT( ready() );
1945 return __sub < size()
1946 ? _Base_type::operator[](__sub)
1947 : __unmatched_sub<_Bi_iter>();
1948 }
1949
1950 /**
1951 * @brief Gets a %sub_match representing the match prefix.
1952 * @pre ready() == true
1953 *
1954 * This function gets a reference to a %sub_match object representing the
1955 * part of the target range between the start of the target range and the
1956 * start of the match.
1957 */
1958 const_reference
1959 prefix() const
1960 {
1961 _GLIBCXX_DEBUG_ASSERT( ready() );
1962 return !empty()
1963 ? _Base_type::operator[](_Base_type::size() - 2)
1964 : __unmatched_sub<_Bi_iter>();
1965 }
1966
1967 /**
1968 * @brief Gets a %sub_match representing the match suffix.
1969 * @pre ready() == true
1970 *
1971 * This function gets a reference to a %sub_match object representing the
1972 * part of the target range between the end of the match and the end of
1973 * the target range.
1974 */
1975 const_reference
1976 suffix() const
1977 {
1978 _GLIBCXX_DEBUG_ASSERT( ready() );
1979 return !empty()
1980 ? _Base_type::operator[](_Base_type::size() - 1)
1981 : __unmatched_sub<_Bi_iter>();
1982 }
1983
1984 /**
1985 * @brief Gets an iterator to the start of the %sub_match collection.
1986 */
1987 const_iterator
1988 begin() const
1989 { return _Base_type::begin(); }
1990
1991 /**
1992 * @brief Gets an iterator to the start of the %sub_match collection.
1993 */
1994 const_iterator
1995 cbegin() const
1996 { return _Base_type::cbegin(); }
1997
1998 /**
1999 * @brief Gets an iterator to one-past-the-end of the collection.
2000 */
2001 const_iterator
2002 end() const
2003 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
2004
2005 /**
2006 * @brief Gets an iterator to one-past-the-end of the collection.
2007 */
2008 const_iterator
2009 cend() const
2010 { return end(); }
2011
2012 //@}
2013
2014 /**
2015 * @name 10.4 Formatting
2016 *
2017 * These functions perform formatted substitution of the matched
2018 * character sequences into their target. The format specifiers and
2019 * escape sequences accepted by these functions are determined by
2020 * their @p flags parameter as documented above.
2021 */
2022 //@{
2023
2024 /**
2025 * @pre ready() == true
2026 * @todo Implement this function.
2027 */
2028 template<typename _Out_iter>
2029 _Out_iter
2030 format(_Out_iter __out, const char_type* __fmt_first,
2031 const char_type* __fmt_last,
2032 match_flag_type __flags = regex_constants::format_default) const
2033 { return __out; }
2034
2035 /**
2036 * @pre ready() == true
2037 */
2038 template<typename _Out_iter, typename _St, typename _Sa>
2039 _Out_iter
2040 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
2041 match_flag_type __flags = regex_constants::format_default) const
2042 {
2043 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
2044 __flags);
2045 }
2046
2047 /**
2048 * @pre ready() == true
2049 */
2050 template<typename _Out_iter, typename _St, typename _Sa>
2051 basic_string<char_type, _St, _Sa>
2052 format(const basic_string<char_type, _St, _Sa>& __fmt,
2053 match_flag_type __flags = regex_constants::format_default) const
2054 {
2055 basic_string<char_type, _St, _Sa> __result;
2056 format(std::back_inserter(__result), __fmt, __flags);
2057 return __result;
2058 }
2059
2060 /**
2061 * @pre ready() == true
2062 */
2063 string_type
2064 format(const char_type* __fmt,
2065 match_flag_type __flags = regex_constants::format_default) const
2066 {
2067 string_type __result;
2068 format(std::back_inserter(__result),
2069 __fmt,
2070 __fmt + char_traits<char_type>::length(__fmt),
2071 __flags);
2072 return __result;
2073 }
2074
2075 //@}
2076
2077 /**
2078 * @name 10.5 Allocator
2079 */
2080 //@{
2081
2082 /**
2083 * @brief Gets a copy of the allocator.
2084 */
2085 allocator_type
2086 get_allocator() const
2087 { return _Base_type::get_allocator(); }
2088
2089 //@}
2090
2091 /**
2092 * @name 10.6 Swap
2093 */
2094 //@{
2095
2096 /**
2097 * @brief Swaps the contents of two match_results.
2098 */
2099 void
2100 swap(match_results& __that)
2101 { _Base_type::swap(__that); }
2102 //@}
2103
2104 private:
2105 template<typename, typename, typename, typename>
2106 friend class __detail::_Executor;
2107
2108 template<typename, typename, typename, typename>
2109 friend class __detail::_DFSExecutor;
2110
2111 template<typename, typename, typename, typename>
2112 friend class __detail::_BFSExecutor;
2113
2114 template<typename _B, typename _A, typename _Ch_type, typename _Rx_traits>
2115 friend bool
2116 regex_match(_B, _B, match_results<_B, _A>&,
2117 const basic_regex<_Ch_type,
2118 _Rx_traits>&,
2119 regex_constants::match_flag_type);
2120
2121 template<typename _B, typename _A, typename _Ch_type, typename _Rx_traits>
2122 friend bool
2123 regex_search(_B, _B, match_results<_B, _A>&,
2124 const basic_regex<_Ch_type,
2125 _Rx_traits>&,
2126 regex_constants::match_flag_type);
2127 };
2128
2129 typedef match_results<const char*> cmatch;
2130 typedef match_results<string::const_iterator> smatch;
2131 #ifdef _GLIBCXX_USE_WCHAR_T
2132 typedef match_results<const wchar_t*> wcmatch;
2133 typedef match_results<wstring::const_iterator> wsmatch;
2134 #endif
2135
2136 // match_results comparisons
2137 /**
2138 * @brief Compares two match_results for equality.
2139 * @returns true if the two objects refer to the same match,
2140 * false otherwise.
2141 */
2142 template<typename _Bi_iter, typename _Alloc>
2143 inline bool
2144 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
2145 const match_results<_Bi_iter, _Alloc>& __m2)
2146 {
2147 if (__m1.ready() != __m2.ready())
2148 return false;
2149 if (!__m1.ready()) // both are not ready
2150 return true;
2151 if (__m1.empty() != __m2.empty())
2152 return false;
2153 if (__m1.empty()) // both are empty
2154 return true;
2155 return __m1.prefix() == __m2.prefix()
2156 && __m1.size() == __m2.size()
2157 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
2158 && __m1.suffix() == __m2.suffix();
2159 }
2160
2161 /**
2162 * @brief Compares two match_results for inequality.
2163 * @returns true if the two objects do not refer to the same match,
2164 * false otherwise.
2165 */
2166 template<typename _Bi_iter, class _Alloc>
2167 inline bool
2168 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
2169 const match_results<_Bi_iter, _Alloc>& __m2)
2170 { return !(__m1 == __m2); }
2171
2172 // [7.10.6] match_results swap
2173 /**
2174 * @brief Swaps two match results.
2175 * @param __lhs A match result.
2176 * @param __rhs A match result.
2177 *
2178 * The contents of the two match_results objects are swapped.
2179 */
2180 template<typename _Bi_iter, typename _Alloc>
2181 inline void
2182 swap(match_results<_Bi_iter, _Alloc>& __lhs,
2183 match_results<_Bi_iter, _Alloc>& __rhs)
2184 { __lhs.swap(__rhs); }
2185
2186 // [7.11.2] Function template regex_match
2187 /**
2188 * @name Matching, Searching, and Replacing
2189 */
2190 //@{
2191
2192 /**
2193 * @brief Determines if there is a match between the regular expression @p e
2194 * and all of the character sequence [first, last).
2195 *
2196 * @param __s Start of the character sequence to match.
2197 * @param __e One-past-the-end of the character sequence to match.
2198 * @param __m The match results.
2199 * @param __re The regular expression.
2200 * @param __flags Controls how the regular expression is matched.
2201 *
2202 * @retval true A match exists.
2203 * @retval false Otherwise.
2204 *
2205 * @throws an exception of type regex_error.
2206 *
2207 * @todo Implement this function.
2208 */
2209 template<typename _Bi_iter, typename _Alloc,
2210 typename _Ch_type, typename _Rx_traits>
2211 bool
2212 regex_match(_Bi_iter __s,
2213 _Bi_iter __e,
2214 match_results<_Bi_iter, _Alloc>& __m,
2215 const basic_regex<_Ch_type, _Rx_traits>& __re,
2216 regex_constants::match_flag_type __flags
2217 = regex_constants::match_default)
2218 {
2219 if (__re._M_automaton == nullptr)
2220 return false;
2221 if (__detail::__get_executor(__s, __e, __m, __re, __flags)->_M_match())
2222 {
2223 for (auto __it : __m)
2224 if (!__it.matched)
2225 __it.first = __it.second = __e;
2226 __m.at(__m.size()).matched = false;
2227 __m.at(__m.size()).first = __s;
2228 __m.at(__m.size()).second = __s;
2229 __m.at(__m.size()+1).matched = false;
2230 __m.at(__m.size()+1).first = __e;
2231 __m.at(__m.size()+1).second = __e;
2232 return true;
2233 }
2234 return false;
2235 }
2236
2237 /**
2238 * @brief Indicates if there is a match between the regular expression @p e
2239 * and all of the character sequence [first, last).
2240 *
2241 * @param __first Beginning of the character sequence to match.
2242 * @param __last One-past-the-end of the character sequence to match.
2243 * @param __re The regular expression.
2244 * @param __flags Controls how the regular expression is matched.
2245 *
2246 * @retval true A match exists.
2247 * @retval false Otherwise.
2248 *
2249 * @throws an exception of type regex_error.
2250 */
2251 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2252 bool
2253 regex_match(_Bi_iter __first, _Bi_iter __last,
2254 const basic_regex<_Ch_type, _Rx_traits>& __re,
2255 regex_constants::match_flag_type __flags
2256 = regex_constants::match_default)
2257 {
2258 match_results<_Bi_iter> __what;
2259 return regex_match(__first, __last, __what, __re, __flags);
2260 }
2261
2262 /**
2263 * @brief Determines if there is a match between the regular expression @p e
2264 * and a C-style null-terminated string.
2265 *
2266 * @param __s The C-style null-terminated string to match.
2267 * @param __m The match results.
2268 * @param __re The regular expression.
2269 * @param __f Controls how the regular expression is matched.
2270 *
2271 * @retval true A match exists.
2272 * @retval false Otherwise.
2273 *
2274 * @throws an exception of type regex_error.
2275 */
2276 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2277 inline bool
2278 regex_match(const _Ch_type* __s,
2279 match_results<const _Ch_type*, _Alloc>& __m,
2280 const basic_regex<_Ch_type, _Rx_traits>& __re,
2281 regex_constants::match_flag_type __f
2282 = regex_constants::match_default)
2283 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2284
2285 /**
2286 * @brief Determines if there is a match between the regular expression @p e
2287 * and a string.
2288 *
2289 * @param __s The string to match.
2290 * @param __m The match results.
2291 * @param __re The regular expression.
2292 * @param __flags Controls how the regular expression is matched.
2293 *
2294 * @retval true A match exists.
2295 * @retval false Otherwise.
2296 *
2297 * @throws an exception of type regex_error.
2298 */
2299 template<typename _Ch_traits, typename _Ch_alloc,
2300 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2301 inline bool
2302 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2303 match_results<typename basic_string<_Ch_type,
2304 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2305 const basic_regex<_Ch_type, _Rx_traits>& __re,
2306 regex_constants::match_flag_type __flags
2307 = regex_constants::match_default)
2308 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2309
2310 /**
2311 * @brief Indicates if there is a match between the regular expression @p e
2312 * and a C-style null-terminated string.
2313 *
2314 * @param __s The C-style null-terminated string to match.
2315 * @param __re The regular expression.
2316 * @param __f Controls how the regular expression is matched.
2317 *
2318 * @retval true A match exists.
2319 * @retval false Otherwise.
2320 *
2321 * @throws an exception of type regex_error.
2322 */
2323 template<typename _Ch_type, class _Rx_traits>
2324 inline bool
2325 regex_match(const _Ch_type* __s,
2326 const basic_regex<_Ch_type, _Rx_traits>& __re,
2327 regex_constants::match_flag_type __f
2328 = regex_constants::match_default)
2329 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2330
2331 /**
2332 * @brief Indicates if there is a match between the regular expression @p e
2333 * and a string.
2334 *
2335 * @param __s [IN] The string to match.
2336 * @param __re [IN] The regular expression.
2337 * @param __flags [IN] Controls how the regular expression is matched.
2338 *
2339 * @retval true A match exists.
2340 * @retval false Otherwise.
2341 *
2342 * @throws an exception of type regex_error.
2343 */
2344 template<typename _Ch_traits, typename _Str_allocator,
2345 typename _Ch_type, typename _Rx_traits>
2346 inline bool
2347 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2348 const basic_regex<_Ch_type, _Rx_traits>& __re,
2349 regex_constants::match_flag_type __flags
2350 = regex_constants::match_default)
2351 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2352
2353 // [7.11.3] Function template regex_search
2354 /**
2355 * Searches for a regular expression within a range.
2356 * @param __first [IN] The start of the string to search.
2357 * @param __last [IN] One-past-the-end of the string to search.
2358 * @param __m [OUT] The match results.
2359 * @param __re [IN] The regular expression to search for.
2360 * @param __flags [IN] Search policy flags.
2361 * @retval true A match was found within the string.
2362 * @retval false No match was found within the string, the content of %m is
2363 * undefined.
2364 *
2365 * @throws an exception of type regex_error.
2366 *
2367 * @todo Implement this function.
2368 */
2369 template<typename _Bi_iter, typename _Alloc,
2370 typename _Ch_type, typename _Rx_traits>
2371 inline bool
2372 regex_search(_Bi_iter __first, _Bi_iter __last,
2373 match_results<_Bi_iter, _Alloc>& __m,
2374 const basic_regex<_Ch_type, _Rx_traits>& __re,
2375 regex_constants::match_flag_type __flags
2376 = regex_constants::match_default)
2377 {
2378 if (__re._M_automaton == nullptr)
2379 return false;
2380 for (auto __cur = __first; __cur != __last; ++__cur) // Any KMP-like algo?
2381 if (__detail::__get_executor(__cur, __last, __m, __re, __flags)
2382 ->_M_search_from_first())
2383 {
2384 for (auto __it : __m)
2385 if (!__it.matched)
2386 __it.first = __it.second = __last;
2387 __m.at(__m.size()).first = __first;
2388 __m.at(__m.size()).second = __m[0].first;
2389 __m.at(__m.size()+1).first = __m[0].second;
2390 __m.at(__m.size()+1).second = __last;
2391 __m.at(__m.size()).matched =
2392 (__m.prefix().first != __m.prefix().second);
2393 __m.at(__m.size()+1).matched =
2394 (__m.suffix().first != __m.suffix().second);
2395 return true;
2396 }
2397 return false;
2398 }
2399
2400
2401 /**
2402 * Searches for a regular expression within a range.
2403 * @param __first [IN] The start of the string to search.
2404 * @param __last [IN] One-past-the-end of the string to search.
2405 * @param __re [IN] The regular expression to search for.
2406 * @param __flags [IN] Search policy flags.
2407 * @retval true A match was found within the string.
2408 * @retval false No match was found within the string.
2409 *
2410 * @throws an exception of type regex_error.
2411 */
2412 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2413 inline bool
2414 regex_search(_Bi_iter __first, _Bi_iter __last,
2415 const basic_regex<_Ch_type, _Rx_traits>& __re,
2416 regex_constants::match_flag_type __flags
2417 = regex_constants::match_default)
2418 {
2419 match_results<_Bi_iter> __what;
2420 return regex_search(__first, __last, __what, __re, __flags);
2421 }
2422
2423 /**
2424 * @brief Searches for a regular expression within a C-string.
2425 * @param __s [IN] A C-string to search for the regex.
2426 * @param __m [OUT] The set of regex matches.
2427 * @param __e [IN] The regex to search for in @p s.
2428 * @param __f [IN] The search flags.
2429 * @retval true A match was found within the string.
2430 * @retval false No match was found within the string, the content of %m is
2431 * undefined.
2432 *
2433 * @throws an exception of type regex_error.
2434 */
2435 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2436 inline bool
2437 regex_search(const _Ch_type* __s,
2438 match_results<const _Ch_type*, _Alloc>& __m,
2439 const basic_regex<_Ch_type, _Rx_traits>& __e,
2440 regex_constants::match_flag_type __f
2441 = regex_constants::match_default)
2442 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2443
2444 /**
2445 * @brief Searches for a regular expression within a C-string.
2446 * @param __s [IN] The C-string to search.
2447 * @param __e [IN] The regular expression to search for.
2448 * @param __f [IN] Search policy flags.
2449 * @retval true A match was found within the string.
2450 * @retval false No match was found within the string.
2451 *
2452 * @throws an exception of type regex_error.
2453 */
2454 template<typename _Ch_type, typename _Rx_traits>
2455 inline bool
2456 regex_search(const _Ch_type* __s,
2457 const basic_regex<_Ch_type, _Rx_traits>& __e,
2458 regex_constants::match_flag_type __f
2459 = regex_constants::match_default)
2460 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2461
2462 /**
2463 * @brief Searches for a regular expression within a string.
2464 * @param __s [IN] The string to search.
2465 * @param __e [IN] The regular expression to search for.
2466 * @param __flags [IN] Search policy flags.
2467 * @retval true A match was found within the string.
2468 * @retval false No match was found within the string.
2469 *
2470 * @throws an exception of type regex_error.
2471 */
2472 template<typename _Ch_traits, typename _String_allocator,
2473 typename _Ch_type, typename _Rx_traits>
2474 inline bool
2475 regex_search(const basic_string<_Ch_type, _Ch_traits,
2476 _String_allocator>& __s,
2477 const basic_regex<_Ch_type, _Rx_traits>& __e,
2478 regex_constants::match_flag_type __flags
2479 = regex_constants::match_default)
2480 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2481
2482 /**
2483 * @brief Searches for a regular expression within a string.
2484 * @param __s [IN] A C++ string to search for the regex.
2485 * @param __m [OUT] The set of regex matches.
2486 * @param __e [IN] The regex to search for in @p s.
2487 * @param __f [IN] The search flags.
2488 * @retval true A match was found within the string.
2489 * @retval false No match was found within the string, the content of %m is
2490 * undefined.
2491 *
2492 * @throws an exception of type regex_error.
2493 */
2494 template<typename _Ch_traits, typename _Ch_alloc,
2495 typename _Alloc, typename _Ch_type,
2496 typename _Rx_traits>
2497 inline bool
2498 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2499 match_results<typename basic_string<_Ch_type,
2500 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2501 const basic_regex<_Ch_type, _Rx_traits>& __e,
2502 regex_constants::match_flag_type __f
2503 = regex_constants::match_default)
2504 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2505
2506 // std [28.11.4] Function template regex_replace
2507 /**
2508 * @doctodo
2509 * @param __out
2510 * @param __first
2511 * @param __last
2512 * @param __e
2513 * @param __fmt
2514 * @param __flags
2515 *
2516 * @returns out
2517 * @throws an exception of type regex_error.
2518 *
2519 * @todo Implement this function.
2520 */
2521 template<typename _Out_iter, typename _Bi_iter,
2522 typename _Rx_traits, typename _Ch_type>
2523 inline _Out_iter
2524 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2525 const basic_regex<_Ch_type, _Rx_traits>& __e,
2526 const basic_string<_Ch_type>& __fmt,
2527 regex_constants::match_flag_type __flags
2528 = regex_constants::match_default)
2529 { return __out; }
2530
2531 /**
2532 * @doctodo
2533 * @param __s
2534 * @param __e
2535 * @param __fmt
2536 * @param __flags
2537 *
2538 * @returns a copy of string @p s with replacements.
2539 *
2540 * @throws an exception of type regex_error.
2541 */
2542 template<typename _Rx_traits, typename _Ch_type>
2543 inline basic_string<_Ch_type>
2544 regex_replace(const basic_string<_Ch_type>& __s,
2545 const basic_regex<_Ch_type, _Rx_traits>& __e,
2546 const basic_string<_Ch_type>& __fmt,
2547 regex_constants::match_flag_type __flags
2548 = regex_constants::match_default)
2549 {
2550 basic_string<_Ch_type> __result;
2551 regex_replace(std::back_inserter(__result),
2552 __s.begin(), __s.end(), __e, __fmt, __flags);
2553 return __result;
2554 }
2555
2556 //@}
2557
2558 // std [28.12] Class template regex_iterator
2559 /**
2560 * An iterator adaptor that will provide repeated calls of regex_search over
2561 * a range until no more matches remain.
2562 */
2563 template<typename _Bi_iter,
2564 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2565 typename _Rx_traits = regex_traits<_Ch_type> >
2566 class regex_iterator
2567 {
2568 public:
2569 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2570 typedef match_results<_Bi_iter> value_type;
2571 typedef std::ptrdiff_t difference_type;
2572 typedef const value_type* pointer;
2573 typedef const value_type& reference;
2574 typedef std::forward_iterator_tag iterator_category;
2575
2576 /**
2577 * @brief Provides a singular iterator, useful for indicating
2578 * one-past-the-end of a range.
2579 */
2580 regex_iterator()
2581 : _M_match()
2582 { }
2583
2584 /**
2585 * Constructs a %regex_iterator...
2586 * @param __a [IN] The start of a text range to search.
2587 * @param __b [IN] One-past-the-end of the text range to search.
2588 * @param __re [IN] The regular expression to match.
2589 * @param __m [IN] Policy flags for match rules.
2590 */
2591 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2592 regex_constants::match_flag_type __m
2593 = regex_constants::match_default)
2594 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2595 { regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags); }
2596
2597 /**
2598 * Copy constructs a %regex_iterator.
2599 */
2600 regex_iterator(const regex_iterator& __rhs) = default;
2601
2602 /**
2603 * @brief Assigns one %regex_iterator to another.
2604 */
2605 regex_iterator&
2606 operator=(const regex_iterator& __rhs) = default;
2607
2608 /**
2609 * @brief Tests the equivalence of two regex iterators.
2610 */
2611 bool
2612 operator==(const regex_iterator& __rhs) const;
2613
2614 /**
2615 * @brief Tests the inequivalence of two regex iterators.
2616 */
2617 bool
2618 operator!=(const regex_iterator& __rhs) const
2619 { return !(*this == __rhs); }
2620
2621 /**
2622 * @brief Dereferences a %regex_iterator.
2623 */
2624 const value_type&
2625 operator*() const
2626 { return _M_match; }
2627
2628 /**
2629 * @brief Selects a %regex_iterator member.
2630 */
2631 const value_type*
2632 operator->() const
2633 { return &_M_match; }
2634
2635 /**
2636 * @brief Increments a %regex_iterator.
2637 */
2638 regex_iterator&
2639 operator++();
2640
2641 /**
2642 * @brief Postincrements a %regex_iterator.
2643 */
2644 regex_iterator
2645 operator++(int)
2646 {
2647 auto __tmp = *this;
2648 ++(*this);
2649 return __tmp;
2650 }
2651
2652 private:
2653 _Bi_iter _M_begin;
2654 _Bi_iter _M_end;
2655 const regex_type* _M_pregex;
2656 regex_constants::match_flag_type _M_flags;
2657 match_results<_Bi_iter> _M_match;
2658 };
2659
2660 template<typename _Bi_iter,
2661 typename _Ch_type,
2662 typename _Rx_traits>
2663 bool
2664 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2665 operator==(const regex_iterator& __rhs) const
2666 {
2667 return (_M_match.empty() && __rhs._M_match.empty())
2668 || (_M_begin == __rhs._M_begin
2669 && _M_end == __rhs._M_end
2670 && _M_pregex == __rhs._M_pregex
2671 && _M_flags == __rhs._M_flags
2672 && _M_match[0] == __rhs._M_match[0]);
2673 }
2674
2675 template<typename _Bi_iter,
2676 typename _Ch_type,
2677 typename _Rx_traits>
2678 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
2679 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2680 operator++()
2681 {
2682 // FIXME: In all cases in which the call to regex_search returns true,
2683 // match.prefix().first shall be equal to the previous value of
2684 // match[0].second, and for each index i in the half-open range
2685 // [0, match.size()) for which match[i].matched is true,
2686 // match[i].position() shall return distance(begin, match[i].first).
2687 // [28.12.1.4.5]
2688 if (_M_match[0].matched)
2689 {
2690 auto __start = _M_match[0].second;
2691 if (_M_match[0].first == _M_match[0].second)
2692 if (__start == _M_end)
2693 {
2694 _M_match = value_type();
2695 return *this;
2696 }
2697 else
2698 {
2699 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags
2700 | regex_constants::match_not_null
2701 | regex_constants::match_continuous))
2702 return *this;
2703 else
2704 ++__start;
2705 }
2706 _M_flags |= regex_constants::match_prev_avail;
2707 if (!regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags))
2708 _M_match = value_type();
2709 }
2710 return *this;
2711 }
2712
2713 typedef regex_iterator<const char*> cregex_iterator;
2714 typedef regex_iterator<string::const_iterator> sregex_iterator;
2715 #ifdef _GLIBCXX_USE_WCHAR_T
2716 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2717 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2718 #endif
2719
2720 // [7.12.2] Class template regex_token_iterator
2721 /**
2722 * Iterates over submatches in a range (or @a splits a text string).
2723 *
2724 * The purpose of this iterator is to enumerate all, or all specified,
2725 * matches of a regular expression within a text range. The dereferenced
2726 * value of an iterator of this class is a std::sub_match object.
2727 */
2728 template<typename _Bi_iter,
2729 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2730 typename _Rx_traits = regex_traits<_Ch_type> >
2731 class regex_token_iterator
2732 {
2733 public:
2734 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2735 typedef sub_match<_Bi_iter> value_type;
2736 typedef std::ptrdiff_t difference_type;
2737 typedef const value_type* pointer;
2738 typedef const value_type& reference;
2739 typedef std::forward_iterator_tag iterator_category;
2740
2741 public:
2742 /**
2743 * @brief Default constructs a %regex_token_iterator.
2744 *
2745 * A default-constructed %regex_token_iterator is a singular iterator
2746 * that will compare equal to the one-past-the-end value for any
2747 * iterator of the same type.
2748 */
2749 regex_token_iterator()
2750 : _M_position(), _M_result(nullptr), _M_suffix(), _M_n(0), _M_subs()
2751 { }
2752
2753 /**
2754 * Constructs a %regex_token_iterator...
2755 * @param __a [IN] The start of the text to search.
2756 * @param __b [IN] One-past-the-end of the text to search.
2757 * @param __re [IN] The regular expression to search for.
2758 * @param __submatch [IN] Which submatch to return. There are some
2759 * special values for this parameter:
2760 * - -1 each enumerated subexpression does NOT
2761 * match the regular expression (aka field
2762 * splitting)
2763 * - 0 the entire string matching the
2764 * subexpression is returned for each match
2765 * within the text.
2766 * - >0 enumerates only the indicated
2767 * subexpression from a match within the text.
2768 * @param __m [IN] Policy flags for match rules.
2769 */
2770 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2771 int __submatch = 0,
2772 regex_constants::match_flag_type __m
2773 = regex_constants::match_default)
2774 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2775 { _M_init(__a, __b); }
2776
2777 /**
2778 * Constructs a %regex_token_iterator...
2779 * @param __a [IN] The start of the text to search.
2780 * @param __b [IN] One-past-the-end of the text to search.
2781 * @param __re [IN] The regular expression to search for.
2782 * @param __submatches [IN] A list of subexpressions to return for each
2783 * regular expression match within the text.
2784 * @param __m [IN] Policy flags for match rules.
2785 */
2786 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2787 const regex_type& __re,
2788 const std::vector<int>& __submatches,
2789 regex_constants::match_flag_type __m
2790 = regex_constants::match_default)
2791 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2792 { _M_init(__a, __b); }
2793
2794 /**
2795 * Constructs a %regex_token_iterator...
2796 * @param __a [IN] The start of the text to search.
2797 * @param __b [IN] One-past-the-end of the text to search.
2798 * @param __re [IN] The regular expression to search for.
2799 * @param __submatches [IN] A list of subexpressions to return for each
2800 * regular expression match within the text.
2801 * @param __m [IN] Policy flags for match rules.
2802 */
2803 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2804 const regex_type& __re,
2805 initializer_list<int> __submatches,
2806 regex_constants::match_flag_type __m
2807 = regex_constants::match_default)
2808 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2809 { _M_init(__a, __b); }
2810
2811 /**
2812 * Constructs a %regex_token_iterator...
2813 * @param __a [IN] The start of the text to search.
2814 * @param __b [IN] One-past-the-end of the text to search.
2815 * @param __re [IN] The regular expression to search for.
2816 * @param __submatches [IN] A list of subexpressions to return for each
2817 * regular expression match within the text.
2818 * @param __m [IN] Policy flags for match rules.
2819 */
2820 template<std::size_t _Nm>
2821 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2822 const regex_type& __re,
2823 const int (&__submatches)[_Nm],
2824 regex_constants::match_flag_type __m
2825 = regex_constants::match_default)
2826 : _M_position(__a, __b, __re, __m),
2827 _M_subs(__submatches, *(&__submatches+1)), _M_n(0)
2828 { _M_init(__a, __b); }
2829
2830 /**
2831 * @brief Copy constructs a %regex_token_iterator.
2832 * @param __rhs [IN] A %regex_token_iterator to copy.
2833 */
2834 regex_token_iterator(const regex_token_iterator& __rhs)
2835 : _M_position(__rhs.position), _M_subs(__rhs.subs), _M_n(__rhs.N),
2836 _M_result(__rhs.result), _M_suffix(__rhs.suffix),
2837 _M_has_m1(__rhs._M_has_m1)
2838 {
2839 if (__rhs._M_result == &__rhs._M_suffix)
2840 _M_result = &_M_suffix;
2841 }
2842
2843 /**
2844 * @brief Assigns a %regex_token_iterator to another.
2845 * @param __rhs [IN] A %regex_token_iterator to copy.
2846 */
2847 regex_token_iterator&
2848 operator=(const regex_token_iterator& __rhs);
2849
2850 /**
2851 * @brief Compares a %regex_token_iterator to another for equality.
2852 */
2853 bool
2854 operator==(const regex_token_iterator& __rhs) const;
2855
2856 /**
2857 * @brief Compares a %regex_token_iterator to another for inequality.
2858 */
2859 bool
2860 operator!=(const regex_token_iterator& __rhs) const
2861 { return !(*this == __rhs); }
2862
2863 /**
2864 * @brief Dereferences a %regex_token_iterator.
2865 */
2866 const value_type&
2867 operator*() const
2868 { return *_M_result; }
2869
2870 /**
2871 * @brief Selects a %regex_token_iterator member.
2872 */
2873 const value_type*
2874 operator->() const
2875 { return _M_result; }
2876
2877 /**
2878 * @brief Increments a %regex_token_iterator.
2879 */
2880 regex_token_iterator&
2881 operator++();
2882
2883 /**
2884 * @brief Postincrements a %regex_token_iterator.
2885 */
2886 regex_token_iterator
2887 operator++(int)
2888 {
2889 auto __tmp = *this;
2890 ++(*this);
2891 return __tmp;
2892 }
2893
2894 private:
2895 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2896
2897 void
2898 _M_init(_Bi_iter __a, _Bi_iter __b);
2899
2900 const value_type&
2901 _M_current_match() const
2902 {
2903 if (_M_subs[_M_n] == -1)
2904 return (*_M_position).prefix();
2905 else
2906 return (*_M_position)[_M_subs[_M_n]];
2907 }
2908
2909 bool
2910 _M_end_of_seq() const
2911 { return _M_result != nullptr; }
2912
2913 _Position _M_position;
2914 const value_type* _M_result;
2915 value_type _M_suffix;
2916 std::size_t _M_n;
2917 std::vector<int> _M_subs;
2918
2919 // Show whether _M_subs contains -1
2920 bool _M_has_m1;
2921 };
2922
2923 template<typename _Bi_iter,
2924 typename _Ch_type,
2925 typename _Rx_traits>
2926 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
2927 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2928 operator=(const regex_token_iterator& __rhs)
2929 {
2930 _M_position = __rhs._M_position;
2931 _M_subs = __rhs._M_subs;
2932 _M_n = __rhs._M_n;
2933 _M_result = __rhs._M_result;
2934 _M_suffix = __rhs._M_suffix;
2935 _M_has_m1 = __rhs._M_has_m1;
2936 if (__rhs._M_result == &__rhs._M_suffix)
2937 _M_result = &_M_suffix;
2938 }
2939
2940 template<typename _Bi_iter,
2941 typename _Ch_type,
2942 typename _Rx_traits>
2943 bool
2944 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2945 operator==(const regex_token_iterator& __rhs) const
2946 {
2947 if (_M_end_of_seq() && __rhs._M_end_of_seq())
2948 return true;
2949 if (_M_suffix.matched && __rhs._M_suffix.matched
2950 && _M_suffix == __rhs._M_suffix)
2951 return true;
2952 if (_M_end_of_seq() || _M_suffix.matched
2953 || __rhs._M_end_of_seq() || __rhs._M_suffix.matched)
2954 return false;
2955 return _M_position == __rhs._M_position
2956 && _M_n == __rhs._M_n
2957 && _M_subs == __rhs._M_subs;
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++()
2966 {
2967 _Position __prev = _M_position;
2968 if (_M_suffix.matched)
2969 *this = regex_token_iterator();
2970 else if (_M_n + 1 < _M_subs.size())
2971 {
2972 _M_n++;
2973 _M_result = &_M_current_match();
2974 }
2975 else
2976 {
2977 _M_n = 0;
2978 ++_M_position;
2979 if (_M_position != _Position())
2980 _M_result = &_M_current_match();
2981 else if (_M_has_m1 && __prev->suffix().length() != 0)
2982 {
2983 _M_suffix.matched = true;
2984 _M_suffix.first = __prev->suffix().first;
2985 _M_suffix.second = __prev->suffix().second;
2986 _M_result = &_M_suffix;
2987 }
2988 else
2989 *this = regex_token_iterator();
2990 }
2991 return *this;
2992 }
2993
2994 template<typename _Bi_iter,
2995 typename _Ch_type,
2996 typename _Rx_traits>
2997 void
2998 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
2999 _M_init(_Bi_iter __a, _Bi_iter __b)
3000 {
3001 _M_has_m1 = false;
3002 for (auto __it : _M_subs)
3003 if (__it == -1)
3004 {
3005 _M_has_m1 = true;
3006 break;
3007 }
3008 if (_M_position != _Position())
3009 _M_result = &_M_current_match();
3010 else if (_M_has_m1)
3011 {
3012 _M_suffix.matched = true;
3013 _M_suffix.first = __a;
3014 _M_suffix.second = __b;
3015 _M_result = &_M_suffix;
3016 }
3017 else
3018 _M_result = nullptr;
3019 }
3020
3021 /** @brief Token iterator for C-style NULL-terminated strings. */
3022 typedef regex_token_iterator<const char*> cregex_token_iterator;
3023
3024 /** @brief Token iterator for standard strings. */
3025 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
3026
3027 #ifdef _GLIBCXX_USE_WCHAR_T
3028 /** @brief Token iterator for C-style NULL-terminated wide strings. */
3029 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
3030
3031 /** @brief Token iterator for standard wide-character strings. */
3032 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
3033 #endif
3034
3035 //@} // group regex
3036 _GLIBCXX_END_NAMESPACE_VERSION
3037 } // namespace
3038