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 // See below __regex_algo_impl to get what this is talking about. The default
32 // value 1 indicated a conservative optimization without giving up worst case
34 #ifndef _GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT
35 #define _GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT 1
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;
66 __res.resize(__re._M_automaton->_M_sub_count() + 2);
67 for (auto& __it : __res)
70 // This function decide which executor to use under given circumstances.
71 // The _S_auto policy now is the following: if a NFA has no
72 // back-references and has more than _GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT
73 // quantifiers (*, +, ?), the BFS executor will be used, other wise
74 // DFS executor. This is because DFS executor has a exponential upper
75 // bound, but better best-case performace. Meanwhile, BFS executor can
76 // effectively prevent from exponential-long time matching (which must
77 // contains many quantifiers), but it's slower in average.
79 // For simple regex, BFS executor could be 2 or more times slower than
82 // Of course, BFS executor cannot handle back-references.
84 if (!__re._M_automaton->_M_has_backref
85 && (__policy == _RegexExecutorPolicy::_S_alternate
86 || __re._M_automaton->_M_quant_count
87 > _GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT))
89 _Executor<_BiIter, _Alloc, _TraitsT, false>
90 __executor(__s, __e, __m, __re, __flags);
92 __ret = __executor._M_match();
94 __ret = __executor._M_search();
98 _Executor<_BiIter, _Alloc, _TraitsT, true>
99 __executor(__s, __e, __m, __re, __flags);
101 __ret = __executor._M_match();
103 __ret = __executor._M_search();
107 for (auto __it : __res)
109 __it.first = __it.second = __e;
110 auto& __pre = __res[__res.size()-2];
111 auto& __suf = __res[__res.size()-1];
114 __pre.matched = false;
117 __suf.matched = false;
124 __pre.second = __res[0].first;
125 __pre.matched = (__pre.first != __pre.second);
126 __suf.first = __res[0].second;
128 __suf.matched = (__suf.first != __suf.second);
134 _GLIBCXX_END_NAMESPACE_VERSION
137 _GLIBCXX_BEGIN_NAMESPACE_VERSION
139 template<typename _Ch_type>
140 template<typename _Fwd_iter>
141 typename regex_traits<_Ch_type>::string_type
142 regex_traits<_Ch_type>::
143 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const
145 typedef std::ctype<char_type> __ctype_type;
146 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
148 static const char* __collatenames[] =
241 "left-square-bracket",
243 "right-square-bracket",
273 "left-curly-bracket",
275 "right-curly-bracket",
280 string __s(__first, __last);
281 for (const auto& __it : __collatenames)
283 return string_type(1, __fctyp.widen(
284 static_cast<char>(&__it - __collatenames)));
286 // TODO Add digraph support:
287 // http://boost.sourceforge.net/libs/regex/doc/collating_names.html
289 return string_type();
292 template<typename _Ch_type>
293 template<typename _Fwd_iter>
294 typename regex_traits<_Ch_type>::char_class_type
295 regex_traits<_Ch_type>::
296 lookup_classname(_Fwd_iter __first, _Fwd_iter __last, bool __icase) const
298 typedef std::ctype<char_type> __ctype_type;
299 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
301 // Mappings from class name to class mask.
302 static const pair<const char*, char_class_type> __classnames[] =
304 {"d", ctype_base::digit},
305 {"w", {ctype_base::alnum, _RegexMask::_S_under}},
306 {"s", ctype_base::space},
307 {"alnum", ctype_base::alnum},
308 {"alpha", ctype_base::alpha},
309 {"blank", {0, _RegexMask::_S_blank}},
310 {"cntrl", ctype_base::cntrl},
311 {"digit", ctype_base::digit},
312 {"graph", ctype_base::graph},
313 {"lower", ctype_base::lower},
314 {"print", ctype_base::print},
315 {"punct", ctype_base::punct},
316 {"space", ctype_base::space},
317 {"upper", ctype_base::upper},
318 {"xdigit", ctype_base::xdigit},
322 for (auto __cur = __first; __cur != __last; ++__cur)
323 __s += __fctyp.narrow(__fctyp.tolower(*__cur), '?');
325 for (const auto& __it : __classnames)
326 if (__s == __it.first)
330 & (ctype_base::lower | ctype_base::upper)) != 0))
331 return ctype_base::alpha;
337 template<typename _Ch_type>
339 regex_traits<_Ch_type>::
340 isctype(_Ch_type __c, char_class_type __f) const
342 typedef std::ctype<char_type> __ctype_type;
343 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
345 return __fctyp.is(__f._M_base, __c)
347 || ((__f._M_extended & _RegexMask::_S_under)
348 && __c == __fctyp.widen('_'))
350 || ((__f._M_extended & _RegexMask::_S_blank)
351 && (__c == __fctyp.widen(' ')
352 || __c == __fctyp.widen('\t')));
355 template<typename _Ch_type>
357 regex_traits<_Ch_type>::
358 value(_Ch_type __ch, int __radix) const
360 std::basic_istringstream<char_type> __is(string_type(1, __ch));
364 else if (__radix == 16)
367 return __is.fail() ? -1 : __v;
370 template<typename _Bi_iter, typename _Alloc>
371 template<typename _Out_iter>
372 _Out_iter match_results<_Bi_iter, _Alloc>::
373 format(_Out_iter __out,
374 const match_results<_Bi_iter, _Alloc>::char_type* __fmt_first,
375 const match_results<_Bi_iter, _Alloc>::char_type* __fmt_last,
376 match_flag_type __flags) const
378 _GLIBCXX_DEBUG_ASSERT( ready() );
379 regex_traits<char_type> __traits;
380 typedef std::ctype<char_type> __ctype_type;
382 __fctyp(use_facet<__ctype_type>(__traits.getloc()));
384 auto __output = [&](size_t __idx)
386 auto& __sub = _Base_type::operator[](__idx);
388 __out = std::copy(__sub.first, __sub.second, __out);
391 if (__flags & regex_constants::format_sed)
393 for (; __fmt_first != __fmt_last;)
394 if (*__fmt_first == '&')
399 else if (*__fmt_first == '\\')
401 if (++__fmt_first != __fmt_last
402 && __fctyp.is(__ctype_type::digit, *__fmt_first))
403 __output(__traits.value(*__fmt_first++, 10));
408 *__out++ = *__fmt_first++;
414 auto __next = std::find(__fmt_first, __fmt_last, '$');
415 if (__next == __fmt_last)
418 __out = std::copy(__fmt_first, __next, __out);
420 auto __eat = [&](char __ch) -> bool
430 if (++__next == __fmt_last)
437 __output(_Base_type::size()-2);
438 else if (__eat('\''))
439 __output(_Base_type::size()-1);
440 else if (__fctyp.is(__ctype_type::digit, *__next))
442 long __num = __traits.value(*__next, 10);
443 if (++__next != __fmt_last
444 && __fctyp.is(__ctype_type::digit, *__next))
447 __num += __traits.value(*__next++, 10);
449 if (0 <= __num && __num < this->size())
454 __fmt_first = __next;
456 __out = std::copy(__fmt_first, __fmt_last, __out);
461 template<typename _Out_iter, typename _Bi_iter,
462 typename _Rx_traits, typename _Ch_type>
464 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
465 const basic_regex<_Ch_type, _Rx_traits>& __e,
466 const _Ch_type* __fmt,
467 regex_constants::match_flag_type __flags)
469 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _IterT;
470 _IterT __i(__first, __last, __e, __flags);
474 if (!(__flags & regex_constants::format_no_copy))
475 __out = std::copy(__first, __last, __out);
479 sub_match<_Bi_iter> __last;
480 auto __len = char_traits<_Ch_type>::length(__fmt);
481 for (; __i != __end; ++__i)
483 if (!(__flags & regex_constants::format_no_copy))
484 __out = std::copy(__i->prefix().first, __i->prefix().second,
486 __out = __i->format(__out, __fmt, __fmt + __len, __flags);
487 __last = __i->suffix();
488 if (__flags & regex_constants::format_first_only)
491 if (!(__flags & regex_constants::format_no_copy))
492 __out = std::copy(__last.first, __last.second, __out);
497 template<typename _Bi_iter,
501 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
502 operator==(const regex_iterator& __rhs) const
504 return (_M_match.empty() && __rhs._M_match.empty())
505 || (_M_begin == __rhs._M_begin
506 && _M_end == __rhs._M_end
507 && _M_pregex == __rhs._M_pregex
508 && _M_flags == __rhs._M_flags
509 && _M_match[0] == __rhs._M_match[0]);
512 template<typename _Bi_iter,
515 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
516 regex_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
519 // In all cases in which the call to regex_search returns true,
520 // match.prefix().first shall be equal to the previous value of
521 // match[0].second, and for each index i in the half-open range
522 // [0, match.size()) for which match[i].matched is true,
523 // match[i].position() shall return distance(begin, match[i].first).
525 if (_M_match[0].matched)
527 auto __start = _M_match[0].second;
528 auto __prefix_first = _M_match[0].second;
529 if (_M_match[0].first == _M_match[0].second)
531 if (__start == _M_end)
533 _M_match = value_type();
538 if (regex_search(__start, _M_end, _M_match, *_M_pregex,
540 | regex_constants::match_not_null
541 | regex_constants::match_continuous))
543 _GLIBCXX_DEBUG_ASSERT(_M_match[0].matched);
544 auto& __prefix = _M_match.at(_M_match.size());
545 __prefix.first = __prefix_first;
546 __prefix.matched = __prefix.first != __prefix.second;
548 _M_match._M_begin = _M_begin;
555 _M_flags |= regex_constants::match_prev_avail;
556 if (regex_search(__start, _M_end, _M_match, *_M_pregex, _M_flags))
558 _GLIBCXX_DEBUG_ASSERT(_M_match[0].matched);
559 auto& __prefix = _M_match.at(_M_match.size());
560 __prefix.first = __prefix_first;
561 __prefix.matched = __prefix.first != __prefix.second;
563 _M_match._M_begin = _M_begin;
566 _M_match = value_type();
571 template<typename _Bi_iter,
574 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
575 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
576 operator=(const regex_token_iterator& __rhs)
578 _M_position = __rhs._M_position;
579 _M_subs = __rhs._M_subs;
581 _M_suffix = __rhs._M_suffix;
582 _M_has_m1 = __rhs._M_has_m1;
583 _M_normalize_result();
587 template<typename _Bi_iter,
591 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
592 operator==(const regex_token_iterator& __rhs) const
594 if (_M_end_of_seq() && __rhs._M_end_of_seq())
596 if (_M_suffix.matched && __rhs._M_suffix.matched
597 && _M_suffix == __rhs._M_suffix)
599 if (_M_end_of_seq() || _M_suffix.matched
600 || __rhs._M_end_of_seq() || __rhs._M_suffix.matched)
602 return _M_position == __rhs._M_position
603 && _M_n == __rhs._M_n
604 && _M_subs == __rhs._M_subs;
607 template<typename _Bi_iter,
610 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>&
611 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
614 _Position __prev = _M_position;
615 if (_M_suffix.matched)
616 *this = regex_token_iterator();
617 else if (_M_n + 1 < _M_subs.size())
620 _M_result = &_M_current_match();
626 if (_M_position != _Position())
627 _M_result = &_M_current_match();
628 else if (_M_has_m1 && __prev->suffix().length() != 0)
630 _M_suffix.matched = true;
631 _M_suffix.first = __prev->suffix().first;
632 _M_suffix.second = __prev->suffix().second;
633 _M_result = &_M_suffix;
636 *this = regex_token_iterator();
641 template<typename _Bi_iter,
645 regex_token_iterator<_Bi_iter, _Ch_type, _Rx_traits>::
646 _M_init(_Bi_iter __a, _Bi_iter __b)
649 for (auto __it : _M_subs)
655 if (_M_position != _Position())
656 _M_result = &_M_current_match();
659 _M_suffix.matched = true;
660 _M_suffix.first = __a;
661 _M_suffix.second = __b;
662 _M_result = &_M_suffix;
668 _GLIBCXX_END_NAMESPACE_VERSION