@@ -465,7 +465,6 @@ namespace MWGui
465465
466466 void Console::findOccurrence (const SearchDirection direction)
467467 {
468-
469468 if (mCurrentSearchTerm .empty ())
470469 {
471470 return ;
@@ -478,17 +477,16 @@ namespace MWGui
478477 size_t firstIndex{ 0 };
479478 size_t lastIndex{ historyText.length () };
480479
481- // If search is not the first adjust the range based on the direction and previous occurrence.
480+ // If this isn't the first search, adjust the range based on the previous occurrence.
482481 if (mCurrentOccurrenceIndex != std::string::npos)
483482 {
484- if (direction == SearchDirection::Forward && mCurrentOccurrenceIndex > 1 )
483+ if (direction == SearchDirection::Forward)
485484 {
486485 firstIndex = mCurrentOccurrenceIndex + mCurrentOccurrenceLength ;
487486 }
488- else if (direction == SearchDirection::Reverse
489- && (historyText.length () - mCurrentOccurrenceIndex ) > mCurrentOccurrenceLength )
487+ else if (direction == SearchDirection::Reverse)
490488 {
491- lastIndex = mCurrentOccurrenceIndex - 1 ;
489+ lastIndex = mCurrentOccurrenceIndex ;
492490 }
493491 }
494492
@@ -523,6 +521,13 @@ namespace MWGui
523521 void Console::findInHistoryText (const std::string& historyText, const SearchDirection direction,
524522 const size_t firstIndex, const size_t lastIndex)
525523 {
524+ if (lastIndex <= firstIndex)
525+ {
526+ mCurrentOccurrenceIndex = std::string::npos;
527+ mCurrentOccurrenceLength = 0 ;
528+ return ;
529+ }
530+
526531 if (mRegExSearch )
527532 {
528533 findWithRegex (historyText, direction, firstIndex, lastIndex);
@@ -570,7 +575,7 @@ namespace MWGui
570575 const size_t firstIndex, const size_t lastIndex)
571576 {
572577 // Search in given text interval for search term
573- const size_t substringLength{ ( lastIndex - firstIndex) + 1 } ;
578+ const size_t substringLength = lastIndex - firstIndex;
574579 const std::string_view historyTextView ((historyText.c_str () + firstIndex), substringLength);
575580 if (direction == SearchDirection::Forward)
576581 {
0 commit comments