AK: Add StringBuilder::string_view() and StringBuilder::clear()

The former allows you to inspect the string while it's being built.
It's an explicit method rather than `operator StringView()` because
you must remember you can only look at it in between modifications;
appending to the StringBuilder invalidates the StringView.

The latter lets you clear the state of a StringBuilder explicitly, to
start from an empty string again.
This commit is contained in:
Sergey Bugaev 2019-09-25 11:49:41 +03:00 committed by Andreas Kling
parent fd0aa5dd43
commit 08d9883306
2 changed files with 15 additions and 2 deletions

View file

@ -68,9 +68,19 @@ ByteBuffer StringBuilder::to_byte_buffer()
String StringBuilder::to_string()
{
auto string = String((const char*)m_buffer.pointer(), m_length);
m_buffer.clear();
m_length = 0;
clear();
return string;
}
StringView StringBuilder::string_view() const
{
return StringView { (const char*)m_buffer.pointer(), m_length };
}
void StringBuilder::clear()
{
m_buffer.clear();
m_length = 0;
}
}

View file

@ -24,6 +24,9 @@ public:
String to_string();
ByteBuffer to_byte_buffer();
StringView string_view() const;
void clear();
private:
void will_append(int);