mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-04-27 19:25:43 -04:00
StringUtil: Pass string_views by value
This commit is contained in:
parent
710698f7e1
commit
4f84a98864
@ -157,7 +157,7 @@ std::size_t StringUtil::Strlcpy(char* dst, const char* src, std::size_t size)
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t StringUtil::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();
|
std::size_t len = src.length();
|
||||||
if (len < size)
|
if (len < size)
|
||||||
@ -173,7 +173,7 @@ std::size_t StringUtil::Strlcpy(char* dst, const std::string_view& src, std::siz
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::vector<u8>> StringUtil::DecodeHex(const std::string_view& in)
|
std::optional<std::vector<u8>> StringUtil::DecodeHex(const std::string_view in)
|
||||||
{
|
{
|
||||||
std::vector<u8> data;
|
std::vector<u8> data;
|
||||||
data.reserve(in.size() / 2);
|
data.reserve(in.size() / 2);
|
||||||
@ -199,7 +199,7 @@ std::string StringUtil::EncodeHex(const u8* data, int length)
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view StringUtil::StripWhitespace(const std::string_view& str)
|
std::string_view StringUtil::StripWhitespace(const std::string_view str)
|
||||||
{
|
{
|
||||||
std::string_view::size_type start = 0;
|
std::string_view::size_type start = 0;
|
||||||
while (start < str.size() && std::isspace(str[start]))
|
while (start < str.size() && std::isspace(str[start]))
|
||||||
@ -235,7 +235,7 @@ void StringUtil::StripWhitespace(std::string* str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string_view> StringUtil::SplitString(const std::string_view& str, char delimiter,
|
std::vector<std::string_view> StringUtil::SplitString(const std::string_view str, char delimiter,
|
||||||
bool skip_empty /*= true*/)
|
bool skip_empty /*= true*/)
|
||||||
{
|
{
|
||||||
std::vector<std::string_view> res;
|
std::vector<std::string_view> res;
|
||||||
@ -260,7 +260,7 @@ std::vector<std::string_view> StringUtil::SplitString(const std::string_view& st
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> StringUtil::SplitNewString(const std::string_view& str, char delimiter,
|
std::vector<std::string> StringUtil::SplitNewString(const std::string_view str, char delimiter,
|
||||||
bool skip_empty /*= true*/)
|
bool skip_empty /*= true*/)
|
||||||
{
|
{
|
||||||
std::vector<std::string> res;
|
std::vector<std::string> res;
|
||||||
@ -285,15 +285,15 @@ std::vector<std::string> StringUtil::SplitNewString(const std::string_view& str,
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string StringUtil::ReplaceAll(const std::string_view& subject, const std::string_view& search,
|
std::string StringUtil::ReplaceAll(const std::string_view subject, const std::string_view search,
|
||||||
const std::string_view& replacement)
|
const std::string_view replacement)
|
||||||
{
|
{
|
||||||
std::string ret(subject);
|
std::string ret(subject);
|
||||||
ReplaceAll(&ret, search, replacement);
|
ReplaceAll(&ret, search, replacement);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringUtil::ReplaceAll(std::string* subject, const std::string_view& search, const std::string_view& replacement)
|
void StringUtil::ReplaceAll(std::string* subject, const std::string_view search, const std::string_view replacement)
|
||||||
{
|
{
|
||||||
if (!subject->empty())
|
if (!subject->empty())
|
||||||
{
|
{
|
||||||
@ -306,7 +306,7 @@ void StringUtil::ReplaceAll(std::string* subject, const std::string_view& search
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StringUtil::ParseAssignmentString(const std::string_view& str, std::string_view* key, std::string_view* value)
|
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('=');
|
const std::string_view::size_type pos = str.find('=');
|
||||||
if (pos == std::string_view::npos)
|
if (pos == std::string_view::npos)
|
||||||
@ -397,7 +397,7 @@ invalid:
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string StringUtil::Ellipsise(const std::string_view& str, u32 max_length, const char* ellipsis /*= "..."*/)
|
std::string StringUtil::Ellipsise(const std::string_view str, u32 max_length, const char* ellipsis /*= "..."*/)
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
ret.reserve(max_length);
|
ret.reserve(max_length);
|
||||||
@ -438,7 +438,7 @@ void StringUtil::EllipsiseInPlace(std::string& str, u32 max_length, const char*
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t StringUtil::DecodeUTF8(const std::string_view& str, size_t offset, char32_t* ch)
|
size_t StringUtil::DecodeUTF8(const std::string_view str, size_t offset, char32_t* ch)
|
||||||
{
|
{
|
||||||
return DecodeUTF8(str.data() + offset, str.length() - offset, ch);
|
return DecodeUTF8(str.data() + offset, str.length() - offset, ch);
|
||||||
}
|
}
|
||||||
@ -450,7 +450,7 @@ size_t StringUtil::DecodeUTF8(const std::string& str, size_t offset, char32_t* c
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
std::wstring StringUtil::UTF8StringToWideString(const std::string_view& str)
|
std::wstring StringUtil::UTF8StringToWideString(const std::string_view str)
|
||||||
{
|
{
|
||||||
std::wstring ret;
|
std::wstring ret;
|
||||||
if (!UTF8StringToWideString(ret, str))
|
if (!UTF8StringToWideString(ret, str))
|
||||||
@ -459,7 +459,7 @@ std::wstring StringUtil::UTF8StringToWideString(const std::string_view& str)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StringUtil::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);
|
int wlen = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), nullptr, 0);
|
||||||
if (wlen < 0)
|
if (wlen < 0)
|
||||||
@ -472,7 +472,7 @@ bool StringUtil::UTF8StringToWideString(std::wstring& dest, const std::string_vi
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string StringUtil::WideStringToUTF8String(const std::wstring_view& str)
|
std::string StringUtil::WideStringToUTF8String(const std::wstring_view str)
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
if (!WideStringToUTF8String(ret, str))
|
if (!WideStringToUTF8String(ret, str))
|
||||||
@ -481,7 +481,7 @@ std::string StringUtil::WideStringToUTF8String(const std::wstring_view& str)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StringUtil::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);
|
int mblen = WideCharToMultiByte(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), nullptr, 0, nullptr, nullptr);
|
||||||
if (mblen < 0)
|
if (mblen < 0)
|
||||||
|
@ -38,7 +38,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 Strlcpy(char* dst, const char* src, std::size_t size);
|
||||||
|
|
||||||
/// Strlcpy from string_view.
|
/// Strlcpy from string_view.
|
||||||
std::size_t Strlcpy(char* dst, const std::string_view& src, std::size_t size);
|
std::size_t Strlcpy(char* dst, const std::string_view src, std::size_t size);
|
||||||
|
|
||||||
/// Platform-independent strcasecmp
|
/// Platform-independent strcasecmp
|
||||||
static inline int Strcasecmp(const char* s1, const char* s2)
|
static inline int Strcasecmp(const char* s1, const char* s2)
|
||||||
@ -71,7 +71,7 @@ static inline bool EqualNoCase(std::string_view s1, std::string_view s2)
|
|||||||
|
|
||||||
/// Wrapper around std::from_chars
|
/// Wrapper around std::from_chars
|
||||||
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||||
inline std::optional<T> FromChars(const std::string_view& str, int base = 10)
|
inline std::optional<T> FromChars(const std::string_view str, int base = 10)
|
||||||
{
|
{
|
||||||
T value;
|
T value;
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ inline std::optional<T> FromChars(const std::string_view& str, int base = 10)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||||
inline std::optional<T> FromChars(const std::string_view& str, int base, std::string_view* endptr)
|
inline std::optional<T> FromChars(const std::string_view str, int base, std::string_view* endptr)
|
||||||
{
|
{
|
||||||
T value;
|
T value;
|
||||||
|
|
||||||
@ -102,7 +102,7 @@ inline std::optional<T> FromChars(const std::string_view& str, int base, std::st
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||||
inline std::optional<T> FromChars(const std::string_view& str)
|
inline std::optional<T> FromChars(const std::string_view str)
|
||||||
{
|
{
|
||||||
T value;
|
T value;
|
||||||
|
|
||||||
@ -113,7 +113,7 @@ inline std::optional<T> FromChars(const std::string_view& str)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||||
inline std::optional<T> FromChars(const std::string_view& str, std::string_view* endptr)
|
inline std::optional<T> FromChars(const std::string_view str, std::string_view* endptr)
|
||||||
{
|
{
|
||||||
T value;
|
T value;
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ inline std::string ToChars(T value)
|
|||||||
|
|
||||||
/// Explicit override for booleans
|
/// Explicit override for booleans
|
||||||
template<>
|
template<>
|
||||||
inline std::optional<bool> FromChars(const std::string_view& str, int base)
|
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 ||
|
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 ||
|
||||||
@ -203,15 +203,15 @@ inline std::string ToChars(bool value, int base)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Encode/decode hexadecimal byte buffers
|
/// Encode/decode hexadecimal byte buffers
|
||||||
std::optional<std::vector<u8>> DecodeHex(const std::string_view& str);
|
std::optional<std::vector<u8>> DecodeHex(const std::string_view str);
|
||||||
std::string EncodeHex(const u8* data, int length);
|
std::string EncodeHex(const u8* data, int length);
|
||||||
|
|
||||||
/// StartsWith/EndsWith variants which aren't case sensitive.
|
/// StartsWith/EndsWith variants which aren't case sensitive.
|
||||||
ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view& str, const std::string_view& prefix)
|
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);
|
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)
|
ALWAYS_INLINE static bool EndsWithNoCase(const std::string_view str, const std::string_view suffix)
|
||||||
{
|
{
|
||||||
const std::size_t suffix_length = suffix.length();
|
const std::size_t suffix_length = suffix.length();
|
||||||
return (str.length() >= suffix_length &&
|
return (str.length() >= suffix_length &&
|
||||||
@ -219,12 +219,12 @@ ALWAYS_INLINE static bool EndsWithNoCase(const std::string_view& str, const std:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Strip whitespace from the start/end of the string.
|
/// Strip whitespace from the start/end of the string.
|
||||||
std::string_view StripWhitespace(const std::string_view& str);
|
std::string_view StripWhitespace(const std::string_view str);
|
||||||
void StripWhitespace(std::string* str);
|
void StripWhitespace(std::string* str);
|
||||||
|
|
||||||
/// Splits a string based on a single character delimiter.
|
/// 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);
|
std::vector<std::string_view> SplitString(const std::string_view str, char delimiter, bool skip_empty = true);
|
||||||
std::vector<std::string> SplitNewString(const std::string_view& str, char delimiter, bool skip_empty = true);
|
std::vector<std::string> SplitNewString(const std::string_view str, char delimiter, bool skip_empty = true);
|
||||||
|
|
||||||
/// Joins a string together using the specified delimiter.
|
/// Joins a string together using the specified delimiter.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -240,7 +240,7 @@ static inline std::string JoinString(const T& start, const T& end, char delimite
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static inline std::string JoinString(const T& start, const T& end, const std::string_view& delimiter)
|
static inline std::string JoinString(const T& start, const T& end, const std::string_view delimiter)
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
for (auto it = start; it != end; ++it)
|
for (auto it = start; it != end; ++it)
|
||||||
@ -253,12 +253,12 @@ static inline std::string JoinString(const T& start, const T& end, const std::st
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Replaces all instances of search in subject with replacement.
|
/// Replaces all instances of search in subject with replacement.
|
||||||
std::string ReplaceAll(const std::string_view& subject, const std::string_view& search,
|
std::string ReplaceAll(const std::string_view subject, const std::string_view search,
|
||||||
const std::string_view& replacement);
|
const std::string_view replacement);
|
||||||
void ReplaceAll(std::string* 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.
|
/// 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);
|
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.
|
/// Appends a UTF-16/UTF-32 codepoint to a UTF-8 string.
|
||||||
void EncodeAndAppendUTF8(std::string& s, char32_t ch);
|
void EncodeAndAppendUTF8(std::string& s, char32_t ch);
|
||||||
@ -266,11 +266,11 @@ void EncodeAndAppendUTF8(std::string& s, char32_t ch);
|
|||||||
/// Decodes UTF-8 to a single codepoint, updating the position parameter.
|
/// Decodes UTF-8 to a single codepoint, updating the position parameter.
|
||||||
/// Returns the number of bytes the codepoint took in the original string.
|
/// Returns the number of bytes the codepoint took in the original string.
|
||||||
size_t DecodeUTF8(const void* bytes, size_t length, char32_t* ch);
|
size_t DecodeUTF8(const void* bytes, size_t length, char32_t* ch);
|
||||||
size_t DecodeUTF8(const std::string_view& str, size_t offset, char32_t* ch);
|
size_t DecodeUTF8(const std::string_view str, size_t offset, char32_t* ch);
|
||||||
size_t DecodeUTF8(const std::string& str, size_t offset, char32_t* ch);
|
size_t DecodeUTF8(const std::string& str, size_t offset, char32_t* ch);
|
||||||
|
|
||||||
// Replaces the end of a string with ellipsis if it exceeds the specified length.
|
// Replaces the end of a string with ellipsis if it exceeds the specified length.
|
||||||
std::string Ellipsise(const std::string_view& str, u32 max_length, const char* ellipsis = "...");
|
std::string Ellipsise(const std::string_view str, u32 max_length, const char* ellipsis = "...");
|
||||||
void EllipsiseInPlace(std::string& str, u32 max_length, const char* ellipsis = "...");
|
void EllipsiseInPlace(std::string& str, u32 max_length, const char* ellipsis = "...");
|
||||||
|
|
||||||
/// Strided memcpy/memcmp.
|
/// Strided memcpy/memcmp.
|
||||||
@ -316,12 +316,12 @@ ALWAYS_INLINE static int StrideMemCmp(const void* p1, std::size_t p1_stride, con
|
|||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
/// Converts the specified UTF-8 string to a wide string.
|
/// Converts the specified UTF-8 string to a wide string.
|
||||||
std::wstring UTF8StringToWideString(const std::string_view& str);
|
std::wstring UTF8StringToWideString(const std::string_view str);
|
||||||
bool UTF8StringToWideString(std::wstring& dest, const std::string_view& str);
|
bool UTF8StringToWideString(std::wstring& dest, const std::string_view str);
|
||||||
|
|
||||||
/// Converts the specified wide string to a UTF-8 string.
|
/// Converts the specified wide string to a UTF-8 string.
|
||||||
std::string WideStringToUTF8String(const std::wstring_view& str);
|
std::string WideStringToUTF8String(const std::wstring_view str);
|
||||||
bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str);
|
bool WideStringToUTF8String(std::string& dest, const std::wstring_view str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user