Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stream): Don't break out of UTF8 character part-way #158

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions crates/anstream/src/adapter/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,19 @@ impl<'s> Iterator for StripStrIter<'s> {
#[inline]
fn next_str<'s>(bytes: &mut &'s [u8], state: &mut State) -> Option<&'s str> {
let offset = bytes.iter().copied().position(|b| {
let (next_state, action) = state_change(*state, b);
let (next_state, action) = dbg!(state_change(*state, b));
if next_state != State::Anywhere {
*state = next_state;
}
is_printable_str(action, b)
is_printable_bytes(action, b)
});
let (_, next) = bytes.split_at(offset.unwrap_or(bytes.len()));
*bytes = next;
*state = State::Ground;

let offset = bytes.iter().copied().position(|b| {
let (_next_state, action) = state_change(State::Ground, b);
!is_printable_str(action, b)
!(is_printable_bytes(action, b) || is_utf8_continuation(b))
});
let (printable, next) = bytes.split_at(offset.unwrap_or(bytes.len()));
*bytes = next;
Expand All @@ -153,19 +153,6 @@ unsafe fn from_utf8_unchecked<'b>(bytes: &'b [u8], safety_justification: &'stati
}
}

#[inline]
fn is_printable_str(action: Action, byte: u8) -> bool {
// VT320 considered 0x7f to be `Print`able but we expect to be working in UTF-8 systems and not
// ISO Latin-1, making it DEL and non-printable
const DEL: u8 = 0x7f;
(action == Action::Print && byte != DEL)
|| action == Action::BeginUtf8
// since we know the input is valid UTF-8, the only thing we can do with
// continuations is to print them
|| is_utf8_continuation(byte)
|| (action == Action::Execute && byte.is_ascii_whitespace())
}

#[inline]
fn is_utf8_continuation(b: u8) -> bool {
matches!(b, 0x80..=0xbf)
Expand Down Expand Up @@ -473,6 +460,15 @@ mod test {
assert_eq!(expected, actual);
}

#[test]
fn test_strip_str_handles_broken_sequence() {
// valid utf8: \xc3\xb6 then \x1b then \xf0\x9f\x98\x80
let s = "ö\x1b😀hello😀goodbye";
let mut it = strip_str(s);
assert_eq!("ö", it.next().unwrap());
assert_eq!("ello😀goodbye", it.next().unwrap());
}

proptest! {
#[test]
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
Expand Down
Loading