Common/String: Add helpers for std::string, std::string_view

This commit is contained in:
Connor McLaughlin 2021-01-30 14:36:03 +10:00
parent d8ea9c2983
commit e614522de5
2 changed files with 75 additions and 0 deletions

View File

@ -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<u32>(appendString.size()));
}
void String::AppendString(const std::string_view& appendString)
{
if (!appendString.empty())
InternalAppend(appendString.data(), static_cast<u32>(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<u32>(appendStr.size()));
}
void String::PrependString(const std::string_view& appendStr)
{
if (!appendStr.empty())
InternalPrepend(appendStr.data(), static_cast<u32>(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<u32>(appendStr.size()));
}
void String::InsertString(s32 offset, const std::string_view& appendStr)
{
InsertString(offset, appendStr.data(), static_cast<u32>(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<String::StringData*>(&s_EmptyStringData);
}
void String::Assign(const std::string& copyString)
{
Clear();
AppendString(copyString.data(), static_cast<u32>(copyString.size()));
}
void String::Assign(const std::string_view& copyString)
{
Clear();
AppendString(copyString.data(), static_cast<u32>(copyString.size()));
}
void String::AssignCopy(const String& copyString)
{
Clear();

View File

@ -4,6 +4,7 @@
#include <cstdarg>
#include <cstring>
#include <limits>
#include <string>
#include <string_view>
//
@ -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<s32>::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<s32>::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;