Skip to content

Commit

Permalink
Minor optimize EditReplaceAll().
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Nov 16, 2023
1 parent fb7742c commit 33dfce6
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -5842,6 +5842,10 @@ bool EditReplaceAll(HWND hwnd, LPCEDITFINDREPLACE lpefr, bool bShowInfo) {
// Show wait cursor...
BeginWaitCursor();
SendMessage(hwnd, WM_SETREDRAW, FALSE, 0);
#if 0
StopWatch watch;
StopWatch_Start(watch);
#endif

const bool bRegexStartOfLine = bReplaceRE && (szFind2[0] == '^');
struct Sci_TextToFindFull ttf = { { 0, SciCall_GetLength() }, szFind2, { 0, 0 } };
Expand All @@ -5855,7 +5859,8 @@ bool EditReplaceAll(HWND hwnd, LPCEDITFINDREPLACE lpefr, bool bShowInfo) {
const Sci_Position iReplacedLen = SciCall_ReplaceTargetEx(bReplaceRE, -1, pszReplace2);

ttf.chrg.cpMin = (ttf.chrgText.cpMin + iReplacedLen);
ttf.chrg.cpMax = SciCall_GetLength();
// document length change: iReplacedLen - (ttf.chrgText.cpMax - ttf.chrgText.cpMin)
ttf.chrg.cpMax += ttf.chrg.cpMin - ttf.chrgText.cpMax;

if (ttf.chrg.cpMin == ttf.chrg.cpMax) {
break;
Expand All @@ -5879,6 +5884,10 @@ bool EditReplaceAll(HWND hwnd, LPCEDITFINDREPLACE lpefr, bool bShowInfo) {
}
}

#if 0
StopWatch_Stop(watch);
StopWatch_ShowLog(&watch, "EditReplaceAll() time");
#endif
SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
if (iCount) {
EditEnsureSelectionVisible();
Expand Down Expand Up @@ -5922,9 +5931,8 @@ bool EditReplaceAllInSelection(HWND hwnd, LPCEDITFINDREPLACE lpefr, bool bShowIn
const bool bRegexStartOfLine = bReplaceRE && (szFind2[0] == '^');
struct Sci_TextToFindFull ttf = { { SciCall_GetSelectionStart(), SciCall_GetLength() }, szFind2, { 0, 0 } };
Sci_Position iCount = 0;
bool fCancel = false;
while (!fCancel && SciCall_FindTextFull(searchFlags, &ttf) >= 0) {
if (ttf.chrgText.cpMin >= SciCall_GetSelectionStart() && ttf.chrgText.cpMax <= SciCall_GetSelectionEnd()) {
while (SciCall_FindTextFull(searchFlags, &ttf) >= 0) {
if (ttf.chrgText.cpMax <= SciCall_GetSelectionEnd()) {
if (++iCount == 1) {
SciCall_BeginUndoAction();
}
Expand All @@ -5933,10 +5941,11 @@ bool EditReplaceAllInSelection(HWND hwnd, LPCEDITFINDREPLACE lpefr, bool bShowIn
const Sci_Position iReplacedLen = SciCall_ReplaceTargetEx(bReplaceRE, -1, pszReplace2);

ttf.chrg.cpMin = (ttf.chrgText.cpMin + iReplacedLen);
ttf.chrg.cpMax = SciCall_GetLength();
// document length change: iReplacedLen - (ttf.chrgText.cpMax - ttf.chrgText.cpMin)
ttf.chrg.cpMax += ttf.chrg.cpMin - ttf.chrgText.cpMax;

if (ttf.chrg.cpMin == ttf.chrg.cpMax) {
fCancel = true;
break;
}

if (ttf.chrgText.cpMin == ttf.chrgText.cpMax && !bRegexStartOfLine) {
Expand All @@ -5956,14 +5965,14 @@ bool EditReplaceAllInSelection(HWND hwnd, LPCEDITFINDREPLACE lpefr, bool bShowIn
}
}
} else { // gone across selection, cancel
fCancel = true;
break;
}
}

SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
if (iCount) {
const Sci_Position iPos = SciCall_GetTargetEnd();
if (SciCall_GetSelectionEnd() < iPos) {
if (SciCall_GetSelectionEnd() < iPos) {
Sci_Position iAnchorPos = SciCall_GetAnchor();
Sci_Position iCurrentPos = SciCall_GetCurrentPos();

Expand Down

0 comments on commit 33dfce6

Please sign in to comment.