From 83f902bcd13e9f67f6abb70722156381b74e5b2b Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 7 Feb 2025 18:26:27 +0100 Subject: [PATCH] Update string replace function in FSWrapper::;GetNewPath --- src/FSWrapper.cpp | 7 +++---- src/FSWrapper.h | 2 +- src/utils/StringTools.cpp | 16 ++++++++++++++++ src/utils/StringTools.h | 3 ++- 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/utils/StringTools.cpp diff --git a/src/FSWrapper.cpp b/src/FSWrapper.cpp index e9ba5c9..6bd913e 100644 --- a/src/FSWrapper.cpp +++ b/src/FSWrapper.cpp @@ -676,14 +676,13 @@ bool FSWrapper::IsFileModeAllowed(const char *mode) { return false; } - bool FSWrapper::IsPathToReplace(const std::string_view &path) { return starts_with_case_insensitive(path, pPathToReplace); } -std::string FSWrapper::GetNewPath(const std::string_view &path) { - auto subStr = path.substr(this->pPathToReplace.length()); - auto res = string_format("%s%.*s", this->pReplacePathWith.c_str(), int(subStr.length()), subStr.data()); +std::string FSWrapper::GetNewPath(const std::string_view &path) const { + auto res = std::string(path); + SafeReplaceInString(res, this->pPathToReplace, this->pReplacePathWith); std::ranges::replace(res, '\\', '/'); diff --git a/src/FSWrapper.h b/src/FSWrapper.h index b3a2005..518d64f 100644 --- a/src/FSWrapper.h +++ b/src/FSWrapper.h @@ -106,7 +106,7 @@ protected: virtual bool IsPathToReplace(const std::string_view &path); - std::string GetNewPath(const std::string_view &path); + [[nodiscard]] virtual std::string GetNewPath(const std::string_view &path) const; std::shared_ptr getDirFromHandle(FSDirectoryHandle handle); std::shared_ptr getFileFromHandle(FSFileHandle handle); diff --git a/src/utils/StringTools.cpp b/src/utils/StringTools.cpp new file mode 100644 index 0000000..fd551a2 --- /dev/null +++ b/src/utils/StringTools.cpp @@ -0,0 +1,16 @@ +#include "StringTools.h" +#include + +void SafeReplaceInString(std::string &subject, std::string search, const std::string &replace) { + if (search.empty() || search == replace) { + return; // Avoid infinite loops and invalid input + } + std::string lowerSubject = subject; + + std::ranges::transform(subject, lowerSubject.begin(), ::tolower); + std::ranges::transform(search, search.begin(), ::tolower); + + if (const size_t pos = lowerSubject.find(search); pos != std::string::npos) { + subject.replace(pos, search.length(), replace); + } +} \ No newline at end of file diff --git a/src/utils/StringTools.h b/src/utils/StringTools.h index 996072a..08d3b47 100644 --- a/src/utils/StringTools.h +++ b/src/utils/StringTools.h @@ -2,7 +2,6 @@ #include #include -#include template std::string string_format(const std::string &format, Args... args) { @@ -13,6 +12,8 @@ std::string string_format(const std::string &format, Args... args) { return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside } +void SafeReplaceInString(std::string &subject, std::string search, const std::string &replace); + static inline bool starts_with_case_insensitive(const std::string_view str, const std::string_view prefix) { if (str.size() < prefix.size()) return false;