mirror of
https://github.com/sharkdp/bat
synced 2026-08-08 19:41:45 +00:00
Performance optimization
This commit is contained in:
+12
-5
@@ -158,17 +158,24 @@ pub fn strip_ansi(line: &str) -> String {
|
|||||||
///
|
///
|
||||||
/// This function removes these sequences, keeping only the visible character.
|
/// This function removes these sequences, keeping only the visible character.
|
||||||
pub fn strip_overstrike(line: &str) -> Cow<'_, str> {
|
pub fn strip_overstrike(line: &str) -> Cow<'_, str> {
|
||||||
if !line.contains('\x08') {
|
let Some(first_bs) = line.find('\x08') else {
|
||||||
return Cow::Borrowed(line);
|
return Cow::Borrowed(line);
|
||||||
}
|
};
|
||||||
|
|
||||||
let mut output = String::with_capacity(line.len());
|
let mut output = String::with_capacity(line.len());
|
||||||
|
output.push_str(&line[..first_bs]);
|
||||||
|
output.pop();
|
||||||
|
|
||||||
for c in line.chars() {
|
let mut remaining = &line[first_bs + 1..];
|
||||||
if c == '\x08' {
|
|
||||||
|
loop {
|
||||||
|
if let Some(bs_pos) = remaining.find('\x08') {
|
||||||
|
output.push_str(&remaining[..bs_pos]);
|
||||||
output.pop();
|
output.pop();
|
||||||
|
remaining = &remaining[bs_pos + 1..];
|
||||||
} else {
|
} else {
|
||||||
output.push(c);
|
output.push_str(remaining);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user