From 4f84a988649f1c02b07cd9bbddd3a3c34940a19a Mon Sep 17 00:00:00 2001 From: Stenzek Date: Wed, 13 Dec 2023 21:20:31 +1000 Subject: [PATCH] StringUtil: Pass string_views by value --- src/common/string_util.cpp | 30 ++++++++++++------------- src/common/string_util.h | 46 +++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 140d94606..32da64b69 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -157,7 +157,7 @@ std::size_t StringUtil::Strlcpy(char* dst, const char* src, std::size_t size) 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(); if (len < size) @@ -173,7 +173,7 @@ std::size_t StringUtil::Strlcpy(char* dst, const std::string_view& src, std::siz return len; } -std::optional> StringUtil::DecodeHex(const std::string_view& in) +std::optional> StringUtil::DecodeHex(const std::string_view in) { std::vector data; data.reserve(in.size() / 2); @@ -199,7 +199,7 @@ std::string StringUtil::EncodeHex(const u8* data, int length) 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; while (start < str.size() && std::isspace(str[start])) @@ -235,7 +235,7 @@ void StringUtil::StripWhitespace(std::string* str) } } -std::vector StringUtil::SplitString(const std::string_view& str, char delimiter, +std::vector StringUtil::SplitString(const std::string_view str, char delimiter, bool skip_empty /*= true*/) { std::vector res; @@ -260,7 +260,7 @@ std::vector StringUtil::SplitString(const std::string_view& st return res; } -std::vector StringUtil::SplitNewString(const std::string_view& str, char delimiter, +std::vector StringUtil::SplitNewString(const std::string_view str, char delimiter, bool skip_empty /*= true*/) { std::vector res; @@ -285,15 +285,15 @@ std::vector StringUtil::SplitNewString(const std::string_view& str, return res; } -std::string StringUtil::ReplaceAll(const std::string_view& subject, const std::string_view& search, - const std::string_view& replacement) +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) +void StringUtil::ReplaceAll(std::string* subject, const std::string_view search, const std::string_view replacement) { 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('='); if (pos == std::string_view::npos) @@ -397,7 +397,7 @@ invalid: 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; 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); } @@ -450,7 +450,7 @@ size_t StringUtil::DecodeUTF8(const std::string& str, size_t offset, char32_t* c #ifdef _WIN32 -std::wstring StringUtil::UTF8StringToWideString(const std::string_view& str) +std::wstring StringUtil::UTF8StringToWideString(const std::string_view str) { std::wstring ret; if (!UTF8StringToWideString(ret, str)) @@ -459,7 +459,7 @@ std::wstring StringUtil::UTF8StringToWideString(const std::string_view& str) 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(str.length()), nullptr, 0); if (wlen < 0) @@ -472,7 +472,7 @@ bool StringUtil::UTF8StringToWideString(std::wstring& dest, const std::string_vi return true; } -std::string StringUtil::WideStringToUTF8String(const std::wstring_view& str) +std::string StringUtil::WideStringToUTF8String(const std::wstring_view str) { std::string ret; if (!WideStringToUTF8String(ret, str)) @@ -481,7 +481,7 @@ std::string StringUtil::WideStringToUTF8String(const std::wstring_view& str) 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(str.length()), nullptr, 0, nullptr, nullptr); if (mblen < 0) diff --git a/src/common/string_util.h b/src/common/string_util.h index 4eabfd307..9300c069c 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -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); /// 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 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 template::value, bool> = true> -inline std::optional FromChars(const std::string_view& str, int base = 10) +inline std::optional FromChars(const std::string_view str, int base = 10) { T value; @@ -82,7 +82,7 @@ inline std::optional FromChars(const std::string_view& str, int base = 10) return value; } template::value, bool> = true> -inline std::optional FromChars(const std::string_view& str, int base, std::string_view* endptr) +inline std::optional FromChars(const std::string_view str, int base, std::string_view* endptr) { T value; @@ -102,7 +102,7 @@ inline std::optional FromChars(const std::string_view& str, int base, std::st } template::value, bool> = true> -inline std::optional FromChars(const std::string_view& str) +inline std::optional FromChars(const std::string_view str) { T value; @@ -113,7 +113,7 @@ inline std::optional FromChars(const std::string_view& str) return value; } template::value, bool> = true> -inline std::optional FromChars(const std::string_view& str, std::string_view* endptr) +inline std::optional FromChars(const std::string_view str, std::string_view* endptr) { T value; @@ -177,7 +177,7 @@ inline std::string ToChars(T value) /// Explicit override for booleans template<> -inline std::optional FromChars(const std::string_view& str, int base) +inline std::optional 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 || @@ -203,15 +203,15 @@ inline std::string ToChars(bool value, int base) } /// Encode/decode hexadecimal byte buffers -std::optional> DecodeHex(const std::string_view& str); +std::optional> DecodeHex(const std::string_view str); std::string EncodeHex(const u8* data, int length); /// 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); } -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(); 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. -std::string_view StripWhitespace(const std::string_view& str); +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 SplitString(const std::string_view& str, char delimiter, bool skip_empty = true); -std::vector SplitNewString(const std::string_view& str, char delimiter, bool skip_empty = true); +std::vector SplitString(const std::string_view str, char delimiter, bool skip_empty = true); +std::vector SplitNewString(const std::string_view str, char delimiter, bool skip_empty = true); /// Joins a string together using the specified delimiter. template @@ -240,7 +240,7 @@ static inline std::string JoinString(const T& start, const T& end, char delimite return ret; } template -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; 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. -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); +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); +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 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. /// 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 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); // 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 = "..."); /// Strided memcpy/memcmp. @@ -316,12 +316,12 @@ ALWAYS_INLINE static int StrideMemCmp(const void* p1, std::size_t p1_stride, con #ifdef _WIN32 /// Converts the specified UTF-8 string to a wide string. -std::wstring UTF8StringToWideString(const std::string_view& str); -bool UTF8StringToWideString(std::wstring& dest, const std::string_view& str); +std::wstring UTF8StringToWideString(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. -std::string WideStringToUTF8String(const std::wstring_view& str); -bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str); +std::string WideStringToUTF8String(const std::wstring_view str); +bool WideStringToUTF8String(std::string& dest, const std::wstring_view str); #endif