Skip to content

Avoid array overallocation when parsing background-image property #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.20.1"
version = "0.20.2"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
6 changes: 5 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,11 @@ impl<'i: 't, 't> Parser<'i, 't> {
#[inline]
pub fn parse_comma_separated<F, T, E>(&mut self, mut parse_one: F) -> Result<Vec<T>, ParseError<'i, E>>
where F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>> {
let mut values = vec![];
// Vec grows from 0 to 4 by default on first push(). So allocate with
// capacity 1, so in the somewhat common case of only one item we don't
// way overallocate. Note that we always push at least one item if
// parsing succeeds.
let mut values = Vec::with_capacity(1);
loop {
self.skip_whitespace(); // Unnecessary for correctness, but may help try() in parse_one rewind less.
values.push(self.parse_until_before(Delimiter::Comma, &mut parse_one)?);
Expand Down