perf(application): single-pass double-space collapse
Replace while-contains-replace loop with a single-pass char iterator. Eliminates O(n²) behavior and repeated allocations on pathological desktop file Exec values.
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user