From 08d988330620725f016b8361f549b33aa5f5f619 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Wed, 25 Sep 2019 11:49:41 +0300 Subject: [PATCH] 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. --- AK/StringBuilder.cpp | 14 ++++++++++++-- AK/StringBuilder.h | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 53ad4ba75d..b692bd4a4d 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -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; +} + } diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h index 9da31a7e86..b5f9a887f7 100644 --- a/AK/StringBuilder.h +++ b/AK/StringBuilder.h @@ -24,6 +24,9 @@ public: String to_string(); ByteBuffer to_byte_buffer(); + StringView string_view() const; + void clear(); + private: void will_append(int);