fix backtracking bug in 'Nfa.getMatchesInRange' (we were passing the wrong value instead of 'strIdx' in the recursive call to the loop function)

This commit is contained in:
2025-09-29 13:13:14 +01:00
parent b6720ed5f1
commit 7dc94632d6
2 changed files with 13 additions and 7 deletions

View File

@@ -238,9 +238,14 @@ struct
| [] => empty | [] => empty
end end
fun buildRange (buffer, searchString, finish) = fun buildRange (buffer, searchString, finishIdx) =
if String.size searchString > 0 then 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 else
empty empty

View File

@@ -172,7 +172,7 @@ struct
val prevIdx = absIdx - String.size prevHd val prevIdx = absIdx - String.size prevHd
val tl = hd :: tl val tl = hd :: tl
in in
if prevIdx < startIdx then if startIdx < prevIdx then
(* keep backtracking *) (* keep backtracking *)
backtrackRange backtrackRange
( prevHd ( prevHd
@@ -243,7 +243,7 @@ struct
case state of case state of
UNTESTED => UNTESTED =>
loop loop
( startIdx + 1 ( strIdx + 1
, hd , hd
, tl , tl
, prevStrings , prevStrings
@@ -259,7 +259,7 @@ struct
val acc = PersistentVector.append (startIdx, acc) val acc = PersistentVector.append (startIdx, acc)
in in
loop loop
( startIdx + 1 ( strIdx + 1
, hd , hd
, tl , tl
, prevStrings , prevStrings
@@ -275,7 +275,7 @@ struct
let let
val prevIdx = absIdx - strIdx val prevIdx = absIdx - strIdx
in in
if prevIdx < startIdx then if startIdx < prevIdx then
backtrackRange backtrackRange
( hd ( hd
, tl , tl
@@ -289,7 +289,7 @@ struct
else else
let let
val strIdx = startIdx - prevIdx + 1 val strIdx = startIdx - prevIdx + 1
val absIdx = absIdx + strIdx val absIdx = prevIdx + strIdx
in in
loop loop
( strIdx ( strIdx
@@ -474,4 +474,5 @@ struct
val parse = ParseNfa.parse val parse = ParseNfa.parse
val getMatches = NfaMatch.getMatches val getMatches = NfaMatch.getMatches
val getMatchesInRange = NfaMatch.getMatchesInRange
end end