Skip to content

Commit

Permalink
fix root case
Browse files Browse the repository at this point in the history
  • Loading branch information
kampersanda committed Sep 17, 2023
1 parent b1b0969 commit 6def4d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ impl<T> Sparse<T> {
}
}
}
// Postprocessing to avoid invalid transitions due to invalid checks.
'a: for ((da_pos, check), &used) in checks.iter_mut().enumerate().zip(is_used.iter()) {
if da_pos != 0 && used {
continue;
}
let da_pos = i32::try_from(da_pos).unwrap();
for k in 0..256 {
let base = da_pos - k;
if !used_bases.contains(&base) {
// There can be no transition using k from an unused BASE value.
*check = u8::try_from(k).unwrap();
continue 'a;
}
}
unreachable!("No unused base found");
}
(bases, checks, values)
}
}
14 changes: 14 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,17 @@ 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() {
let f = |text| {
trie_match! {
match text {
"\u{1}" => 1,
_ => 0,
}
}
};
assert_eq!(f("\u{0}\u{1}"), 0);
}

0 comments on commit 6def4d2

Please sign in to comment.