Skip to content

Commit efcfb37

Browse files
author
bors-servo
authored
Auto merge of #171 - servo:token-cache, r=emilio
Add a one-token cache This makes the `Servo_DeclarationBlock_SetPropertyById` micro-benchmark being added in https://bugzilla.mozilla.org/show_bug.cgi?id=1344131 faster by ~10%. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/171) <!-- Reviewable:end -->
2 parents e009794 + 850eac1 commit efcfb37

12 files changed

+570
-524
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "cssparser"
4-
version = "0.17.0"
4+
version = "0.18.0"
55
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
66

77
description = "Rust implementation of CSS Syntax Level 3"

src/color.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ impl Color {
141141
///
142142
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
143143
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Color, BasicParseError<'i>> {
144-
let token = input.next()?;
144+
// FIXME: remove clone() when lifetimes are non-lexical
145+
let token = input.next()?.clone();
145146
match token {
146147
Token::Hash(ref value) | Token::IDHash(ref value) => {
147148
Color::parse_hash(value.as_bytes())
@@ -422,21 +423,17 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
422423
if uses_commas {
423424
arguments.expect_comma()?;
424425
} else {
425-
match arguments.next()? {
426-
Token::Delim('/') => {},
427-
t => return Err(BasicParseError::UnexpectedToken(t)),
428-
};
426+
arguments.expect_delim('/')?;
429427
};
430-
let token = arguments.next()?;
431-
match token {
428+
match *arguments.next()? {
432429
Token::Number { value: v, .. } => {
433430
clamp_unit_f32(v)
434431
}
435432
Token::Percentage { unit_value: v, .. } => {
436433
clamp_unit_f32(v)
437434
}
438-
t => {
439-
return Err(BasicParseError::UnexpectedToken(t))
435+
ref t => {
436+
return Err(BasicParseError::UnexpectedToken(t.clone()))
440437
}
441438
}
442439
} else {
@@ -457,10 +454,10 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
457454

458455
// Either integers or percentages, but all the same type.
459456
// https://drafts.csswg.org/css-color/#rgb-functions
460-
match arguments.next()? {
457+
match arguments.next()?.clone() {
461458
Token::Number { value: v, .. } => {
462459
red = clamp_floor_256_f32(v);
463-
green = clamp_floor_256_f32(match arguments.next()? {
460+
green = clamp_floor_256_f32(match arguments.next()?.clone() {
464461
Token::Number { value: v, .. } => v,
465462
Token::Comma => {
466463
uses_commas = true;
@@ -475,7 +472,7 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
475472
}
476473
Token::Percentage { unit_value, .. } => {
477474
red = clamp_unit_f32(unit_value);
478-
green = clamp_unit_f32(match arguments.next()? {
475+
green = clamp_unit_f32(match arguments.next()?.clone() {
479476
Token::Percentage { unit_value, .. } => unit_value,
480477
Token::Comma => {
481478
uses_commas = true;
@@ -498,28 +495,26 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
498495
let mut uses_commas = false;
499496
// Hue given as an angle
500497
// https://drafts.csswg.org/css-values/#angles
501-
let token = arguments.next()?;
502-
let hue_degrees = match token {
503-
Token::Number { value: v, .. } => Ok(v),
498+
let hue_degrees = match *arguments.next()? {
499+
Token::Number { value: v, .. } => v,
504500
Token::Dimension { value: v, ref unit, .. } => {
505501
match_ignore_ascii_case! { &*unit,
506-
"deg" => Ok(v),
507-
"grad" => Ok(v * 360. / 400.),
508-
"rad" => Ok(v * 360. / (2. * PI)),
509-
"turn" => Ok(v * 360.),
510-
_ => Err(()),
502+
"deg" => v,
503+
"grad" => v * 360. / 400.,
504+
"rad" => v * 360. / (2. * PI),
505+
"turn" => v * 360.,
506+
_ => return Err(BasicParseError::UnexpectedToken(Token::Ident(unit.clone()))),
511507
}
512508
}
513-
t => return Err(BasicParseError::UnexpectedToken(t))
509+
ref t => return Err(BasicParseError::UnexpectedToken(t.clone()))
514510
};
515-
let hue_degrees = hue_degrees.map_err(|()| BasicParseError::UnexpectedToken(token))?;
516511
// Subtract an integer before rounding, to avoid some rounding errors:
517512
let hue_normalized_degrees = hue_degrees - 360. * (hue_degrees / 360.).floor();
518513
let hue = hue_normalized_degrees / 360.;
519514

520515
// Saturation and lightness are clamped to 0% ... 100%
521516
// https://drafts.csswg.org/css-color/#the-hsl-notation
522-
let saturation = match arguments.next()? {
517+
let saturation = match arguments.next()?.clone() {
523518
Token::Percentage { unit_value, .. } => unit_value,
524519
Token::Comma => {
525520
uses_commas = true;

src/compact_cow_str.rs

Lines changed: 0 additions & 246 deletions
This file was deleted.

0 commit comments

Comments
 (0)