mirror of
https://github.com/stenzek/duckstation.git
synced 2026-02-14 10:24:37 +00:00
StringUtil: Backport helper functions
This commit is contained in:
@@ -162,6 +162,20 @@ std::size_t StringUtil::Strlcpy(char* dst, const std::string_view src, std::size
|
||||
return len;
|
||||
}
|
||||
|
||||
std::string StringUtil::StripControlCharacters(std::string_view str)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(str.length());
|
||||
for (size_t i = 0; i < str.length();)
|
||||
{
|
||||
char32_t ch;
|
||||
i += StringUtil::DecodeUTF8(str, i, &ch);
|
||||
ch = (ch < 0x20) ? '_' : ch;
|
||||
StringUtil::EncodeAndAppendUTF8(out, ch);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
u8 StringUtil::DecodeHexDigit(char ch)
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
@@ -321,6 +335,17 @@ std::string StringUtil::EncodeBase64(const std::span<u8> data)
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t StringUtil::CountChar(const std::string_view str, char ch)
|
||||
{
|
||||
return std::count(str.begin(), str.end(), ch);
|
||||
}
|
||||
|
||||
size_t StringUtil::CountCharNoCase(const std::string_view str, char ch)
|
||||
{
|
||||
ch = ToLower(ch);
|
||||
return std::count_if(str.begin(), str.end(), [ch](char och) { return (ch == ToLower(och)); });
|
||||
}
|
||||
|
||||
std::string_view StringUtil::StripWhitespace(const std::string_view str)
|
||||
{
|
||||
std::string_view::size_type start = 0;
|
||||
|
||||
@@ -267,6 +267,9 @@ ALWAYS_INLINE bool IsWhitespace(char ch)
|
||||
ch == 0x20); // space
|
||||
}
|
||||
|
||||
/// Removes control characters from the given string.
|
||||
std::string StripControlCharacters(std::string_view str);
|
||||
|
||||
/// Encode/decode hexadecimal byte buffers
|
||||
u8 DecodeHexDigit(char ch);
|
||||
size_t DecodeHex(std::span<u8> dest, const std::string_view str);
|
||||
@@ -347,6 +350,10 @@ ALWAYS_INLINE bool EndsWithNoCase(const std::string_view str, const std::string_
|
||||
Strncasecmp(str.data() + (str.length() - suffix_length), suffix.data(), suffix_length) == 0);
|
||||
}
|
||||
|
||||
/// Returns the number of occurrences of the given character in the string.
|
||||
size_t CountChar(const std::string_view str, char ch);
|
||||
size_t CountCharNoCase(const std::string_view str, char ch);
|
||||
|
||||
/// Strip whitespace from the start/end of the string.
|
||||
std::string_view StripWhitespace(const std::string_view str);
|
||||
void StripWhitespace(std::string* str);
|
||||
|
||||
Reference in New Issue
Block a user