use unicode_segmentation::UnicodeSegmentation;
pub fn grapheme_byte_offsets(text: &str) -> Vec<usize> {
let mut offsets: Vec<usize> = text.grapheme_indices(true).map(|(i, _)| i).collect();
offsets.push(text.len());
offsets
}
pub fn grapheme_col_to_byte(text: &str, col: usize) -> usize {
let offsets = grapheme_byte_offsets(text);
offsets.get(col).copied().unwrap_or(text.len())
}
pub fn byte_to_grapheme_col(text: &str, byte: usize) -> usize {
let offsets = grapheme_byte_offsets(text);
let byte = byte.min(text.len());
match offsets.binary_search(&byte) {
Ok(col) => col,
Err(next) => next.saturating_sub(1),
}
}
pub fn floor_grapheme_boundary(text: &str, byte: usize) -> usize {
grapheme_col_to_byte(text, byte_to_grapheme_col(text, byte))
}
pub fn prev_grapheme_boundary(text: &str, byte: usize) -> usize {
let offsets = grapheme_byte_offsets(text);
let byte = byte.min(text.len());
match offsets.binary_search(&byte) {
Ok(0) | Err(0) => 0,
Ok(col) => offsets[col - 1],
Err(next) => offsets[next - 1],
}
}
pub fn next_grapheme_boundary(text: &str, byte: usize) -> usize {
let offsets = grapheme_byte_offsets(text);
let byte = byte.min(text.len());
match offsets.binary_search(&byte) {
Ok(col) => offsets.get(col + 1).copied().unwrap_or(text.len()),
Err(next) => offsets.get(next).copied().unwrap_or(text.len()),
}
}
pub fn grapheme_count(text: &str) -> usize {
text.graphemes(true).count()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ascii_is_unchanged() {
let t = "hello";
assert_eq!(grapheme_count(t), 5);
assert_eq!(grapheme_col_to_byte(t, 2), 2);
assert_eq!(byte_to_grapheme_col(t, 3), 3);
assert_eq!(prev_grapheme_boundary(t, 3), 2);
assert_eq!(next_grapheme_boundary(t, 3), 4);
}
#[test]
fn emoji_zwj_is_one_grapheme() {
let family = "👨👩👧👦";
let t = format!("A{family}B");
let offsets = grapheme_byte_offsets(&t);
assert_eq!(offsets.len(), 4, "A + family + B + end = 4 个边界");
assert_eq!(offsets[2] - offsets[1], family.len(), "ZWJ 序列整体是一个簇");
assert_eq!(grapheme_count(&t), 3);
let middle = offsets[1] + 2;
assert_eq!(prev_grapheme_boundary(&t, middle), offsets[1]);
assert_eq!(next_grapheme_boundary(&t, middle), offsets[2]);
}
#[test]
fn combining_mark_stays_with_base() {
let t = "e\u{301}x"; assert_eq!(grapheme_count(t), 2);
assert_eq!(grapheme_col_to_byte(t, 1), "e\u{301}".len());
assert_eq!(byte_to_grapheme_col(t, 1), 0, "落在组合记号中间向下取整到基字");
}
#[test]
fn out_of_range_is_clamped() {
let t = "你好";
assert_eq!(grapheme_col_to_byte(t, 99), t.len());
assert_eq!(byte_to_grapheme_col(t, 99), 2);
assert_eq!(prev_grapheme_boundary(t, 0), 0);
assert_eq!(next_grapheme_boundary(t, t.len()), t.len());
}
#[test]
fn empty_text_has_single_boundary() {
assert_eq!(grapheme_byte_offsets(""), vec![0]);
assert_eq!(grapheme_count(""), 0);
assert_eq!(grapheme_col_to_byte("", 3), 0);
}
#[test]
fn floor_boundary_keeps_end_position() {
let t = "e\u{301}x";
assert_eq!(floor_grapheme_boundary(t, 1), 0, "组合记号中间向下取整到基字");
assert_eq!(floor_grapheme_boundary(t, t.len()), t.len(), "末尾不应回退到最后一个簇起点");
assert_eq!(floor_grapheme_boundary(t, 99), t.len());
}
}