use crate::style::width_method;
use crate::unicode::first_width_cluster;
pub(super) fn clusters(s: &str) -> Vec<(&str, usize)> {
let mut out = Vec::new();
let mut rest = s;
while !rest.is_empty() {
let (cluster, w) = first_width_cluster(rest, width_method());
let len = cluster
.len()
.max(rest.chars().next().map_or(1, char::len_utf8));
out.push((&rest[..len], w));
rest = &rest[len..];
}
out
}
pub(super) fn grapheme_at(line: &[char], col: usize) -> (usize, usize) {
if col >= line.len() {
return (col, col);
}
let text: String = line.iter().collect();
let mut start = 0;
for (cluster, _) in clusters(&text) {
let end = start + cluster.chars().count();
if col < end {
return (start, end);
}
start = end;
}
(col, col + 1)
}
pub(super) fn is_space(c: char) -> bool {
c.is_whitespace()
}
pub(super) fn sanitize(chars: &[char], newline: &str, tab: &str) -> Vec<char> {
let mut out = Vec::with_capacity(chars.len());
for &c in chars {
match c {
'\u{FFFD}' => {}
'\r' | '\n' => out.extend(newline.chars()),
'\t' => out.extend(tab.chars()),
c if c.is_control() => {}
c => out.push(c),
}
}
out
}
#[cfg(test)]
mod tests;