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