From 7ea53c779c536ea17bd8a0ac0b74632e39462db8 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 5 Nov 2024 11:56:54 +0100 Subject: [PATCH 1/3] perf: use eoi instead of eol where applicable in str_sill_eol Signed-off-by: ljedrz --- console/network/environment/src/helpers/sanitizer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/console/network/environment/src/helpers/sanitizer.rs b/console/network/environment/src/helpers/sanitizer.rs index 8892ef34fd..49cb85f638 100644 --- a/console/network/environment/src/helpers/sanitizer.rs +++ b/console/network/environment/src/helpers/sanitizer.rs @@ -128,7 +128,7 @@ impl Sanitizer { if !contains_unsafe_chars { Ok((after, before)) } else { - recognize(Self::till(value((), Sanitizer::parse_safe_char), Self::eol))(before) + recognize(Self::till(value((), Sanitizer::parse_safe_char), Self::eoi))(before) } } else { map( From c02d7d5585cbb1f502781c9867c31ca666e6e438 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Wed, 6 Nov 2024 12:03:19 +0100 Subject: [PATCH 2/3] tests: add a few more cases to test_comments Signed-off-by: ljedrz --- .../environment/src/helpers/sanitizer.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/console/network/environment/src/helpers/sanitizer.rs b/console/network/environment/src/helpers/sanitizer.rs index 49cb85f638..cdc6f21baa 100644 --- a/console/network/environment/src/helpers/sanitizer.rs +++ b/console/network/environment/src/helpers/sanitizer.rs @@ -277,6 +277,23 @@ mod tests { ("hello world", "// hel\u{4141}lo\n"), Sanitizer::parse_comments("// hel\u{4141}lo\nhello world").unwrap() ); + assert_eq!( + ("hello world", "/* multi\n line comment\n*/\n"), + Sanitizer::parse_comments("/* multi\n line comment\n*/\nhello world").unwrap() + ); + assert_eq!( + ("hello world", "// multiple\n// line\n// comments\n"), + Sanitizer::parse_comments("// multiple\n// line\n// comments\nhello world").unwrap() + ); + assert_eq!( + ("hello world", "/* multi\n line comment\n*/\n/* and\n another\n one\n*/\n"), + Sanitizer::parse_comments("/* multi\n line comment\n*/\n/* and\n another\n one\n*/\nhello world") + .unwrap() + ); + assert_eq!( + ("hello world", "/* multi\n line comment\n*/\n// two single\n// line comments\n/* and\n another\n multi-liner\n*/\n"), + Sanitizer::parse_comments("/* multi\n line comment\n*/\n// two single\n// line comments\n/* and\n another\n multi-liner\n*/\nhello world").unwrap() + ); assert!(Sanitizer::parse_comments("// hel\x08lo\nhello world").is_err()); assert!(Sanitizer::parse_comments("// hel\u{2066}lo\nhello world").is_err()); assert!(Sanitizer::parse_comments("/* hel\x7flo */\nhello world").is_err()); From 352540fb79dae4cace0c1aeec478d2b007ebc262 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Tue, 5 Nov 2024 11:56:11 +0100 Subject: [PATCH 3/3] docs: extra doc comments in str_till_eol Signed-off-by: ljedrz --- console/network/environment/src/helpers/sanitizer.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/console/network/environment/src/helpers/sanitizer.rs b/console/network/environment/src/helpers/sanitizer.rs index cdc6f21baa..184f99583d 100644 --- a/console/network/environment/src/helpers/sanitizer.rs +++ b/console/network/environment/src/helpers/sanitizer.rs @@ -117,8 +117,9 @@ impl Sanitizer { /// /// Discard any leading newline. fn str_till_eol(string: &str) -> ParserResult<&str> { - // A heuristic approach is applied here in order to avoid - // costly parsing operations in the most common scenarios. + // A heuristic approach is applied here in order to avoid costly parsing operations in the + // most common scenarios: non-parsing methods are used to verify if the string has multiple + // lines and if there are any unsafe characters. if let Some((before, after)) = string.split_once('\n') { let is_multiline = before.ends_with('\\'); @@ -128,6 +129,8 @@ impl Sanitizer { if !contains_unsafe_chars { Ok((after, before)) } else { + // `eoi` is used here instead of `eol`, since the earlier call to `split_once` + // already removed the newline recognize(Self::till(value((), Sanitizer::parse_safe_char), Self::eoi))(before) } } else {