1 // class template regex -*- C++ -*-
3 // Copyright (C) 2013-2014 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 // A non-standard switch to let the user pick the matching algorithm.
32 // If _GLIBCXX_REGEX_USE_THOMPSON_NFA is defined, the thompson NFA
33 // algorithm will be used. This algorithm is not enabled by default,
34 // and cannot be used if the regex contains back-references, but has better
35 // (polynomial instead of exponential) worst case performance.
36 // See __regex_algo_impl below.
38 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 // Result of merging regex_match and regex_search.
46 // __policy now can be _S_auto (auto dispatch) and _S_alternate (use
47 // the other one if possible, for test purpose).
49 // That __match_mode is true means regex_match, else regex_search.
50 template<typename _BiIter, typename _Alloc,
51 typename _CharT, typename _TraitsT,
52 _RegexExecutorPolicy __policy,
55 __regex_algo_impl(_BiIter __s,
57 match_results<_BiIter, _Alloc>& __m,
58 const basic_regex<_CharT, _TraitsT>& __re,
59 regex_constants::match_flag_type __flags)
61 if (__re._M_automaton == nullptr)
64 typename match_results<_BiIter, _Alloc>::_Base_type& __res = __m;
65 __res.resize(__re._M_automaton->_M_sub_count() + 2);
66 for (auto& __it : __res)
69 // __policy is used by testsuites so that they can use Thompson NFA
70 // without defining a macro. Users should define
71 // _GLIBCXX_REGEX_USE_THOMPSON_NFA if they need to use this approach.
73 if (!__re._M_automaton->_M_has_backref
74 && !(__re._M_flags & regex_constants::ECMAScript)
75 #ifndef _GLIBCXX_REGEX_USE_THOMPSON_NFA
76 && __policy == _RegexExecutorPolicy::_S_alternate
80 _Executor<_BiIter, _Alloc, _TraitsT, false>
81 __executor(__s, __e, __m, __re, __flags);
83 __ret = __executor._M_match();
85 __ret = __executor._M_search();
89 _Executor<_BiIter, _Alloc, _TraitsT, true>
90 __executor(__s, __e, __m, __re, __flags);
92 __ret = __executor._M_match();
94 __ret = __executor._M_search();
98 for (auto __it : __res)
100 __it.first = __it.second = __e;
101 auto& __pre = __res[__res.size()-2];
102 auto& __suf = __res[__res.size()-1];
105 __pre.matched = false;
108 __suf.matched = false;
115 __pre.second = __res[0].first;
116 __pre.matched = (__pre.first != __pre.second);
117 __suf.first = __res[0].second;
119 __suf.matched = (__suf.first != __suf.second);
125 _GLIBCXX_END_NAMESPACE_VERSION
128 _GLIBCXX_BEGIN_NAMESPACE_VERSION
130 template<typename _Ch_type>
131 template<typename _Fwd_iter>
132 typename regex_traits<_Ch_type>::string_type
133 regex_traits<_Ch_type>::
134 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
136 typedef std::ctype<char_type> __ctype_type;
137 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
139 static const char* __collatenames[] =
232 "left-square-bracket",
234 "right-square-bracket",
264 "left-curly-bracket",
266 "right-curly-bracket",
273 //static const char* __digraphs[] =
299 std::string __s(__last - __first, '?');
300 __fctyp.narrow(__first, __last, '?', &*__s.begin());
302 for (unsigned int __i = 0; *__collatenames[__i]; __i++)
303 if (__s == __collatenames[__i])
304 return string_type(1, __fctyp.widen(static_cast<char>(__i)));
306 //for (unsigned int __i = 0; *__digraphs[__i]; __i++)
308 // const char* __now = __digraphs[__i];
311 // string_type ret(__s.size(), __fctyp.widen('?'));
312 // __fctyp.widen(__now, __now + 2/* ouch */, &*ret.begin());
316 return string_type();
319 template<typename _Ch_type>
320 template<typename _Fwd_iter>
321 typename regex_traits<_Ch_type>::char_class_type
322 regex_traits<_Ch_type>::
323 lookup_classname(_Fwd_iter __first, _Fwd_iter __last, bool __icase) const
325 typedef std::ctype<char_type> __ctype_type;
326 typedef std::ctype<char> __cctype_type;
327 typedef const pair<const char*, char_class_type> _ClassnameEntry;
328 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
329 const __cctype_type& __cctyp(use_facet<__cctype_type>(_M_locale));
331 static _ClassnameEntry __classnames[] =
333 {"d", ctype_base::digit},
334 {"w", {ctype_base::alnum, _RegexMask::_S_under}},
335 {"s", ctype_base::space},
336 {"alnum", ctype_base::alnum},
337 {"alpha", ctype_base::alpha},
338 {"blank", ctype_base::blank},
339 {"cntrl", ctype_base::cntrl},
340 {"digit", ctype_base::digit},
341 {"graph", ctype_base::graph},
342 {"lower", ctype_base::lower},
343 {"print", ctype_base::print},
344 {"punct", ctype_base::punct},
345 {"space", ctype_base::space},
346 {"upper", ctype_base::upper},
347 {"xdigit", ctype_base::xdigit},
350 std::string __s(__last - __first, '?');
351 __fctyp.narrow(__first, __last, '?', &__s[0]);
352 __cctyp.tolower(&*__s.begin(), &*__s.begin() + __s.size());
353 for (_ClassnameEntry* __it = __classnames;
354 __it < *(&__classnames + 1);
357 if (__s == __it->first)
361 & (ctype_base::lower | ctype_base::upper)) != 0))
362 return ctype_base::alpha;
369 template<typename _Ch_type>
371 regex_traits<_Ch_type>::
372 isctype(_Ch_type __c, char_class_type __f) const
374 typedef std::ctype<char_type> __ctype_type;
375 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
377 return __fctyp.is(__f._M_base, __c)
379 || ((__f._M_extended & _RegexMask::_S_under)
380 && __c == __fctyp.widen('_'));
383 template<typename _Ch_type>
385 regex_traits<_Ch_type>::
386 value(_Ch_type __ch, int __radix) const
388 std::basic_istringstream<char_type> __is(string_type(1, __ch));
392 else if (__radix == 16)
395 return __is.fail() ? -1 : __v;
398 template<typename _Bi_iter, typename _Alloc>
399 template<typename _Out_iter>
400 _Out_iter match_results<_Bi_iter, _Alloc>::
401 format(_Out_iter __out,
402 const match_results<_Bi_iter, _Alloc>::char_type* __fmt_first,
403 const match_results<_Bi_iter, _Alloc>::char_type* __fmt_last,
404 match_flag_type __flags) const
406 _GLIBCXX_DEBUG_ASSERT( ready() );
407 regex_traits<char_type> __traits;
408 typedef std::ctype<char_type> __ctype_type;
410 __fctyp(use_facet<__ctype_type>(__traits.getloc()));
412 auto __output = [&](size_t __idx)
414 auto& __sub = _Base_type::operator[](__idx);
416 __out = std::copy(__sub.first, __sub.second, __out);
419 if (__flags & regex_constants::format_sed)
421 for (; __fmt_first != __fmt_last;)
422 if (*__fmt_first == '&')
427 else if (*__fmt_first == '\\')
429 if (++__fmt_first != __fmt_last
430 && __fctyp.is(__ctype_type::digit, *__fmt_first))
431 __output(__traits.value(*__fmt_first++, 10));
436 *__out++ = *__fmt_first++;
442 auto __next = std::find(__fmt_first, __fmt_last, '$');
443 if (__next == __fmt_last)
446 __out = std::copy(__fmt_first, __next, __out);
448 auto __eat = [&](char __ch) -> bool
458 if (++__next == __fmt_last)
465 __output(_Base_type::size()-2);
466 else if (__eat('\''))
467 __output(_Base_type::size()-1);
468 else if (__fctyp.is(__ctype_type::digit, *__next))
470 long __num = __traits.value(*__next, 10);
471 if (++__next != __fmt_last
472 && __fctyp.is(__ctype_type::digit, *__next))
475 __num += __traits.value(*__next++, 10);
477 if (0 <= __num && __num < this->size())
482 __fmt_first = __next;
484 __out = std::copy(__fmt_first, __fmt_last, __out);
489 template<typename _Out_iter, typename _Bi_iter,
490 typename _Rx_traits, typename _Ch_type>
492 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
493 const basic_regex<_Ch_type, _Rx_traits>& __e,
494 const _Ch_type* __fmt,
495 regex_constants::match_flag_type __flags)
497 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _IterT;
498 _IterT __i(__first, __last, __e, __flags);
502 if (!(__flags & regex_constants::format_no_copy))
503 __out = std::copy(__first, __last, __out);
507 sub_match<_Bi_iter> __last;
508 auto __len = char_traits<_Ch_type>::length(__fmt);
509 for (; __i != __end; ++__i)
511 if (!(__flags & regex_constants::format_no_copy))
512 __out = std::copy(__i->prefix().first, __i->prefix().second,
514 __out = __i->format(__out, __fmt, __fmt + __len, __flags);
515 __last = __i->suffix();
516 if (__flags & regex_constants::format_first_only)
519 if (!(__flags & regex_constants::format_no_copy))
520 __out = std::copy(__last.first, __last.second, __out);
525 template<typename _Bi_iter,
529 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
530 operator==(const regex_iterator& __rhs) const
532 return (_M_match.empty() && __rhs._M_match.empty())
533 || (_M_begin == __rhs._M_begin
534 && _M_end == __rhs._M_end
535 && _M_pregex == __rhs._M_pregex
536 && _M_flags == __rhs._M_flags
537 && _M_match[0] == __rhs._M_match[0]);
540 template<typename _Bi_iter,
543 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
544 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
547 // In all cases in which the call to regex_search returns true,
548 // match.prefix().first shall be equal to the previous value of
549 // match[0].second, and for each index i in the half-open range
550 // [0, match.size()) for which match[i].matched is true,
551 // match[i].position() shall return distance(begin, match[i].first).
553 if (_M_match[0].matched)
555 auto __start = _M_match[0].second;
556 auto __prefix_first = _M_match[0].second;
557 if (_M_match[0].first == _M_match[0].second)
559 if (__start == _M_end)
561 _M_match = value_type();
566 if (regex_search(__start, _M_end, _M_match, *_M_pregex,
568 | regex_constants::match_not_null
569 | regex_constants::match_continuous))
571 _GLIBCXX_DEBUG_ASSERT(_M_match[0].matched);
572 _M_match.at(_M_match.size()).first = __prefix_first;
573 _M_match._M_in_iterator = true;
574 _M_match._M_begin = _M_begin;
581 _M_flags |= regex_constants::match_prev_avail;
582 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags))
584 _GLIBCXX_DEBUG_ASSERT(_M_match[0].matched);
585 _M_match.at(_M_match.size()).first = __prefix_first;
586 _M_match._M_in_iterator = true;
587 _M_match._M_begin = _M_begin;
590 _M_match = value_type();
595 template<typename _Bi_iter,
598 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
599 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
600 operator=(const regex_token_iterator& __rhs)
602 _M_position = __rhs._M_position;
603 _M_subs = __rhs._M_subs;
605 _M_result = __rhs._M_result;
606 _M_suffix = __rhs._M_suffix;
607 _M_has_m1 = __rhs._M_has_m1;
608 if (__rhs._M_result == &__rhs._M_suffix)
609 _M_result = &_M_suffix;
613 template<typename _Bi_iter,
617 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
618 operator==(const regex_token_iterator& __rhs) const
620 if (_M_end_of_seq() && __rhs._M_end_of_seq())
622 if (_M_suffix.matched && __rhs._M_suffix.matched
623 && _M_suffix == __rhs._M_suffix)
625 if (_M_end_of_seq() || _M_suffix.matched
626 || __rhs._M_end_of_seq() || __rhs._M_suffix.matched)
628 return _M_position == __rhs._M_position
629 && _M_n == __rhs._M_n
630 && _M_subs == __rhs._M_subs;
633 template<typename _Bi_iter,
636 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
637 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
640 _Position __prev = _M_position;
641 if (_M_suffix.matched)
642 *this = regex_token_iterator();
643 else if (_M_n + 1 < _M_subs.size())
646 _M_result = &_M_current_match();
652 if (_M_position != _Position())
653 _M_result = &_M_current_match();
654 else if (_M_has_m1 && __prev->suffix().length() != 0)
656 _M_suffix.matched = true;
657 _M_suffix.first = __prev->suffix().first;
658 _M_suffix.second = __prev->suffix().second;
659 _M_result = &_M_suffix;
662 *this = regex_token_iterator();
667 template<typename _Bi_iter,
671 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
672 _M_init(_Bi_iter __a, _Bi_iter __b)
675 for (auto __it : _M_subs)
681 if (_M_position != _Position())
682 _M_result = &_M_current_match();
685 _M_suffix.matched = true;
686 _M_suffix.first = __a;
687 _M_suffix.second = __b;
688 _M_result = &_M_suffix;
694 _GLIBCXX_END_NAMESPACE_VERSION