[cfe-commits] [libcxx] r109534 - in /libcxx/trunk: include/regex test/re/re.alg/re.alg.search/egrep.pass.cpp test/re/re.alg/re.alg.search/grep.pass.cpp
Howard Hinnant
hhinnant at apple.com
Tue Jul 27 12:53:10 PDT 2010
Author: hhinnant
Date: Tue Jul 27 14:53:10 2010
New Revision: 109534
URL: http://llvm.org/viewvc/llvm-project?rev=109534&view=rev
Log:
grep and egrep grammars
Added:
libcxx/trunk/test/re/re.alg/re.alg.search/egrep.pass.cpp
libcxx/trunk/test/re/re.alg/re.alg.search/grep.pass.cpp
Modified:
libcxx/trunk/include/regex
Modified: libcxx/trunk/include/regex
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/regex?rev=109534&r1=109533&r2=109534&view=diff
==============================================================================
--- libcxx/trunk/include/regex (original)
+++ libcxx/trunk/include/regex Tue Jul 27 14:53:10 2010
@@ -2701,6 +2701,12 @@
template <class _ForwardIterator>
_ForwardIterator
__parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
+ template <class _ForwardIterator>
+ _ForwardIterator
+ __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
+ template <class _ForwardIterator>
+ _ForwardIterator
+ __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
void __push_l_anchor() {__left_anchor_ = true;}
void __push_r_anchor();
@@ -2832,8 +2838,10 @@
case awk:
break;
case grep:
+ __parse_grep(__first, __last);
break;
case egrep:
+ __parse_egrep(__first, __last);
break;
default:
throw regex_error(regex_constants::__re_err_grammar);
@@ -4109,6 +4117,68 @@
}
template <class _CharT, class _Traits>
+template <class _ForwardIterator>
+_ForwardIterator
+basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
+ _ForwardIterator __last)
+{
+ __owns_one_state<_CharT>* __sa = __end_;
+ _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
+ if (__t1 != __first)
+ __parse_basic_reg_exp(__first, __t1);
+ else
+ __push_empty();
+ __first = __t1;
+ if (__first != __last)
+ ++__first;
+ while (__first != __last)
+ {
+ __t1 = _STD::find(__first, __last, _CharT('\n'));
+ __owns_one_state<_CharT>* __sb = __end_;
+ if (__t1 != __first)
+ __parse_basic_reg_exp(__first, __t1);
+ else
+ __push_empty();
+ __push_alternation(__sa, __sb);
+ __first = __t1;
+ if (__first != __last)
+ ++__first;
+ }
+ return __first;
+}
+
+template <class _CharT, class _Traits>
+template <class _ForwardIterator>
+_ForwardIterator
+basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
+ _ForwardIterator __last)
+{
+ __owns_one_state<_CharT>* __sa = __end_;
+ _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
+ if (__t1 != __first)
+ __parse_extended_reg_exp(__first, __t1);
+ else
+ __push_empty();
+ __first = __t1;
+ if (__first != __last)
+ ++__first;
+ while (__first != __last)
+ {
+ __t1 = _STD::find(__first, __last, _CharT('\n'));
+ __owns_one_state<_CharT>* __sb = __end_;
+ if (__t1 != __first)
+ __parse_extended_reg_exp(__first, __t1);
+ else
+ __push_empty();
+ __push_alternation(__sa, __sb);
+ __first = __t1;
+ if (__first != __last)
+ ++__first;
+ }
+ return __first;
+}
+
+template <class _CharT, class _Traits>
void
basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
__owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
Added: libcxx/trunk/test/re/re.alg/re.alg.search/egrep.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/re/re.alg/re.alg.search/egrep.pass.cpp?rev=109534&view=auto
==============================================================================
--- libcxx/trunk/test/re/re.alg/re.alg.search/egrep.pass.cpp (added)
+++ libcxx/trunk/test/re/re.alg/re.alg.search/egrep.pass.cpp Tue Jul 27 14:53:10 2010
@@ -0,0 +1,92 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <regex>
+
+// template <class BidirectionalIterator, class Allocator, class charT, class traits>
+// bool
+// regex_search(BidirectionalIterator first, BidirectionalIterator last,
+// match_results<BidirectionalIterator, Allocator>& m,
+// const basic_regex<charT, traits>& e,
+// regex_constants::match_flag_type flags = regex_constants::match_default);
+
+#include <iostream>
+
+#include <regex>
+#include <cassert>
+
+#include "../../iterators.h"
+
+int main()
+{
+ {
+ std::cmatch m;
+ const char s[] = "tournament";
+ assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
+ std::regex_constants::egrep)));
+ assert(m.size() == 1);
+ assert(!m.prefix().matched);
+ assert(m.prefix().first == s);
+ assert(m.prefix().second == m[0].first);
+ assert(!m.suffix().matched);
+ assert(m.suffix().first == m[0].second);
+ assert(m.suffix().second == s + std::char_traits<char>::length(s));
+ assert(m.length(0) == 10);
+ assert(m.position(0) == 0);
+ assert(m.str(0) == "tournament");
+ }
+ {
+ std::cmatch m;
+ const char s[] = "ment";
+ assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
+ std::regex_constants::egrep)));
+ assert(m.size() == 1);
+ assert(!m.prefix().matched);
+ assert(m.prefix().first == s);
+ assert(m.prefix().second == m[0].first);
+ assert(m.suffix().matched);
+ assert(m.suffix().first == m[0].second);
+ assert(m.suffix().second == s + std::char_traits<char>::length(s));
+ assert(m.length(0) == 0);
+ assert(m.position(0) == 0);
+ assert(m.str(0) == "");
+ }
+ {
+ std::cmatch m;
+ const char s[] = "tournament";
+ assert(std::regex_search(s, m, std::regex("(tour|to|tournament)+\ntourna",
+ std::regex_constants::egrep)));
+ assert(m.size() == 2);
+ assert(!m.prefix().matched);
+ assert(m.prefix().first == s);
+ assert(m.prefix().second == m[0].first);
+ assert(!m.suffix().matched);
+ assert(m.suffix().first == m[0].second);
+ assert(m.suffix().second == s + std::char_traits<char>::length(s));
+ assert(m.length(0) == 10);
+ assert(m.position(0) == 0);
+ assert(m.str(0) == "tournament");
+ }
+ {
+ std::cmatch m;
+ const char s[] = "tourna";
+ assert(std::regex_search(s, m, std::regex("(tour|to|tournament)+\ntourna",
+ std::regex_constants::egrep)));
+ assert(m.size() == 2);
+ assert(!m.prefix().matched);
+ assert(m.prefix().first == s);
+ assert(m.prefix().second == m[0].first);
+ assert(!m.suffix().matched);
+ assert(m.suffix().first == m[0].second);
+ assert(m.suffix().second == s + std::char_traits<char>::length(s));
+ assert(m.length(0) == 6);
+ assert(m.position(0) == 0);
+ assert(m.str(0) == "tourna");
+ }
+}
Added: libcxx/trunk/test/re/re.alg/re.alg.search/grep.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/re/re.alg/re.alg.search/grep.pass.cpp?rev=109534&view=auto
==============================================================================
--- libcxx/trunk/test/re/re.alg/re.alg.search/grep.pass.cpp (added)
+++ libcxx/trunk/test/re/re.alg/re.alg.search/grep.pass.cpp Tue Jul 27 14:53:10 2010
@@ -0,0 +1,60 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <regex>
+
+// template <class BidirectionalIterator, class Allocator, class charT, class traits>
+// bool
+// regex_search(BidirectionalIterator first, BidirectionalIterator last,
+// match_results<BidirectionalIterator, Allocator>& m,
+// const basic_regex<charT, traits>& e,
+// regex_constants::match_flag_type flags = regex_constants::match_default);
+
+#include <iostream>
+
+#include <regex>
+#include <cassert>
+
+#include "../../iterators.h"
+
+int main()
+{
+ {
+ std::cmatch m;
+ const char s[] = "tournament";
+ assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
+ std::regex_constants::grep)));
+ assert(m.size() == 1);
+ assert(!m.prefix().matched);
+ assert(m.prefix().first == s);
+ assert(m.prefix().second == m[0].first);
+ assert(!m.suffix().matched);
+ assert(m.suffix().first == m[0].second);
+ assert(m.suffix().second == s + std::char_traits<char>::length(s));
+ assert(m.length(0) == 10);
+ assert(m.position(0) == 0);
+ assert(m.str(0) == "tournament");
+ }
+ {
+ std::cmatch m;
+ const char s[] = "ment";
+ assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
+ std::regex_constants::grep)));
+ assert(m.size() == 1);
+ assert(!m.prefix().matched);
+ assert(m.prefix().first == s);
+ assert(m.prefix().second == m[0].first);
+ assert(m.suffix().matched);
+ assert(m.suffix().first == m[0].second);
+ assert(m.suffix().second == s + std::char_traits<char>::length(s));
+ assert(m.length(0) == 0);
+ assert(m.position(0) == 0);
+ assert(m.str(0) == "");
+ }
+}
More information about the cfe-commits
mailing list