Skip to content

Commit b656c07

Browse files
authored
Merge pull request #532 from mgeisler/skip-ansi-escape-sequence-early-return
Early return in `skip_ansi_escape_sequence`
2 parents e7a20ef + 599b78a commit b656c07

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/core.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,45 @@ const CSI: (char, char) = ('\x1b', '[');
4141
/// The final bytes of an ANSI escape sequence must be in this range.
4242
const ANSI_FINAL_BYTE: std::ops::RangeInclusive<char> = '\x40'..='\x7e';
4343

44-
/// Skip ANSI escape sequences. The `ch` is the current `char`, the
45-
/// `chars` provide the following characters. The `chars` will be
46-
/// modified if `ch` is the start of an ANSI escape sequence.
44+
/// Skip ANSI escape sequences.
45+
///
46+
/// The `ch` is the current `char`, the `chars` provide the following
47+
/// characters. The `chars` will be modified if `ch` is the start of
48+
/// an ANSI escape sequence.
49+
///
50+
/// Returns `true` if one or more chars were skipped.
4751
#[inline]
4852
pub(crate) fn skip_ansi_escape_sequence<I: Iterator<Item = char>>(ch: char, chars: &mut I) -> bool {
49-
if ch == CSI.0 {
50-
let next = chars.next();
51-
if next == Some(CSI.1) {
52-
// We have found the start of an ANSI escape code, typically
53-
// used for colored terminal text. We skip until we find a
54-
// "final byte" in the range 0x40–0x7E.
55-
for ch in chars {
56-
if ANSI_FINAL_BYTE.contains(&ch) {
57-
return true;
58-
}
53+
if ch != CSI.0 {
54+
return false; // Nothing to skip here.
55+
}
56+
57+
let next = chars.next();
58+
if next == Some(CSI.1) {
59+
// We have found the start of an ANSI escape code, typically
60+
// used for colored terminal text. We skip until we find a
61+
// "final byte" in the range 0x40–0x7E.
62+
for ch in chars {
63+
if ANSI_FINAL_BYTE.contains(&ch) {
64+
break;
5965
}
60-
} else if next == Some(']') {
61-
// We have found the start of an Operating System Command, which
62-
// extends until the next sequence "\x1b\\" (the String Terminator
63-
// sequence) or the BEL character. The BEL character is non-standard,
64-
// but it is still used quite often, for example, by GNU ls.
65-
let mut last = ']';
66-
for new in chars {
67-
if new == '\x07' || (new == '\\' && last == CSI.0) {
68-
return true;
69-
}
70-
last = new;
66+
}
67+
} else if next == Some(']') {
68+
// We have found the start of an Operating System Command,
69+
// which extends until the next sequence "\x1b\\" (the String
70+
// Terminator sequence) or the BEL character. The BEL
71+
// character is non-standard, but it is still used quite
72+
// often, for example, by GNU ls.
73+
let mut last = ']';
74+
for new in chars {
75+
if new == '\x07' || (new == '\\' && last == CSI.0) {
76+
break;
7177
}
78+
last = new;
7279
}
7380
}
74-
false
81+
82+
true // Indicate that some chars were skipped.
7583
}
7684

7785
#[cfg(feature = "unicode-width")]

0 commit comments

Comments
 (0)