From f7a33d24e2fc7b48476699c7e14b0ba58ab3f606 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Mon, 26 May 2025 02:52:11 +0300 Subject: [PATCH] Uncursify forward/reverse console search (#8532) Correct search start/end calculation, give normal search the memo that the end iterator's meaning changed and fix broken invalid range guards. --- apps/openmw/mwgui/console.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/apps/openmw/mwgui/console.cpp b/apps/openmw/mwgui/console.cpp index a188f3c86b..a94736baee 100644 --- a/apps/openmw/mwgui/console.cpp +++ b/apps/openmw/mwgui/console.cpp @@ -465,7 +465,6 @@ namespace MWGui void Console::findOccurrence(const SearchDirection direction) { - if (mCurrentSearchTerm.empty()) { return; @@ -478,17 +477,16 @@ namespace MWGui size_t firstIndex{ 0 }; size_t lastIndex{ historyText.length() }; - // If search is not the first adjust the range based on the direction and previous occurrence. + // If this isn't the first search, adjust the range based on the previous occurrence. if (mCurrentOccurrenceIndex != std::string::npos) { - if (direction == SearchDirection::Forward && mCurrentOccurrenceIndex > 1) + if (direction == SearchDirection::Forward) { firstIndex = mCurrentOccurrenceIndex + mCurrentOccurrenceLength; } - else if (direction == SearchDirection::Reverse - && (historyText.length() - mCurrentOccurrenceIndex) > mCurrentOccurrenceLength) + else if (direction == SearchDirection::Reverse) { - lastIndex = mCurrentOccurrenceIndex - 1; + lastIndex = mCurrentOccurrenceIndex; } } @@ -523,6 +521,13 @@ namespace MWGui void Console::findInHistoryText(const std::string& historyText, const SearchDirection direction, const size_t firstIndex, const size_t lastIndex) { + if (lastIndex <= firstIndex) + { + mCurrentOccurrenceIndex = std::string::npos; + mCurrentOccurrenceLength = 0; + return; + } + if (mRegExSearch) { findWithRegex(historyText, direction, firstIndex, lastIndex); @@ -570,7 +575,7 @@ namespace MWGui const size_t firstIndex, const size_t lastIndex) { // Search in given text interval for search term - const size_t substringLength{ (lastIndex - firstIndex) + 1 }; + const size_t substringLength = lastIndex - firstIndex; const std::string_view historyTextView((historyText.c_str() + firstIndex), substringLength); if (direction == SearchDirection::Forward) {