pub fn byte_col_to_char_col(line: &str, byte_col: usize) -> usize {
let safe = (0..=byte_col.min(line.len()))
.rev()
.find(|&i| line.is_char_boundary(i))
.unwrap_or(0);
line[..safe].chars().count()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ascii() {
assert_eq!(byte_col_to_char_col("hello", 3), 3);
}
#[test]
fn multibyte_after_char() {
assert_eq!(byte_col_to_char_col("wørld", 3), 2);
}
#[test]
fn snaps_mid_codepoint() {
assert_eq!(byte_col_to_char_col("wørld", 2), 1);
}
#[test]
fn clamps_past_end() {
assert_eq!(byte_col_to_char_col("ab", 99), 2);
}
#[test]
fn zero() {
assert_eq!(byte_col_to_char_col("ab", 0), 0);
}
}