Skip to content

Commit

Permalink
Fix bug transitions from invalid checks (#11)
Browse files Browse the repository at this point in the history
* fix root case

* add comment

* simplify

* update
  • Loading branch information
kampersanda authored Sep 17, 2023
1 parent b1b0969 commit 4537eb7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ impl<T> Sparse<T> {
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];
Expand Down
19 changes: 19 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4537eb7

Please sign in to comment.