mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Misc: std::string -> std::string_view in a bunch of places, notably chat commands and Util.h
(cherry picked from commit a41bbd9ad9)
This commit is contained in:
@@ -35,10 +35,10 @@
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
Tokenizer::Tokenizer(const std::string &src, const char sep, uint32 vectorReserve /*= 0*/, bool keepEmptyStrings /*= true*/)
|
||||
Tokenizer::Tokenizer(std::string_view src, const char sep, uint32 vectorReserve /*= 0*/, bool keepEmptyStrings /*= true*/)
|
||||
{
|
||||
m_str = new char[src.length() + 1];
|
||||
memcpy(m_str, src.c_str(), src.length() + 1);
|
||||
memcpy(m_str, src.data(), src.length() + 1);
|
||||
|
||||
if (vectorReserve)
|
||||
m_storage.reserve(vectorReserve);
|
||||
@@ -400,12 +400,12 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr)
|
||||
bool Utf8toWStr(std::string_view utf8str, std::wstring& wstr)
|
||||
{
|
||||
wstr.clear();
|
||||
try
|
||||
{
|
||||
utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), std::back_inserter(wstr));
|
||||
utf8::utf8to16(utf8str.begin(), utf8str.end(), std::back_inserter(wstr));
|
||||
}
|
||||
catch (std::exception const&)
|
||||
{
|
||||
@@ -439,7 +439,7 @@ bool WStrToUtf8(wchar_t const* wstr, size_t size, std::string& utf8str)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WStrToUtf8(std::wstring const& wstr, std::string& utf8str)
|
||||
bool WStrToUtf8(std::wstring_view wstr, std::string& utf8str)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -448,7 +448,7 @@ bool WStrToUtf8(std::wstring const& wstr, std::string& utf8str)
|
||||
|
||||
if (wstr.size())
|
||||
{
|
||||
char* oend = utf8::utf16to8(wstr.c_str(), wstr.c_str()+wstr.size(), &utf8str2[0]);
|
||||
char* oend = utf8::utf16to8(wstr.begin(), wstr.end(), &utf8str2[0]);
|
||||
utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail
|
||||
}
|
||||
utf8str = utf8str2;
|
||||
@@ -462,7 +462,12 @@ bool WStrToUtf8(std::wstring const& wstr, std::string& utf8str)
|
||||
return true;
|
||||
}
|
||||
|
||||
std::wstring wstrCaseAccentInsensitiveParse(std::wstring const& wstr, LocaleConstant locale)
|
||||
void wstrToUpper(std::wstring& str) { std::transform(std::begin(str), std::end(str), std::begin(str), wcharToUpper); }
|
||||
void wstrToLower(std::wstring& str) { std::transform(std::begin(str), std::end(str), std::begin(str), wcharToLower); }
|
||||
void strToUpper(std::string& str) { std::transform(std::begin(str), std::end(str), std::begin(str), charToUpper); }
|
||||
void strToLower(std::string& str) { std::transform(std::begin(str), std::end(str), std::begin(str), charToLower); }
|
||||
|
||||
std::wstring wstrCaseAccentInsensitiveParse(std::wstring_view wstr, LocaleConstant locale)
|
||||
{
|
||||
std::wstring result;
|
||||
result.reserve(wstr.length() * 2);
|
||||
@@ -647,21 +652,6 @@ std::wstring wstrCaseAccentInsensitiveParse(std::wstring const& wstr, LocaleCons
|
||||
return result;
|
||||
}
|
||||
|
||||
void wstrToUpper(std::wstring& str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), wcharToUpper);
|
||||
}
|
||||
|
||||
void strToLower(std::string& str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), [](char c) { return std::tolower(c); });
|
||||
}
|
||||
|
||||
void wstrToLower(std::wstring& str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), wcharToLower);
|
||||
}
|
||||
|
||||
std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension)
|
||||
{
|
||||
// supported only Cyrillic cases
|
||||
@@ -711,7 +701,7 @@ std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension)
|
||||
return wname;
|
||||
}
|
||||
|
||||
bool utf8ToConsole(const std::string& utf8str, std::string& conStr)
|
||||
bool utf8ToConsole(std::string_view utf8str, std::string& conStr)
|
||||
{
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
std::wstring wstr;
|
||||
@@ -728,7 +718,7 @@ bool utf8ToConsole(const std::string& utf8str, std::string& conStr)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool consoleToUtf8(const std::string& conStr, std::string& utf8str)
|
||||
bool consoleToUtf8(std::string_view conStr, std::string& utf8str)
|
||||
{
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
std::wstring wstr;
|
||||
@@ -743,7 +733,7 @@ bool consoleToUtf8(const std::string& conStr, std::string& utf8str)
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Utf8FitTo(const std::string& str, std::wstring const& search)
|
||||
bool Utf8FitTo(std::string_view str, std::wstring_view search)
|
||||
{
|
||||
std::wstring temp;
|
||||
|
||||
@@ -823,7 +813,7 @@ std::string Trinity::Impl::ByteArrayToHexStr(uint8 const* bytes, size_t arrayLen
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void Trinity::Impl::HexStrToByteArray(std::string const& str, uint8* out, size_t outlen, bool reverse /*= false*/)
|
||||
void Trinity::Impl::HexStrToByteArray(std::string_view str, uint8* out, size_t outlen, bool reverse /*= false*/)
|
||||
{
|
||||
ASSERT(str.size() == (2 * outlen));
|
||||
|
||||
@@ -846,14 +836,12 @@ void Trinity::Impl::HexStrToByteArray(std::string const& str, uint8* out, size_t
|
||||
}
|
||||
}
|
||||
|
||||
bool StringToBool(std::string const& str)
|
||||
bool StringToBool(std::string_view str)
|
||||
{
|
||||
std::string lowerStr = str;
|
||||
std::transform(str.begin(), str.end(), lowerStr.begin(), [](char c) { return char(::tolower(c)); });
|
||||
return lowerStr == "1" || lowerStr == "true" || lowerStr == "yes";
|
||||
return ((str == "1") || StringEqualI(str, "true") || StringEqualI(str, "yes"));
|
||||
}
|
||||
|
||||
bool StringEqualI(std::string const& str1, std::string const& str2)
|
||||
bool StringEqualI(std::string_view str1, std::string_view str2)
|
||||
{
|
||||
return std::equal(str1.begin(), str1.end(), str2.begin(), str2.end(),
|
||||
[](char a, char b)
|
||||
@@ -862,12 +850,12 @@ bool StringEqualI(std::string const& str1, std::string const& str2)
|
||||
});
|
||||
}
|
||||
|
||||
bool StringStartsWith(std::string const& haystack, std::string const& needle)
|
||||
bool StringStartsWith(std::string_view haystack, std::string_view needle)
|
||||
{
|
||||
return (haystack.rfind(needle, 0) == 0);
|
||||
}
|
||||
|
||||
bool StringContainsStringI(std::string const& haystack, std::string const& needle)
|
||||
bool StringContainsStringI(std::string_view haystack, std::string_view needle)
|
||||
{
|
||||
return haystack.end() !=
|
||||
std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(), [](char c1, char c2) { return std::toupper(c1) == std::toupper(c2); });
|
||||
|
||||
Reference in New Issue
Block a user