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