Regex: Add support for \K that reset the start capture

This commit is contained in:
Maxime Coste 2017-09-27 00:01:06 +09:00
parent 2f450e0080
commit 4ac0d35d1e

View File

@ -28,6 +28,7 @@ struct ParsedRegex
NotWordBoundary, NotWordBoundary,
SubjectBegin, SubjectBegin,
SubjectEnd, SubjectEnd,
ResetStart,
}; };
struct Quantifier struct Quantifier
@ -154,6 +155,7 @@ private:
case 'B': m_pos += 2; return new_node(ParsedRegex::NotWordBoundary); case 'B': m_pos += 2; return new_node(ParsedRegex::NotWordBoundary);
case '`': m_pos += 2; return new_node(ParsedRegex::SubjectBegin); case '`': m_pos += 2; return new_node(ParsedRegex::SubjectBegin);
case '\'': m_pos += 2; return new_node(ParsedRegex::SubjectEnd); case '\'': m_pos += 2; return new_node(ParsedRegex::SubjectEnd);
case 'K': m_pos += 2; return new_node(ParsedRegex::ResetStart);
} }
break; break;
/* TODO: look ahead, look behind */ /* TODO: look ahead, look behind */
@ -514,6 +516,10 @@ private:
case ParsedRegex::SubjectEnd: case ParsedRegex::SubjectEnd:
push_op(CompiledRegex::SubjectEnd); push_op(CompiledRegex::SubjectEnd);
break; break;
case ParsedRegex::ResetStart:
push_op(CompiledRegex::Save);
push_byte(0);
break;
} }
for (auto& offset : goto_inner_end_offsets) for (auto& offset : goto_inner_end_offsets)
@ -986,6 +992,13 @@ auto test_regex = UnitTest{[]{
kak_assert(vm.exec("...")); kak_assert(vm.exec("..."));
kak_assert(not vm.exec("bla")); kak_assert(not vm.exec("bla"));
} }
{
TestVM vm{R"(foo\Kbar)"};
kak_assert(vm.exec("foobar", true, true));
kak_assert(StringView{vm.m_captures[0], vm.m_captures[1]} == "bar");
kak_assert(not vm.exec("bar", true, true));
}
}}; }};
} }