Regex: support escaping characters in character classes

This commit is contained in:
Maxime Coste 2017-10-02 13:49:08 +08:00
parent 3d2262bebf
commit 76dcfd5c52

View File

@ -307,7 +307,7 @@ private:
Vector<std::pair<wctype_t, bool>> ctypes; Vector<std::pair<wctype_t, bool>> ctypes;
while (m_pos != m_regex.end() and *m_pos != ']') while (m_pos != m_regex.end() and *m_pos != ']')
{ {
const auto cp = *m_pos++; auto cp = *m_pos++;
if (cp == '-') if (cp == '-')
{ {
ranges.push_back({ '-', '-' }); ranges.push_back({ '-', '-' });
@ -335,6 +335,13 @@ private:
++m_pos; ++m_pos;
continue; continue;
} }
else // its just an escaped character
{
if (++m_pos == m_regex.end())
break;
cp = *m_pos;
}
} }
CharRange range = { cp, cp }; CharRange range = { cp, cp };
@ -1223,6 +1230,12 @@ auto test_regex = UnitTest{[]{
TestVM vm{R"(Foo(?i)f[oB]+)"}; TestVM vm{R"(Foo(?i)f[oB]+)"};
kak_assert(vm.exec("FooFOoBb")); kak_assert(vm.exec("FooFOoBb"));
} }
{
TestVM vm{R"([^\]]+)"};
kak_assert(not vm.exec("a]c"));
kak_assert(vm.exec("abc"));
}
}}; }};
} }