Skip to content

Commit

Permalink
Optimization: check for non-ASCII character before allocating space f…
Browse files Browse the repository at this point in the history
…or escape sequence
  • Loading branch information
Pr0methean authored Jun 6, 2024
1 parent 1dd585c commit 41637aa
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions escape_string/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,11 @@ pub fn escape<'a>(text: &'a str) -> Cow<'a, str> {
let mut owned = None;

for pos in 0..bytes.len() {
let special: Box<[u8]> = std::ascii::escape_default(bytes[pos]).collect();
if special.len() > 1 {
if !(bytes[pos] as char).is_ascii() {
if owned.is_none() {
owned = Some(bytes[0..pos].to_owned());
}
owned.as_mut().unwrap().push(special);
owned.as_mut().unwrap().expand(std::ascii::escape_default(bytes[pos]));

Check failure on line 157 in escape_string/src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

no method named `expand` found for mutable reference `&mut Vec<u8>` in the current scope
} else if let Some(owned) = owned.as_mut() {
owned.push(bytes[pos]);
}
Expand Down

0 comments on commit 41637aa

Please sign in to comment.