From befa410e3731e668a2f12bde805953371e6a2635 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sat, 11 Jun 2022 21:00:57 +0200 Subject: [PATCH] Fix hash prefix collision For example `(UniCase::new("prefix"), UniCase::new("suffix"))` would always collide with `(Unicase::new("pre"), Unicase::new("fixsuffix"))`. See also https://github.com/rust-lang/rust/issues/5257. --- src/ascii.rs | 1 + src/unicode/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ascii.rs b/src/ascii.rs index 8940733..b5e9e44 100644 --- a/src/ascii.rs +++ b/src/ascii.rs @@ -127,6 +127,7 @@ impl> Hash for Ascii { for byte in self.as_ref().bytes().map(|b| b.to_ascii_lowercase()) { hasher.write_u8(byte); } + hasher.write_u8(0xff); } } diff --git a/src/unicode/mod.rs b/src/unicode/mod.rs index 8b88733..3a03503 100644 --- a/src/unicode/mod.rs +++ b/src/unicode/mod.rs @@ -59,8 +59,9 @@ impl> Hash for Unicode { let mut buf = [0; 4]; for c in self.0.as_ref().chars().flat_map(|c| lookup(c)) { let len = char_to_utf8(c, &mut buf); - hasher.write(&buf[..len]) + hasher.write(&buf[..len]); } + hasher.write_u8(0xff); } }