mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:17:44 +00:00
AK: StringBuilder with 0 initial capacity shouldn't build null String
With 0 initial capacity, we don't allocate an underlying ByteBuffer for the StringBuilder, which would then lead to a null String() being returned from to_string(). This patch makes sure we always build a valid String.
This commit is contained in:
parent
85f2987848
commit
d8aa2a6997
2 changed files with 11 additions and 0 deletions
|
@ -91,6 +91,8 @@ ByteBuffer StringBuilder::to_byte_buffer() const
|
|||
|
||||
String StringBuilder::to_string() const
|
||||
{
|
||||
if (is_empty())
|
||||
return String::empty();
|
||||
return String((const char*)m_buffer.data(), m_length);
|
||||
}
|
||||
|
||||
|
|
|
@ -199,4 +199,13 @@ TEST_CASE(split)
|
|||
EXPECT_EQ(parts[2].characters()[3], '\0');
|
||||
}
|
||||
|
||||
TEST_CASE(builder_zero_initial_capacity)
|
||||
{
|
||||
StringBuilder builder(0);
|
||||
builder.append("");
|
||||
auto built = builder.build();
|
||||
EXPECT_EQ(built.is_null(), false);
|
||||
EXPECT_EQ(built.length(), 0u);
|
||||
}
|
||||
|
||||
TEST_MAIN(String)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue