diff options
author | Simon Sapin <simon.sapin@exyr.org> | 2014-12-21 11:17:53 +0000 |
---|---|---|
committer | Simon Sapin <simon.sapin@exyr.org> | 2014-12-29 16:19:10 +0100 |
commit | 2e35d4e9879089759a0afe865cb6fb7796084938 (patch) | |
tree | 4486c7c01673916b0c48b21c5226afb1b67071f6 | |
parent | 540d21888558751a5fae0b71011306068a58bc6f (diff) | |
download | servo-2e35d4e9879089759a0afe865cb6fb7796084938.tar.gz servo-2e35d4e9879089759a0afe865cb6fb7796084938.zip |
Add a match_ignore_ascii_case! macro that does not allocate.
It should replace `match foo.to_ascii_lower().as_slice() { ...}`
@Manishearth I changed map.get to map.find in the lint to work around an ICE:
task 'rustc' panicked at 'couldn't find node id 0 in the AST map'
Does this look OK?
-rw-r--r-- | components/plugins/lib.rs | 25 | ||||
-rw-r--r-- | components/plugins/lints.rs | 6 | ||||
-rw-r--r-- | components/servo/Cargo.lock | 1 | ||||
-rw-r--r-- | components/util/Cargo.toml | 3 | ||||
-rw-r--r-- | components/util/cursor.rs | 4 | ||||
-rw-r--r-- | components/util/lib.rs | 7 |
6 files changed, 34 insertions, 12 deletions
diff --git a/components/plugins/lib.rs b/components/plugins/lib.rs index 662eeedc385..69f823c2d91 100644 --- a/components/plugins/lib.rs +++ b/components/plugins/lib.rs @@ -65,11 +65,10 @@ macro_rules! define_css_keyword_enum { impl $name { pub fn parse(component_value: &::cssparser::ast::ComponentValue) -> Result<$name, ()> { - use std::ascii::AsciiExt; match component_value { &::cssparser::ast::Ident(ref value) => { - match value.to_ascii_lower().as_slice() { - $( concat!($css) => Ok($name::$variant), )+ + match_ignore_ascii_case! { value: + $( $css => Ok($name::$variant) ),+ _ => Err(()) } } @@ -96,3 +95,23 @@ macro_rules! define_css_keyword_enum { } } } + + +#[macro_export] +macro_rules! match_ignore_ascii_case { + ( $value: expr: $( $string: expr => $result: expr ),+ _ => $fallback: expr, ) => { + match_ignore_ascii_case! { $value: + $( $string => $result ),+ + _ => $fallback + } + }; + ( $value: expr: $( $string: expr => $result: expr ),+ _ => $fallback: expr ) => { + { + use std::ascii::AsciiExt; + match $value.as_slice() { + $( s if s.eq_ignore_ascii_case($string) => $result, )+ + _ => $fallback + } + } + }; +} diff --git a/components/plugins/lints.rs b/components/plugins/lints.rs index a170cb6a4a1..3cb4cb91e1f 100644 --- a/components/plugins/lints.rs +++ b/components/plugins/lints.rs @@ -104,8 +104,8 @@ fn lint_unrooted_ty(cx: &Context, ty: &ast::Ty, warning: &str) { // Determines if a block is in an unsafe context so that an unhelpful // lint can be aborted. fn unsafe_context(map: &ast_map::Map, id: ast::NodeId) -> bool { - match map.get(map.get_parent(id)) { - ast_map::NodeImplItem(itm) => { + match map.find(map.get_parent(id)) { + Some(ast_map::NodeImplItem(itm)) => { match *itm { ast::MethodImplItem(ref meth) => match meth.node { ast::MethDecl(_, _, _, _, style, _, _, _) => match style { @@ -117,7 +117,7 @@ fn unsafe_context(map: &ast_map::Map, id: ast::NodeId) -> bool { _ => false, } }, - ast_map::NodeItem(itm) => { + Some(ast_map::NodeItem(itm)) => { match itm.node { ast::ItemFn(_, style, _, _, _) => match style { ast::UnsafeFn => true, diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 9075a588342..e3662c1a3c4 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -713,6 +713,7 @@ dependencies = [ "cssparser 0.1.0 (git+https://github.com/servo/rust-cssparser)", "geom 0.1.0 (git+https://github.com/servo/rust-geom)", "layers 0.1.0 (git+https://github.com/servo/rust-layers)", + "plugins 0.0.1", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "task_info 0.0.1", diff --git a/components/util/Cargo.toml b/components/util/Cargo.toml index 086a57dad39..87969f90af7 100644 --- a/components/util/Cargo.toml +++ b/components/util/Cargo.toml @@ -7,6 +7,9 @@ authors = ["The Servo Project Developers"] name = "util" path = "lib.rs" +[dependencies.plugins] +path = "../plugins" + [dependencies.cssparser] git = "https://github.com/servo/rust-cssparser" diff --git a/components/util/cursor.rs b/components/util/cursor.rs index 7a12dc05ec6..0549ff7592c 100644 --- a/components/util/cursor.rs +++ b/components/util/cursor.rs @@ -19,8 +19,8 @@ macro_rules! define_cursor { impl Cursor { pub fn from_css_keyword(keyword: &str) -> Result<Cursor, ()> { - match keyword.to_ascii_lower().as_slice() { - $( concat!($css) => Ok(Cursor::$variant), )+ + match_ignore_ascii_case! { keyword: + $( concat!($css) => Ok(Cursor::$variant) ),+ _ => Err(()) } } diff --git a/components/util/lib.rs b/components/util/lib.rs index 78160d8950a..6dbac1094d6 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -31,10 +31,9 @@ extern crate string_cache; extern crate unicode; extern crate url; -#[phase(plugin)] -extern crate string_cache_macros; -#[phase(plugin)] -extern crate lazy_static; +#[phase(plugin)] extern crate plugins; +#[phase(plugin)] extern crate string_cache_macros; +#[phase(plugin)] extern crate lazy_static; use std::sync::Arc; |