From 7dc94632d659e66bf75ccb24d73d2b704a8bbbd0 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Mon, 29 Sep 2025 13:13:14 +0100 Subject: [PATCH] fix backtracking bug in 'Nfa.getMatchesInRange' (we were passing the wrong value instead of 'strIdx' in the recursive call to the loop function) --- fcore/search-list.sml | 9 +++++++-- fcore/search-list/nfa.sml | 11 ++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/fcore/search-list.sml b/fcore/search-list.sml index be5a443..2a0f752 100644 --- a/fcore/search-list.sml +++ b/fcore/search-list.sml @@ -238,9 +238,14 @@ struct | [] => empty end - fun buildRange (buffer, searchString, finish) = + fun buildRange (buffer, searchString, finishIdx) = if String.size searchString > 0 then - searchRange (buffer, searchString, finish) + let + val nfa = Nfa.parse searchString + val startIdx = #idx buffer + in + Nfa.getMatchesInRange (startIdx, finishIdx, buffer : LineGap.t, nfa) + end else empty diff --git a/fcore/search-list/nfa.sml b/fcore/search-list/nfa.sml index b7beade..d054df3 100644 --- a/fcore/search-list/nfa.sml +++ b/fcore/search-list/nfa.sml @@ -172,7 +172,7 @@ struct val prevIdx = absIdx - String.size prevHd val tl = hd :: tl in - if prevIdx < startIdx then + if startIdx < prevIdx then (* keep backtracking *) backtrackRange ( prevHd @@ -243,7 +243,7 @@ struct case state of UNTESTED => loop - ( startIdx + 1 + ( strIdx + 1 , hd , tl , prevStrings @@ -259,7 +259,7 @@ struct val acc = PersistentVector.append (startIdx, acc) in loop - ( startIdx + 1 + ( strIdx + 1 , hd , tl , prevStrings @@ -275,7 +275,7 @@ struct let val prevIdx = absIdx - strIdx in - if prevIdx < startIdx then + if startIdx < prevIdx then backtrackRange ( hd , tl @@ -289,7 +289,7 @@ struct else let val strIdx = startIdx - prevIdx + 1 - val absIdx = absIdx + strIdx + val absIdx = prevIdx + strIdx in loop ( strIdx @@ -474,4 +474,5 @@ struct val parse = ParseNfa.parse val getMatches = NfaMatch.getMatches + val getMatchesInRange = NfaMatch.getMatchesInRange end