From 3c2e5812cd123e3a303f548286742cb036c9f4cd Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 8 Oct 2025 06:35:49 +0100 Subject: [PATCH] reimplement function to search through text from scratch --- fcore/app-type.sml | 2 +- fcore/search-list/search-list.sml | 132 +++++++++--------------------- lib/brolib-sml | 2 +- 3 files changed, 42 insertions(+), 94 deletions(-) diff --git a/fcore/app-type.sml b/fcore/app-type.sml index 869b1da..b9be7a6 100644 --- a/fcore/app-type.sml +++ b/fcore/app-type.sml @@ -33,7 +33,7 @@ struct { mode = NORMAL_MODE "" , buffer = buffer , bufferModifyTime = time - , searchList = SearchList.empty + , searchList = PersistentVector.empty , windowWidth = windowWidth , windowHeight = windowHeight , startLine = 0 diff --git a/fcore/search-list/search-list.sml b/fcore/search-list/search-list.sml index f339c79..7cfc002 100644 --- a/fcore/search-list/search-list.sml +++ b/fcore/search-list/search-list.sml @@ -1,99 +1,47 @@ structure SearchList = struct - val empty = PersistentVector.empty - - fun backtrackFull (pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl) = - if pos < 0 then - case prevTl of - prevHd :: prevTl => - let - val tl = hd :: tl - in - backtrackFull - ( String.size prevHd - 1 - , prevHd - , absIdx - , tl - , acc - , searchPos - , searchString - , prevTl - ) - end - | [] => - (* Should never be called *) - raise Fail "SearchList.backtrackFull error: line 24\n" - else if searchPos <= 1 then - (* we are trying to backtrack to index 1, - * and then continue are search from here *) - loopSearch (pos, hd, absIdx, tl, acc, 0, searchString, prevTl) - else - backtrackFull - (pos - 1, hd, absIdx - 1, tl, acc, searchPos - 1, searchString, prevTl) - - and loopSearch (pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl) = - if pos = String.size hd then - case tl of - newHd :: newTl => - loopSearch - ( 0 - , newHd - , absIdx - , newTl - , acc - , searchPos - , searchString - , hd :: prevTl - ) - | [] => acc - else - let - val bufferChr = String.sub (hd, pos) - val searchChr = String.sub (searchString, searchPos) - in - if bufferChr = searchChr then - if searchPos + 1 = String.size searchString then - (* we fully matched the search string *) - let - val foundIdx = absIdx - String.size searchString + 1 - val acc = PersistentVector.append (foundIdx, absIdx, acc) - in - loopSearch - (pos + 1, hd, absIdx + 1, tl, acc, 0, searchString, prevTl) - end - else - loopSearch - ( pos + 1 - , hd - , absIdx + 1 - , tl - , acc - , searchPos + 1 - , searchString - , prevTl - ) - else - (if searchPos = 0 then - loopSearch - (pos + 1, hd, absIdx + 1, tl, acc, 0, searchString, prevTl) - else - backtrackFull - (pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl)) - end - - fun search ({rightStrings, leftStrings, ...}: LineGap.t, searchString) = - case rightStrings of - hd :: tl => - loopSearch (0, hd, 0, tl, PersistentVector.empty, 0, searchString, []) - | [] => PersistentVector.empty - - (* Prerequisite: move buffer/LineGap to start *) - fun build (buffer, searchString) = - if String.size searchString > 0 then search (buffer, searchString) - else PersistentVector.empty - structure DfaGen = CaseInsensitiveDfa + fun buildLoop (idx, iterator, dfa, acc, curState, startPos, prevFinalPos) = + let + val iterator = LineGap.moveIteratorToIdx (idx, iterator) + in + if LineGap.isIteratorAtEnd iterator then + if prevFinalPos < 0 then acc + else PersistentVector.append (startPos, prevFinalPos, acc) + else + let + val chr = LineGap.subIterator (idx, iterator) + val newState = DfaGen.nextState (dfa, curState, chr) + val prevFinalPos = + if DfaGen.isFinal (dfa, newState) then idx else prevFinalPos + in + if DfaGen.isDead newState then + if prevFinalPos = ~1 then + (* no match found: restart search from `startPos + 1` *) + buildLoop (startPos + 1, iterator, dfa, acc, 0, startPos + 1, ~1) + else + (* match found: append and continue *) + let + val acc = PersistentVector.append (startPos, prevFinalPos, acc) + + (* we start 1 idx after the final position we found *) + val newStart = prevFinalPos + 1 + in + buildLoop (newStart, iterator, dfa, acc, 0, newStart, ~1) + end + else + buildLoop + (idx + 1, iterator, dfa, acc, newState, startPos, prevFinalPos) + end + end + + fun build (iterator, dfa) = + if Vector.length dfa > 0 then + buildLoop (0, iterator, dfa, PersistentVector.empty, 0, 0, ~1) + else + PersistentVector.empty + fun rangeLoop ( dfa , bufferPos diff --git a/lib/brolib-sml b/lib/brolib-sml index 56a469e..1d62136 160000 --- a/lib/brolib-sml +++ b/lib/brolib-sml @@ -1 +1 @@ -Subproject commit 56a469e5782f81276e005e666bdd831ed5aba25a +Subproject commit 1d6213636e6b3cecad74fa7af19a9e0e81788aab