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