1 // class template regex -*- C++ -*-
3 // Copyright (C) 2013 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 * @file bits/regex.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
31 namespace std _GLIBCXX_VISIBILITY(default)
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39 // Result of merging regex_match and regex_search.
41 // __policy now can be _S_auto(auto dispatch by checking back-references)
42 // and _S_force_dfs(just use _DFSExecutor).
44 // That __match_mode is true means regex_match, else regex_search.
45 template<typename _BiIter, typename _Alloc,
46 typename _CharT, typename _TraitsT,
47 _RegexExecutorPolicy __policy,
50 __regex_algo_impl(_BiIter __s,
52 match_results<_BiIter, _Alloc>& __m,
53 const basic_regex<_CharT, _TraitsT>& __re,
54 regex_constants::match_flag_type __flags)
56 if (__re._M_automaton == nullptr)
59 typename match_results<_BiIter, _Alloc>::_Base_type& __res = __m;
60 __res.resize(__re._M_automaton->_M_sub_count() + 2);
61 for (auto& __it : __res)
64 typedef std::unique_ptr<_Executor<_BiIter, _Alloc, _CharT, _TraitsT>>
66 typedef _DFSExecutor<_BiIter, _Alloc, _CharT, _TraitsT> _DFSExecutorT;
67 typedef _BFSExecutor<_BiIter, _Alloc, _CharT, _TraitsT> _BFSExecutorT;
69 _ExecutorPtr __executor =
70 __get_executor<_BiIter, _Alloc, _CharT, _TraitsT,
71 __policy>(__s, __e, __res, __re, __flags);
75 __ret = __executor->_M_match();
77 __ret = __executor->_M_search();
80 for (auto __it : __res)
82 __it.first = __it.second = __e;
83 auto& __pre = __res[__res.size()-2];
84 auto& __suf = __res[__res.size()-1];
87 __pre.matched = false;
90 __suf.matched = false;
97 __pre.second = __res[0].first;
98 __pre.matched = (__pre.first != __pre.second);
99 __suf.first = __res[0].second;
101 __suf.matched = (__suf.first != __suf.second);
103 if (__re.flags() & regex_constants::nosubs)
109 _GLIBCXX_END_NAMESPACE_VERSION
112 template<typename _Ch_type>
113 template<typename _Fwd_iter>
114 typename regex_traits<_Ch_type>::string_type
115 regex_traits<_Ch_type>::
116 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
118 typedef std::ctype<char_type> __ctype_type;
119 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
121 static const char* __collatenames[] =
214 "left-square-bracket",
216 "right-square-bracket",
246 "left-curly-bracket",
248 "right-curly-bracket",
255 //static const char* __digraphs[] =
281 std::string __s(__last - __first, '?');
282 __fctyp.narrow(__first, __last, '?', &*__s.begin());
284 for (unsigned int __i = 0; *__collatenames[__i]; __i++)
285 if (__s == __collatenames[__i])
286 return string_type(1, __fctyp.widen(static_cast<char>(__i)));
288 //for (unsigned int __i = 0; *__digraphs[__i]; __i++)
290 // const char* __now = __digraphs[__i];
293 // string_type ret(__s.size(), __fctyp.widen('?'));
294 // __fctyp.widen(__now, __now + 2/* ouch */, &*ret.begin());
298 return string_type();
301 template<typename _Ch_type>
302 template<typename _Fwd_iter>
303 typename regex_traits<_Ch_type>::char_class_type
304 regex_traits<_Ch_type>::
305 lookup_classname(_Fwd_iter __first, _Fwd_iter __last, bool __icase) const
307 typedef std::ctype<char_type> __ctype_type;
308 typedef std::ctype<char> __cctype_type;
309 typedef const pair<const char*, char_class_type> _ClassnameEntry;
310 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
311 const __cctype_type& __cctyp(use_facet<__cctype_type>(_M_locale));
313 static _ClassnameEntry __classnames[] =
315 {"d", ctype_base::digit},
316 {"w", {ctype_base::alnum, _RegexMask::_S_under}},
317 {"s", ctype_base::space},
318 {"alnum", ctype_base::alnum},
319 {"alpha", ctype_base::alpha},
320 {"blank", {0, _RegexMask::_S_blank}},
321 {"cntrl", ctype_base::cntrl},
322 {"digit", ctype_base::digit},
323 {"graph", ctype_base::graph},
324 {"lower", ctype_base::lower},
325 {"print", ctype_base::print},
326 {"punct", ctype_base::punct},
327 {"space", ctype_base::space},
328 {"upper", ctype_base::upper},
329 {"xdigit", ctype_base::xdigit},
332 std::string __s(__last - __first, '?');
333 __fctyp.narrow(__first, __last, '?', &__s[0]);
334 __cctyp.tolower(&*__s.begin(), &*__s.begin() + __s.size());
335 for (_ClassnameEntry* __it = __classnames;
336 __it < *(&__classnames + 1);
339 if (__s == __it->first)
343 & (ctype_base::lower | ctype_base::upper)) != 0))
344 return ctype_base::alpha;
351 template<typename _Ch_type>
353 regex_traits<_Ch_type>::
354 isctype(_Ch_type __c, char_class_type __f) const
356 typedef std::ctype<char_type> __ctype_type;
357 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
359 return __fctyp.is(__f._M_base, __c)
361 || ((__f._M_extended & _RegexMask::_S_under)
362 && __c == __fctyp.widen('_'))
364 || ((__f._M_extended & _RegexMask::_S_blank)
365 && (__c == __fctyp.widen(' ')
366 || __c == __fctyp.widen('\t')));
369 template<typename _Ch_type>
371 regex_traits<_Ch_type>::
372 value(_Ch_type __ch, int __radix) const
374 std::basic_istringstream<char_type> __is(string_type(1, __ch));
378 else if (__radix == 16)
381 return __is.fail() ? -1 : __v;
384 template<typename _Bi_iter, typename _Alloc>
385 template<typename _Out_iter>
386 _Out_iter match_results<_Bi_iter, _Alloc>::
387 format(_Out_iter __out,
388 const match_results<_Bi_iter, _Alloc>::char_type* __fmt_first,
389 const match_results<_Bi_iter, _Alloc>::char_type* __fmt_last,
390 match_flag_type __flags) const
392 _GLIBCXX_DEBUG_ASSERT( ready() );
393 regex_traits<char_type> __traits;
394 typedef std::ctype<char_type> __ctype_type;
396 __fctyp(use_facet<__ctype_type>(__traits.getloc()));
398 auto __output = [&](size_t __idx)
400 auto& __sub = _Base_type::operator[](__idx);
402 std::copy(__sub.first, __sub.second, __out);
405 if (__flags & regex_constants::format_sed)
407 for (; __fmt_first != __fmt_last;)
408 if (*__fmt_first == '&')
413 else if (*__fmt_first == '\\')
415 if (++__fmt_first != __fmt_last
416 && __fctyp.is(__ctype_type::digit, *__fmt_first))
417 __output(__traits.value(*__fmt_first++, 10));
422 *__out++ = *__fmt_first++;
428 auto __next = std::find(__fmt_first, __fmt_last, '$');
429 if (__next == __fmt_last)
432 std::copy(__fmt_first, __next, __out);
434 auto __eat = [&](char __ch) -> bool
444 if (++__next == __fmt_last)
451 __output(_Base_type::size()-2);
452 else if (__eat('\''))
453 __output(_Base_type::size()-1);
454 else if (__fctyp.is(__ctype_type::digit, *__next))
456 long __num = __traits.value(*__next, 10);
457 if (++__next != __fmt_last
458 && __fctyp.is(__ctype_type::digit, *__next))
461 __num += __traits.value(*__next++, 10);
463 if (0 <= __num && __num < this->size())
468 __fmt_first = __next;
470 std::copy(__fmt_first, __fmt_last, __out);
475 template<typename _Out_iter, typename _Bi_iter,
476 typename _Rx_traits, typename _Ch_type>
478 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
479 const basic_regex<_Ch_type, _Rx_traits>& __e,
480 const _Ch_type* __fmt,
481 regex_constants::match_flag_type __flags)
483 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _IterT;
484 _IterT __i(__first, __last, __e, __flags);
488 if (!(__flags & regex_constants::format_no_copy))
489 std::copy(__first, __last, __out);
493 sub_match<_Bi_iter> __last;
494 auto __len = char_traits<_Ch_type>::length(__fmt);
495 for (; __i != __end; ++__i)
497 if (!(__flags & regex_constants::format_no_copy))
498 std::copy(__i->prefix().first, __i->prefix().second, __out);
499 __out = __i->format(__out, __fmt, __fmt + __len, __flags);
500 __last = __i->suffix();
501 if (__flags & regex_constants::format_first_only)
504 if (!(__flags & regex_constants::format_no_copy))
505 std::copy(__last.first, __last.second, __out);
510 template<typename _Bi_iter,
514 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
515 operator==(const regex_iterator& __rhs) const
517 return (_M_match.empty() && __rhs._M_match.empty())
518 || (_M_begin == __rhs._M_begin
519 && _M_end == __rhs._M_end
520 && _M_pregex == __rhs._M_pregex
521 && _M_flags == __rhs._M_flags
522 && _M_match[0] == __rhs._M_match[0]);
525 template<typename _Bi_iter,
528 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
529 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
532 // In all cases in which the call to regex_search returns true,
533 // match.prefix().first shall be equal to the previous value of
534 // match[0].second, and for each index i in the half-open range
535 // [0, match.size()) for which match[i].matched is true,
536 // match[i].position() shall return distance(begin, match[i].first).
538 if (_M_match[0].matched)
540 auto __start = _M_match[0].second;
541 auto __prefix_first = _M_match[0].second;
542 if (_M_match[0].first == _M_match[0].second)
543 if (__start == _M_end)
545 _M_match = value_type();
550 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags
551 | regex_constants::match_not_null
552 | regex_constants::match_continuous))
554 _GLIBCXX_DEBUG_ASSERT(_M_match[0].matched);
555 _M_match.at(_M_match.size()).first = __prefix_first;
556 _M_match._M_in_iterator = true;
557 _M_match._M_begin = _M_begin;
563 _M_flags |= regex_constants::match_prev_avail;
564 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags))
566 _GLIBCXX_DEBUG_ASSERT(_M_match[0].matched);
567 _M_match.at(_M_match.size()).first = __prefix_first;
568 _M_match._M_in_iterator = true;
569 _M_match._M_begin = _M_begin;
572 _M_match = value_type();
577 template<typename _Bi_iter,
580 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
581 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
582 operator=(const regex_token_iterator& __rhs)
584 _M_position = __rhs._M_position;
585 _M_subs = __rhs._M_subs;
587 _M_result = __rhs._M_result;
588 _M_suffix = __rhs._M_suffix;
589 _M_has_m1 = __rhs._M_has_m1;
590 if (__rhs._M_result == &__rhs._M_suffix)
591 _M_result = &_M_suffix;
595 template<typename _Bi_iter,
599 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
600 operator==(const regex_token_iterator& __rhs) const
602 if (_M_end_of_seq() && __rhs._M_end_of_seq())
604 if (_M_suffix.matched && __rhs._M_suffix.matched
605 && _M_suffix == __rhs._M_suffix)
607 if (_M_end_of_seq() || _M_suffix.matched
608 || __rhs._M_end_of_seq() || __rhs._M_suffix.matched)
610 return _M_position == __rhs._M_position
611 && _M_n == __rhs._M_n
612 && _M_subs == __rhs._M_subs;
615 template<typename _Bi_iter,
618 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
619 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
622 _Position __prev = _M_position;
623 if (_M_suffix.matched)
624 *this = regex_token_iterator();
625 else if (_M_n + 1 < _M_subs.size())
628 _M_result = &_M_current_match();
634 if (_M_position != _Position())
635 _M_result = &_M_current_match();
636 else if (_M_has_m1 && __prev->suffix().length() != 0)
638 _M_suffix.matched = true;
639 _M_suffix.first = __prev->suffix().first;
640 _M_suffix.second = __prev->suffix().second;
641 _M_result = &_M_suffix;
644 *this = regex_token_iterator();
649 template<typename _Bi_iter,
653 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
654 _M_init(_Bi_iter __a, _Bi_iter __b)
657 for (auto __it : _M_subs)
663 if (_M_position != _Position())
664 _M_result = &_M_current_match();
667 _M_suffix.matched = true;
668 _M_suffix.first = __a;
669 _M_suffix.second = __b;
670 _M_result = &_M_suffix;
676 _GLIBCXX_END_NAMESPACE_VERSION