Sync string_util.cpp

This commit is contained in:
Connor McLaughlin 2022-07-08 19:38:18 +10:00
parent 97730b56c8
commit b3c3d5d954
2 changed files with 213 additions and 24 deletions

View File

@ -8,9 +8,7 @@
#include "windows_headers.h"
#endif
namespace StringUtil {
std::string StdStringFromFormat(const char* format, ...)
std::string StringUtil::StdStringFromFormat(const char* format, ...)
{
std::va_list ap;
va_start(ap, format);
@ -19,7 +17,7 @@ std::string StdStringFromFormat(const char* format, ...)
return ret;
}
std::string StdStringFromFormatV(const char* format, std::va_list ap)
std::string StringUtil::StdStringFromFormatV(const char* format, std::va_list ap)
{
std::va_list ap_copy;
va_copy(ap_copy, ap);
@ -32,12 +30,18 @@ std::string StdStringFromFormatV(const char* format, std::va_list ap)
va_end(ap_copy);
std::string ret;
ret.resize(len);
std::vsnprintf(ret.data(), ret.size() + 1, format, ap);
// If an encoding error occurs, len is -1. Which we definitely don't want to resize to.
if (len > 0)
{
ret.resize(len);
std::vsnprintf(ret.data(), ret.size() + 1, format, ap);
}
return ret;
}
bool WildcardMatch(const char* subject, const char* mask, bool case_sensitive /*= true*/)
bool StringUtil::WildcardMatch(const char* subject, const char* mask, bool case_sensitive /*= true*/)
{
if (case_sensitive)
{
@ -133,7 +137,7 @@ bool WildcardMatch(const char* subject, const char* mask, bool case_sensitive /*
}
}
std::size_t Strlcpy(char* dst, const char* src, std::size_t size)
std::size_t StringUtil::Strlcpy(char* dst, const char* src, std::size_t size)
{
std::size_t len = std::strlen(src);
if (len < size)
@ -148,7 +152,7 @@ std::size_t Strlcpy(char* dst, const char* src, std::size_t size)
return len;
}
std::size_t Strlcpy(char* dst, const std::string_view& src, std::size_t size)
std::size_t StringUtil::Strlcpy(char* dst, const std::string_view& src, std::size_t size)
{
std::size_t len = src.length();
if (len < size)
@ -164,7 +168,7 @@ std::size_t Strlcpy(char* dst, const std::string_view& src, std::size_t size)
return len;
}
std::optional<std::vector<u8>> DecodeHex(const std::string_view& in)
std::optional<std::vector<u8>> StringUtil::DecodeHex(const std::string_view& in)
{
std::vector<u8> data;
data.reserve(in.size() / 2);
@ -181,18 +185,138 @@ std::optional<std::vector<u8>> DecodeHex(const std::string_view& in)
return {data};
}
std::string EncodeHex(const u8* data, int length)
std::string StringUtil::EncodeHex(const u8* data, int length)
{
std::stringstream ss;
for (int i = 0; i < length; i++)
ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(data[i]);
return ss.str();
}
std::string_view StringUtil::StripWhitespace(const std::string_view& str)
{
std::string_view::size_type start = 0;
while (start < str.size() && std::isspace(str[start]))
start++;
if (start == str.size())
return {};
std::string_view::size_type end = str.size() - 1;
while (end > start && std::isspace(str[end]))
end--;
return str.substr(start, end - start + 1);
}
void StringUtil::StripWhitespace(std::string* str)
{
{
const char* cstr = str->c_str();
std::string_view::size_type start = 0;
while (start < str->size() && std::isspace(cstr[start]))
start++;
if (start != 0)
str->erase(0, start);
}
{
const char* cstr = str->c_str();
std::string_view::size_type start = str->size();
while (start > 0 && std::isspace(cstr[start - 1]))
start--;
if (start != str->size())
str->erase(start);
}
}
std::vector<std::string_view> StringUtil::SplitString(const std::string_view& str, char delimiter,
bool skip_empty /*= true*/)
{
std::vector<std::string_view> res;
std::string_view::size_type last_pos = 0;
std::string_view::size_type pos;
while (last_pos < str.size() && (pos = str.find(delimiter, last_pos)) != std::string_view::npos)
{
std::string_view part(StripWhitespace(str.substr(last_pos, pos - last_pos)));
if (!skip_empty || !part.empty())
res.push_back(std::move(part));
last_pos = pos + 1;
}
if (last_pos < str.size())
{
std::string_view part(StripWhitespace(str.substr(last_pos)));
if (!skip_empty || !part.empty())
res.push_back(std::move(part));
}
return res;
}
std::string StringUtil::ReplaceAll(const std::string_view& subject, const std::string_view& search,
const std::string_view& replacement)
{
std::string ret(subject);
ReplaceAll(&ret, search, replacement);
return ret;
}
void StringUtil::ReplaceAll(std::string* subject, const std::string_view& search, const std::string_view& replacement)
{
if (!subject->empty())
{
std::string::size_type start_pos = 0;
while ((start_pos = subject->find(search, start_pos)) != std::string::npos)
{
subject->replace(start_pos, search.length(), replacement);
start_pos += replacement.length();
}
}
}
bool StringUtil::ParseAssignmentString(const std::string_view& str, std::string_view* key, std::string_view* value)
{
const std::string_view::size_type pos = str.find('=');
if (pos == std::string_view::npos)
{
*key = std::string_view();
*value = std::string_view();
return false;
}
*key = StripWhitespace(str.substr(0, pos));
if (pos != (str.size() - 1))
*value = StripWhitespace(str.substr(pos + 1));
else
*value = std::string_view();
return true;
}
void StringUtil::AppendUTF16CharacterToUTF8(std::string& s, u16 ch)
{
if (ch & 0xf800)
{
s.push_back(static_cast<char>(static_cast<u8>(0xe0 | static_cast<u8>(ch >> 12))));
s.push_back(static_cast<char>(static_cast<u8>(0x80 | static_cast<u8>(((ch >> 6) & 0x3f)))));
s.push_back(static_cast<char>(static_cast<u8>(0x80 | static_cast<u8>((ch & 0x3f)))));
}
else if (ch & 0xff80)
{
s.push_back(static_cast<char>(static_cast<u8>(0xc0 | static_cast<u8>((ch >> 6)))));
s.push_back(static_cast<char>(static_cast<u8>(0x80 | static_cast<u8>((ch & 0x3f)))));
}
else
{
s.push_back(static_cast<char>(static_cast<u8>(ch)));
}
}
#ifdef _WIN32
std::wstring UTF8StringToWideString(const std::string_view& str)
std::wstring StringUtil::UTF8StringToWideString(const std::string_view& str)
{
std::wstring ret;
if (!UTF8StringToWideString(ret, str))
@ -201,7 +325,7 @@ std::wstring UTF8StringToWideString(const std::string_view& str)
return ret;
}
bool UTF8StringToWideString(std::wstring& dest, const std::string_view& str)
bool StringUtil::UTF8StringToWideString(std::wstring& dest, const std::string_view& str)
{
int wlen = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
if (wlen < 0)
@ -214,7 +338,7 @@ bool UTF8StringToWideString(std::wstring& dest, const std::string_view& str)
return true;
}
std::string WideStringToUTF8String(const std::wstring_view& str)
std::string StringUtil::WideStringToUTF8String(const std::wstring_view& str)
{
std::string ret;
if (!WideStringToUTF8String(ret, str))
@ -223,7 +347,7 @@ std::string WideStringToUTF8String(const std::wstring_view& str)
return ret;
}
bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str)
bool StringUtil::WideStringToUTF8String(std::string& dest, const std::wstring_view& str)
{
int mblen = WideCharToMultiByte(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), nullptr, 0, nullptr, nullptr);
if (mblen < 0)
@ -240,5 +364,3 @@ bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str)
}
#endif
} // namespace StringUtil

