Skip to content

Commit

Permalink
perf: make parse_scheme slightly faster (servo#1025)
Browse files Browse the repository at this point in the history
* perf: make parse_scheme slightly faster

* add comment and unit test

* move to one line
  • Loading branch information
dsherret authored Feb 13, 2025
1 parent 267afa5 commit 4b9f1e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,15 @@ impl<'a> Parser<'a> {
}

pub fn parse_scheme<'i>(&mut self, mut input: Input<'i>) -> Result<Input<'i>, ()> {
if input.is_empty() || !input.starts_with(ascii_alpha) {
// starts_with will also fail for empty strings so we can skip that comparison for perf
if !input.starts_with(ascii_alpha) {
return Err(());
}
debug_assert!(self.serialization.is_empty());
while let Some(c) = input.next() {
match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '+' | '-' | '.' => {
self.serialization.push(c.to_ascii_lowercase())
}
'a'..='z' | '0'..='9' | '+' | '-' | '.' => self.serialization.push(c),
'A'..='Z' => self.serialization.push(c.to_ascii_lowercase()),
':' => return Ok(input),
_ => {
self.serialization.clear();
Expand Down
8 changes: 8 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,14 @@ fn test_set_scheme_to_file_with_host() {
assert_eq!(result, Err(()));
}

#[test]
fn test_set_scheme_empty_err() {
let mut url: Url = "http://localhost:6767/foo/bar".parse().unwrap();
let result = url.set_scheme("");
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar");
assert_eq!(result, Err(()));
}

#[test]
fn no_panic() {
let mut url = Url::parse("arhttpsps:/.//eom/dae.com/\\\\t\\:").unwrap();
Expand Down

0 comments on commit 4b9f1e6

Please sign in to comment.