From aa383d0bdab9ed2ad9f6587785c4017e521363b9 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 13 Sep 2020 00:05:17 +1000 Subject: [PATCH] Common/String: Support constructing from string_view --- src/common/string.cpp | 5 +++++ src/common/string.h | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/common/string.cpp b/src/common/string.cpp index 6b57854af..ecde9ee40 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -166,6 +166,11 @@ String::String(String&& moveString) Assign(moveString); } +String::String(const std::string_view& sv) +{ + AppendString(sv.data(), static_cast(sv.size())); +} + String::~String() { StringDataRelease(m_pStringData); diff --git a/src/common/string.h b/src/common/string.h index a0c1c7b9d..90d3c5966 100644 --- a/src/common/string.h +++ b/src/common/string.h @@ -54,6 +54,9 @@ public: // Construct a string from a data object, does not increment the reference count on the string data, use carefully. explicit String(StringData* pStringData) : m_pStringData(pStringData) {} + // Creates string from string_view. + String(const std::string_view& sv); + // Destructor. Child classes may not have any destructors, as this is not virtual. ~String(); @@ -307,6 +310,12 @@ public: Assign(copyString.GetCharArray()); } + StackString(const std::string_view& sv) : String(&m_sStringData) + { + InitStackStringData(); + AppendString(sv.data(), static_cast(sv.size())); + } + // Override the fromstring method static StackString FromFormat(const char* FormatString, ...) {