STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
regex.h
Go to the documentation of this file.
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 
31 namespace std _GLIBCXX_VISIBILITY(default)
32 {
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
34 
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  typedef std::ctype_base::mask char_class_type;
58 
59  public:
63  regex_traits() { }
64 
75  static std::size_t
76  length(const char_type* __p)
77  { return string_type::traits_type::length(__p); }
78 
86  char_type
87  translate(char_type __c) const
88  { return __c; }
89 
99  char_type
100  translate_nocase(char_type __c) const
101  {
102  typedef std::ctype<char_type> __ctype_type;
103  const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
104  return __fctyp.tolower(__c);
105  }
106 
127  template<typename _Fwd_iter>
128  string_type
129  transform(_Fwd_iter __first, _Fwd_iter __last) const
130  {
131  typedef std::collate<char_type> __collate_type;
132  const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
133  string_type __s(__first, __last);
134  return __fclt.transform(__s.data(), __s.data() + __s.size());
135  }
136 
151  template<typename _Fwd_iter>
152  string_type
153  transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
154  { return string_type(); }
155 
169  template<typename _Fwd_iter>
170  string_type
171  lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
172  { return string_type(); }
173 
213  template<typename _Fwd_iter>
214  char_class_type
215  lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
216  bool __icase = false) const
217  { return 0; }
218 
231  bool
232  isctype(_Ch_type __c, char_class_type __f) const;
233 
244  int
245  value(_Ch_type __ch, int __radix) const;
246 
258  locale_type
259  imbue(locale_type __loc)
260  {
261  std::swap(_M_locale, __loc);
262  return __loc;
263  }
264 
269  locale_type
270  getloc() const
271  { return _M_locale; }
272 
273  protected:
274  locale_type _M_locale;
275  };
276 
277  template<typename _Ch_type>
278  bool
279  regex_traits<_Ch_type>::
280  isctype(_Ch_type __c, char_class_type __f) const
281  {
282  typedef std::ctype<char_type> __ctype_type;
283  const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
284 
285  if (__fctyp.is(__f, __c))
286  return true;
287 
288  // special case of underscore in [[:w:]]
289  if (__c == __fctyp.widen('_'))
290  {
291  const char __wb[] = "w";
292  char_class_type __wt = this->lookup_classname(__wb,
293  __wb + sizeof(__wb));
294  if (__f | __wt)
295  return true;
296  }
297 
298  // special case of [[:space:]] in [[:blank:]]
299  if (__fctyp.is(std::ctype_base::space, __c))
300  {
301  const char __bb[] = "blank";
302  char_class_type __bt = this->lookup_classname(__bb,
303  __bb + sizeof(__bb));
304  if (__f | __bt)
305  return true;
306  }
307 
308  return false;
309  }
310 
311  template<typename _Ch_type>
312  int
313  regex_traits<_Ch_type>::
314  value(_Ch_type __ch, int __radix) const
315  {
316  std::basic_istringstream<char_type> __is(string_type(1, __ch));
317  int __v;
318  if (__radix == 8)
319  __is >> std::oct;
320  else if (__radix == 16)
321  __is >> std::hex;
322  __is >> __v;
323  return __is.fail() ? -1 : __v;
324  }
325 
326  // [7.8] Class basic_regex
334  template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type> >
335  class basic_regex
336  {
337  public:
338  // types:
339  typedef _Ch_type value_type;
340  typedef _Rx_traits traits_type;
341  typedef typename traits_type::string_type string_type;
342  typedef regex_constants::syntax_option_type flag_type;
343  typedef typename traits_type::locale_type locale_type;
344 
350  static constexpr flag_type icase = regex_constants::icase;
351  static constexpr flag_type nosubs = regex_constants::nosubs;
352  static constexpr flag_type optimize = regex_constants::optimize;
353  static constexpr flag_type collate = regex_constants::collate;
354  static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
355  static constexpr flag_type basic = regex_constants::basic;
356  static constexpr flag_type extended = regex_constants::extended;
357  static constexpr flag_type awk = regex_constants::awk;
358  static constexpr flag_type grep = regex_constants::grep;
359  static constexpr flag_type egrep = regex_constants::egrep;
361 
362  // [7.8.2] construct/copy/destroy
367  basic_regex()
368  : _M_flags(ECMAScript),
369  _M_automaton(__detail::__compile<const _Ch_type*, _Rx_traits>(0, 0,
370  _M_traits, _M_flags))
371  { }
372 
384  explicit
385  basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
386  : _M_flags(__f),
387  _M_automaton(__detail::__compile(__p, __p + _Rx_traits::length(__p),
388  _M_traits, _M_flags))
389  { }
390 
403  basic_regex(const _Ch_type* __p, std::size_t __len, flag_type __f)
404  : _M_flags(__f),
405  _M_automaton(__detail::__compile(__p, __p + __len, _M_traits, _M_flags))
406  { }
407 
413  basic_regex(const basic_regex& __rhs)
414  : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
415  _M_automaton(__rhs._M_automaton)
416  { }
417 
423  basic_regex(const basic_regex&& __rhs) noexcept
424  : _M_flags(__rhs._M_flags), _M_traits(__rhs._M_traits),
425  _M_automaton(std::move(__rhs._M_automaton))
426  { }
427 
437  template<typename _Ch_traits, typename _Ch_alloc>
438  explicit
439  basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
440  _Ch_alloc>& __s,
441  flag_type __f = ECMAScript)
442  : _M_flags(__f),
443  _M_automaton(__detail::__compile(__s.begin(), __s.end(),
444  _M_traits, _M_flags))
445  { }
446 
460  template<typename _InputIterator>
461  basic_regex(_InputIterator __first, _InputIterator __last,
462  flag_type __f = ECMAScript)
463  : _M_flags(__f),
464  _M_automaton(__detail::__compile(__first, __last, _M_traits, _M_flags))
465  { }
466 
475  basic_regex(initializer_list<_Ch_type> __l,
476  flag_type __f = ECMAScript)
477  : _M_flags(__f),
478  _M_automaton(__detail::__compile(__l.begin(), __l.end(),
479  _M_traits, _M_flags))
480  { }
481 
485  ~basic_regex()
486  { }
487 
491  basic_regex&
492  operator=(const basic_regex& __rhs)
493  { return this->assign(__rhs); }
494 
498  basic_regex&
499  operator=(basic_regex&& __rhs) noexcept
500  { return this->assign(std::move(__rhs)); }
501 
509  basic_regex&
510  operator=(const _Ch_type* __p)
511  { return this->assign(__p, flags()); }
512 
519  template<typename _Ch_typeraits, typename _Alloc>
520  basic_regex&
522  { return this->assign(__s, flags()); }
523 
524  // [7.8.3] assign
530  basic_regex&
531  assign(const basic_regex& __rhs)
532  {
533  basic_regex __tmp(__rhs);
534  this->swap(__tmp);
535  return *this;
536  }
537 
543  basic_regex&
544  assign(basic_regex&& __rhs) noexcept
545  {
546  basic_regex __tmp(std::move(__rhs));
547  this->swap(__tmp);
548  return *this;
549  }
550 
564  basic_regex&
565  assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
566  { return this->assign(string_type(__p), __flags); }
567 
581  basic_regex&
582  assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
583  { return this->assign(string_type(__p, __len), __flags); }
584 
596  template<typename _Ch_typeraits, typename _Alloc>
597  basic_regex&
599  flag_type __flags = ECMAScript)
600  {
601  basic_regex __tmp(__s, __flags);
602  this->swap(__tmp);
603  return *this;
604  }
605 
619  template<typename _InputIterator>
620  basic_regex&
621  assign(_InputIterator __first, _InputIterator __last,
622  flag_type __flags = ECMAScript)
623  { return this->assign(string_type(__first, __last), __flags); }
624 
636  basic_regex&
637  assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
638  { return this->assign(__l.begin(), __l.end(), __flags); }
639 
640  // [7.8.4] const operations
645  unsigned int
646  mark_count() const
647  { return _M_automaton->_M_sub_count() - 1; }
648 
653  flag_type
654  flags() const
655  { return _M_flags; }
656 
657  // [7.8.5] locale
663  locale_type
664  imbue(locale_type __loc)
665  { return _M_traits.imbue(__loc); }
666 
671  locale_type
672  getloc() const
673  { return _M_traits.getloc(); }
674 
675  // [7.8.6] swap
681  void
682  swap(basic_regex& __rhs)
683  {
684  std::swap(_M_flags, __rhs._M_flags);
685  std::swap(_M_traits, __rhs._M_traits);
686  std::swap(_M_automaton, __rhs._M_automaton);
687  }
688 
689 #ifdef _GLIBCXX_DEBUG
690  void
691  _M_dot(std::ostream& __ostr)
692  { _M_automaton->_M_dot(__ostr); }
693 #endif
694 
695  const __detail::_AutomatonPtr&
696  _M_get_automaton() const
697  { return _M_automaton; }
698 
699  protected:
700  flag_type _M_flags;
701  _Rx_traits _M_traits;
702  __detail::_AutomatonPtr _M_automaton;
703  };
704 
706  typedef basic_regex<char> regex;
707 
708 #ifdef _GLIBCXX_USE_WCHAR_T
709 
710  typedef basic_regex<wchar_t> wregex;
711 #endif
712 
713 
714  // [7.8.6] basic_regex swap
720  template<typename _Ch_type, typename _Rx_traits>
721  inline void
722  swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
723  basic_regex<_Ch_type, _Rx_traits>& __rhs)
724  { __lhs.swap(__rhs); }
725 
726 
727  // [7.9] Class template sub_match
740  template<typename _BiIter>
741  class sub_match : public std::pair<_BiIter, _BiIter>
742  {
743  typedef iterator_traits<_BiIter> __iter_traits;
744 
745  public:
746  typedef typename __iter_traits::value_type value_type;
747  typedef typename __iter_traits::difference_type difference_type;
748  typedef _BiIter iterator;
749  typedef std::basic_string<value_type> string_type;
750 
751  bool matched;
752 
753  constexpr sub_match() : matched() { }
754 
758  difference_type
759  length() const
760  { return this->matched ? std::distance(this->first, this->second) : 0; }
761 
772  operator string_type() const
773  {
774  return this->matched
775  ? string_type(this->first, this->second)
776  : string_type();
777  }
778 
784  string_type
785  str() const
786  {
787  return this->matched
788  ? string_type(this->first, this->second)
789  : string_type();
790  }
791 
801  int
802  compare(const sub_match& __s) const
803  { return this->str().compare(__s.str()); }
804 
814  int
815  compare(const string_type& __s) const
816  { return this->str().compare(__s); }
817 
827  int
828  compare(const value_type* __s) const
829  { return this->str().compare(__s); }
830  };
831 
832 
834  typedef sub_match<const char*> csub_match;
835 
837  typedef sub_match<string::const_iterator> ssub_match;
838 
839 #ifdef _GLIBCXX_USE_WCHAR_T
840 
841  typedef sub_match<const wchar_t*> wcsub_match;
842 
844  typedef sub_match<wstring::const_iterator> wssub_match;
845 #endif
846 
847  // [7.9.2] sub_match non-member operators
848 
855  template<typename _BiIter>
856  inline bool
857  operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
858  { return __lhs.compare(__rhs) == 0; }
859 
866  template<typename _BiIter>
867  inline bool
868  operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
869  { return __lhs.compare(__rhs) != 0; }
870 
877  template<typename _BiIter>
878  inline bool
879  operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
880  { return __lhs.compare(__rhs) < 0; }
881 
888  template<typename _BiIter>
889  inline bool
890  operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
891  { return __lhs.compare(__rhs) <= 0; }
892 
899  template<typename _BiIter>
900  inline bool
901  operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
902  { return __lhs.compare(__rhs) >= 0; }
903 
910  template<typename _BiIter>
911  inline bool
912  operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
913  { return __lhs.compare(__rhs) > 0; }
914 
915  // Alias for sub_match'd string.
916  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
917  using __sub_match_string = basic_string<
918  typename iterator_traits<_Bi_iter>::value_type,
919  _Ch_traits, _Ch_alloc>;
920 
928  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
929  inline bool
930  operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
931  const sub_match<_Bi_iter>& __rhs)
932  { return __rhs.compare(__lhs.c_str()) == 0; }
933 
941  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
942  inline bool
943  operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
944  const sub_match<_Bi_iter>& __rhs)
945  { return !(__lhs == __rhs); }
946 
953  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
954  inline bool
955  operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
956  const sub_match<_Bi_iter>& __rhs)
957  { return __rhs.compare(__lhs.c_str()) > 0; }
958 
965  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
966  inline bool
967  operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
968  const sub_match<_Bi_iter>& __rhs)
969  { return __rhs < __lhs; }
970 
977  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
978  inline bool
979  operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
980  const sub_match<_Bi_iter>& __rhs)
981  { return !(__lhs < __rhs); }
982 
989  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
990  inline bool
991  operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
992  const sub_match<_Bi_iter>& __rhs)
993  { return !(__rhs < __lhs); }
994 
1002  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1003  inline bool
1004  operator==(const sub_match<_Bi_iter>& __lhs,
1005  const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1006  { return __lhs.compare(__rhs.c_str()) == 0; }
1007 
1015  template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1016  inline bool
1017  operator!=(const sub_match<_Bi_iter>& __lhs,
1018  const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1019  { return !(__lhs == __rhs); }
1020 
1027  template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1028  inline bool
1029  operator<(const sub_match<_Bi_iter>& __lhs,
1030  const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1031  { return __lhs.compare(__rhs.c_str()) < 0; }
1032 
1039  template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1040  inline bool
1041  operator>(const sub_match<_Bi_iter>& __lhs,
1042  const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1043  { return __rhs < __lhs; }
1044 
1051  template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1052  inline bool
1053  operator>=(const sub_match<_Bi_iter>& __lhs,
1054  const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1055  { return !(__lhs < __rhs); }
1056 
1063  template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1064  inline bool
1065  operator<=(const sub_match<_Bi_iter>& __lhs,
1066  const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1067  { return !(__rhs < __lhs); }
1068 
1076  template<typename _Bi_iter>
1077  inline bool
1078  operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1079  const sub_match<_Bi_iter>& __rhs)
1080  { return __rhs.compare(__lhs) == 0; }
1081 
1089  template<typename _Bi_iter>
1090  inline bool
1091  operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1092  const sub_match<_Bi_iter>& __rhs)
1093  { return !(__lhs == __rhs); }
1094 
1101  template<typename _Bi_iter>
1102  inline bool
1103  operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1104  const sub_match<_Bi_iter>& __rhs)
1105  { return __rhs.compare(__lhs) > 0; }
1106 
1113  template<typename _Bi_iter>
1114  inline bool
1115  operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1116  const sub_match<_Bi_iter>& __rhs)
1117  { return __rhs < __lhs; }
1118 
1125  template<typename _Bi_iter>
1126  inline bool
1127  operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1128  const sub_match<_Bi_iter>& __rhs)
1129  { return !(__lhs < __rhs); }
1130 
1137  template<typename _Bi_iter>
1138  inline bool
1139  operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1140  const sub_match<_Bi_iter>& __rhs)
1141  { return !(__rhs < __lhs); }
1142 
1150  template<typename _Bi_iter>
1151  inline bool
1152  operator==(const sub_match<_Bi_iter>& __lhs,
1153  typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1154  { return __lhs.compare(__rhs) == 0; }
1155 
1163  template<typename _Bi_iter>
1164  inline bool
1165  operator!=(const sub_match<_Bi_iter>& __lhs,
1166  typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1167  { return !(__lhs == __rhs); }
1168 
1175  template<typename _Bi_iter>
1176  inline bool
1177  operator<(const sub_match<_Bi_iter>& __lhs,
1178  typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1179  { return __lhs.compare(__rhs) < 0; }
1180 
1187  template<typename _Bi_iter>
1188  inline bool
1189  operator>(const sub_match<_Bi_iter>& __lhs,
1190  typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1191  { return __rhs < __lhs; }
1192 
1199  template<typename _Bi_iter>
1200  inline bool
1201  operator>=(const sub_match<_Bi_iter>& __lhs,
1202  typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1203  { return !(__lhs < __rhs); }
1204 
1211  template<typename _Bi_iter>
1212  inline bool
1213  operator<=(const sub_match<_Bi_iter>& __lhs,
1214  typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1215  { return !(__rhs < __lhs); }
1216 
1224  template<typename _Bi_iter>
1225  inline bool
1226  operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1227  const sub_match<_Bi_iter>& __rhs)
1228  {
1229  typedef typename sub_match<_Bi_iter>::string_type string_type;
1230  return __rhs.compare(string_type(1, __lhs)) == 0;
1231  }
1232 
1240  template<typename _Bi_iter>
1241  inline bool
1242  operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1243  const sub_match<_Bi_iter>& __rhs)
1244  { return !(__lhs == __rhs); }
1245 
1252  template<typename _Bi_iter>
1253  inline bool
1254  operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1255  const sub_match<_Bi_iter>& __rhs)
1256  {
1257  typedef typename sub_match<_Bi_iter>::string_type string_type;
1258  return __rhs.compare(string_type(1, __lhs)) > 0;
1259  }
1260 
1267  template<typename _Bi_iter>
1268  inline bool
1269  operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1270  const sub_match<_Bi_iter>& __rhs)
1271  { return __rhs < __lhs; }
1272 
1279  template<typename _Bi_iter>
1280  inline bool
1281  operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1282  const sub_match<_Bi_iter>& __rhs)
1283  { return !(__lhs < __rhs); }
1284 
1291  template<typename _Bi_iter>
1292  inline bool
1293  operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1294  const sub_match<_Bi_iter>& __rhs)
1295  { return !(__rhs < __lhs); }
1296 
1304  template<typename _Bi_iter>
1305  inline bool
1306  operator==(const sub_match<_Bi_iter>& __lhs,
1307  typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1308  {
1309  typedef typename sub_match<_Bi_iter>::string_type string_type;
1310  return __lhs.compare(string_type(1, __rhs)) == 0;
1311  }
1312 
1320  template<typename _Bi_iter>
1321  inline bool
1322  operator!=(const sub_match<_Bi_iter>& __lhs,
1323  typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1324  { return !(__lhs == __rhs); }
1325 
1332  template<typename _Bi_iter>
1333  inline bool
1334  operator<(const sub_match<_Bi_iter>& __lhs,
1335  typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1336  {
1337  typedef typename sub_match<_Bi_iter>::string_type string_type;
1338  return __lhs.compare(string_type(1, __rhs)) < 0;
1339  }
1340 
1347  template<typename _Bi_iter>
1348  inline bool
1349  operator>(const sub_match<_Bi_iter>& __lhs,
1350  typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1351  { return __rhs < __lhs; }
1352 
1359  template<typename _Bi_iter>
1360  inline bool
1361  operator>=(const sub_match<_Bi_iter>& __lhs,
1362  typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1363  { return !(__lhs < __rhs); }
1364 
1371  template<typename _Bi_iter>
1372  inline bool
1373  operator<=(const sub_match<_Bi_iter>& __lhs,
1374  typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1375  { return !(__rhs < __lhs); }
1376 
1385  template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1386  inline
1387  basic_ostream<_Ch_type, _Ch_traits>&
1388  operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1389  const sub_match<_Bi_iter>& __m)
1390  { return __os << __m.str(); }
1391 
1392  // [7.10] Class template match_results
1393 
1394  /*
1395  * Special sub_match object representing an unmatched sub-expression.
1396  */
1397  template<typename _Bi_iter>
1398  inline const sub_match<_Bi_iter>&
1399  __unmatched_sub()
1400  {
1401  static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1402  return __unmatched;
1403  }
1404 
1427  template<typename _Bi_iter,
1428  typename _Alloc = allocator<sub_match<_Bi_iter> > >
1429  class match_results
1430  : private std::vector<sub_match<_Bi_iter>, _Alloc>
1431  {
1432  private:
1433  /*
1434  * The vector base is empty if this does not represent a successful match.
1435  * Otherwise it contains n+3 elements where n is the number of marked
1436  * sub-expressions:
1437  * [0] entire match
1438  * [1] 1st marked subexpression
1439  * ...
1440  * [n] nth marked subexpression
1441  * [n+1] prefix
1442  * [n+2] suffix
1443  */
1444  typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1445  typedef std::iterator_traits<_Bi_iter> __iter_traits;
1446  typedef regex_constants::match_flag_type match_flag_type;
1447 
1448  public:
1453  typedef _Alloc allocator_type;
1454  typedef sub_match<_Bi_iter> value_type;
1455  typedef const value_type& const_reference;
1456  typedef const_reference reference;
1457  typedef typename _Base_type::const_iterator const_iterator;
1458  typedef const_iterator iterator;
1459  typedef typename __iter_traits::difference_type difference_type;
1460  typedef typename __iter_traits::value_type char_type;
1461  typedef typename allocator_traits<_Alloc>::size_type size_type;
1462 
1463 
1464  typedef std::basic_string<char_type> string_type;
1466 
1467  public:
1472 
1477  explicit
1478  match_results(const _Alloc& __a = _Alloc())
1479  : _Base_type(__a)
1480  { }
1481 
1485  match_results(const match_results& __rhs)
1486  : _Base_type(__rhs)
1487  { }
1488 
1492  match_results(match_results&& __rhs) noexcept
1493  : _Base_type(std::move(__rhs))
1494  { }
1495 
1499  match_results&
1500  operator=(const match_results& __rhs)
1501  {
1502  match_results(__rhs).swap(*this);
1503  return *this;
1504  }
1505 
1509  match_results&
1510  operator=(match_results&& __rhs)
1511  {
1512  match_results(std::move(__rhs)).swap(*this);
1513  return *this;
1514  }
1515 
1519  ~match_results()
1520  { }
1521 
1523 
1524  // 28.10.2, state:
1530  bool ready() const { return !_Base_type::empty(); }
1531 
1536 
1546  size_type
1547  size() const
1548  {
1549  size_type __size = _Base_type::size();
1550  return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1551  }
1552 
1553  size_type
1554  max_size() const
1555  { return _Base_type::max_size(); }
1556 
1562  bool
1563  empty() const
1564  { return size() == 0; }
1565 
1567 
1572 
1581  difference_type
1582  length(size_type __sub = 0) const
1583  { return (*this)[__sub].length(); }
1584 
1598  difference_type
1599  position(size_type __sub = 0) const
1600  {
1601  return __sub < size() ? std::distance(this->prefix().first,
1602  (*this)[__sub].first) : -1;
1603  }
1604 
1614  string_type
1615  str(size_type __sub = 0) const
1616  { return (*this)[__sub].str(); }
1617 
1629  const_reference
1630  operator[](size_type __sub) const
1631  {
1632  _GLIBCXX_DEBUG_ASSERT( ready() );
1633  return __sub < size()
1634  ? _Base_type::operator[](__sub)
1635  : __unmatched_sub<_Bi_iter>();
1636  }
1637 
1646  const_reference
1647  prefix() const
1648  {
1649  _GLIBCXX_DEBUG_ASSERT( ready() );
1650  return !empty()
1651  ? _Base_type::operator[](_Base_type::size() - 2)
1652  : __unmatched_sub<_Bi_iter>();
1653  }
1654 
1663  const_reference
1664  suffix() const
1665  {
1666  _GLIBCXX_DEBUG_ASSERT( ready() );
1667  return !empty()
1668  ? _Base_type::operator[](_Base_type::size() - 1)
1669  : __unmatched_sub<_Bi_iter>();
1670  }
1671 
1675  const_iterator
1676  begin() const
1677  { return _Base_type::begin(); }
1678 
1682  const_iterator
1683  cbegin() const
1684  { return _Base_type::cbegin(); }
1685 
1689  const_iterator
1690  end() const
1691  { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
1692 
1696  const_iterator
1697  cend() const
1698  { return end(); }
1699 
1701 
1711 
1716  template<typename _Out_iter>
1717  _Out_iter
1718  format(_Out_iter __out, const char_type* __fmt_first,
1719  const char_type* __fmt_last,
1720  match_flag_type __flags = regex_constants::format_default) const
1721  { return __out; }
1722 
1726  template<typename _Out_iter, typename _St, typename _Sa>
1727  _Out_iter
1728  format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1729  match_flag_type __flags = regex_constants::format_default) const
1730  {
1731  return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1732  __flags);
1733  }
1734 
1738  template<typename _Out_iter, typename _St, typename _Sa>
1740  format(const basic_string<char_type, _St, _Sa>& __fmt,
1741  match_flag_type __flags = regex_constants::format_default) const
1742  {
1744  format(std::back_inserter(__result), __fmt, __flags);
1745  return __result;
1746  }
1747 
1751  string_type
1752  format(const char_type* __fmt,
1753  match_flag_type __flags = regex_constants::format_default) const
1754  {
1755  string_type __result;
1756  format(std::back_inserter(__result),
1757  __fmt + char_traits<char_type>::length(__fmt),
1758  __flags);
1759  return __result;
1760  }
1761 
1763 
1768 
1772  allocator_type
1773  get_allocator() const
1774  { return _Base_type::get_allocator(); }
1775 
1777 
1782 
1786  void
1787  swap(match_results& __that)
1788  { _Base_type::swap(__that); }
1790 
1791  private:
1792  friend class __detail::_SpecializedResults<_Bi_iter, _Alloc>;
1793  };
1794 
1795  typedef match_results<const char*> cmatch;
1796  typedef match_results<string::const_iterator> smatch;
1797 #ifdef _GLIBCXX_USE_WCHAR_T
1798  typedef match_results<const wchar_t*> wcmatch;
1799  typedef match_results<wstring::const_iterator> wsmatch;
1800 #endif
1801 
1802  // match_results comparisons
1808  template<typename _Bi_iter, typename _Alloc>
1809  inline bool
1810  operator==(const match_results<_Bi_iter, _Alloc>& __m1,
1811  const match_results<_Bi_iter, _Alloc>& __m2)
1812  {
1813  if (__m1.ready() != __m2.ready())
1814  return false;
1815  if (!__m1.ready()) // both are not ready
1816  return true;
1817  if (__m1.empty() != __m2.empty())
1818  return false;
1819  if (__m1.empty()) // both are empty
1820  return true;
1821  return __m1.prefix() == __m2.prefix()
1822  && __m1.size() == __m2.size()
1823  && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1824  && __m1.suffix() == __m2.suffix();
1825  }
1826 
1832  template<typename _Bi_iter, class _Alloc>
1833  inline bool
1834  operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
1835  const match_results<_Bi_iter, _Alloc>& __m2)
1836  { return !(__m1 == __m2); }
1837 
1838  // [7.10.6] match_results swap
1846  template<typename _Bi_iter, typename _Alloc>
1847  inline void
1848  swap(match_results<_Bi_iter, _Alloc>& __lhs,
1849  match_results<_Bi_iter, _Alloc>& __rhs)
1850  { __lhs.swap(__rhs); }
1851 
1852  // [7.11.2] Function template regex_match
1857 
1875  template<typename _Bi_iter, typename _Alloc,
1876  typename _Ch_type, typename _Rx_traits>
1877  bool
1878  regex_match(_Bi_iter __s,
1879  _Bi_iter __e,
1880  match_results<_Bi_iter, _Alloc>& __m,
1881  const basic_regex<_Ch_type, _Rx_traits>& __re,
1882  regex_constants::match_flag_type __flags
1883  = regex_constants::match_default)
1884  {
1885  __detail::_AutomatonPtr __a = __re._M_get_automaton();
1886  __detail::_Automaton::_SizeT __sz = __a->_M_sub_count();
1887  __detail::_SpecializedCursor<_Bi_iter> __cs(__s, __e);
1888  __detail::_SpecializedResults<_Bi_iter, _Alloc> __r(__sz, __cs, __m);
1889  __detail::_Grep_matcher __matcher(__cs, __r, __a, __flags);
1890  return __m[0].matched;
1891  }
1892 
1907  template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1908  bool
1909  regex_match(_Bi_iter __first, _Bi_iter __last,
1910  const basic_regex<_Ch_type, _Rx_traits>& __re,
1911  regex_constants::match_flag_type __flags
1912  = regex_constants::match_default)
1913  {
1914  match_results<_Bi_iter> __what;
1915  return regex_match(__first, __last, __what, __re, __flags);
1916  }
1917 
1932  template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
1933  inline bool
1934  regex_match(const _Ch_type* __s,
1935  match_results<const _Ch_type*, _Alloc>& __m,
1936  const basic_regex<_Ch_type, _Rx_traits>& __re,
1937  regex_constants::match_flag_type __f
1938  = regex_constants::match_default)
1939  { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
1940 
1955  template<typename _Ch_traits, typename _Ch_alloc,
1956  typename _Alloc, typename _Ch_type, typename _Rx_traits>
1957  inline bool
1958  regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
1959  match_results<typename basic_string<_Ch_type,
1960  _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
1961  const basic_regex<_Ch_type, _Rx_traits>& __re,
1962  regex_constants::match_flag_type __flags
1963  = regex_constants::match_default)
1964  { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
1965 
1979  template<typename _Ch_type, class _Rx_traits>
1980  inline bool
1981  regex_match(const _Ch_type* __s,
1982  const basic_regex<_Ch_type, _Rx_traits>& __re,
1983  regex_constants::match_flag_type __f
1984  = regex_constants::match_default)
1985  { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
1986 
2000  template<typename _Ch_traits, typename _Str_allocator,
2001  typename _Ch_type, typename _Rx_traits>
2002  inline bool
2004  const basic_regex<_Ch_type, _Rx_traits>& __re,
2005  regex_constants::match_flag_type __flags
2006  = regex_constants::match_default)
2007  { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2008 
2009  // [7.11.3] Function template regex_search
2025  template<typename _Bi_iter, typename _Alloc,
2026  typename _Ch_type, typename _Rx_traits>
2027  inline bool
2028  regex_search(_Bi_iter __first, _Bi_iter __last,
2029  match_results<_Bi_iter, _Alloc>& __m,
2030  const basic_regex<_Ch_type, _Rx_traits>& __re,
2031  regex_constants::match_flag_type __flags
2032  = regex_constants::match_default)
2033  { return false; }
2034 
2047  template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2048  inline bool
2049  regex_search(_Bi_iter __first, _Bi_iter __last,
2050  const basic_regex<_Ch_type, _Rx_traits>& __re,
2051  regex_constants::match_flag_type __flags
2052  = regex_constants::match_default)
2053  {
2054  match_results<_Bi_iter> __what;
2055  return regex_search(__first, __last, __what, __re, __flags);
2056  }
2057 
2071  template<typename _Ch_type, class _Alloc, class _Rx_traits>
2072  inline bool
2073  regex_search(const _Ch_type* __s,
2074  match_results<const _Ch_type*, _Alloc>& __m,
2075  const basic_regex<_Ch_type, _Rx_traits>& __e,
2076  regex_constants::match_flag_type __f
2077  = regex_constants::match_default)
2078  { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2079 
2091  template<typename _Ch_type, typename _Rx_traits>
2092  inline bool
2093  regex_search(const _Ch_type* __s,
2094  const basic_regex<_Ch_type, _Rx_traits>& __e,
2095  regex_constants::match_flag_type __f
2096  = regex_constants::match_default)
2097  { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2098 
2110  template<typename _Ch_traits, typename _String_allocator,
2111  typename _Ch_type, typename _Rx_traits>
2112  inline bool
2113  regex_search(const basic_string<_Ch_type, _Ch_traits,
2114  _String_allocator>& __s,
2115  const basic_regex<_Ch_type, _Rx_traits>& __e,
2116  regex_constants::match_flag_type __flags
2117  = regex_constants::match_default)
2118  { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2119 
2132  template<typename _Ch_traits, typename _Ch_alloc,
2133  typename _Alloc, typename _Ch_type,
2134  typename _Rx_traits>
2135  inline bool
2136  regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2137  match_results<typename basic_string<_Ch_type,
2138  _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2139  const basic_regex<_Ch_type, _Rx_traits>& __e,
2140  regex_constants::match_flag_type __f
2141  = regex_constants::match_default)
2142  { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2143 
2144  // std [28.11.4] Function template regex_replace
2159  template<typename _Out_iter, typename _Bi_iter,
2160  typename _Rx_traits, typename _Ch_type>
2161  inline _Out_iter
2162  regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2163  const basic_regex<_Ch_type, _Rx_traits>& __e,
2164  const basic_string<_Ch_type>& __fmt,
2165  regex_constants::match_flag_type __flags
2166  = regex_constants::match_default)
2167  { return __out; }
2168 
2180  template<typename _Rx_traits, typename _Ch_type>
2181  inline basic_string<_Ch_type>
2182  regex_replace(const basic_string<_Ch_type>& __s,
2183  const basic_regex<_Ch_type, _Rx_traits>& __e,
2184  const basic_string<_Ch_type>& __fmt,
2185  regex_constants::match_flag_type __flags
2186  = regex_constants::match_default)
2187  {
2188  basic_string<_Ch_type> __result;
2189  regex_replace(std::back_inserter(__result),
2190  __s.begin(), __s.end(), __e, __fmt, __flags);
2191  return __result;
2192  }
2193 
2195 
2196  // std [28.12] Class template regex_iterator
2201  template<typename _Bi_iter,
2202  typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2203  typename _Rx_traits = regex_traits<_Ch_type> >
2204  class regex_iterator
2205  {
2206  public:
2207  typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2208  typedef match_results<_Bi_iter> value_type;
2209  typedef std::ptrdiff_t difference_type;
2210  typedef const value_type* pointer;
2211  typedef const value_type& reference;
2212  typedef std::forward_iterator_tag iterator_category;
2213 
2220  regex_iterator();
2221 
2231  regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2232  regex_constants::match_flag_type __m
2233  = regex_constants::match_default);
2234 
2240  regex_iterator(const regex_iterator& __rhs);
2241 
2246  regex_iterator&
2247  operator=(const regex_iterator& __rhs);
2248 
2253  bool
2254  operator==(const regex_iterator& __rhs);
2255 
2260  bool
2261  operator!=(const regex_iterator& __rhs);
2262 
2267  const value_type&
2268  operator*();
2269 
2274  const value_type*
2275  operator->();
2276 
2281  regex_iterator&
2282  operator++();
2283 
2288  regex_iterator
2289  operator++(int);
2290 
2291  private:
2292  // these members are shown for exposition only:
2293  _Bi_iter begin;
2294  _Bi_iter end;
2295  const regex_type* pregex;
2296  regex_constants::match_flag_type flags;
2297  match_results<_Bi_iter> match;
2298  };
2299 
2300  typedef regex_iterator<const char*> cregex_iterator;
2301  typedef regex_iterator<string::const_iterator> sregex_iterator;
2302 #ifdef _GLIBCXX_USE_WCHAR_T
2303  typedef regex_iterator<const wchar_t*> wcregex_iterator;
2304  typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2305 #endif
2306 
2307  // [7.12.2] Class template regex_token_iterator
2315  template<typename _Bi_iter,
2316  typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2317  typename _Rx_traits = regex_traits<_Ch_type> >
2318  class regex_token_iterator
2319  {
2320  public:
2321  typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2322  typedef sub_match<_Bi_iter> value_type;
2323  typedef std::ptrdiff_t difference_type;
2324  typedef const value_type* pointer;
2325  typedef const value_type& reference;
2326  typedef std::forward_iterator_tag iterator_category;
2327 
2328  public:
2337  regex_token_iterator();
2338 
2359  regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2360  int __submatch = 0,
2361  regex_constants::match_flag_type __m
2362  = regex_constants::match_default);
2363 
2376  regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2377  const regex_type& __re,
2378  const std::vector<int>& __submatches,
2379  regex_constants::match_flag_type __m
2380  = regex_constants::match_default);
2381 
2394  template<std::size_t _Nm>
2395  regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2396  const regex_type& __re,
2397  const int (&__submatches)[_Nm],
2398  regex_constants::match_flag_type __m
2399  = regex_constants::match_default);
2400 
2406  regex_token_iterator(const regex_token_iterator& __rhs);
2407 
2413  regex_token_iterator&
2414  operator=(const regex_token_iterator& __rhs);
2415 
2420  bool
2421  operator==(const regex_token_iterator& __rhs);
2422 
2427  bool
2428  operator!=(const regex_token_iterator& __rhs);
2429 
2434  const value_type&
2435  operator*();
2436 
2441  const value_type*
2442  operator->();
2443 
2448  regex_token_iterator&
2449  operator++();
2450 
2455  regex_token_iterator
2456  operator++(int);
2457 
2458  private: // data members for exposition only:
2459  typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> position_iterator;
2460 
2461  position_iterator __position;
2462  const value_type* __result;
2463  value_type __suffix;
2464  std::size_t __n;
2465  std::vector<int> __subs;
2466  };
2467 
2469  typedef regex_token_iterator<const char*> cregex_token_iterator;
2470 
2472  typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2473 
2474 #ifdef _GLIBCXX_USE_WCHAR_T
2475 
2476  typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2477 
2479  typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2480 #endif
2481 
2483 _GLIBCXX_END_NAMESPACE_VERSION
2484 } // namespace
2485 
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
std::size_t __size(__stack_t __stack)
Definition: profiler_node.h:68
#define _GLIBCXX_DEBUG_ASSERT(_Condition)
Definition: debug.h:61
namespace std _GLIBCXX_VISIBILITY(default)
Definition: auto_ptr.h:36
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
__SIZE_TYPE__ size_t
Definition: stddef.h:212
__PTRDIFF_TYPE__ ptrdiff_t
Definition: stddef.h:147
Definition: basic_string.h:45
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160