grep and egrep grammars
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@109534 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
ad2a7ab9a9
commit
856846b66f
@ -2701,6 +2701,12 @@ private:
|
|||||||
template <class _ForwardIterator>
|
template <class _ForwardIterator>
|
||||||
_ForwardIterator
|
_ForwardIterator
|
||||||
__parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
|
__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_l_anchor() {__left_anchor_ = true;}
|
||||||
void __push_r_anchor();
|
void __push_r_anchor();
|
||||||
@ -2832,8 +2838,10 @@ basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
|
|||||||
case awk:
|
case awk:
|
||||||
break;
|
break;
|
||||||
case grep:
|
case grep:
|
||||||
|
__parse_grep(__first, __last);
|
||||||
break;
|
break;
|
||||||
case egrep:
|
case egrep:
|
||||||
|
__parse_egrep(__first, __last);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw regex_error(regex_constants::__re_err_grammar);
|
throw regex_error(regex_constants::__re_err_grammar);
|
||||||
@ -4108,6 +4116,68 @@ basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first
|
|||||||
return __first;
|
return __first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>
|
template <class _CharT, class _Traits>
|
||||||
void
|
void
|
||||||
basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
|
basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
|
||||||
|
92
test/re/re.alg/re.alg.search/egrep.pass.cpp
Normal file
92
test/re/re.alg/re.alg.search/egrep.pass.cpp
Normal file
@ -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");
|
||||||
|
}
|
||||||
|
}
|
60
test/re/re.alg/re.alg.search/grep.pass.cpp
Normal file
60
test/re/re.alg/re.alg.search/grep.pass.cpp
Normal file
@ -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) == "");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user