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