Skip to content

Commit

Permalink
removed CodePage setting for replace strings
Browse files Browse the repository at this point in the history
  • Loading branch information
daddel80 committed Feb 1, 2025
1 parent 8dc91b7 commit b95a2ab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 53 deletions.
57 changes: 5 additions & 52 deletions src/MultiReplacePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<sptr_t>(replaceTextCp.c_str()));
// Set UTF-8-Text direcltly
send(SCI_REPLACETARGET, replaceTextUtf8.size(), reinterpret_cast<sptr_t>(replaceTextUtf8.c_str()));

// Get the end position after the replacement
Sci_Position newTargetEnd = static_cast<Sci_Position>(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;
}

Expand All @@ -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<int>(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<WPARAM>(-1), reinterpret_cast<sptr_t>(replaceTextCp.c_str()));
// Perform the regex replacement directly with UTF-8 text
send(SCI_REPLACETARGETRE, static_cast<WPARAM>(-1), reinterpret_cast<sptr_t>(replaceTextUtf8.c_str()));

// Get the end position after the replacement
Sci_Position newTargetEnd = static_cast<Sci_Position>(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;
}

Expand Down Expand Up @@ -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<wchar_t> 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<char> 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");
Expand Down
1 change: 0 additions & 1 deletion src/MultiReplacePanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b95a2ab

Please sign in to comment.