pub use crate::text::WidthMode;
use crate::unicode::graphemes;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenKind {
Text,
Escape,
Control,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token<'a> {
pub kind: TokenKind,
pub bytes: &'a [u8],
pub width: u16,
pub control: u8,
}
pub fn string_width(bytes: &[u8], mode: WidthMode, eaw_wide: bool) -> usize {
tokenize(bytes, mode, eaw_wide)
.map(|t| usize::from(t.width))
.sum()
}
pub fn tokenize(bytes: &[u8], mode: WidthMode, eaw_wide: bool) -> Tokenizer<'_> {
Tokenizer {
bytes,
pos: 0,
mode,
eaw_wide,
scan_end: 0,
run: "",
#[cfg(test)]
scanned: 0,
#[cfg(test)]
validated: 0,
}
}
pub struct Tokenizer<'a> {
bytes: &'a [u8],
pos: usize,
mode: WidthMode,
eaw_wide: bool,
scan_end: usize,
run: &'a str,
#[cfg(test)]
scanned: usize,
#[cfg(test)]
validated: usize,
}
impl<'a> Tokenizer<'a> {
fn control_token(&mut self, control: u8, size: usize) -> Token<'a> {
let start = self.pos;
self.pos += size;
Token {
kind: TokenKind::Control,
bytes: &self.bytes[start..self.pos],
width: 0,
control,
}
}
#[inline]
fn validate(&mut self, to: usize) {
let from = self.pos;
#[cfg(test)]
{
self.validated += to - from;
}
self.run = std::str::from_utf8(&self.bytes[from..to]).unwrap_or("");
}
}
impl<'a> Iterator for Tokenizer<'a> {
type Item = Token<'a>;
fn next(&mut self) -> Option<Token<'a>> {
let b = *self.bytes.get(self.pos)?;
if (0x20..0x7f).contains(&b) && self.bytes.get(self.pos + 1).is_none_or(u8::is_ascii) {
let start = self.pos;
self.pos += 1;
if !self.run.is_empty() {
self.run = &self.run[1..];
}
return Some(Token {
kind: TokenKind::Text,
bytes: &self.bytes[start..self.pos],
width: 1,
control: 0,
});
}
let c1 = c1_at(self.bytes, self.pos);
if b == 0x1b || c1.is_some_and(|(control, _)| is_c1_introducer(control)) {
let start = self.pos;
let end = scan_sequence(self.bytes, start);
self.pos = end;
return Some(Token {
kind: TokenKind::Escape,
bytes: &self.bytes[start..end],
width: 0,
control: 0,
});
}
if let Some((control, size)) = c1 {
return Some(self.control_token(control, size));
}
if self.run.is_empty() {
if b < 0x20 || b == 0x7f {
return Some(self.control_token(b, 1));
}
if self.pos >= self.scan_end {
let mut end = self.pos;
while end < self.bytes.len() {
let bb = self.bytes[end];
if bb < 0x20 || bb == 0x7f || c1_at(self.bytes, end).is_some() {
break;
}
match utf8_char_at(self.bytes, end) {
Some(n) => end += n,
None => break,
}
}
#[cfg(test)]
{
self.scanned += end.max(self.pos + 1) - self.pos;
}
self.scan_end = end;
self.validate(end);
}
if self.run.is_empty() {
return Some(self.control_token(b, 1));
}
}
let g = graphemes(self.run).next()?;
let start = self.pos;
self.pos += g.len();
self.run = &self.run[g.len()..];
let width = self.mode.grapheme_width(g, self.eaw_wide) as u16;
Some(Token {
kind: TokenKind::Text,
bytes: &self.bytes[start..self.pos],
width,
control: 0,
})
}
}
fn utf8_char_at(bytes: &[u8], i: usize) -> Option<usize> {
let n = utf8_char_len(bytes[i]);
if n == 0 || i + n > bytes.len() {
return None;
}
if n > 1 && std::str::from_utf8(&bytes[i..i + n]).is_err() {
return None;
}
Some(n)
}
#[inline]
fn utf8_char_len(b: u8) -> usize {
match b {
0x00..=0x7f => 1,
0xc2..=0xdf => 2,
0xe0..=0xef => 3,
0xf0..=0xf4 => 4,
_ => 0,
}
}
#[inline]
fn is_c1_introducer(b: u8) -> bool {
matches!(b, 0x90 | 0x98 | 0x9b | 0x9d | 0x9e | 0x9f)
}
#[inline]
fn c1_at(bytes: &[u8], at: usize) -> Option<(u8, usize)> {
let b = bytes[at];
if (0x80..=0x9f).contains(&b) {
Some((b, 1))
} else if b == 0xc2
&& let Some(&control @ 0x80..=0x9f) = bytes.get(at + 1)
{
Some((control, 2))
} else {
None
}
}
fn scan_sequence(bytes: &[u8], start: usize) -> usize {
let len = bytes.len();
let (head, size) = c1_at(bytes, start).unwrap_or((bytes[start], 1));
match head {
0x1b => {
let i = start + 1;
if i >= len {
return len;
}
match bytes[i] {
b'[' => scan_csi(bytes, i + 1),
b']' => scan_string(bytes, i + 1, true),
b'P' | b'X' | b'^' | b'_' => scan_string(bytes, i + 1, false),
_ => scan_esc_intermediate(bytes, i),
}
}
0x9b => scan_csi(bytes, start + size),
0x9d => scan_string(bytes, start + size, true),
0x90 | 0x98 | 0x9e | 0x9f => scan_string(bytes, start + size, false),
_ => start + 1,
}
}
fn scan_csi(bytes: &[u8], from: usize) -> usize {
let len = bytes.len();
let mut i = from;
while i < len {
if (0x40..=0x7e).contains(&bytes[i]) {
return i + 1;
}
i += 1;
}
len
}
fn scan_string(bytes: &[u8], from: usize, bel_ends: bool) -> usize {
let len = bytes.len();
let mut i = from;
while i < len {
let b = bytes[i];
if let Some((0x9c, size)) = c1_at(bytes, i) {
return i + size;
}
if b == 0x07 && bel_ends {
return i + 1;
}
if b == 0x1b && i + 1 < len && bytes[i + 1] == b'\\' {
return i + 2;
}
if b == 0x1b {
return i;
}
i += utf8_char_at(bytes, i).unwrap_or(1);
}
len
}
fn scan_esc_intermediate(bytes: &[u8], at: usize) -> usize {
let len = bytes.len();
let mut i = at;
while i < len && (0x20..=0x2f).contains(&bytes[i]) {
i += 1;
}
if i < len {
i + utf8_char_at(bytes, i).unwrap_or(1)
} else {
len
}
}
#[cfg(test)]
mod util {
use super::*;
pub(super) fn text_token(bytes: &[u8], width: u16) -> Token<'_> {
Token {
kind: TokenKind::Text,
bytes,
width,
control: 0,
}
}
pub(super) fn escape_token(bytes: &[u8]) -> Token<'_> {
Token {
kind: TokenKind::Escape,
bytes,
width: 0,
control: 0,
}
}
pub(super) fn control_token(bytes: &[u8]) -> Token<'_> {
Token {
kind: TokenKind::Control,
bytes,
width: 0,
control: *bytes.last().unwrap(),
}
}
pub(super) fn tokens(input: &[u8]) -> Vec<Token<'_>> {
tokenize(input, WidthMode::Grapheme, false).collect()
}
pub(super) fn visible(toks: &[Token<'_>]) -> String {
toks.iter()
.filter(|t| t.kind == TokenKind::Text)
.map(|t| std::str::from_utf8(t.bytes).expect("a text token is whole UTF-8"))
.collect()
}
pub(super) fn rebuild(toks: &[Token<'_>]) -> Vec<u8> {
let mut out = Vec::new();
for t in toks {
out.extend_from_slice(t.bytes);
}
out
}
pub(super) fn forms(seven: &[u8], eight: u8) -> [(&'static str, Vec<u8>); 3] {
[
("7-bit", seven.to_vec()),
("8-bit", vec![eight]),
("UTF-8", vec![0xc2, eight]),
]
}
}
#[cfg(test)]
mod tests {
use super::util::*;
use super::*;
#[test]
fn token_fields_preserve_bytes_widths_and_control_codes() {
let input = b"A\x1b[31m\t\x85\xc2\x85\xff\xe4\xb8\xad";
let expected = [
text_token(b"A", 1),
escape_token(b"\x1b[31m"),
control_token(b"\t"),
control_token(b"\x85"),
control_token(b"\xc2\x85"),
control_token(b"\xff"),
text_token(b"\xe4\xb8\xad", 2),
];
for mode in [WidthMode::Wc, WidthMode::Grapheme] {
let toks: Vec<_> = tokenize(input, mode, false).collect();
assert_eq!(toks, expected);
let mut offset = 0;
for token in &toks {
assert_eq!(token.bytes.as_ptr(), input[offset..].as_ptr());
offset += token.bytes.len();
}
}
}
#[test]
fn tokenize_csi() {
let toks: Vec<_> = tokenize(b"\x1b[31mhi\x1b[m", WidthMode::Grapheme, false).collect();
assert_eq!(toks.len(), 4);
assert_eq!(toks[0], escape_token(b"\x1b[31m"));
assert_eq!(toks[3], escape_token(b"\x1b[m"));
}
#[test]
fn utf8_c1_sequences_are_zero_width() {
for input in [
&b"\x9b31mhello\x9bm"[..],
b"\xc2\x9b31mhello\xc2\x9bm",
b"\xc2\x9d8;;https://example.com\xc2\x9chello\xc2\x9d8;;\xc2\x9c",
] {
for mode in [WidthMode::Wc, WidthMode::Grapheme] {
assert_eq!(string_width(input, mode, false), 5, "{input:x?}");
}
}
}
#[test]
fn tokenize_osc_bel() {
let toks: Vec<_> = tokenize(b"\x1b]0;title\x07rest", WidthMode::Grapheme, false).collect();
assert_eq!(toks[0], escape_token(b"\x1b]0;title\x07"));
}
#[test]
fn tokenize_osc_st() {
let toks: Vec<_> =
tokenize(b"\x1b]0;title\x1b\\rest", WidthMode::Grapheme, false).collect();
assert_eq!(toks[0], escape_token(b"\x1b]0;title\x1b\\"));
}
#[test]
fn tokenize_wide_char() {
let toks: Vec<_> = tokenize("ä¸".as_bytes(), WidthMode::Grapheme, false).collect();
assert_eq!(toks.len(), 1);
assert_eq!(toks[0], text_token("ä¸".as_bytes(), 2));
}
#[test]
fn tokenize_newline_control() {
let toks: Vec<_> = tokenize(b"a\nb", WidthMode::Grapheme, false).collect();
assert_eq!(toks.len(), 3);
assert_eq!(toks[1], control_token(b"\n"));
}
#[test]
fn string_width_ignores_escapes() {
assert_eq!(
string_width(b"\x1b[31mhello\x1b[m", WidthMode::Grapheme, false),
5
);
assert_eq!(
string_width("䏿–‡".as_bytes(), WidthMode::Grapheme, false),
4
);
}
#[test]
fn tokenize_8bit_csi() {
let toks: Vec<_> = tokenize(b"\x9b31mhi\x9bm", WidthMode::Grapheme, false).collect();
assert_eq!(toks[0], escape_token(b"\x9b31m"));
assert_eq!(toks[3], escape_token(b"\x9bm"));
}
#[test]
fn tokenize_8bit_osc_with_8bit_st() {
let toks: Vec<_> = tokenize(b"\x9d0;title\x9crest", WidthMode::Grapheme, false).collect();
assert_eq!(toks[0], escape_token(b"\x9d0;title\x9c"));
assert_eq!(toks.len(), 5);
}
#[test]
fn tokenize_8bit_dcs() {
let toks: Vec<_> = tokenize(b"\x90q!data\x9cafter", WidthMode::Grapheme, false).collect();
assert_eq!(toks[0], escape_token(b"\x90q!data\x9c"));
}
#[test]
fn tokenize_8bit_sos_pm_apc() {
for &intro in &[0x98u8, 0x9e, 0x9f] {
let mut input = vec![intro];
input.extend_from_slice(b"payload");
input.push(0x9c);
let toks: Vec<_> = tokenize(&input, WidthMode::Grapheme, false).collect();
assert_eq!(toks[0], escape_token(&input));
}
}
#[test]
fn tokenize_standalone_c1_is_control() {
let toks: Vec<_> = tokenize(b"a\x84b", WidthMode::Grapheme, false).collect();
assert_eq!(toks.len(), 3);
assert_eq!(toks[1], control_token(b"\x84"));
}
#[test]
fn tokenize_two_byte_esc() {
let toks: Vec<_> = tokenize(b"a\x1b=b", WidthMode::Grapheme, false).collect();
assert_eq!(toks.len(), 3);
assert_eq!(toks[1], escape_token(b"\x1b="));
}
}
#[cfg(test)]
mod sequences {
use super::util::*;
use super::*;
const STRINGS: &[(&str, &[u8], u8)] = &[
("OSC", b"\x1b]", 0x9d),
("DCS", b"\x1bP", 0x90),
("SOS", b"\x1bX", 0x98),
("PM", b"\x1b^", 0x9e),
("APC", b"\x1b_", 0x9f),
];
const TERMINATORS: &[(&str, &[u8])] = &[
("BEL", b"\x07"),
("ESC backslash", b"\x1b\\"),
("8-bit ST", b"\x9c"),
("UTF-8 ST", b"\xc2\x9c"),
];
const PAYLOADS: &[(&str, &str)] = &[
("ascii", "0;plain title"),
("utf8", "0;caf\u{e9} \u{2705} \u{4e00} \u{1f600}"),
];
#[test]
fn every_string_sequence_is_one_whole_token() {
for (name, seven, eight) in STRINGS {
for (pname, payload) in PAYLOADS {
for (tname, term) in TERMINATORS {
if *term == b"\x07" && *name != "OSC" {
continue;
}
for (form, intro) in forms(seven, *eight) {
let mut input = intro;
input.extend_from_slice(payload.as_bytes());
input.extend_from_slice(term);
let seq_len = input.len();
input.extend_from_slice("after \u{2705}".as_bytes());
let what = format!("{name} {form} {pname} payload, {tname} terminator");
let toks = tokens(&input);
assert_eq!(
toks.first(),
Some(&escape_token(&input[..seq_len])),
"{what}: the sequence is not one token"
);
assert_eq!(
visible(&toks[1..]),
"after \u{2705}",
"{what}: text after was lost"
);
assert_eq!(
string_width(&input, WidthMode::Grapheme, false),
8,
"{what}: the sequence was measured"
);
}
}
}
}
}
#[test]
fn an_unterminated_string_sequence_runs_to_the_end() {
for (name, seven, eight) in STRINGS {
for (form, intro) in forms(seven, *eight) {
for (pname, payload) in PAYLOADS {
let mut input = intro.clone();
input.extend_from_slice(payload.as_bytes());
let toks = tokens(&input);
assert_eq!(
toks,
vec![escape_token(&input[..])],
"{name} {form} {pname}: an unterminated sequence should be one token"
);
assert_eq!(string_width(&input, WidthMode::Grapheme, false), 0);
}
}
}
}
#[test]
fn a_lone_esc_ends_a_string_and_is_reparsed() {
for (name, seven, eight) in STRINGS {
for (form, intro) in forms(seven, *eight) {
let mut input = intro.clone();
input.extend_from_slice("pay\u{2705}".as_bytes());
let cut = input.len();
input.extend_from_slice(b"\x1b[31mZ");
let toks = tokens(&input);
assert_eq!(
toks[0],
escape_token(&input[..cut]),
"{name} {form}: the string should stop at the ESC"
);
assert_eq!(
toks[1],
escape_token(b"\x1b[31m"),
"{name} {form}: the CSI after it should survive whole"
);
assert_eq!(toks[2], text_token(b"Z", 1));
}
}
}
#[test]
fn adjacent_sequences_do_not_merge() {
let cases: &[(&[u8], &[u8], &[u8])] = &[
(
b"\x1b]0;a\x07\x1bPq\x1b\\Z",
b"\x1b]0;a\x07",
b"\x1bPq\x1b\\",
),
(b"\x9fa\x9c\x9eb\x9cZ", b"\x9fa\x9c", b"\x9eb\x9c"),
(b"\x9d0;a\x1b\\\x1b^b\x9cZ", b"\x9d0;a\x1b\\", b"\x1b^b\x9c"),
];
for (input, first, second) in cases {
let toks = tokens(input);
assert_eq!(toks[0], escape_token(first), "{input:x?}");
assert_eq!(toks[1], escape_token(second), "{input:x?}");
assert_eq!(toks[2], text_token(b"Z", 1), "{input:x?}");
}
}
#[test]
fn real_payloads_survive() {
let cases: &[(&str, &[u8], &[u8], &str)] = &[
(
"sixel",
b"\x1bPq#0;2;0;0;0#1;2;100;100;100\x1b\\ok",
b"\x1bPq#0;2;0;0;0#1;2;100;100;100\x1b\\",
"ok",
),
(
"DECRQSS reply",
b"\x1bP1$r0;1m\x1b\\ok",
b"\x1bP1$r0;1m\x1b\\",
"ok",
),
(
"OSC 8 hyperlink with a UTF-8 target",
b"\x1b]8;;https://example.com/\xe2\x9c\x85\x07link\x1b]8;;\x07",
b"\x1b]8;;https://example.com/\xe2\x9c\x85\x07",
"link",
),
(
"OSC with an empty payload",
b"\x1b]\x07ok",
b"\x1b]\x07",
"ok",
),
(
"8-bit APC with an empty payload",
b"\x9f\x9cok",
b"\x9f\x9c",
"ok",
),
];
for (what, input, opening, want) in cases {
let toks = tokens(input);
assert_eq!(toks.first(), Some(&escape_token(opening)), "{what}");
assert_eq!(visible(&toks), *want, "{what}");
assert_eq!(
rebuild(&toks),
*input,
"{what}: the stream did not reassemble"
);
}
}
#[test]
fn c1_encodings_preserve_sequence_boundaries() {
for input in [
&b"\x1b]0;title\x9cZ"[..],
b"\x1b]0;title\xc2\x9cZ",
b"\x9d0;title\x07Z",
b"\xc2\x9d0;title\x07Z",
b"\x1b]0;\xe2\x9c\x85\x07Z",
] {
let toks = tokens(input);
assert_eq!(toks[0], escape_token(&input[..input.len() - 1]));
assert_eq!(visible(&toks), "Z");
assert_eq!(rebuild(&toks), input);
}
}
#[test]
fn bel_ends_an_osc_and_nothing_else() {
for (name, seven, eight) in STRINGS {
for (form, intro) in forms(seven, *eight) {
let mut input = intro;
input.extend_from_slice(b"pay\x07load");
let bel_at = input.len() - b"load".len();
input.extend_from_slice(b"\x9cZ");
let toks = tokens(&input);
let what = format!("{name} {form}");
if *name == "OSC" {
assert_eq!(
toks[0],
escape_token(&input[..bel_at]),
"{what}: BEL should have ended it"
);
assert!(
toks.iter()
.any(|t| t.kind == TokenKind::Text && t.bytes == b"l"),
"{what}: text after the BEL went missing"
);
} else {
assert_eq!(
toks[0],
escape_token(&input[..input.len() - 1]),
"{what}: BEL is payload, so it should have run to the ST"
);
assert_eq!(toks[1], text_token(b"Z", 1), "{what}");
assert_eq!(string_width(&input, WidthMode::Grapheme, false), 1);
}
}
}
}
#[test]
fn every_c1_control_preserves_its_encoding() {
for c in 0x80u8..=0x9f {
for input in [vec![c], vec![0xc2, c]] {
for mode in [WidthMode::Wc, WidthMode::Grapheme] {
let toks: Vec<_> = tokenize(&input, mode, false).collect();
let expected = if is_c1_introducer(c) {
escape_token(&input)
} else {
control_token(&input)
};
assert_eq!(toks, [expected]);
assert_eq!(string_width(&input, mode, false), 0);
let payload = [b"\x1b]0;", input.as_slice(), b"Z\x07"].concat();
let expected_len = if c == 0x9c {
4 + input.len()
} else {
payload.len()
};
assert_eq!(
tokenize(&payload, mode, false).next(),
Some(escape_token(&payload[..expected_len])),
);
}
}
}
}
}
#[cfg(test)]
mod utf8_c1 {
use super::{util::*, *};
use crate::ansi::{strip::strip, truncate, wrap};
#[test]
fn text_utilities_preserve_standalone_control_encoding() {
let input = std::str::from_utf8(b"ab\xc2\x80cdef").unwrap();
assert_eq!(strip(input), input);
assert_eq!(
wrap::hardwrap(input, 3, true).as_bytes(),
b"ab\xc2\x80c\ndef"
);
for result in [
wrap::wordwrap(
std::str::from_utf8(b"ab\xc2\x80 cd").unwrap(),
2,
wrap::DEFAULT_BREAKPOINTS,
),
wrap::wrap(
std::str::from_utf8(b"ab\xc2\x80 cd").unwrap(),
2,
wrap::DEFAULT_BREAKPOINTS,
),
] {
assert_eq!(result.as_bytes(), b"ab\xc2\x80\ncd");
}
assert_eq!(truncate::truncate(input, 4, "").as_bytes(), b"ab\xc2\x80cd");
assert_eq!(
truncate::truncate_left(input, 4, "").as_bytes(),
b"\xc2\x80cdef"
);
assert_eq!(truncate::cut(input, 1, 1).as_bytes(), b"b\xc2\x80cde");
}
#[test]
fn text_utilities_preserve_encoded_sequences() {
let input = std::str::from_utf8(b"\xc2\x9b31mabcdef\xc2\x9bm").unwrap();
assert_eq!(strip(input), "abcdef");
assert_eq!(
wrap::hardwrap(input, 3, true).as_bytes(),
b"\xc2\x9b31mabc\ndef\xc2\x9bm"
);
assert_eq!(
truncate::truncate(input, 4, "").as_bytes(),
b"\xc2\x9b31mabcd\xc2\x9bm"
);
assert_eq!(
truncate::truncate_left(input, 4, "").as_bytes(),
b"\xc2\x9b31mcdef\xc2\x9bm"
);
assert_eq!(
truncate::cut(input, 1, 1).as_bytes(),
b"\xc2\x9b31mbcde\xc2\x9bm"
);
}
#[test]
fn other_two_byte_characters_remain_text() {
for lead in 0xc2..=0xdf {
for tail in 0x80..=0xbf {
if lead == 0xc2 && tail <= 0x9f {
continue;
}
let input = [lead, tail];
let toks = tokens(&input);
assert_eq!(toks.len(), 1);
assert_eq!(toks[0].kind, TokenKind::Text);
assert_eq!(toks[0].bytes, input);
}
}
}
#[test]
fn malformed_leads_preserve_encoded_controls_and_progress() {
for input in [
&b"\xc2\xc2\x9b31mZ"[..],
b"\xe0\xc2\x9d0;title\xc2\x9cZ",
b"\xc2\x1b]0;title\xc2\x9cZ",
] {
let toks = tokens(input);
assert_eq!(visible(&toks), "Z");
assert_eq!(rebuild(&toks), input);
}
}
}
#[cfg(test)]
mod fast_path {
use super::util::*;
use super::*;
#[test]
fn cached_runs_stop_before_controls_escapes_and_invalid_bytes() {
let before = "abä¸e\u{301}".as_bytes();
let after = "xy界".as_bytes();
for boundary in [
&b"\n"[..],
b"\x7f",
b"\x9c",
b"\xc2\x9c",
b"\xff",
b"\x1b[31m",
b"\x9b31m",
b"\xc2\x9b31m",
b"\xc2\x9d0;title\xc2\x9c",
b"\x1b]0;title\x07",
b"\x1bPpayload\x1b\\",
] {
let input = [before, boundary, after].concat();
for mode in [WidthMode::Wc, WidthMode::Grapheme] {
for eaw_wide in [false, true] {
let expected: Vec<_> = [before, boundary, after]
.into_iter()
.flat_map(|part| tokenize(part, mode, eaw_wide))
.collect();
let mut iter = tokenize(&input, mode, eaw_wide);
assert_eq!(iter.by_ref().collect::<Vec<_>>(), expected);
assert_eq!(iter.next(), None);
}
}
}
}
fn text_tokens_eaw(s: &str, mode: WidthMode, eaw_wide: bool) -> Vec<(String, u16)> {
tokenize(s.as_bytes(), mode, eaw_wide)
.filter(|t| t.kind == TokenKind::Text)
.map(|t| {
(
std::str::from_utf8(t.bytes)
.expect("a text token is whole UTF-8")
.to_owned(),
t.width,
)
})
.collect()
}
#[test]
fn the_ascii_shortcut_agrees_with_grapheme_segmentation() {
for mode in [WidthMode::Wc, WidthMode::Grapheme] {
for eaw_wide in [false, true] {
let toks = |s: &str| text_tokens_eaw(s, mode, eaw_wide);
assert_eq!(
toks("abc"),
vec![("a".into(), 1), ("b".into(), 1), ("c".into(), 1)],
"plain ASCII is one column per byte"
);
assert_eq!(
toks("e\u{301}f"),
vec![("e\u{301}".into(), 1), ("f".into(), 1)],
"a combining mark still joins the ASCII base before it"
);
assert_eq!(
toks("a"),
vec![("a".into(), 1)],
"the last byte of the input takes the shortcut too"
);
assert_eq!(
toks("1\u{fe0f}\u{20e3}z"),
vec![("1\u{fe0f}\u{20e3}".into(), 1), ("z".into(), 1)],
"a keycap keeps its ASCII digit"
);
assert_eq!(
toks("a\u{200d}b"),
vec![("a\u{200d}".into(), 1), ("b".into(), 1)],
"a ZWJ joins the ASCII base before it"
);
assert_eq!(
toks("a\u{4e00}b"),
vec![("a".into(), 1), ("\u{4e00}".into(), 2), ("b".into(), 1)],
"a wide character between two ASCII ones is still wide"
);
}
}
assert_eq!(
text_tokens_eaw("a\u{2018}b", WidthMode::Wc, false),
vec![("a".into(), 1), ("\u{2018}".into(), 1), ("b".into(), 1)]
);
assert_eq!(
text_tokens_eaw("a\u{2018}b", WidthMode::Wc, true),
vec![("a".into(), 1), ("\u{2018}".into(), 2), ("b".into(), 1)]
);
}
#[test]
fn controls_never_reach_the_ascii_shortcut() {
for b in 0u8..=0x7f {
let input = [b];
let toks: Vec<_> = tokenize(&input, WidthMode::Wc, false).collect();
match toks.as_slice() {
[
Token {
kind: TokenKind::Text,
bytes,
width,
control,
},
] => {
assert!(
(0x20..=0x7e).contains(&b),
"0x{b:02x} was emitted as text, but only printable ASCII may be"
);
assert_eq!(*bytes, &input[..]);
assert_eq!(*width, 1);
assert_eq!(*control, 0);
}
[token] if token.kind == TokenKind::Control => {
assert_eq!(*token, control_token(&input));
assert!(
b < 0x20 || b == 0x7f,
"0x{b:02x} is printable but was emitted as a control"
);
}
[token] if token.kind == TokenKind::Escape => {
assert_eq!(*token, escape_token(b"\x1b"));
}
other => panic!("0x{b:02x} produced {other:?}"),
}
}
}
#[test]
fn a_continuation_byte_does_not_terminate_a_string_sequence() {
const PREFIXES: &[&[u8]] = &[
b"\x1b]0;", b"\x1bP", b"\x1b_", b"\x1b^", b"\x1bX", b"\x1b", b"\x1b ", b"\x1b#",
];
for c in [
'\u{2705}',
'\u{2714}',
'\u{2728}',
'\u{171c}',
'\u{4e00}',
'\u{1f600}',
] {
for prefix in PREFIXES {
let mut input = prefix.to_vec();
input.extend_from_slice(c.to_string().as_bytes());
input.extend_from_slice(b"\x07after");
for t in tokenize(&input, WidthMode::Wc, false) {
if t.kind == TokenKind::Control {
continue;
}
let bytes = t.bytes;
assert!(
std::str::from_utf8(bytes).is_ok(),
"{input:x?} produced a token split mid-character: {bytes:x?}"
);
}
}
}
for c in ['\u{2705}', '\u{2714}', '\u{2728}', '\u{171c}'] {
let mut input = b"\x1b]0;Build ".to_vec();
input.extend_from_slice(c.to_string().as_bytes());
input.extend_from_slice(b"\x07after");
let toks: Vec<_> = tokenize(&input, WidthMode::Wc, false).collect();
let (escape, rest) = toks.split_first().expect("a token");
let mut want = b"\x1b]0;Build ".to_vec();
want.extend_from_slice(c.to_string().as_bytes());
want.push(0x07);
assert_eq!(*escape, escape_token(&want), "the sequence is whole");
assert_eq!(visible(rest), "after");
}
let toks: Vec<_> = tokenize(b"\x1b\xe2\x9c\x85x", WidthMode::Wc, false).collect();
assert_eq!(toks[0], escape_token(b"\x1b\xe2\x9c\x85"));
let toks: Vec<_> = tokenize(b"\x1b]0;t\x9cx", WidthMode::Wc, false).collect();
assert_eq!(toks[0], escape_token(b"\x1b]0;t\x9c"));
}
#[test]
fn a_malformed_byte_does_not_swallow_a_terminator() {
let cases: &[(&[u8], &[u8])] = &[
(b"\x1b]0;\xe0\x07Zz", b"\x1b]0;\xe0\x07"),
(b"\x1b]0;\xf0\x1b\\Zz", b"\x1b]0;\xf0\x1b\\"),
(b"\x1b]0;\xe0\x9cZz", b"\x1b]0;\xe0\x9c"),
(b"\x1b_G\xf0\x9f\x1b\\Zz", b"\x1b_G\xf0\x9f\x1b\\"),
(b"\x1b]0;\xc2\x07visible", b"\x1b]0;\xc2\x07"),
];
for (input, escape) in cases {
let toks: Vec<_> = tokenize(input, WidthMode::Wc, false).collect();
assert_eq!(
toks.first(),
Some(&escape_token(escape)),
"input {input:x?} did not stop at its terminator"
);
assert!(
!visible(&toks).is_empty(),
"input {input:x?} lost all its text"
);
}
let toks: Vec<_> = tokenize(b"\x1b\xe0\x1b[31mZ", WidthMode::Wc, false).collect();
assert_eq!(toks[0], escape_token(b"\x1b\xe0"));
assert_eq!(toks[1], escape_token(b"\x1b[31m"));
}
#[test]
fn arbitrary_bytes_never_panic_or_split_a_character() {
let pinned: &[(&[u8], &str)] = &[
(
b"\xe0\x20\x0d\xef\xb8\x8f",
"C[e0] T[20]w1 C[0d] T[ef, b8, 8f]w0",
),
(
b"\xf0\x30\x0d\x5b\xe2\x9c\x85\x30",
"C[f0] T[30]w1 C[0d] T[5b]w1 T[e2, 9c, 85]w2 T[30]w1",
),
(
b"\xe0\x41\x1b\x41\xc3\xa9",
"C[e0] T[41]w1 E[1b, 41] T[c3, a9]w1",
),
(b"\xcf\xc8\x9c", "C[cf] T[c8, 9c]w1"),
(
b"\xe3\x61\x0a\xc4\x81\xc4\x81\x78",
"C[e3] T[61]w1 C[0a] T[c4, 81]w1 T[c4, 81]w1 T[78]w1",
),
];
for (seed, expected) in pinned {
let rendered: Vec<String> = tokenize(seed, WidthMode::Grapheme, false)
.map(|t| match t.kind {
TokenKind::Text => format!("T{:x?}w{}", t.bytes, t.width),
TokenKind::Escape => format!("E{:x?}", t.bytes),
TokenKind::Control => format!("C{:02x?}", t.bytes),
})
.collect();
assert_eq!(rendered.join(" "), *expected, "input {seed:x?}");
string_width(seed, WidthMode::Grapheme, false);
string_width(seed, WidthMode::Wc, false);
}
let mut state = 0x2545_f491_4f6c_dd1du64;
let mut buf = [0u8; 24];
for _ in 0..200_000 {
let len = {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
(state % 24) as usize
};
for b in buf.iter_mut().take(len) {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
*b =
match state % 4 {
0 => (state >> 8) as u8,
1 => [0x1b, 0x9b, 0x9d, 0x90, 0x98, 0x9e, 0x9f, 0x07]
[(state >> 8) as usize % 8],
2 => [0xc2, 0xe0, 0xe2, 0xf0, 0xf4, 0xff, 0x9c, 0x80]
[(state >> 8) as usize % 8],
_ => b"abc \n\r\\;0"[(state >> 8) as usize % 9],
};
}
let input = &buf[..len];
for t in tokenize(input, WidthMode::Grapheme, false) {
assert!(!t.bytes.is_empty());
match t.kind {
TokenKind::Text => assert_eq!(t.control, 0),
TokenKind::Escape => {
assert_eq!(t.width, 0);
assert_eq!(t.control, 0);
}
TokenKind::Control => {
assert_eq!(t.width, 0);
assert_eq!(t.control, *t.bytes.last().unwrap());
if t.bytes.len() == 2 {
assert_eq!(t.bytes[0], 0xc2);
assert!((0x80..=0x9f).contains(&t.control));
} else {
assert_eq!(t.bytes.len(), 1);
}
}
}
if t.kind == TokenKind::Text {
assert!(
std::str::from_utf8(t.bytes).is_ok(),
"{input:x?} produced a split character {:x?}",
t.bytes
);
}
}
string_width(input, WidthMode::Grapheme, false);
}
}
}
#[cfg(test)]
mod scaling {
use super::util::*;
use super::*;
fn scan_cost(bytes: &[u8]) -> (usize, usize) {
let mut t = tokenize(bytes, WidthMode::Wc, false);
let toks: Vec<_> = (&mut t).collect();
let _ = visible(&toks);
assert_eq!(rebuild(&toks), bytes, "tokens did not reassemble the input");
(t.scanned, t.validated)
}
#[test]
fn utf8_c1_boundaries_keep_scans_linear() {
for unit in [
&b"\xe7\x95\x8c\xc2\x9b31m\xe7\x95\x8c\xc2\x9bm"[..],
b"\xe7\x95\x8c\xc2\x80\xe7\x95\x8c",
b"\xe7\x95\x8c\xc2\x9d0;title\xc2\x9c",
] {
for count in [1_000, 4_000, 16_000] {
let input = unit.repeat(count);
let (scanned, validated) = scan_cost(&input);
assert!(scanned <= input.len());
assert!(validated <= input.len());
}
}
}
#[test]
fn tokenizing_one_long_line_is_linear_in_its_length() {
for n in [1_000usize, 4_000, 16_000, 64_000] {
let line: String = std::iter::repeat_n('x', n).collect();
assert_eq!(
scan_cost(line.as_bytes()),
(0, 0),
"printable ASCII needs neither a run scan nor UTF-8 validation"
);
}
}
#[test]
fn tokenizing_a_long_non_ascii_line_is_linear_too() {
for unit in ["\u{e9}", "\u{4e00}", "\u{1f600}", "e\u{301}"] {
for n in [1_000usize, 4_000, 16_000, 64_000] {
let line = unit.repeat(n);
let len = line.len();
assert_eq!(
scan_cost(line.as_bytes()),
(len, len),
"{unit:?} x{n}: the run is found once and validated once"
);
}
}
}
#[test]
fn malformed_utf8_is_linear_too() {
for n in [1_000usize, 4_000, 16_000, 64_000] {
let bytes: Vec<u8> = std::iter::repeat_n([0xc2u8, 0x41], n / 2)
.flatten()
.collect();
let (scanned, validated) = scan_cost(&bytes);
assert!(
scanned <= 2 * n,
"scanning {n} malformed bytes visited {scanned} of them"
);
assert!(
validated <= 2 * n,
"validating {n} malformed bytes submitted {validated} of them"
);
}
}
}