Skip to content

Commit

Permalink
perf: allow Sanitizer::str_till_eol to omit allocations
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <ljedrz@users.noreply.github.com>
  • Loading branch information
ljedrz committed Jun 6, 2024
1 parent 74a4378 commit ad5bfa2
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions console/network/environment/src/helpers/sanitizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,33 @@ impl Sanitizer {
///
/// Discard any leading newline.
fn str_till_eol(string: &str) -> ParserResult<&str> {
map(
recognize(Self::till(alt((value((), tag("\\\n")), value((), Sanitizer::parse_safe_char))), Self::eol)),
|i| {
if i.as_bytes().last() == Some(&b'\n') { &i[0..i.len() - 1] } else { i }
},
)(string)
// A heuristic approach is applied here in order to avoid
// costly parsing operations in the most common scenarios.
if let Some((before, after)) = string.split_once('\n') {
let is_multiline = before.ends_with('\\');

if !is_multiline {
let contains_unsafe_chars = !before.chars().all(is_char_supported);

if !contains_unsafe_chars {
Ok((after, before))
} else {
recognize(Self::till(value((), Sanitizer::parse_safe_char), Self::eol))(before)
}
} else {
map(
recognize(Self::till(
alt((value((), tag("\\\n")), value((), Sanitizer::parse_safe_char))),
Self::eol,
)),
|i| {
if i.as_bytes().last() == Some(&b'\n') { &i[0..i.len() - 1] } else { i }
},
)(string)
}
} else {
Ok((string, ""))
}
}

/// Parse a string until `*/` is encountered.
Expand Down

0 comments on commit ad5bfa2

Please sign in to comment.