mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Use ByteBuffer::append for the StringBuilder class
Previously the StringBuilder class would use memcpy() to write directly into the ByteBuffer's buffer. Instead we should use the append() method which ensures we don't overrun the buffer.
This commit is contained in:
parent
425bfabd66
commit
8f755c9d07
Notes:
sideshowbarker
2024-07-18 17:08:11 +09:00
Author: https://github.com/gunnarbeutner Commit: https://github.com/SerenityOS/serenity/commit/8f755c9d075 Pull-request: https://github.com/SerenityOS/serenity/pull/7609 Reviewed-by: https://github.com/alimpfard
1 changed files with 4 additions and 4 deletions
|
@ -26,12 +26,12 @@ inline void StringBuilder::will_append(size_t size)
|
|||
if (needed_capacity > inline_capacity)
|
||||
expanded_capacity *= 2;
|
||||
VERIFY(!expanded_capacity.has_overflow());
|
||||
m_buffer.resize(expanded_capacity.value());
|
||||
m_buffer.ensure_capacity(expanded_capacity.value());
|
||||
}
|
||||
|
||||
StringBuilder::StringBuilder(size_t initial_capacity)
|
||||
: m_buffer(decltype(m_buffer)::create_uninitialized(initial_capacity))
|
||||
{
|
||||
m_buffer.ensure_capacity(initial_capacity);
|
||||
}
|
||||
|
||||
void StringBuilder::append(const StringView& str)
|
||||
|
@ -39,7 +39,7 @@ void StringBuilder::append(const StringView& str)
|
|||
if (str.is_empty())
|
||||
return;
|
||||
will_append(str.length());
|
||||
memcpy(data() + m_length, str.characters_without_null_termination(), str.length());
|
||||
m_buffer.append(str.characters_without_null_termination(), str.length());
|
||||
m_length += str.length();
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ void StringBuilder::append(const char* characters, size_t length)
|
|||
void StringBuilder::append(char ch)
|
||||
{
|
||||
will_append(1);
|
||||
data()[m_length] = ch;
|
||||
m_buffer.append(&ch, 1);
|
||||
m_length += 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue