From ab0eb43457207452f5ae8e032550256655b39b16 Mon Sep 17 00:00:00 2001 From: Bush2021 <79072750+Bush2021@users.noreply.github.com> Date: Sat, 2 Nov 2024 00:29:29 -0400 Subject: [PATCH] refactor: replace `ReplaceStringIni` with `ReplaceStringInPlace` for consistency --- src/config.h | 2 +- src/portable.h | 2 +- src/utils.h | 24 +++++++++++++----------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/config.h b/src/config.h index cfd4e1a..6abdd89 100644 --- a/src/config.h +++ b/src/config.h @@ -40,7 +40,7 @@ std::wstring GetDirPath(const std::wstring& dir_type) { } std::wstring expanded_path = ExpandEnvironmentPath(dir_buffer); - ReplaceStringIni(expanded_path, L"%app%", GetAppDir()); + ReplaceStringInPlace(expanded_path, L"%app%", GetAppDir()); std::wstring dir = GetAbsolutePath(expanded_path); return dir; } diff --git a/src/portable.h b/src/portable.h index ca2b8ed..b693af5 100644 --- a/src/portable.h +++ b/src/portable.h @@ -94,7 +94,7 @@ void LaunchCommands(const std::wstring& get_commands, } for (const auto& command : commands) { std::wstring expanded_path = ExpandEnvironmentPath(command); - ReplaceStringIni(expanded_path, L"%app%", GetAppDir()); + ReplaceStringInPlace(expanded_path, L"%app%", GetAppDir()); HANDLE handle = RunExecute(expanded_path.c_str(), show_command); if (program_handles != nullptr && handle != nullptr) { program_handles->push_back(handle); diff --git a/src/utils.h b/src/utils.h index f26190d..2ecd95e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -104,17 +104,6 @@ std::vector StringSplit(const std::wstring& str, return result; } -// Replace string in INI file. -void ReplaceStringIni(std::wstring& subject, - const std::wstring& search, - const std::wstring& replace) { - size_t pos = 0; - while ((pos = subject.find(search, pos)) != std::wstring::npos) { - subject.replace(pos, search.length(), replace); - pos += replace.length(); - } -} - // Compression html. std::string& ltrim(std::string& s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), @@ -168,6 +157,19 @@ bool ReplaceStringInPlace(std::string& subject, return find; } +bool ReplaceStringInPlace(std::wstring& subject, + const std::wstring& search, + const std::wstring& replace) { + bool find = false; + size_t pos = 0; + while ((pos = subject.find(search, pos)) != std::wstring::npos) { + subject.replace(pos, search.length(), replace); + pos += replace.length(); + find = true; + } + return find; +} + std::wstring QuoteSpaceIfNeeded(const std::wstring& str) { if (str.find(L' ') == std::wstring::npos) return std::move(str);