View File

@ -100,13 +100,15 @@ template<>
inline std::optional<bool> FromChars(const std::string_view& str, int base)
{
if (Strncasecmp("true", str.data(), str.length()) == 0 || Strncasecmp("yes", str.data(), str.length()) == 0 ||
Strncasecmp("on", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0)
Strncasecmp("on", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0 ||
Strncasecmp("enabled", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0)
{
return true;
}
if (Strncasecmp("false", str.data(), str.length()) == 0 || Strncasecmp("no", str.data(), str.length()) == 0 ||
Strncasecmp("off", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0)
Strncasecmp("off", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0 ||
Strncasecmp("disabled", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0)
{
return false;
}
@ -119,20 +121,82 @@ std::optional<std::vector<u8>> DecodeHex(const std::string_view& str);
std::string EncodeHex(const u8* data, int length);
/// starts_with from C++20
ALWAYS_INLINE static bool StartsWith(const std::string_view& str, const char* prefix)
ALWAYS_INLINE static bool StartsWith(const std::string_view& str, const std::string_view& prefix)
{
return (str.compare(0, std::strlen(prefix), prefix) == 0);
return (str.compare(0, prefix.length(), prefix) == 0);
}
ALWAYS_INLINE static bool EndsWith(const std::string_view& str, const char* suffix)
ALWAYS_INLINE static bool EndsWith(const std::string_view& str, const std::string_view& suffix)
{
const std::size_t suffix_length = std::strlen(suffix);
const std::size_t suffix_length = suffix.length();
return (str.length() >= suffix_length && str.compare(str.length() - suffix_length, suffix_length, suffix) == 0);
}
/// StartsWith/EndsWith variants which aren't case sensitive.
ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view& str, const std::string_view& prefix)
{
return (!str.empty() && Strncasecmp(str.data(), prefix.data(), prefix.length()) == 0);
}
ALWAYS_INLINE static bool EndsWithNoCase(const std::string_view& str, const std::string_view& suffix)
{
const std::size_t suffix_length = suffix.length();
return (str.length() >= suffix_length &&
Strncasecmp(str.data() + (str.length() - suffix_length), suffix.data(), suffix_length) == 0);
}
/// Strip whitespace from the start/end of the string.
std::string_view StripWhitespace(const std::string_view& str);
void StripWhitespace(std::string* str);
/// Splits a string based on a single character delimiter.
std::vector<std::string_view> SplitString(const std::string_view& str, char delimiter, bool skip_empty = true);
/// Joins a string together using the specified delimiter.
template<typename T>
static inline std::string JoinString(const T& start, const T& end, char delimiter)
{
std::string ret;
for (auto it = start; it != end; ++it)
{
if (it != start)
ret += delimiter;
ret.append(*it);
}
return ret;
}
template<typename T>
static inline std::string JoinString(const T& start, const T& end, const std::string_view& delimiter)
{
std::string ret;
for (auto it = start; it != end; ++it)
{
if (it != start)
ret.append(delimiter);
ret.append(*it);
}
return ret;
}
/// Replaces all instances of search in subject with replacement.
std::string ReplaceAll(const std::string_view& subject, const std::string_view& search,
const std::string_view& replacement);
void ReplaceAll(std::string* subject, const std::string_view& search, const std::string_view& replacement);
/// Parses an assignment string (Key = Value) into its two components.
bool ParseAssignmentString(const std::string_view& str, std::string_view* key, std::string_view* value);
/// Appends a UTF-16/UTF-32 codepoint to a UTF-8 string.
void AppendUTF16CharacterToUTF8(std::string& s, u16 ch);
/// Strided memcpy/memcmp.
ALWAYS_INLINE static void StrideMemCpy(void* dst, std::size_t dst_stride, const void* src, std::size_t src_stride,
std::size_t copy_size, std::size_t count)
{
if (src_stride == dst_stride && src_stride == copy_size)
{
std::memcpy(dst, src, src_stride * count);
return;
}
const u8* src_ptr = static_cast<const u8*>(src);
u8* dst_ptr = static_cast<u8*>(dst);
for (std::size_t i = 0; i < count; i++)
@ -146,6 +210,9 @@ ALWAYS_INLINE static void StrideMemCpy(void* dst, std::size_t dst_stride, const
ALWAYS_INLINE static int StrideMemCmp(const void* p1, std::size_t p1_stride, const void* p2, std::size_t p2_stride,
std::size_t copy_size, std::size_t count)
{
if (p1_stride == p2_stride && p1_stride == copy_size)
return std::memcmp(p1, p2, p1_stride * count);
const u8* p1_ptr = static_cast<const u8*>(p1);
const u8* p2_ptr = static_cast<const u8*>(p2);
for (std::size_t i = 0; i < count; i++)