Skip to content

Commit

Permalink
Rewrite builder to use perfect hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ogham committed Feb 12, 2016
1 parent d5518e3 commit da27df4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions data-crate-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ path = "src/main.rs"

[dependencies]
getopts = "0.2"
phf_codegen = "0.7.12"
quick-error = "0.2"

[dependencies.zoneinfo_parse]
Expand Down
16 changes: 11 additions & 5 deletions data-crate-builder/src/data_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use zoneinfo_parse::table::{Table, TableBuilder};
use zoneinfo_parse::structure::{Structure, Child};
use zoneinfo_parse::transitions::{TableTransitions};

use phf_codegen::Map as PHFMap;

use errors::{Error, ParseError};


Expand Down Expand Up @@ -164,13 +166,16 @@ impl DataCrate {
}

try!(writeln!(base_w, "\n\n"));
try!(writeln!(base_w, "pub fn lookup(input: &str) -> Option<&'static StaticTimeZone<'static>> {{"));
try!(write!(base_w, "static ZONES: phf::Map<&'static str, &'static StaticTimeZone<'static>> = "));

let mut phf_map = PHFMap::new();
for name in &keys {
try!(writeln!(base_w, " if input == {:?} {{", name));
try!(writeln!(base_w, " return Some(&{});", sanitise_name(name).replace("/", "::")));
try!(writeln!(base_w, " }}"));
phf_map.entry(&***name, &format!("&{}", sanitise_name(name).replace("/", "::")));
}
try!(writeln!(base_w, " return None;"));
try!(phf_map.build(&mut base_w));

try!(writeln!(base_w, ";\n\npub fn lookup(input: &str) -> Option<&'static StaticTimeZone<'static>> {{"));
try!(writeln!(base_w, " ZONES.get(input).cloned()"));
try!(writeln!(base_w, "}}"));

Ok(())
Expand Down Expand Up @@ -244,4 +249,5 @@ use datetime::zone::{StaticTimeZone, FixedTimespanSet, FixedTimespan};
/// The imports needed for a `mod.rs` file.
const MOD_HEADER: &'static str = r##"
use datetime::zone::StaticTimeZone;
use phf;
"##;
3 changes: 2 additions & 1 deletion data-crate-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use std::env::args_os;
use std::io::{Write, stderr};
use std::process::exit;

extern crate getopts;
extern crate datetime;
extern crate getopts;
extern crate phf_codegen;
extern crate zoneinfo_parse;

#[macro_use]
Expand Down

0 comments on commit da27df4

Please sign in to comment.