From c93b11e899a2451a786a7f05c9f89759c77a485f Mon Sep 17 00:00:00 2001 From: vikingowl Date: Sun, 29 Mar 2026 20:44:27 +0200 Subject: [PATCH] perf(application): single-pass double-space collapse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace while-contains-replace loop with a single-pass char iterator. Eliminates O(n²) behavior and repeated allocations on pathological desktop file Exec values. --- .../owlry-core/src/providers/application.rs | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/owlry-core/src/providers/application.rs b/crates/owlry-core/src/providers/application.rs index 4886dab..24cdd47 100644 --- a/crates/owlry-core/src/providers/application.rs +++ b/crates/owlry-core/src/providers/application.rs @@ -58,9 +58,19 @@ fn clean_desktop_exec_field(cmd: &str) -> String { } // Clean up any double spaces that may have resulted from removing field codes - let mut cleaned = result.trim().to_string(); - while cleaned.contains(" ") { - cleaned = cleaned.replace(" ", " "); + let trimmed = result.trim(); + let mut cleaned = String::with_capacity(trimmed.len()); + let mut prev_space = false; + for c in trimmed.chars() { + if c == ' ' { + if !prev_space { + cleaned.push(' '); + } + prev_space = true; + } else { + cleaned.push(c); + prev_space = false; + } } cleaned @@ -271,4 +281,11 @@ mod tests { "env FOO=bar BAZ=qux myapp" ); } + + #[test] + fn test_clean_desktop_exec_collapses_spaces() { + assert_eq!(clean_desktop_exec_field("app --flag arg"), "app --flag arg"); + let input = format!("app{}arg", " ".repeat(100)); + assert_eq!(clean_desktop_exec_field(&input), "app arg"); + } }