mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-21 18:15:58 +00:00
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:
parent
fd0aa5dd43
commit
08d9883306
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,6 +24,9 @@ public:
|
|||
String to_string();
|
||||
ByteBuffer to_byte_buffer();
|
||||
|
||||
StringView string_view() const;
|
||||
void clear();
|
||||
|
||||
private:
|
||||
void will_append(int);
|
||||
|
||||
|
|
Loading…
Reference in a new issue