Use ascii_case_insensitive_phf_map! in PropertyId::parse

This commit is contained in:
Simon Sapin 2017-02-25 19:00:29 +01:00
parent 33ef6f78e5
commit 2f996e34f5
7 changed files with 21 additions and 69 deletions

1
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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() {

View file

@ -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, {

View file

@ -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<str>) -> Result<Self, ()> {
if let Ok(name) = ::custom_properties::parse_name(&s) {
pub fn parse(property_name: Cow<str>) -> Result<Self, ()> {
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<StaticId> = {
% 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(()),

View file

@ -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<I, T>(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<Cow<'a, str>>>(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
}

View file

@ -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"));
}