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