From 19eeaef2ea8c8ec717d1ed2910467eda5d76ccd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 13 Nov 2023 23:36:34 +0100 Subject: [PATCH] More uses of string_view --- Common/File/AndroidContentURI.cpp | 2 +- Common/File/FileUtil.cpp | 4 ++-- Common/File/PathBrowser.cpp | 2 +- Common/GPU/OpenGL/GLQueueRunner.cpp | 6 +++--- Common/Input/InputState.cpp | 9 +++++---- Common/Input/InputState.h | 2 +- Common/Net/HTTPHeaders.cpp | 6 +++--- Common/Net/URL.cpp | 30 +++++++++++------------------ Common/Net/URL.h | 4 ++-- Common/StringUtils.cpp | 28 ++++++++++++++++++++------- Common/StringUtils.h | 5 +++-- 11 files changed, 53 insertions(+), 45 deletions(-) diff --git a/Common/File/AndroidContentURI.cpp b/Common/File/AndroidContentURI.cpp index f0e9db513d..5c5a722218 100644 --- a/Common/File/AndroidContentURI.cpp +++ b/Common/File/AndroidContentURI.cpp @@ -8,7 +8,7 @@ bool AndroidContentURI::Parse(std::string_view path) { std::string_view components = path.substr(strlen(prefix)); - std::vector parts; + std::vector parts; SplitString(components, '/', parts); if (parts.size() == 3) { // Single file URI. diff --git a/Common/File/FileUtil.cpp b/Common/File/FileUtil.cpp index 9754450ce8..d41e49f68c 100644 --- a/Common/File/FileUtil.cpp +++ b/Common/File/FileUtil.cpp @@ -592,7 +592,7 @@ bool CreateFullPath(const Path &path) { return false; } - std::vector parts; + std::vector parts; SplitString(diff, '/', parts); // Probably not necessary sanity check, ported from the old code. @@ -602,7 +602,7 @@ bool CreateFullPath(const Path &path) { } Path curPath = root; - for (auto &part : parts) { + for (auto part : parts) { curPath /= part; if (!File::Exists(curPath)) { File::CreateDir(curPath); diff --git a/Common/File/PathBrowser.cpp b/Common/File/PathBrowser.cpp index 9af0f4f86e..6091b73ef3 100644 --- a/Common/File/PathBrowser.cpp +++ b/Common/File/PathBrowser.cpp @@ -78,7 +78,7 @@ bool LoadRemoteFileList(const Path &url, const std::string &userAgent, bool *can return false; } - for (std::string item : items) { + for (auto &item : items) { // Apply some workarounds. if (item.empty()) continue; diff --git a/Common/GPU/OpenGL/GLQueueRunner.cpp b/Common/GPU/OpenGL/GLQueueRunner.cpp index 0419b0d885..6514399bdd 100644 --- a/Common/GPU/OpenGL/GLQueueRunner.cpp +++ b/Common/GPU/OpenGL/GLQueueRunner.cpp @@ -334,10 +334,10 @@ void GLQueueRunner::RunInitSteps(const FastVec &steps, bool skipGLC step.create_shader.shader->desc.c_str(), infoLog.c_str(), LineNumberString(code).c_str()); - std::vector lines; + std::vector lines; SplitString(errorString, '\n', lines); - for (auto &line : lines) { - ERROR_LOG(G3D, "%s", line.c_str()); + for (auto line : lines) { + ERROR_LOG(G3D, "%.*s", line.size(), line.data()); } if (errorCallback_) { std::string desc = StringFromFormat("Shader compilation failed: %s", step.create_shader.stage == GL_VERTEX_SHADER ? "vertex" : "fragment"); diff --git a/Common/Input/InputState.cpp b/Common/Input/InputState.cpp index 794ab788bd..95282f7e2b 100644 --- a/Common/Input/InputState.cpp +++ b/Common/Input/InputState.cpp @@ -86,11 +86,12 @@ int GetAnalogYDirection(InputDeviceID deviceId) { } // NOTE: Changing the format of FromConfigString/ToConfigString breaks controls.ini backwards compatibility. -InputMapping InputMapping::FromConfigString(const std::string &str) { - std::vector parts; +InputMapping InputMapping::FromConfigString(const std::string_view str) { + std::vector parts; SplitString(str, '-', parts); - InputDeviceID deviceId = (InputDeviceID)(atoi(parts[0].c_str())); - InputKeyCode keyCode = (InputKeyCode)atoi(parts[1].c_str()); + // We only convert to std::string here to add null terminators for atoi. + InputDeviceID deviceId = (InputDeviceID)(atoi(std::string(parts[0]).c_str())); + InputKeyCode keyCode = (InputKeyCode)atoi(std::string(parts[1]).c_str()); InputMapping mapping; mapping.deviceId = deviceId; diff --git a/Common/Input/InputState.h b/Common/Input/InputState.h index 2297d6bbf0..0a0874de0b 100644 --- a/Common/Input/InputState.h +++ b/Common/Input/InputState.h @@ -79,7 +79,7 @@ public: _dbg_assert_(direction != 0); } - static InputMapping FromConfigString(const std::string &str); + static InputMapping FromConfigString(std::string_view str); std::string ToConfigString() const; InputDeviceID deviceId; diff --git a/Common/Net/HTTPHeaders.cpp b/Common/Net/HTTPHeaders.cpp index 18450030eb..19a68d8883 100644 --- a/Common/Net/HTTPHeaders.cpp +++ b/Common/Net/HTTPHeaders.cpp @@ -26,12 +26,12 @@ bool RequestHeader::GetParamValue(const char *param_name, std::string *value) co if (!params) return false; std::string p(params); - std::vector v; + std::vector v; SplitString(p, '&', v); for (size_t i = 0; i < v.size(); i++) { - std::vector parts; + std::vector parts; SplitString(v[i], '=', parts); - DEBUG_LOG(IO, "Param: %s Value: %s", parts[0].c_str(), parts[1].c_str()); + DEBUG_LOG(IO, "Param: %.*s Value: %.*s", parts[0].size(), parts[0].data(), parts[1].size(), parts[1].data()); if (parts[0] == param_name) { *value = parts[1]; return true; diff --git a/Common/Net/URL.cpp b/Common/Net/URL.cpp index cc9d5fcacc..0fdeab107c 100644 --- a/Common/Net/URL.cpp +++ b/Common/Net/URL.cpp @@ -115,13 +115,13 @@ const char HEX2DEC[256] = /* F */ N1,N1,N1,N1, N1,N1,N1,N1, N1,N1,N1,N1, N1,N1,N1,N1 }; -std::string UriDecode(const std::string & sSrc) +std::string UriDecode(std::string_view sSrc) { // Note from RFC1630: "Sequences which start with a percent sign // but are not followed by two hexadecimal characters (0-9, A-F) are reserved // for future extension" - const unsigned char * pSrc = (const unsigned char *)sSrc.c_str(); + const unsigned char * pSrc = (const unsigned char *)sSrc.data(); const size_t SRC_LEN = sSrc.length(); const unsigned char * const SRC_END = pSrc + SRC_LEN; const unsigned char * const SRC_LAST_DEC = SRC_END - 2; // last decodable '%' @@ -129,14 +129,10 @@ std::string UriDecode(const std::string & sSrc) char * const pStart = new char[SRC_LEN]; // Output will be shorter. char * pEnd = pStart; - while (pSrc < SRC_LAST_DEC) - { - if (*pSrc == '%') - { + while (pSrc < SRC_LAST_DEC) { + if (*pSrc == '%') { char dec1, dec2; - if (N1 != (dec1 = HEX2DEC[*(pSrc + 1)]) - && N1 != (dec2 = HEX2DEC[*(pSrc + 2)])) - { + if (N1 != (dec1 = HEX2DEC[*(pSrc + 1)]) && N1 != (dec2 = HEX2DEC[*(pSrc + 2)])) { *pEnd++ = (dec1 << 4) + dec2; pSrc += 3; continue; @@ -156,8 +152,7 @@ std::string UriDecode(const std::string & sSrc) } // Only alphanum and underscore is safe. -const char SAFE[256] = -{ +static const char SAFE[256] = { /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ /* 0 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* 1 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, @@ -180,21 +175,18 @@ const char SAFE[256] = /* F */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }; -std::string UriEncode(const std::string & sSrc) -{ +std::string UriEncode(std::string_view sSrc) { const char DEC2HEX[16 + 1] = "0123456789ABCDEF"; - const unsigned char * pSrc = (const unsigned char *)sSrc.c_str(); + const unsigned char * pSrc = (const unsigned char *)sSrc.data(); const size_t SRC_LEN = sSrc.length(); unsigned char * const pStart = new unsigned char[SRC_LEN * 3]; unsigned char * pEnd = pStart; const unsigned char * const SRC_END = pSrc + SRC_LEN; - for (; pSrc < SRC_END; ++pSrc) - { - if (SAFE[*pSrc]) + for (; pSrc < SRC_END; ++pSrc) { + if (SAFE[*pSrc]) { *pEnd++ = *pSrc; - else - { + } else { // escape this char *pEnd++ = '%'; *pEnd++ = DEC2HEX[*pSrc >> 4]; diff --git a/Common/Net/URL.h b/Common/Net/URL.h index e7f33e6d8a..b6288eaa6c 100644 --- a/Common/Net/URL.h +++ b/Common/Net/URL.h @@ -203,5 +203,5 @@ private: }; -std::string UriDecode(const std::string & sSrc); -std::string UriEncode(const std::string & sSrc); +std::string UriDecode(std::string_view sSrc); +std::string UriEncode(std::string_view sSrc); diff --git a/Common/StringUtils.cpp b/Common/StringUtils.cpp index bfa0de2c26..2cf21ebc23 100644 --- a/Common/StringUtils.cpp +++ b/Common/StringUtils.cpp @@ -284,6 +284,23 @@ std::string_view StripQuotes(std::string_view s) { return s; } +void SplitString(std::string_view str, const char delim, std::vector &output) { + size_t next = 0; + for (size_t pos = 0, len = str.length(); pos < len; ++pos) { + if (str[pos] == delim) { + output.emplace_back(str.substr(next, pos - next)); + // Skip the delimiter itself. + next = pos + 1; + } + } + + if (next == 0) { + output.push_back(str); + } else if (next < str.length()) { + output.emplace_back(str.substr(next)); + } +} + void SplitString(std::string_view str, const char delim, std::vector &output) { size_t next = 0; for (size_t pos = 0, len = str.length(); pos < len; ++pos) { @@ -295,7 +312,7 @@ void SplitString(std::string_view str, const char delim, std::vector& output) -{ +void GetQuotedStrings(const std::string& str, std::vector &output) { size_t next = 0; bool even = 0; for (size_t pos = 0, len = str.length(); pos < len; ++pos) { @@ -340,15 +356,13 @@ void GetQuotedStrings(const std::string& str, std::vector& output) } } -std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) -{ +std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) { size_t pos = 0; if (src == dest) return result; - while (1) - { + while (true) { pos = result.find(src, pos); if (pos == result.npos) break; diff --git a/Common/StringUtils.h b/Common/StringUtils.h index f1000208df..43079da696 100644 --- a/Common/StringUtils.h +++ b/Common/StringUtils.h @@ -43,7 +43,7 @@ inline bool startsWith(std::string_view str, std::string_view key) { return !memcmp(str.data(), key.data(), key.size()); } -inline bool endsWith(const std::string &str, const std::string &what) { +inline bool endsWith(std::string_view str, std::string_view what) { if (str.size() < what.size()) return false; return str.substr(str.size() - what.size()) == what; @@ -82,7 +82,8 @@ std::string_view StripSpaces(std::string_view s); std::string_view StripQuotes(std::string_view s); // TODO: Make this a lot more efficient by outputting string_views. -void SplitString(std::string_view str, const char delim, std::vector& output); +void SplitString(std::string_view str, const char delim, std::vector &output); +void SplitString(std::string_view str, const char delim, std::vector &output); void GetQuotedStrings(const std::string& str, std::vector& output);