AK: Inline all the trivial Utf8View functions
This improves parsing time on a large chunk of JS by ~3%.
This commit is contained in:
parent
1be4cbd639
commit
391352c112
Notes:
sideshowbarker
2024-07-18 03:43:15 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/391352c1121
2 changed files with 15 additions and 52 deletions
|
@ -11,26 +11,6 @@
|
|||
|
||||
namespace AK {
|
||||
|
||||
const unsigned char* Utf8View::begin_ptr() const
|
||||
{
|
||||
return (const unsigned char*)m_string.characters_without_null_termination();
|
||||
}
|
||||
|
||||
const unsigned char* Utf8View::end_ptr() const
|
||||
{
|
||||
return begin_ptr() + m_string.length();
|
||||
}
|
||||
|
||||
Utf8CodePointIterator Utf8View::begin() const
|
||||
{
|
||||
return { begin_ptr(), m_string.length() };
|
||||
}
|
||||
|
||||
Utf8CodePointIterator Utf8View::end() const
|
||||
{
|
||||
return { end_ptr(), 0 };
|
||||
}
|
||||
|
||||
Utf8CodePointIterator Utf8View::iterator_at_byte_offset(size_t byte_offset) const
|
||||
{
|
||||
size_t current_offset = 0;
|
||||
|
@ -65,12 +45,6 @@ size_t Utf8View::byte_offset_of(size_t code_point_offset) const
|
|||
return byte_offset;
|
||||
}
|
||||
|
||||
Utf8View Utf8View::substring_view(size_t byte_offset, size_t byte_length) const
|
||||
{
|
||||
StringView string = m_string.substring_view(byte_offset, byte_length);
|
||||
return Utf8View { string };
|
||||
}
|
||||
|
||||
Utf8View Utf8View::unicode_substring_view(size_t code_point_offset, size_t code_point_length) const
|
||||
{
|
||||
if (code_point_length == 0)
|
||||
|
@ -214,22 +188,6 @@ Utf8View Utf8View::trim(const Utf8View& characters, TrimMode mode) const
|
|||
return substring_view(substring_start, substring_length);
|
||||
}
|
||||
|
||||
Utf8CodePointIterator::Utf8CodePointIterator(const unsigned char* ptr, size_t length)
|
||||
: m_ptr(ptr)
|
||||
, m_length(length)
|
||||
{
|
||||
}
|
||||
|
||||
bool Utf8CodePointIterator::operator==(const Utf8CodePointIterator& other) const
|
||||
{
|
||||
return m_ptr == other.m_ptr && m_length == other.m_length;
|
||||
}
|
||||
|
||||
bool Utf8CodePointIterator::operator!=(const Utf8CodePointIterator& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
Utf8CodePointIterator& Utf8CodePointIterator::operator++()
|
||||
{
|
||||
VERIFY(m_length > 0);
|
||||
|
|
|
@ -22,8 +22,8 @@ public:
|
|||
Utf8CodePointIterator() = default;
|
||||
~Utf8CodePointIterator() = default;
|
||||
|
||||
bool operator==(const Utf8CodePointIterator&) const;
|
||||
bool operator!=(const Utf8CodePointIterator&) const;
|
||||
bool operator==(Utf8CodePointIterator const&) const = default;
|
||||
bool operator!=(Utf8CodePointIterator const&) const = default;
|
||||
Utf8CodePointIterator& operator++();
|
||||
u32 operator*() const;
|
||||
// NOTE: This returns {} if the peek is at or past EOF.
|
||||
|
@ -44,9 +44,14 @@ public:
|
|||
bool done() const { return m_length == 0; }
|
||||
|
||||
private:
|
||||
Utf8CodePointIterator(const unsigned char*, size_t);
|
||||
const unsigned char* m_ptr { nullptr };
|
||||
size_t m_length;
|
||||
Utf8CodePointIterator(u8 const* ptr, size_t length)
|
||||
: m_ptr(ptr)
|
||||
, m_length(length)
|
||||
{
|
||||
}
|
||||
|
||||
u8 const* m_ptr { nullptr };
|
||||
size_t m_length { 0 };
|
||||
};
|
||||
|
||||
class Utf8View {
|
||||
|
@ -71,8 +76,8 @@ public:
|
|||
|
||||
const StringView& as_string() const { return m_string; }
|
||||
|
||||
Utf8CodePointIterator begin() const;
|
||||
Utf8CodePointIterator end() const;
|
||||
Utf8CodePointIterator begin() const { return { begin_ptr(), m_string.length() }; }
|
||||
Utf8CodePointIterator end() const { return { end_ptr(), 0 }; }
|
||||
Utf8CodePointIterator iterator_at_byte_offset(size_t) const;
|
||||
|
||||
const unsigned char* bytes() const { return begin_ptr(); }
|
||||
|
@ -80,7 +85,7 @@ public:
|
|||
size_t byte_offset_of(const Utf8CodePointIterator&) const;
|
||||
size_t byte_offset_of(size_t code_point_offset) const;
|
||||
|
||||
Utf8View substring_view(size_t byte_offset, size_t byte_length) const;
|
||||
Utf8View substring_view(size_t byte_offset, size_t byte_length) const { return Utf8View { m_string.substring_view(byte_offset, byte_length) }; }
|
||||
Utf8View substring_view(size_t byte_offset) const { return substring_view(byte_offset, byte_length() - byte_offset); }
|
||||
Utf8View unicode_substring_view(size_t code_point_offset, size_t code_point_length) const;
|
||||
Utf8View unicode_substring_view(size_t code_point_offset) const { return unicode_substring_view(code_point_offset, length() - code_point_offset); }
|
||||
|
@ -114,8 +119,8 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
const unsigned char* begin_ptr() const;
|
||||
const unsigned char* end_ptr() const;
|
||||
u8 const* begin_ptr() const { return (u8 const*)m_string.characters_without_null_termination(); }
|
||||
u8 const* end_ptr() const { return begin_ptr() + m_string.length(); }
|
||||
size_t calculate_length() const;
|
||||
|
||||
StringView m_string;
|
||||
|
|
Loading…
Add table
Reference in a new issue