From 26842af6012fa5e026d313e22420895c3e39f346 Mon Sep 17 00:00:00 2001 From: Thomas Knoefel Date: Thu, 10 Aug 2023 12:53:32 +0200 Subject: [PATCH] added Codepage support for Regex Peplace --- src/MultiReplacePanel.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/MultiReplacePanel.cpp b/src/MultiReplacePanel.cpp index c78b806..88b95b5 100644 --- a/src/MultiReplacePanel.cpp +++ b/src/MultiReplacePanel.cpp @@ -1446,15 +1446,21 @@ Sci_Position MultiReplace::performRegexReplace(const std::string& replaceTextUtf // Set the target range for the replacement ::SendMessage(_hScintilla, SCI_SETTARGETRANGE, pos, pos + length); + // Get the codepage of the document + int cp = static_cast(::SendMessage(_hScintilla, 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 - ::SendMessage(_hScintilla, SCI_REPLACETARGETRE, static_cast(-1), reinterpret_cast(replaceTextUtf8.c_str())); + ::SendMessage(_hScintilla, SCI_REPLACETARGETRE, static_cast(-1), reinterpret_cast(replaceTextCp.c_str())); // We need to return the new position after the replacement. // Note: The length of the replaced string may differ from the length of the original match. - int newLength = static_cast(replaceTextUtf8.length()); - return pos + newLength; + return pos + static_cast(replaceTextCp.length()); } + std::string MultiReplace::utf8ToCodepage(const std::string& utf8Str, int codepage) { // Convert the UTF-8 string to a wide string int lenWc = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, nullptr, 0);