Use to_ascii_lower() and eq_ignore_ascii_case() instead of unsafe code.

This commit is contained in:
Simon Sapin 2014-04-17 15:26:16 +01:00
parent 28baa1f4d1
commit 47f24dee78

View file

@ -6,6 +6,8 @@
#[allow(non_camel_case_types, uppercase_variables)]; #[allow(non_camel_case_types, uppercase_variables)];
pub use std::ascii::StrAsciiExt;
pub use servo_util::url::parse_url; pub use servo_util::url::parse_url;
use sync::Arc; use sync::Arc;
pub use url::Url; pub use url::Url;
@ -226,15 +228,12 @@ pub mod longhands {
match component_value { match component_value {
&Ident(ref value) => { &Ident(ref value) => {
// FIXME: Workaround for https://github.com/mozilla/rust/issues/10683 // FIXME: Workaround for https://github.com/mozilla/rust/issues/10683
unsafe { let value_lower = value.to_ascii_lower();
let tmp_for_lifetime = value.to_ascii_nocheck().to_lower(); match value_lower.as_slice() {
let value_lower = tmp_for_lifetime.as_str_ascii(); "thin" => Some(specified::Length::from_px(1.)),
match value_lower.as_slice() { "medium" => Some(specified::Length::from_px(3.)),
"thin" => Some(specified::Length::from_px(1.)), "thick" => Some(specified::Length::from_px(5.)),
"medium" => Some(specified::Length::from_px(3.)), _ => None
"thick" => Some(specified::Length::from_px(5.)),
_ => None
}
} }
}, },
_ => specified::Length::parse_non_negative(component_value) _ => specified::Length::parse_non_negative(component_value)
@ -373,7 +372,7 @@ pub mod longhands {
&Dimension(ref value, ref unit) if value.value >= 0. &Dimension(ref value, ref unit) if value.value >= 0.
=> specified::Length::parse_dimension(value.value, unit.as_slice()) => specified::Length::parse_dimension(value.value, unit.as_slice())
.map(SpecifiedLength), .map(SpecifiedLength),
&Ident(ref value) if unsafe { value.to_ascii_nocheck().eq_ignore_case("normal".to_ascii_nocheck())} &Ident(ref value) if value.eq_ignore_ascii_case("normal")
=> Some(SpecifiedNormal), => Some(SpecifiedNormal),
_ => None, _ => None,
} }
@ -453,15 +452,12 @@ pub mod longhands {
match input { match input {
&Ident(ref value) => { &Ident(ref value) => {
// FIXME: Workaround for https://github.com/mozilla/rust/issues/10683 // FIXME: Workaround for https://github.com/mozilla/rust/issues/10683
unsafe { let value_lower = value.to_ascii_lower();
let tmp_for_lifetime = value.to_ascii_nocheck().to_lower(); match value_lower.as_slice() {
let value_lower = tmp_for_lifetime.as_str_ascii(); % for keyword in vertical_align_keywords:
match value_lower.as_slice() { "${keyword}" => Some(Specified_${to_rust_ident(keyword)}),
% for keyword in vertical_align_keywords: % endfor
"${keyword}" => Some(Specified_${to_rust_ident(keyword)}), _ => None,
% endfor
_ => None,
}
} }
}, },
_ => specified::LengthOrPercentage::parse_non_negative(input) _ => specified::LengthOrPercentage::parse_non_negative(input)
@ -532,15 +528,13 @@ pub mod longhands {
pub fn parse(input: &[ComponentValue], _base_url: &Url) -> Option<SpecifiedValue> { pub fn parse(input: &[ComponentValue], _base_url: &Url) -> Option<SpecifiedValue> {
match one_component_value(input) { match one_component_value(input) {
Some(&Ident(ref keyword)) => { Some(&Ident(ref keyword)) => {
unsafe { // FIXME: Workaround for https://github.com/mozilla/rust/issues/10683
let tmp_for_lifetime = keyword.to_ascii_nocheck().to_lower(); let keyword_lower = keyword.to_ascii_lower();
let keyword_lower = tmp_for_lifetime.as_str_ascii(); match keyword_lower.as_slice() {
match keyword_lower.as_slice() {
"normal" => return Some(normal), "normal" => return Some(normal),
"none" => return Some(none), "none" => return Some(none),
_ => () _ => ()
}
} }
}, },
_ => () _ => ()
@ -581,7 +575,7 @@ pub mod longhands {
let image_url = parse_url(url.as_slice(), Some(base_url.clone())); let image_url = parse_url(url.as_slice(), Some(base_url.clone()));
Some(Some(image_url)) Some(Some(image_url))
}, },
&ast::Ident(ref value) if unsafe {value.to_ascii_nocheck()}.eq_ignore_case(unsafe {"none".to_ascii_nocheck()}) => Some(None), &ast::Ident(ref value) if value.eq_ignore_ascii_case("none") => Some(None),
_ => None, _ => None,
} }
} }
@ -731,10 +725,7 @@ pub mod longhands {
Some(&String(ref value)) => add!(FamilyName(value.to_owned()), break 'outer), Some(&String(ref value)) => add!(FamilyName(value.to_owned()), break 'outer),
Some(&Ident(ref value)) => { Some(&Ident(ref value)) => {
// FIXME: Workaround for https://github.com/mozilla/rust/issues/10683 // FIXME: Workaround for https://github.com/mozilla/rust/issues/10683
let value = value.as_slice(); let value_lower = value.to_ascii_lower();
unsafe {
let tmp_for_lifetime = value.to_ascii_nocheck().to_lower();
let value_lower = tmp_for_lifetime.as_str_ascii();
match value_lower.as_slice() { match value_lower.as_slice() {
// "serif" => add!(Serif, break 'outer), // "serif" => add!(Serif, break 'outer),
// "sans-serif" => add!(SansSerif, break 'outer), // "sans-serif" => add!(SansSerif, break 'outer),
@ -742,7 +733,7 @@ pub mod longhands {
// "fantasy" => add!(Fantasy, break 'outer), // "fantasy" => add!(Fantasy, break 'outer),
// "monospace" => add!(Monospace, break 'outer), // "monospace" => add!(Monospace, break 'outer),
_ => { _ => {
let mut idents = ~[value]; let mut idents = ~[value.as_slice()];
loop { loop {
match iter.next() { match iter.next() {
Some(&Ident(ref value)) => idents.push(value.as_slice()), Some(&Ident(ref value)) => idents.push(value.as_slice()),
@ -758,7 +749,7 @@ pub mod longhands {
} }
} }
} }
}} }
} }
_ => return None, _ => return None,
} }
@ -786,16 +777,13 @@ pub mod longhands {
match input { match input {
&Ident(ref value) => { &Ident(ref value) => {
// FIXME: Workaround for https://github.com/mozilla/rust/issues/10683 // FIXME: Workaround for https://github.com/mozilla/rust/issues/10683
unsafe { let value_lower = value.to_ascii_lower();
let tmp_for_lifetime = value.to_ascii_nocheck().to_lower(); match value_lower.as_slice() {
let value_lower = tmp_for_lifetime.as_str_ascii(); "bold" => Some(SpecifiedWeight700),
match value_lower.as_slice() { "normal" => Some(SpecifiedWeight400),
"bold" => Some(SpecifiedWeight700), "bolder" => Some(Bolder),
"normal" => Some(SpecifiedWeight400), "lighter" => Some(Lighter),
"bolder" => Some(Bolder), _ => None,
"lighter" => Some(Lighter),
_ => None,
}
} }
}, },
&Number(ref value) => match value.int_value { &Number(ref value) => match value.int_value {
@ -1190,10 +1178,12 @@ pub mod shorthands {
// Special-case 'normal' because it is valid in each of // Special-case 'normal' because it is valid in each of
// font-style, font-weight and font-variant. // font-style, font-weight and font-variant.
// Leaves the values to None, 'normal' is the initial value for each of them. // Leaves the values to None, 'normal' is the initial value for each of them.
if get_ident_lower(component_value).filtered( match get_ident_lower(component_value) {
|v| unsafe { v.to_ascii_nocheck() }.eq_ignore_case(unsafe {"normal".to_ascii_nocheck()})).is_some() { Some(ref ident) if ident.eq_ignore_ascii_case("normal") => {
nb_normals += 1; nb_normals += 1;
continue; continue;
}
_ => {}
} }
if style.is_none() { if style.is_none() {
match font_style::from_component_value(component_value, base_url) { match font_style::from_component_value(component_value, base_url) {
@ -1344,8 +1334,7 @@ impl PropertyDeclaration {
result_list: &mut ~[PropertyDeclaration], result_list: &mut ~[PropertyDeclaration],
base_url: &Url) -> PropertyDeclarationParseResult { base_url: &Url) -> PropertyDeclarationParseResult {
// FIXME: local variable to work around Rust #10683 // FIXME: local variable to work around Rust #10683
let tmp_for_lifetime = unsafe {name.to_ascii_nocheck()}.to_lower(); let name_lower = name.to_ascii_lower();
let name_lower = tmp_for_lifetime.as_str_ascii();
match name_lower.as_slice() { match name_lower.as_slice() {
% for property in LONGHANDS: % for property in LONGHANDS:
% if property.derived_from is None: % if property.derived_from is None: