From 4537eb77fddff5cdc590a84a514bd377d21de822 Mon Sep 17 00:00:00 2001 From: Shunsuke Kanda Date: Sun, 17 Sep 2023 19:52:18 +0900 Subject: [PATCH] Fix bug transitions from invalid checks (#11) * fix root case * add comment * simplify * update --- src/trie.rs | 4 +++- tests/tests.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/trie.rs b/src/trie.rs index 2b8b9b0..b4c88f3 100644 --- a/src/trie.rs +++ b/src/trie.rs @@ -87,7 +87,9 @@ impl Sparse { let mut values = vec![wildcard_value]; let mut is_used = vec![true]; let mut stack = vec![(0, 0)]; - let mut used_bases = HashSet::new(); + // base=0 must be reserved for avoiding invalid transitions. + // See https://github.com/daac-tools/trie-match/pull/11. + let mut used_bases = HashSet::from([0]); let mut search_start = 0; while let Some((state_id, da_pos)) = stack.pop() { let state = &self.states[state_id]; diff --git a/tests/tests.rs b/tests/tests.rs index 3632f37..261ce8c 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -125,3 +125,22 @@ fn test_try_base_conflict() { assert_eq!(f("\u{2}\u{3}"), 1); assert_eq!(f("\u{3}"), 1); } + +// This test confirms that check[0] does not have an invalid value of zero. +#[test] +fn test_invalid_root_check_of_zero() { + // [0] -x01-> [1] + // \-x00-> [0] ? If check[0] is 0, such an invalid transition is possible. + // + // base: [0, MAX] + // check: [0, 1] + let f = |text| { + trie_match! { + match text { + "\u{1}" => 1, + _ => 0, + } + } + }; + assert_eq!(f("\u{0}\u{1}"), 0); +}