From 917ccb19eae8e39a119cda10d82e23dc60d133a4 Mon Sep 17 00:00:00 2001 From: Emanuel Sprung Date: Wed, 6 May 2020 18:53:05 +0200 Subject: [PATCH] AK: Add to_string() method to StringView This allows easy creation of a new string from an existing StringView. Can be used e.g. for output with printf(..., view.to_string().characters()) instead of writing printf(..., String{view}.characters()). --- AK/StringView.cpp | 2 ++ AK/StringView.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/AK/StringView.cpp b/AK/StringView.cpp index 88a4d9217b..f65e324a3f 100644 --- a/AK/StringView.cpp +++ b/AK/StringView.cpp @@ -264,4 +264,6 @@ Optional StringView::find_last_of(const StringView& view) const return {}; } +String StringView::to_string() const { return String { *this }; } + } diff --git a/AK/StringView.h b/AK/StringView.h index 3d7105888d..e82dbf5455 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -152,6 +152,8 @@ public: const StringImpl* impl() const { return m_impl; } + String to_string() const; + private: friend class String; const StringImpl* m_impl { nullptr };