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