style: Use more Box<[]> and Box<str> for quotes.

This commit is contained in:
Emilio Cobos Álvarez 2017-12-14 03:59:39 +01:00
parent 16f627a18a
commit 03f3521216
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
5 changed files with 50 additions and 31 deletions

View file

@ -311,9 +311,9 @@ impl<'a, 'b> ResolveGeneratedContentFragmentMutator<'a, 'b> {
&quotes.0[self.traversal.quote as usize] &quotes.0[self.traversal.quote as usize]
}; };
if close { if close {
close_quote.clone() close_quote.to_string()
} else { } else {
open_quote.clone() open_quote.to_string()
} }
} }
} }

View file

@ -4229,8 +4229,11 @@ fn static_assert() {
let ref gecko_quote_values = *self.gecko.mQuotes.mRawPtr; let ref gecko_quote_values = *self.gecko.mQuotes.mRawPtr;
longhands::quotes::computed_value::T( longhands::quotes::computed_value::T(
gecko_quote_values.mQuotePairs.iter().map(|gecko_pair| { gecko_quote_values.mQuotePairs.iter().map(|gecko_pair| {
(gecko_pair.first.to_string(), gecko_pair.second.to_string()) (
}).collect() gecko_pair.first.to_string().into_boxed_str(),
gecko_pair.second.to_string().into_boxed_str(),
)
}).collect::<Vec<_>>().into_boxed_slice()
) )
} }
} }

View file

@ -7,12 +7,20 @@
pub use values::specified::list::Quotes; pub use values::specified::list::Quotes;
impl Quotes { impl Quotes {
/// Initial value for `quotes` /// Initial value for `quotes`.
///
/// FIXME(emilio): This should ideally not allocate.
#[inline] #[inline]
pub fn get_initial_value() -> Quotes { pub fn get_initial_value() -> Quotes {
Quotes(vec![ Quotes(vec![
("\u{201c}".to_owned(), "\u{201d}".to_owned()), (
("\u{2018}".to_owned(), "\u{2019}".to_owned()), "\u{201c}".to_owned().into_boxed_str(),
]) "\u{201d}".to_owned().into_boxed_str(),
),
(
"\u{2018}".to_owned().into_boxed_str(),
"\u{2019}".to_owned().into_boxed_str(),
),
].into_boxed_slice())
} }
} }

View file

@ -401,6 +401,7 @@ trivial_to_computed_value!(BorderStyle);
trivial_to_computed_value!(Cursor); trivial_to_computed_value!(Cursor);
trivial_to_computed_value!(Namespace); trivial_to_computed_value!(Namespace);
trivial_to_computed_value!(String); trivial_to_computed_value!(String);
trivial_to_computed_value!(Box<str>);
/// A `<number>` value. /// A `<number>` value.
pub type Number = CSSFloat; pub type Number = CSSFloat;

View file

@ -4,31 +4,38 @@
//! `list` specified values. //! `list` specified values.
use cssparser::{Parser, Token, serialize_string}; use cssparser::{Parser, Token};
use parser::{Parse, ParserContext}; use parser::{Parse, ParserContext};
use std::fmt; use std::fmt;
use style_traits::{ParseError, StyleParseErrorKind, ToCss}; use style_traits::{ParseError, StyleParseErrorKind, ToCss};
/// Specified and computed `quote` property /// Specified and computed `quote` property.
///
/// FIXME(emilio): It's a shame that this allocates all the time it's computed,
/// probably should just be refcounted.
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)] #[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)]
pub struct Quotes(pub Vec<(String, String)>); pub struct Quotes(pub Box<[(Box<str>, Box<str>)]>);
impl ToCss for Quotes { impl ToCss for Quotes {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if self.0.is_empty() { let mut iter = self.0.iter();
return dest.write_str("none")
match iter.next() {
Some(&(ref l, ref r)) => {
l.to_css(dest)?;
dest.write_char(' ')?;
r.to_css(dest)?;
}
None => return dest.write_str("none"),
} }
let mut first = true; for &(ref l, ref r) in iter {
for pair in &self.0 { dest.write_char(' ')?;
if !first { l.to_css(dest)?;
dest.write_str(" ")?; dest.write_char(' ')?;
} r.to_css(dest)?;
first = false;
serialize_string(&*pair.0, dest)?;
dest.write_str(" ")?;
serialize_string(&*pair.1, dest)?;
} }
Ok(()) Ok(())
} }
} }
@ -36,27 +43,27 @@ impl ToCss for Quotes {
impl Parse for Quotes { impl Parse for Quotes {
fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Quotes, ParseError<'i>> { fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Quotes, ParseError<'i>> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() { if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(Quotes(Vec::new())) return Ok(Quotes(Vec::new().into_boxed_slice()))
} }
let mut quotes = Vec::new(); let mut quotes = Vec::new();
loop { loop {
let location = input.current_source_location(); let location = input.current_source_location();
let first = match input.next() { let first = match input.next() {
Ok(&Token::QuotedString(ref value)) => value.as_ref().to_owned(), Ok(&Token::QuotedString(ref value)) => {
value.as_ref().to_owned().into_boxed_str()
},
Ok(t) => return Err(location.new_unexpected_token_error(t.clone())), Ok(t) => return Err(location.new_unexpected_token_error(t.clone())),
Err(_) => break, Err(_) => break,
}; };
let location = input.current_source_location();
let second = match input.next() { let second =
Ok(&Token::QuotedString(ref value)) => value.as_ref().to_owned(), input.expect_string()?.as_ref().to_owned().into_boxed_str();
Ok(t) => return Err(location.new_unexpected_token_error(t.clone())),
Err(e) => return Err(e.into()),
};
quotes.push((first, second)) quotes.push((first, second))
} }
if !quotes.is_empty() { if !quotes.is_empty() {
Ok(Quotes(quotes)) Ok(Quotes(quotes.into_boxed_slice()))
} else { } else {
Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)) Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
} }