From 22a8b807d29e00623aa40666a374b4c5cddaaa02 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 14 Oct 2025 02:24:45 +0100 Subject: [PATCH] handle edge case when building dfa from a string, where an exception was raised when our search regex contains an alternation where the second alternation is a substring of the first alternation, and add a test for it to make sure that it passes --- fcore/search-list/dfa-gen.sml | 10 +++++++++- test/regression.sml | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/fcore/search-list/dfa-gen.sml b/fcore/search-list/dfa-gen.sml index 6b1b96a..800a145 100644 --- a/fcore/search-list/dfa-gen.sml +++ b/fcore/search-list/dfa-gen.sml @@ -729,13 +729,21 @@ struct in Vector.concat [dtran, el] end - else + else if dStateIdx < Vector.length dtran then let val el = Vector.sub (dtran, dStateIdx) val el = Set.insertOrReplace (char, toStateIdx, el) in Vector.update (dtran, dStateIdx, el) end + else + let + val appendLength = dStateIdx - Vector.length dtran + val appendVecs = Vector.tabulate (appendLength, fn _ => Set.LEAF) + val dtran = Vector.concat [dtran, appendVecs] + in + insert (dStateIdx, char, toStateIdx, dtran) + end end fun convertChar diff --git a/test/regression.sml b/test/regression.sml index 4be552e..d3c4c79 100644 --- a/test/regression.sml +++ b/test/regression.sml @@ -67,6 +67,15 @@ struct in Expect.isTrue true end) + , test + "DfaGen does not cause exception \ + \when parsing alternation that contains a char \ + \from the previous alternation (1)" + (fn _ => + (let val dfa = CaseSensitiveDfa.fromString "str|s" + in Expect.isTrue true + end) + handle _ => Expect.isTrue false) ] val tests = [charEventTests]