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