mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +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
2 changed files with 15 additions and 2 deletions
|
@ -68,9 +68,19 @@ ByteBuffer StringBuilder::to_byte_buffer()
|
||||||
String StringBuilder::to_string()
|
String StringBuilder::to_string()
|
||||||
{
|
{
|
||||||
auto string = String((const char*)m_buffer.pointer(), m_length);
|
auto string = String((const char*)m_buffer.pointer(), m_length);
|
||||||
m_buffer.clear();
|
clear();
|
||||||
m_length = 0;
|
|
||||||
return string;
|
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();
|
String to_string();
|
||||||
ByteBuffer to_byte_buffer();
|
ByteBuffer to_byte_buffer();
|
||||||
|
|
||||||
|
StringView string_view() const;
|
||||||
|
void clear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void will_append(int);
|
void will_append(int);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue