Skip to content

Avoid boundary check in parse_n_dash_digits #199

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
Oct 1, 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
Avoid boundary check in parse_n_dash_digits
  • Loading branch information
upsuper committed Oct 1, 2017
commit 6067f7dbe19be82b109142edd017448583b91e1d
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.21.2"
version = "0.21.3"
authors = [ "Simon Sapin <[email protected]>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
4 changes: 3 additions & 1 deletion src/css-parsing-tests/An+B.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
" +n - 1", [1, -1],
" -n - 1", [-1, -1],
"+ N - 1", null,
"- N - 1", null
"- N - 1", null,

"2n\u22121", null

]
7 changes: 4 additions & 3 deletions src/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ fn parse_signless_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32, b_sign: i32) ->
}

fn parse_n_dash_digits(string: &str) -> Result<i32, ()> {
if string.len() >= 3
&& string[..2].eq_ignore_ascii_case("n-")
&& string[2..].chars().all(|c| matches!(c, '0'...'9'))
let bytes = string.as_bytes();
if bytes.len() >= 3
&& bytes[..2].eq_ignore_ascii_case(b"n-")
&& bytes[2..].iter().all(|&c| matches!(c, b'0'...b'9'))
{
Ok(parse_number_saturate(&string[1..]).unwrap()) // Include the minus sign
} else {
Expand Down