From d761144daf92ce6aed83165aa840a1ae72bd0bb2 Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Tue, 8 Mar 2016 23:01:14 +0000 Subject: [PATCH] Add an impl of PhfHash for UniCase Fixes #21 --- phf/Cargo.toml | 1 + phf_codegen/test/Cargo.toml | 7 +++++++ phf_codegen/test/build.rs | 12 ++++++++++++ phf_codegen/test/src/lib.rs | 11 +++++++++++ phf_shared/Cargo.toml | 3 +++ phf_shared/src/lib.rs | 11 +++++++++++ 6 files changed, 45 insertions(+) diff --git a/phf/Cargo.toml b/phf/Cargo.toml index d75964a8..93a9b9a0 100644 --- a/phf/Cargo.toml +++ b/phf/Cargo.toml @@ -14,6 +14,7 @@ test = false [features] core = ["phf_shared/core"] +unicase = ["phf_shared/unicase"] [dependencies] phf_shared = { version = "=0.7.13", path = "../phf_shared" } diff --git a/phf_codegen/test/Cargo.toml b/phf_codegen/test/Cargo.toml index 1c32f68c..cc62ba2a 100644 --- a/phf_codegen/test/Cargo.toml +++ b/phf_codegen/test/Cargo.toml @@ -4,8 +4,15 @@ authors = ["Steven Fackler "] version = "0.0.0" build = "build.rs" +[dependencies] +unicase = "1.1" + +[build-dependencies] +unicase = "1.1" + [build-dependencies.phf_codegen] path = ".." [dependencies.phf] path = "../../phf" +features = ["unicase"] diff --git a/phf_codegen/test/build.rs b/phf_codegen/test/build.rs index 6fd95b3b..49b5a9ab 100644 --- a/phf_codegen/test/build.rs +++ b/phf_codegen/test/build.rs @@ -1,10 +1,13 @@ extern crate phf_codegen; +extern crate unicase; use std::env; use std::fs::File; use std::io::{BufWriter, Write}; use std::path::Path; +use unicase::UniCase; + fn main() { let file = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs"); let mut file = BufWriter::new(File::create(&file).unwrap()); @@ -53,4 +56,13 @@ fn main() { .build(&mut file) .unwrap(); write!(&mut file, ";\n").unwrap(); + + write!(&mut file, "static UNICASE_MAP: ::phf::Map<::unicase::UniCase<&'static str>, \ + &'static str> = ").unwrap(); + phf_codegen::Map::new() + .entry(UniCase("abc"), "\"a\"") + .entry(UniCase("DEF"), "\"b\"") + .build(&mut file) + .unwrap(); + write!(&mut file, ";\n").unwrap(); } diff --git a/phf_codegen/test/src/lib.rs b/phf_codegen/test/src/lib.rs index 3e50383d..a5aeeee2 100644 --- a/phf_codegen/test/src/lib.rs +++ b/phf_codegen/test/src/lib.rs @@ -1,7 +1,10 @@ extern crate phf; +extern crate unicase; #[cfg(test)] mod test { + use unicase::UniCase; + include!(concat!(env!("OUT_DIR"), "/codegen.rs")); #[test] @@ -44,4 +47,12 @@ mod test { assert_eq!(2, STR_KEYS["b"]); assert_eq!(3, STR_KEYS["c"]); } + + #[test] + fn unicase_map() { + assert_eq!("a", UNICASE_MAP[&UniCase("AbC")]); + assert_eq!("a", UNICASE_MAP[&UniCase("abc")]); + assert_eq!("b", UNICASE_MAP[&UniCase("DEf")]); + assert!(!UNICASE_MAP.contains_key(&UniCase("XyZ"))); + } } diff --git a/phf_shared/Cargo.toml b/phf_shared/Cargo.toml index 2dad2e5b..5e08f92a 100644 --- a/phf_shared/Cargo.toml +++ b/phf_shared/Cargo.toml @@ -14,3 +14,6 @@ test = false [features] core = [] + +[dependencies] +unicase = { version = "1.1", optional = true } diff --git a/phf_shared/src/lib.rs b/phf_shared/src/lib.rs index 00bcb109..404a0b70 100644 --- a/phf_shared/src/lib.rs +++ b/phf_shared/src/lib.rs @@ -3,6 +3,9 @@ #[cfg(not(feature = "core"))] extern crate std as core; +#[cfg(feature = "unicase")] +extern crate unicase; + use core::hash::{Hasher, Hash, SipHasher}; #[inline] @@ -103,6 +106,14 @@ impl PhfHash for [u8] { } } +#[cfg(feature = "unicase")] +impl PhfHash for unicase::UniCase +where unicase::UniCase: Hash { + #[inline] + fn phf_hash(&self, state: &mut H) { + self.hash(state) + } +} macro_rules! sip_impl( (le $t:ty) => (