Skip to content

Commit

Permalink
Add UniCase support to phf_macros and bump unicase version
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobo1239 committed Aug 20, 2016
1 parent 839f06d commit 2af3abb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
5 changes: 5 additions & 0 deletions phf_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test = false

[features]
stats = ["time"]
unicase_support = ["unicase", "phf_shared/unicase"]

[dependencies.phf_generator]
path = "../phf_generator"
Expand All @@ -28,6 +29,10 @@ version = "=0.7.16"
version = "0.1"
optional = true

[dependencies.unicase]
version = "1.4"
optional = true

[dev-dependencies.phf]
path = "../phf"
version = "=0.7.16"
28 changes: 27 additions & 1 deletion phf_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extern crate time;
extern crate rustc_plugin;
extern crate phf_shared;
extern crate phf_generator;
#[cfg(feature = "unicase_support")]
extern crate unicase;

use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
Expand All @@ -55,6 +57,8 @@ use syntax::ptr::P;
use rustc_plugin::Registry;
use phf_generator::HashState;
use std::env;
#[cfg(feature = "unicase_support")]
use unicase::UniCase;

use util::{Entry, Key};
use util::{create_map, create_set, create_ordered_map, create_ordered_set};
Expand Down Expand Up @@ -282,8 +286,30 @@ fn parse_key(cx: &mut ExtCtxt, e: &Expr) -> Option<Key> {
None
}
}
#[cfg(feature = "unicase_support")]
ExprKind::Call(ref f, ref args) => {
if let ExprKind::Path(_, ref path) = f.node {
if path.segments.last().unwrap().identifier.name.as_str() == "UniCase" {
if args.len() == 1 {
if let ExprKind::Lit(ref lit) = args.first().unwrap().node {
if let ast::LitKind::Str(ref s, _) = lit.node {
return Some(Key::UniCase(UniCase(s.to_string())));
} else {
cx.span_err(e.span, "only a str literal is allowed in UniCase");
return None;
}
}
} else {
cx.span_err(e.span, "only one str literal is allowed in UniCase");
return None;
}
}
}
cx.span_err(e.span, "only UniCase is allowed besides literals");
None
},
_ => {
cx.span_err(e.span, "expected a literal");
cx.span_err(e.span, "expected a literal (or a UniCase if the unicase_support feature is enabled)");
None
}
}
Expand Down
6 changes: 6 additions & 0 deletions phf_macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub enum Key {
U64(u64),
I64(i64),
Bool(bool),
#[cfg(feature = "unicase_support")]
UniCase(::unicase::UniCase<String>),
}

impl Hash for Key {
Expand All @@ -42,6 +44,8 @@ impl Hash for Key {
Key::U64(b) => b.hash(state),
Key::I64(b) => b.hash(state),
Key::Bool(b) => b.hash(state),
#[cfg(feature = "unicase_support")]
Key::UniCase(ref u) => u.hash(state),
}
}
}
Expand All @@ -61,6 +65,8 @@ impl PhfHash for Key {
Key::U64(b) => b.phf_hash(state),
Key::I64(b) => b.phf_hash(state),
Key::Bool(b) => b.phf_hash(state),
#[cfg(feature = "unicase_support")]
Key::UniCase(ref u) => u.phf_hash(state),
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions phf_macros/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#![plugin(phf_macros)]

extern crate phf;
#[cfg(feature = "unicase_support")]
extern crate unicase;

mod map {
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -231,6 +233,32 @@ mod map {
assert_eq!(&10, v)
}
}

#[cfg(feature = "unicase_support")]
#[test]
fn test_unicase() {
use unicase::UniCase;
static MAP: phf::Map<UniCase<&'static str>, isize> = phf_map!(
UniCase("FOO") => 10,
UniCase("Bar") => 11,
);
assert!(Some(&10) == MAP.get(&UniCase("FOo")));
assert!(Some(&11) == MAP.get(&UniCase("bar")));
assert_eq!(None, MAP.get(&UniCase("asdf")));
}

#[cfg(feature = "unicase_support")]
#[test]
fn test_unicase_alt() {
use unicase;
static MAP: phf::Map<unicase::UniCase<&'static str>, isize> = phf_map!(
unicase::UniCase("FOO") => 10,
unicase::UniCase("Bar") => 11,
);
assert!(Some(&10) == MAP.get(&unicase::UniCase("FOo")));
assert!(Some(&11) == MAP.get(&unicase::UniCase("bar")));
assert_eq!(None, MAP.get(&unicase::UniCase("asdf")));
}
}

mod set {
Expand Down
2 changes: 1 addition & 1 deletion phf_shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ test = false
core = []

[dependencies]
unicase = { version = "1.1", optional = true }
unicase = { version = "1.4", optional = true }

0 comments on commit 2af3abb

Please sign in to comment.