Update string replace function in FSWrapper::;GetNewPath

This commit is contained in:
Maschell 2025-02-07 18:26:27 +01:00
parent f86a3e47c6
commit 83f902bcd1
4 changed files with 22 additions and 6 deletions

View File

@ -676,14 +676,13 @@ bool FSWrapper::IsFileModeAllowed(const char *mode) {
return false; return false;
} }
bool FSWrapper::IsPathToReplace(const std::string_view &path) { bool FSWrapper::IsPathToReplace(const std::string_view &path) {
return starts_with_case_insensitive(path, pPathToReplace); return starts_with_case_insensitive(path, pPathToReplace);
} }
std::string FSWrapper::GetNewPath(const std::string_view &path) { std::string FSWrapper::GetNewPath(const std::string_view &path) const {
auto subStr = path.substr(this->pPathToReplace.length()); auto res = std::string(path);
auto res = string_format("%s%.*s", this->pReplacePathWith.c_str(), int(subStr.length()), subStr.data()); SafeReplaceInString(res, this->pPathToReplace, this->pReplacePathWith);
std::ranges::replace(res, '\\', '/'); std::ranges::replace(res, '\\', '/');

View File

@ -106,7 +106,7 @@ protected:
virtual bool IsPathToReplace(const std::string_view &path); 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<DirInfo> getDirFromHandle(FSDirectoryHandle handle); std::shared_ptr<DirInfo> getDirFromHandle(FSDirectoryHandle handle);
std::shared_ptr<FileInfo> getFileFromHandle(FSFileHandle handle); std::shared_ptr<FileInfo> getFileFromHandle(FSFileHandle handle);

16
src/utils/StringTools.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "StringTools.h"
#include <algorithm>
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);
}
}

View File

@ -2,7 +2,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector>
template<typename... Args> template<typename... Args>
std::string string_format(const std::string &format, Args... args) { 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 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) { static inline bool starts_with_case_insensitive(const std::string_view str, const std::string_view prefix) {
if (str.size() < prefix.size()) if (str.size() < prefix.size())
return false; return false;