1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:17:35 +00:00

LibRegex: Avoid slicing a RegexStringView in non-unicode Compare ops

Getting a single code point is much faster than slicing into the string.
This commit is contained in:
Ali Mohammad Pur 2023-07-28 20:57:51 +03:30 committed by Andreas Kling
parent 11abca421a
commit 221c52c696
2 changed files with 43 additions and 8 deletions

View file

@ -434,6 +434,29 @@ public:
});
}
u32 code_unit_at(size_t code_unit_index) const
{
if (unicode())
return operator[](code_unit_index);
return m_view.visit(
[&](StringView view) -> u32 {
auto ch = view[code_unit_index];
if constexpr (IsSigned<char>) {
if (ch < 0)
return 256u + ch;
return ch;
}
},
[&](Utf32View const& view) -> u32 { return view[code_unit_index]; },
[&](Utf16View const& view) -> u32 { return view.code_unit_at(code_unit_index); },
[&](Utf8View const& view) -> u32 {
auto it = view.iterator_at_byte_offset(code_unit_index);
VERIFY(it != view.end());
return *it;
});
}
size_t code_unit_offset_of(size_t code_point_index) const
{
return m_view.visit(