mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:04:59 +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
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…
Add table
Add a link
Reference in a new issue