From b95a2abdeaa53a6041c040bc41508c288123dc4a Mon Sep 17 00:00:00 2001 From: Thomas Knoefel Date: Sat, 1 Feb 2025 12:20:04 +0100 Subject: [PATCH] removed CodePage setting for replace strings --- src/MultiReplacePanel.cpp | 57 ++++----------------------------------- src/MultiReplacePanel.h | 1 - 2 files changed, 5 insertions(+), 53 deletions(-) diff --git a/src/MultiReplacePanel.cpp b/src/MultiReplacePanel.cpp index 35af695..c53a31d 100644 --- a/src/MultiReplacePanel.cpp +++ b/src/MultiReplacePanel.cpp @@ -4256,30 +4256,18 @@ bool MultiReplace::replaceAll(const ReplaceItemData& itemData, int& findCount, i return true; } + Sci_Position MultiReplace::performReplace(const std::string& replaceTextUtf8, Sci_Position pos, Sci_Position length) { // Set the target range for the replacement send(SCI_SETTARGETRANGE, pos, pos + length); - // Get the codepage of the document - int cp = static_cast(send(SCI_GETCODEPAGE, 0, 0)); - - // Convert the string from UTF-8 to the codepage of the document - std::string replaceTextCp = utf8ToCodepage(replaceTextUtf8, cp); - - // Perform the replacement - send(SCI_REPLACETARGET, replaceTextCp.size(), reinterpret_cast(replaceTextCp.c_str())); + // Set UTF-8-Text direcltly + send(SCI_REPLACETARGET, replaceTextUtf8.size(), reinterpret_cast(replaceTextUtf8.c_str())); // Get the end position after the replacement Sci_Position newTargetEnd = static_cast(send(SCI_GETTARGETEND, 0, 0)); - // Set the cursor to the end of the replaced text - //send(SCI_SETCURRENTPOS, newTargetEnd, 0); - - // Clear selection - //send(SCI_SETSELECTIONSTART, newTargetEnd, 0); - //send(SCI_SETSELECTIONEND, newTargetEnd, 0); - return newTargetEnd; } @@ -4288,25 +4276,12 @@ Sci_Position MultiReplace::performRegexReplace(const std::string& replaceTextUtf // Set the target range for the replacement send(SCI_SETTARGETRANGE, pos, pos + length); - // Get the codepage of the document - int cp = static_cast(send(SCI_GETCODEPAGE, 0, 0)); - - // Convert the string from UTF-8 to the codepage of the document - std::string replaceTextCp = utf8ToCodepage(replaceTextUtf8, cp); - - // Perform the regex replacement - send(SCI_REPLACETARGETRE, static_cast(-1), reinterpret_cast(replaceTextCp.c_str())); + // Perform the regex replacement directly with UTF-8 text + send(SCI_REPLACETARGETRE, static_cast(-1), reinterpret_cast(replaceTextUtf8.c_str())); // Get the end position after the replacement Sci_Position newTargetEnd = static_cast(send(SCI_GETTARGETEND, 0, 0)); - // Set the cursor to the end of the replaced text - //send(SCI_SETCURRENTPOS, newTargetEnd, 0); - - // Clear selection - //send(SCI_SETSELECTIONSTART, newTargetEnd, 0); - //send(SCI_SETSELECTIONEND, newTargetEnd, 0); - return newTargetEnd; } @@ -8059,28 +8034,6 @@ std::wstring MultiReplace::utf8ToWString(const char* cstr) const { return std::wstring(&wideStringResult[0]); } -std::string MultiReplace::utf8ToCodepage(const std::string& utf8Str, int codepage) const { - // Convert the UTF-8 string to a wide string - int lenWc = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, nullptr, 0); - if (lenWc == 0) { - // Handle error - return std::string(); - } - std::vector wideStr(lenWc); - MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], lenWc); - - // Convert the wide string to the specific codepage - int lenMbcs = WideCharToMultiByte(codepage, 0, &wideStr[0], -1, nullptr, 0, nullptr, nullptr); - if (lenMbcs == 0) { - // Handle error - return std::string(); - } - std::vector cpStr(lenMbcs); - WideCharToMultiByte(codepage, 0, &wideStr[0], -1, &cpStr[0], lenMbcs, nullptr, nullptr); - - return std::string(cpStr.data(), lenMbcs - 1); // -1 to exclude the null character -} - std::wstring MultiReplace::trim(const std::wstring& str) { // Find the first character that is not whitespace, tab, newline, or carriage return const auto strBegin = str.find_first_not_of(L" \t\n\r"); diff --git a/src/MultiReplacePanel.h b/src/MultiReplacePanel.h index 232ff6c..743fac1 100644 --- a/src/MultiReplacePanel.h +++ b/src/MultiReplacePanel.h @@ -749,7 +749,6 @@ class MultiReplace : public StaticDialog std::wstring stringToWString(const std::string& encodedInput) const; std::string wstringToString(const std::wstring& input) const; std::wstring utf8ToWString(const char* cstr) const; - std::string utf8ToCodepage(const std::string& utf8Str, int codepage) const; std::wstring trim(const std::wstring& str); //FileOperations