From e614522de50cc3735e0055def2d74891ba3229bb Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 30 Jan 2021 14:36:03 +1000 Subject: [PATCH] Common/String: Add helpers for std::string, std::string_view --- src/common/string.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++ src/common/string.h | 29 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/common/string.cpp b/src/common/string.cpp index ecde9ee40..08e0768fd 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -266,6 +266,18 @@ void String::AppendString(const char* appendString, u32 Count) InternalAppend(appendString, Count); } +void String::AppendString(const std::string& appendString) +{ + if (!appendString.empty()) + InternalAppend(appendString.c_str(), static_cast(appendString.size())); +} + +void String::AppendString(const std::string_view& appendString) +{ + if (!appendString.empty()) + InternalAppend(appendString.data(), static_cast(appendString.size())); +} + void String::AppendSubString(const String& appendStr, s32 Offset /* = 0 */, s32 Count /* = INT_std::max */) { u32 appendStrLength = appendStr.GetLength(); @@ -379,6 +391,18 @@ void String::PrependString(const char* appendString, u32 Count) InternalPrepend(appendString, Count); } +void String::PrependString(const std::string& appendStr) +{ + if (!appendStr.empty()) + InternalPrepend(appendStr.c_str(), static_cast(appendStr.size())); +} + +void String::PrependString(const std::string_view& appendStr) +{ + if (!appendStr.empty()) + InternalPrepend(appendStr.data(), static_cast(appendStr.size())); +} + void String::PrependSubString(const String& appendStr, s32 Offset /* = 0 */, s32 Count /* = INT_std::max */) { u32 appendStrLength = appendStr.GetLength(); @@ -504,6 +528,16 @@ void String::InsertString(s32 offset, const char* appendStr, u32 appendStrLength m_pStringData->pBuffer[m_pStringData->StringLength] = 0; } +void String::InsertString(s32 offset, const std::string& appendStr) +{ + InsertString(offset, appendStr.c_str(), static_cast(appendStr.size())); +} + +void String::InsertString(s32 offset, const std::string_view& appendStr) +{ + InsertString(offset, appendStr.data(), static_cast(appendStr.size())); +} + void String::Format(const char* FormatString, ...) { va_list ap; @@ -555,6 +589,18 @@ void String::Assign(String&& moveString) moveString.m_pStringData = const_cast(&s_EmptyStringData); } +void String::Assign(const std::string& copyString) +{ + Clear(); + AppendString(copyString.data(), static_cast(copyString.size())); +} + +void String::Assign(const std::string_view& copyString) +{ + Clear(); + AppendString(copyString.data(), static_cast(copyString.size())); +} + void String::AssignCopy(const String& copyString) { Clear(); diff --git a/src/common/string.h b/src/common/string.h index 90d3c5966..3e35e3d34 100644 --- a/src/common/string.h +++ b/src/common/string.h @@ -4,6 +4,7 @@ #include #include #include +#include #include // @@ -63,6 +64,8 @@ public: // manual assignment void Assign(const String& copyString); void Assign(const char* copyText); + void Assign(const std::string& copyString); + void Assign(const std::string_view& copyString); void Assign(String&& moveString); // assignment but ensures that we have our own copy. @@ -90,6 +93,8 @@ public: void AppendString(const String& appendStr); void AppendString(const char* appendText); void AppendString(const char* appendString, u32 Count); + void AppendString(const std::string& appendString); + void AppendString(const std::string_view& appendString); // append a substring of the specified string to this string void AppendSubString(const String& appendStr, s32 Offset = 0, s32 Count = std::numeric_limits::max()); @@ -106,6 +111,8 @@ public: void PrependString(const String& appendStr); void PrependString(const char* appendText); void PrependString(const char* appendString, u32 Count); + void PrependString(const std::string& appendStr); + void PrependString(const std::string_view& appendStr); // append a substring of the specified string to this string void PrependSubString(const String& appendStr, s32 Offset = 0, s32 Count = std::numeric_limits::max()); @@ -119,6 +126,8 @@ public: void InsertString(s32 offset, const String& appendStr); void InsertString(s32 offset, const char* appendStr); void InsertString(s32 offset, const char* appendStr, u32 appendStrLength); + void InsertString(s32 offset, const std::string& appendStr); + void InsertString(s32 offset, const std::string_view& appendStr); // set to formatted string void Format(const char* FormatString, ...); @@ -243,6 +252,16 @@ public: Assign(Text); return *this; } + String& operator=(const std::string& Text) + { + Assign(Text); + return *this; + } + String& operator=(const std::string_view& Text) + { + Assign(Text); + return *this; + } // Move operator. String& operator=(String&& moveString) @@ -348,6 +367,16 @@ public: Assign(Text); return *this; } + StackString& operator=(const std::string& Text) + { + Assign(Text); + return *this; + } + StackString& operator=(const std::string_view& Text) + { + Assign(Text); + return *this; + } private: StringData m_sStringData;