2017-09-17 10:50:53 +02:00
|
|
|
#ifndef regex_impl_hh_INCLUDED
|
|
|
|
#define regex_impl_hh_INCLUDED
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
#include "exception.hh"
|
2017-10-07 04:43:21 +02:00
|
|
|
#include "flags.hh"
|
|
|
|
#include "ref_ptr.hh"
|
2017-10-02 08:59:04 +02:00
|
|
|
#include "unicode.hh"
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "utf8_iterator.hh"
|
|
|
|
#include "vector.hh"
|
|
|
|
|
2017-10-07 04:22:50 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-09-26 09:44:30 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
enum class MatchDirection
|
|
|
|
{
|
|
|
|
Forward,
|
|
|
|
Backward
|
|
|
|
};
|
|
|
|
|
2017-10-06 07:40:27 +02:00
|
|
|
struct CompiledRegex : RefCountable
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
|
|
|
enum Op : char
|
|
|
|
{
|
|
|
|
Match,
|
|
|
|
Literal,
|
|
|
|
LiteralIgnoreCase,
|
|
|
|
AnyChar,
|
|
|
|
Matcher,
|
|
|
|
Jump,
|
|
|
|
Split_PrioritizeParent,
|
|
|
|
Split_PrioritizeChild,
|
|
|
|
Save,
|
|
|
|
LineStart,
|
|
|
|
LineEnd,
|
|
|
|
WordBoundary,
|
|
|
|
NotWordBoundary,
|
|
|
|
SubjectBegin,
|
|
|
|
SubjectEnd,
|
|
|
|
LookAhead,
|
|
|
|
NegativeLookAhead,
|
2017-10-04 17:00:19 +02:00
|
|
|
LookBehind,
|
2017-10-02 08:59:04 +02:00
|
|
|
NegativeLookBehind,
|
|
|
|
};
|
|
|
|
|
|
|
|
using Offset = unsigned;
|
|
|
|
explicit operator bool() const { return not bytecode.empty(); }
|
|
|
|
|
|
|
|
Vector<char> bytecode;
|
|
|
|
Vector<std::function<bool (Codepoint)>> matchers;
|
2017-10-07 06:46:27 +02:00
|
|
|
MatchDirection direction;
|
2017-10-02 08:59:04 +02:00
|
|
|
size_t save_count;
|
2017-10-06 07:40:27 +02:00
|
|
|
|
|
|
|
struct StartChars { bool map[256]; };
|
|
|
|
std::unique_ptr<StartChars> start_chars;
|
2017-10-02 08:59:04 +02:00
|
|
|
};
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
CompiledRegex compile_regex(StringView re, MatchDirection direction = MatchDirection::Forward);
|
2017-10-02 08:59:04 +02:00
|
|
|
|
2017-10-02 16:34:57 +02:00
|
|
|
enum class RegexExecFlags
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
Search = 1 << 0,
|
|
|
|
NotBeginOfLine = 1 << 1,
|
|
|
|
NotEndOfLine = 1 << 2,
|
|
|
|
NotBeginOfWord = 1 << 3,
|
|
|
|
NotEndOfWord = 1 << 4,
|
|
|
|
NotBeginOfSubject = 1 << 5,
|
|
|
|
NotInitialNull = 1 << 6,
|
2017-10-03 13:07:44 +02:00
|
|
|
AnyMatch = 1 << 7,
|
|
|
|
NoSaves = 1 << 8,
|
2017-10-02 16:34:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
constexpr bool with_bit_ops(Meta::Type<RegexExecFlags>) { return true; }
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
template<typename Iterator, MatchDirection direction>
|
|
|
|
struct ChooseUtf8It
|
|
|
|
{
|
|
|
|
using Type = utf8::iterator<Iterator>;
|
|
|
|
};
|
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
template<typename Iterator>
|
2017-10-07 06:46:27 +02:00
|
|
|
struct ChooseUtf8It<Iterator, MatchDirection::Backward>
|
|
|
|
{
|
|
|
|
using Type = std::reverse_iterator<utf8::iterator<Iterator>>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Iterator, MatchDirection direction>
|
2017-10-06 13:30:46 +02:00
|
|
|
class ThreadedRegexVM
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-06 13:30:46 +02:00
|
|
|
public:
|
2017-10-02 08:59:04 +02:00
|
|
|
ThreadedRegexVM(const CompiledRegex& program)
|
2017-10-07 06:46:27 +02:00
|
|
|
: m_program{program}
|
|
|
|
{
|
|
|
|
kak_assert(m_program);
|
|
|
|
if (direction != program.direction)
|
|
|
|
throw runtime_error{"Regex and VM direction mismatch"};
|
|
|
|
}
|
2017-10-02 08:59:04 +02:00
|
|
|
|
2017-10-04 14:11:15 +02:00
|
|
|
ThreadedRegexVM(const ThreadedRegexVM&) = delete;
|
2017-10-06 13:30:46 +02:00
|
|
|
ThreadedRegexVM& operator=(const ThreadedRegexVM&) = delete;
|
2017-10-04 14:11:15 +02:00
|
|
|
|
2017-10-04 05:14:24 +02:00
|
|
|
~ThreadedRegexVM()
|
|
|
|
{
|
|
|
|
for (auto* saves : m_saves)
|
|
|
|
{
|
|
|
|
for (size_t i = m_program.save_count-1; i > 0; --i)
|
|
|
|
saves->pos[i].~Iterator();
|
|
|
|
saves->~Saves();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 13:30:46 +02:00
|
|
|
bool exec(Iterator begin, Iterator end, RegexExecFlags flags)
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
const bool forward = direction == MatchDirection::Forward;
|
|
|
|
m_begin = Utf8It{utf8::iterator<Iterator>{forward ? begin : end, begin, end}};
|
|
|
|
m_end = Utf8It{utf8::iterator<Iterator>{forward ? end : begin, begin, end}};
|
2017-10-06 13:30:46 +02:00
|
|
|
m_flags = flags;
|
|
|
|
|
|
|
|
if (flags & RegexExecFlags::NotInitialNull and m_begin == m_end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Vector<Thread> current_threads, next_threads;
|
2017-10-07 08:25:14 +02:00
|
|
|
std::unique_ptr<bool[]> inst_processed{new bool[m_program.bytecode.size()]};
|
2017-10-06 13:30:46 +02:00
|
|
|
|
|
|
|
const bool no_saves = (m_flags & RegexExecFlags::NoSaves);
|
2017-10-07 06:46:27 +02:00
|
|
|
Utf8It start{m_begin};
|
2017-10-06 13:30:46 +02:00
|
|
|
|
|
|
|
const bool* start_chars = m_program.start_chars ? m_program.start_chars->map : nullptr;
|
|
|
|
|
|
|
|
if (flags & RegexExecFlags::Search)
|
2017-10-07 06:46:27 +02:00
|
|
|
to_next_start(start, m_end, start_chars);
|
2017-10-06 13:30:46 +02:00
|
|
|
|
|
|
|
if (exec_from(start, no_saves ? nullptr : new_saves<false>(nullptr),
|
2017-10-07 08:25:14 +02:00
|
|
|
current_threads, next_threads, inst_processed.get()))
|
2017-10-06 13:30:46 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (not (flags & RegexExecFlags::Search))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
to_next_start(++start, m_end, start_chars);
|
2017-10-06 13:30:46 +02:00
|
|
|
if (exec_from(start, no_saves ? nullptr : new_saves<false>(nullptr),
|
2017-10-07 08:25:14 +02:00
|
|
|
current_threads, next_threads, inst_processed.get()))
|
2017-10-06 13:30:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-07 06:46:27 +02:00
|
|
|
while (start != m_end);
|
2017-10-06 13:30:46 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayView<const Iterator> captures() const
|
|
|
|
{
|
|
|
|
if (m_captures)
|
|
|
|
return { m_captures->pos, m_program.save_count };
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-10-03 04:54:43 +02:00
|
|
|
struct Saves
|
|
|
|
{
|
|
|
|
int refcount;
|
2017-10-04 05:14:24 +02:00
|
|
|
Iterator pos[1];
|
2017-10-03 04:54:43 +02:00
|
|
|
};
|
|
|
|
|
2017-10-04 13:49:16 +02:00
|
|
|
template<bool copy>
|
|
|
|
Saves* new_saves(Iterator* pos)
|
2017-10-03 04:54:43 +02:00
|
|
|
{
|
2017-10-04 13:49:16 +02:00
|
|
|
kak_assert(not copy or pos != nullptr);
|
|
|
|
const auto count = m_program.save_count;
|
2017-10-03 04:54:43 +02:00
|
|
|
if (not m_free_saves.empty())
|
|
|
|
{
|
2017-10-03 12:23:31 +02:00
|
|
|
Saves* res = m_free_saves.back();
|
2017-10-03 04:54:43 +02:00
|
|
|
m_free_saves.pop_back();
|
2017-10-03 12:23:31 +02:00
|
|
|
res->refcount = 1;
|
2017-10-04 13:49:16 +02:00
|
|
|
if (copy)
|
|
|
|
std::copy(pos, pos + count, res->pos);
|
|
|
|
else
|
|
|
|
std::fill(res->pos, res->pos + count, Iterator{});
|
|
|
|
|
2017-10-03 12:23:31 +02:00
|
|
|
return res;
|
2017-10-03 04:54:43 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:49:16 +02:00
|
|
|
void* ptr = ::operator new (sizeof(Saves) + (count-1) * sizeof(Iterator));
|
|
|
|
Saves* saves = new (ptr) Saves{1, copy ? pos[0] : Iterator{}};
|
|
|
|
for (size_t i = 1; i < count; ++i)
|
|
|
|
new (&saves->pos[i]) Iterator{copy ? pos[i] : Iterator{}};
|
|
|
|
m_saves.push_back(saves);
|
|
|
|
return saves;
|
2017-10-03 04:54:43 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 04:49:40 +02:00
|
|
|
void release_saves(Saves* saves)
|
|
|
|
{
|
|
|
|
if (saves and --saves->refcount == 0)
|
|
|
|
m_free_saves.push_back(saves);
|
|
|
|
};
|
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
struct Thread
|
|
|
|
{
|
|
|
|
const char* inst;
|
2017-10-03 04:54:43 +02:00
|
|
|
Saves* saves;
|
2017-10-02 08:59:04 +02:00
|
|
|
};
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
using Utf8It = typename ChooseUtf8It<Iterator, direction>::Type;
|
2017-10-04 06:16:52 +02:00
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
enum class StepResult { Consumed, Matched, Failed };
|
2017-10-07 10:36:53 +02:00
|
|
|
|
|
|
|
// Steps a thread until it consumes the current character, matches or fail
|
2017-10-07 08:25:14 +02:00
|
|
|
StepResult step(const Utf8It& pos, Thread& thread, Vector<Thread>& threads, bool* inst_processed)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
|
|
|
const auto prog_start = m_program.bytecode.data();
|
|
|
|
const auto prog_end = prog_start + m_program.bytecode.size();
|
|
|
|
while (true)
|
|
|
|
{
|
2017-10-07 10:36:53 +02:00
|
|
|
// If we have hit this instruction on this character, in this thread or another, do not try again
|
2017-10-07 08:25:14 +02:00
|
|
|
const auto inst_offset = thread.inst - prog_start;
|
|
|
|
if (inst_processed[inst_offset])
|
|
|
|
return StepResult::Failed;
|
|
|
|
inst_processed[inst_offset] = true;
|
|
|
|
|
2017-10-04 14:11:15 +02:00
|
|
|
const Codepoint cp = pos == m_end ? 0 : *pos;
|
2017-10-02 08:59:04 +02:00
|
|
|
const CompiledRegex::Op op = (CompiledRegex::Op)*thread.inst++;
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case CompiledRegex::Literal:
|
|
|
|
if (utf8::read_codepoint(thread.inst, prog_end) == cp)
|
|
|
|
return StepResult::Consumed;
|
|
|
|
return StepResult::Failed;
|
|
|
|
case CompiledRegex::LiteralIgnoreCase:
|
|
|
|
if (utf8::read_codepoint(thread.inst, prog_end) == to_lower(cp))
|
|
|
|
return StepResult::Consumed;
|
|
|
|
return StepResult::Failed;
|
|
|
|
case CompiledRegex::AnyChar:
|
|
|
|
return StepResult::Consumed;
|
|
|
|
case CompiledRegex::Jump:
|
2017-10-07 04:22:50 +02:00
|
|
|
thread.inst = prog_start + get_offset(thread.inst);
|
2017-10-02 08:59:04 +02:00
|
|
|
break;
|
|
|
|
case CompiledRegex::Split_PrioritizeParent:
|
|
|
|
{
|
2017-10-03 04:54:43 +02:00
|
|
|
auto parent = thread.inst + sizeof(CompiledRegex::Offset);
|
2017-10-07 04:22:50 +02:00
|
|
|
auto child = prog_start + get_offset(thread.inst);
|
2017-10-03 04:54:43 +02:00
|
|
|
thread.inst = parent;
|
2017-10-03 13:07:44 +02:00
|
|
|
if (thread.saves)
|
|
|
|
++thread.saves->refcount;
|
2017-10-04 04:49:40 +02:00
|
|
|
threads.push_back({child, thread.saves});
|
2017-10-02 08:59:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CompiledRegex::Split_PrioritizeChild:
|
|
|
|
{
|
2017-10-03 04:54:43 +02:00
|
|
|
auto parent = thread.inst + sizeof(CompiledRegex::Offset);
|
2017-10-07 04:22:50 +02:00
|
|
|
auto child = prog_start + get_offset(thread.inst);
|
2017-10-03 04:54:43 +02:00
|
|
|
thread.inst = child;
|
2017-10-03 13:07:44 +02:00
|
|
|
if (thread.saves)
|
|
|
|
++thread.saves->refcount;
|
2017-10-04 04:49:40 +02:00
|
|
|
threads.push_back({parent, thread.saves});
|
2017-10-02 08:59:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CompiledRegex::Save:
|
|
|
|
{
|
2017-10-04 16:31:15 +02:00
|
|
|
const size_t index = *thread.inst++;
|
2017-10-03 13:07:44 +02:00
|
|
|
if (thread.saves == nullptr)
|
|
|
|
break;
|
2017-10-03 04:54:43 +02:00
|
|
|
if (thread.saves->refcount > 1)
|
|
|
|
{
|
|
|
|
--thread.saves->refcount;
|
2017-10-04 13:49:16 +02:00
|
|
|
thread.saves = new_saves<true>(thread.saves->pos);
|
2017-10-03 04:54:43 +02:00
|
|
|
}
|
2017-10-07 06:46:27 +02:00
|
|
|
thread.saves->pos[index] = get_base(pos);
|
2017-10-02 08:59:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CompiledRegex::Matcher:
|
|
|
|
{
|
|
|
|
const int matcher_id = *thread.inst++;
|
2017-10-04 14:11:15 +02:00
|
|
|
return m_program.matchers[matcher_id](cp) ?
|
2017-10-02 08:59:04 +02:00
|
|
|
StepResult::Consumed : StepResult::Failed;
|
|
|
|
}
|
|
|
|
case CompiledRegex::LineStart:
|
2017-10-04 14:11:15 +02:00
|
|
|
if (not is_line_start(pos))
|
2017-10-02 08:59:04 +02:00
|
|
|
return StepResult::Failed;
|
|
|
|
break;
|
|
|
|
case CompiledRegex::LineEnd:
|
2017-10-04 14:11:15 +02:00
|
|
|
if (not is_line_end(pos))
|
2017-10-02 08:59:04 +02:00
|
|
|
return StepResult::Failed;
|
|
|
|
break;
|
|
|
|
case CompiledRegex::WordBoundary:
|
2017-10-04 14:11:15 +02:00
|
|
|
if (not is_word_boundary(pos))
|
2017-10-02 08:59:04 +02:00
|
|
|
return StepResult::Failed;
|
|
|
|
break;
|
|
|
|
case CompiledRegex::NotWordBoundary:
|
2017-10-04 14:11:15 +02:00
|
|
|
if (is_word_boundary(pos))
|
2017-10-02 08:59:04 +02:00
|
|
|
return StepResult::Failed;
|
|
|
|
break;
|
|
|
|
case CompiledRegex::SubjectBegin:
|
2017-10-04 14:11:15 +02:00
|
|
|
if (pos != m_begin or (m_flags & RegexExecFlags::NotBeginOfSubject))
|
2017-10-02 08:59:04 +02:00
|
|
|
return StepResult::Failed;
|
|
|
|
break;
|
|
|
|
case CompiledRegex::SubjectEnd:
|
2017-10-04 14:11:15 +02:00
|
|
|
if (pos != m_end)
|
2017-10-02 08:59:04 +02:00
|
|
|
return StepResult::Failed;
|
|
|
|
break;
|
|
|
|
case CompiledRegex::LookAhead:
|
|
|
|
case CompiledRegex::NegativeLookAhead:
|
|
|
|
{
|
|
|
|
int count = *thread.inst++;
|
2017-10-04 14:11:15 +02:00
|
|
|
for (auto it = pos; count and it != m_end; ++it, --count)
|
2017-10-02 08:59:04 +02:00
|
|
|
if (*it != utf8::read(thread.inst))
|
|
|
|
break;
|
|
|
|
if ((op == CompiledRegex::LookAhead and count != 0) or
|
|
|
|
(op == CompiledRegex::NegativeLookAhead and count == 0))
|
|
|
|
return StepResult::Failed;
|
|
|
|
thread.inst = utf8::advance(thread.inst, prog_end, CharCount{count - 1});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CompiledRegex::LookBehind:
|
|
|
|
case CompiledRegex::NegativeLookBehind:
|
|
|
|
{
|
|
|
|
int count = *thread.inst++;
|
2017-10-04 14:11:15 +02:00
|
|
|
for (auto it = pos-1; count and it >= m_begin; --it, --count)
|
2017-10-02 08:59:04 +02:00
|
|
|
if (*it != utf8::read(thread.inst))
|
|
|
|
break;
|
|
|
|
if ((op == CompiledRegex::LookBehind and count != 0) or
|
|
|
|
(op == CompiledRegex::NegativeLookBehind and count == 0))
|
|
|
|
return StepResult::Failed;
|
|
|
|
thread.inst = utf8::advance(thread.inst, prog_end, CharCount{count - 1});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CompiledRegex::Match:
|
|
|
|
return StepResult::Matched;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return StepResult::Failed;
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:25:14 +02:00
|
|
|
bool exec_from(const Utf8It& start, Saves* initial_saves, Vector<Thread>& current_threads, Vector<Thread>& next_threads, bool* inst_processed)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-04 06:16:52 +02:00
|
|
|
current_threads.push_back({m_program.bytecode.data(), initial_saves});
|
|
|
|
next_threads.clear();
|
2017-10-03 13:07:44 +02:00
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
bool found_match = false;
|
2017-10-04 14:11:15 +02:00
|
|
|
for (Utf8It pos = start; pos != m_end; ++pos)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-07 10:36:53 +02:00
|
|
|
memset(inst_processed, 0, m_program.bytecode.size() * sizeof(bool));
|
2017-10-04 04:49:40 +02:00
|
|
|
while (not current_threads.empty())
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-04 04:49:40 +02:00
|
|
|
auto thread = current_threads.back();
|
|
|
|
current_threads.pop_back();
|
2017-10-07 08:25:14 +02:00
|
|
|
switch (step(pos, thread, current_threads, inst_processed))
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-03 12:00:52 +02:00
|
|
|
case StepResult::Matched:
|
2017-10-04 06:16:52 +02:00
|
|
|
if (not (m_flags & RegexExecFlags::Search) or // We are not at end, this is not a full match
|
2017-10-04 14:11:15 +02:00
|
|
|
(m_flags & RegexExecFlags::NotInitialNull and pos == m_begin))
|
2017-10-02 10:24:38 +02:00
|
|
|
{
|
2017-10-03 12:00:52 +02:00
|
|
|
release_saves(thread.saves);
|
2017-10-02 16:34:57 +02:00
|
|
|
continue;
|
2017-10-02 10:24:38 +02:00
|
|
|
}
|
2017-10-02 08:59:04 +02:00
|
|
|
|
2017-10-04 14:11:15 +02:00
|
|
|
release_saves(m_captures);
|
2017-10-04 05:28:58 +02:00
|
|
|
m_captures = thread.saves;
|
2017-10-04 06:16:52 +02:00
|
|
|
if (m_flags & RegexExecFlags::AnyMatch)
|
2017-10-02 16:34:57 +02:00
|
|
|
return true;
|
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
found_match = true;
|
2017-10-04 04:49:40 +02:00
|
|
|
current_threads.clear(); // remove this and lower priority threads
|
2017-10-03 12:00:52 +02:00
|
|
|
break;
|
|
|
|
case StepResult::Failed:
|
|
|
|
release_saves(thread.saves);
|
|
|
|
break;
|
|
|
|
case StepResult::Consumed:
|
2017-10-04 04:49:40 +02:00
|
|
|
if (contains_that(next_threads, [&](auto& t) { return t.inst == thread.inst; }))
|
2017-10-03 12:00:52 +02:00
|
|
|
release_saves(thread.saves);
|
|
|
|
else
|
2017-10-04 04:49:40 +02:00
|
|
|
next_threads.push_back(thread);
|
2017-10-03 12:00:52 +02:00
|
|
|
break;
|
2017-10-02 10:24:38 +02:00
|
|
|
}
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
2017-10-04 04:49:40 +02:00
|
|
|
if (next_threads.empty())
|
2017-10-02 08:59:04 +02:00
|
|
|
return found_match;
|
2017-10-03 12:00:52 +02:00
|
|
|
|
2017-10-04 04:49:40 +02:00
|
|
|
std::swap(current_threads, next_threads);
|
|
|
|
std::reverse(current_threads.begin(), current_threads.end());
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
2017-10-02 19:16:30 +02:00
|
|
|
if (found_match)
|
|
|
|
return true;
|
2017-10-02 08:59:04 +02:00
|
|
|
|
2017-10-07 10:36:53 +02:00
|
|
|
memset(inst_processed, 0, m_program.bytecode.size() * sizeof(bool));
|
2017-10-02 08:59:04 +02:00
|
|
|
// Step remaining threads to see if they match without consuming anything else
|
2017-10-04 04:49:40 +02:00
|
|
|
while (not current_threads.empty())
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-04 04:49:40 +02:00
|
|
|
auto thread = current_threads.back();
|
|
|
|
current_threads.pop_back();
|
2017-10-07 08:25:14 +02:00
|
|
|
if (step(m_end, thread, current_threads, inst_processed) == StepResult::Matched)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-04 14:11:15 +02:00
|
|
|
release_saves(m_captures);
|
2017-10-04 05:28:58 +02:00
|
|
|
m_captures = thread.saves;
|
2017-10-02 19:16:30 +02:00
|
|
|
return true;
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-02 19:16:30 +02:00
|
|
|
return false;
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
void to_next_start(Utf8It& start, const Utf8It& end, const bool* start_chars)
|
2017-10-06 07:40:27 +02:00
|
|
|
{
|
|
|
|
if (not start_chars)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (start != end and *start >= 0 and *start < 256 and
|
|
|
|
not start_chars[*start])
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
|
2017-10-07 04:22:50 +02:00
|
|
|
static CompiledRegex::Offset get_offset(const char* ptr)
|
|
|
|
{
|
|
|
|
CompiledRegex::Offset res;
|
|
|
|
memcpy(&res, ptr, sizeof(CompiledRegex::Offset));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-04 14:11:15 +02:00
|
|
|
bool is_line_start(const Utf8It& pos) const
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-04 14:11:15 +02:00
|
|
|
return (pos == m_begin and not (m_flags & RegexExecFlags::NotBeginOfLine)) or
|
|
|
|
*(pos-1) == '\n';
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 14:11:15 +02:00
|
|
|
bool is_line_end(const Utf8It& pos) const
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-04 14:11:15 +02:00
|
|
|
return (pos == m_end and not (m_flags & RegexExecFlags::NotEndOfLine)) or
|
|
|
|
*pos == '\n';
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 14:11:15 +02:00
|
|
|
bool is_word_boundary(const Utf8It& pos) const
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-04 14:11:15 +02:00
|
|
|
return (pos == m_begin and not (m_flags & RegexExecFlags::NotBeginOfWord)) or
|
|
|
|
(pos == m_end and not (m_flags & RegexExecFlags::NotEndOfWord)) or
|
|
|
|
is_word(*(pos-1)) != is_word(*pos);
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
static const Iterator& get_base(const utf8::iterator<Iterator>& it) { return it.base(); }
|
2017-10-07 10:09:43 +02:00
|
|
|
static Iterator get_base(const std::reverse_iterator<utf8::iterator<Iterator>>& it) { return it.base().base(); }
|
2017-10-07 06:46:27 +02:00
|
|
|
|
2017-10-02 08:59:04 +02:00
|
|
|
const CompiledRegex& m_program;
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
Utf8It m_begin;
|
|
|
|
Utf8It m_end;
|
2017-10-02 16:34:57 +02:00
|
|
|
RegexExecFlags m_flags;
|
2017-10-02 08:59:04 +02:00
|
|
|
|
2017-10-04 05:14:24 +02:00
|
|
|
Vector<Saves*> m_saves;
|
2017-10-03 04:54:43 +02:00
|
|
|
Vector<Saves*> m_free_saves;
|
|
|
|
|
2017-10-04 05:14:24 +02:00
|
|
|
Saves* m_captures = nullptr;
|
2017-10-02 08:59:04 +02:00
|
|
|
};
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
template<typename It, MatchDirection direction = MatchDirection::Forward>
|
2017-10-02 16:34:57 +02:00
|
|
|
bool regex_match(It begin, It end, const CompiledRegex& re, RegexExecFlags flags = RegexExecFlags::None)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
ThreadedRegexVM<It, direction> vm{re};
|
2017-10-03 13:07:44 +02:00
|
|
|
return vm.exec(begin, end, (RegexExecFlags)(flags & ~(RegexExecFlags::Search)) |
|
|
|
|
RegexExecFlags::AnyMatch | RegexExecFlags::NoSaves);
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
template<typename It, MatchDirection direction = MatchDirection::Forward>
|
2017-10-02 16:34:57 +02:00
|
|
|
bool regex_match(It begin, It end, Vector<It>& captures, const CompiledRegex& re,
|
|
|
|
RegexExecFlags flags = RegexExecFlags::None)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
ThreadedRegexVM<It, direction> vm{re};
|
2017-10-02 16:34:57 +02:00
|
|
|
if (vm.exec(begin, end, flags & ~(RegexExecFlags::Search)))
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-06 13:30:46 +02:00
|
|
|
std::copy(vm.captures().begin(), vm.captures().end(), std::back_inserter(captures));
|
2017-10-02 08:59:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
template<typename It, MatchDirection direction = MatchDirection::Forward>
|
2017-10-02 16:34:57 +02:00
|
|
|
bool regex_search(It begin, It end, const CompiledRegex& re,
|
|
|
|
RegexExecFlags flags = RegexExecFlags::None)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
ThreadedRegexVM<It, direction> vm{re};
|
2017-10-03 13:07:44 +02:00
|
|
|
return vm.exec(begin, end, flags | RegexExecFlags::Search | RegexExecFlags::AnyMatch | RegexExecFlags::NoSaves);
|
2017-10-02 08:59:04 +02:00
|
|
|
}
|
2017-09-26 09:44:30 +02:00
|
|
|
|
2017-10-07 06:46:27 +02:00
|
|
|
template<typename It, MatchDirection direction = MatchDirection::Forward>
|
2017-10-02 16:34:57 +02:00
|
|
|
bool regex_search(It begin, It end, Vector<It>& captures, const CompiledRegex& re,
|
|
|
|
RegexExecFlags flags = RegexExecFlags::None)
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-07 06:46:27 +02:00
|
|
|
ThreadedRegexVM<It, direction> vm{re};
|
2017-10-02 16:34:57 +02:00
|
|
|
if (vm.exec(begin, end, flags | RegexExecFlags::Search))
|
2017-10-02 08:59:04 +02:00
|
|
|
{
|
2017-10-06 13:30:46 +02:00
|
|
|
std::copy(vm.captures().begin(), vm.captures().end(), std::back_inserter(captures));
|
2017-10-02 08:59:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-26 09:44:30 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-17 10:50:53 +02:00
|
|
|
#endif // regex_impl_hh_INCLUDED
|