From 2f996e34f54cc2903d44945345bb0cd7f80922a3 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 25 Feb 2017 19:00:29 +0100 Subject: [PATCH] Use ascii_case_insensitive_phf_map! in PropertyId::parse --- Cargo.lock | 1 - components/style/Cargo.toml | 1 - components/style/build.rs | 20 ------------ components/style/properties/build.py | 14 --------- .../style/properties/properties.mako.rs | 31 ++++++++++++------- components/style/str.rs | 11 ------- tests/unit/style/str.rs | 12 +------ 7 files changed, 21 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79ff61eca36..126715323a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2750,7 +2750,6 @@ dependencies = [ "parking_lot 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "pdqsort 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", - "phf_codegen 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "selectors 0.18.0", diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 34f0c1e2da9..6dd79ceb77a 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -63,6 +63,5 @@ kernel32-sys = "0.2" [build-dependencies] lazy_static = "0.2" bindgen = { version = "0.22", optional = true } -phf_codegen = "0.7.20" regex = {version = "0.2", optional = true} walkdir = "1.0" diff --git a/components/style/build.rs b/components/style/build.rs index d59d2272dd7..de9a1dffa0f 100644 --- a/components/style/build.rs +++ b/components/style/build.rs @@ -10,11 +10,8 @@ extern crate bindgen; #[cfg(feature = "bindgen")] extern crate regex; extern crate walkdir; -extern crate phf_codegen; use std::env; -use std::fs::File; -use std::io::{BufWriter, BufReader, BufRead, Write}; use std::path::Path; use std::process::{Command, exit}; use walkdir::WalkDir; @@ -78,23 +75,6 @@ fn generate_properties() { if !status.success() { exit(1) } - - let path = Path::new(&env::var("OUT_DIR").unwrap()).join("static_ids.rs"); - let static_ids = Path::new(&env::var("OUT_DIR").unwrap()).join("static_ids.txt"); - let mut file = BufWriter::new(File::create(&path).unwrap()); - let static_ids = BufReader::new(File::open(&static_ids).unwrap()); - - write!(&mut file, "static STATIC_IDS: ::phf::Map<&'static str, StaticId> = ").unwrap(); - let mut map = phf_codegen::Map::new(); - for result in static_ids.lines() { - let line = result.unwrap(); - let mut split = line.split('\t'); - let key = split.next().unwrap().to_owned(); - let value = split.next().unwrap(); - map.entry(key, value); - } - map.build(&mut file).unwrap(); - write!(&mut file, ";\n").unwrap(); } fn main() { diff --git a/components/style/properties/build.py b/components/style/properties/build.py index 2c0d167c89f..ea20134639b 100644 --- a/components/style/properties/build.py +++ b/components/style/properties/build.py @@ -33,7 +33,6 @@ def main(): rust = render(template, product=product, data=properties, __file__=template) if output == "style-crate": write(os.environ["OUT_DIR"], "properties.rs", rust) - write(os.environ["OUT_DIR"], "static_ids.txt", static_ids(properties)) if product == "gecko": template = os.path.join(BASE, "gecko.mako.rs") rust = render(template, data=properties) @@ -72,19 +71,6 @@ def write(directory, filename, content): open(os.path.join(directory, filename), "wb").write(content) -def static_id_generator(properties): - for kind, props in [("Longhand", properties.longhands), - ("Shorthand", properties.shorthands)]: - for p in props: - yield "%s\tStaticId::%s(%sId::%s)" % (p.name, kind, kind, p.camel_case) - for alias in p.alias: - yield "%s\tStaticId::%s(%sId::%s)" % (alias, kind, kind, p.camel_case) - - -def static_ids(properties): - return '\n'.join(static_id_generator(properties)) - - def write_html(properties): properties = dict( (p.name, { diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 2b22cc6ba55..75dd08cad2e 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -731,24 +731,33 @@ impl ToCss for PropertyId { } } -// FIXME(https://github.com/rust-lang/rust/issues/33156): remove this enum and use PropertyId -// when stable Rust allows destructors in statics. -enum StaticId { - Longhand(LonghandId), - Shorthand(ShorthandId), -} -include!(concat!(env!("OUT_DIR"), "/static_ids.rs")); impl PropertyId { /// Returns a given property from the string `s`. /// /// Returns Err(()) for unknown non-custom properties - pub fn parse(s: Cow) -> Result { - if let Ok(name) = ::custom_properties::parse_name(&s) { + pub fn parse(property_name: Cow) -> Result { + if let Ok(name) = ::custom_properties::parse_name(&property_name) { return Ok(PropertyId::Custom(::custom_properties::Name::from(name))) } - let lower_case = ::str::cow_into_ascii_lowercase(s); - match STATIC_IDS.get(&*lower_case) { + // FIXME(https://github.com/rust-lang/rust/issues/33156): remove this enum and use PropertyId + // when stable Rust allows destructors in statics. + enum StaticId { + Longhand(LonghandId), + Shorthand(ShorthandId), + } + ascii_case_insensitive_phf_map! { + StaticIds: Map = { + % for (kind, properties) in [("Longhand", data.longhands), ("Shorthand", data.shorthands)]: + % for property in properties: + % for name in [property.name] + property.alias: + "${name}" => "StaticId::${kind}(${kind}Id::${property.camel_case})", + % endfor + % endfor + % endfor + } + } + match StaticIds::get(&property_name) { Some(&StaticId::Longhand(id)) => Ok(PropertyId::Longhand(id)), Some(&StaticId::Shorthand(id)) => Ok(PropertyId::Shorthand(id)), None => Err(()), diff --git a/components/style/str.rs b/components/style/str.rs index ee805b1d801..f5778828d1a 100644 --- a/components/style/str.rs +++ b/components/style/str.rs @@ -7,8 +7,6 @@ #![deny(missing_docs)] use num_traits::ToPrimitive; -use std::ascii::AsciiExt; -use std::borrow::Cow; use std::convert::AsRef; use std::iter::{Filter, Peekable}; use std::str::Split; @@ -146,12 +144,3 @@ pub fn str_join(strs: I, join: &str) -> String acc }) } - -/// Like AsciiExt::to_ascii_lowercase, but avoids allocating when the input is already lower-case. -pub fn cow_into_ascii_lowercase<'a, S: Into>>(s: S) -> Cow<'a, str> { - let mut cow = s.into(); - if let Some(first_uppercase) = cow.bytes().position(|byte| byte >= b'A' && byte <= b'Z') { - cow.to_mut()[first_uppercase..].make_ascii_lowercase(); - } - cow -} diff --git a/tests/unit/style/str.rs b/tests/unit/style/str.rs index 45d747d4da8..9625fea72a4 100644 --- a/tests/unit/style/str.rs +++ b/tests/unit/style/str.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::borrow::Cow; -use style::str::{split_html_space_chars, str_join, cow_into_ascii_lowercase}; +use style::str::{split_html_space_chars, str_join}; #[test] pub fn split_html_space_chars_whitespace() { @@ -34,13 +34,3 @@ pub fn test_str_join_many() { let expected = "-alpha--beta-gamma-"; assert_eq!(actual, expected); } - -#[test] -pub fn test_cow_into_ascii_lowercase() { - assert!(matches!(cow_into_ascii_lowercase("abc.d"), Cow::Borrowed("abc.d"))); - let string = String::from("abc.d"); - assert!(matches!(cow_into_ascii_lowercase(string), Cow::Owned(ref s) if s == "abc.d")); - assert!(matches!(cow_into_ascii_lowercase("Abc.d"), Cow::Owned(ref s) if s == "abc.d")); - assert!(matches!(cow_into_ascii_lowercase("aBC.D"), Cow::Owned(ref s) if s == "abc.d")); - assert!(matches!(cow_into_ascii_lowercase("abc.D"), Cow::Owned(ref s) if s == "abc.d")); -}