pub fn is_combining_mark(ch: char) -> bool {
let c = ch as u32;
matches!(c,
0x0300..=0x036F | 0x0483..=0x0489 | 0x0591..=0x05BD | 0x0610..=0x061A | 0x064B..=0x065F | 0x0670
| 0x06D6..=0x06DC
| 0x06DF..=0x06E4
| 0x06E7..=0x06E8
| 0x06EA..=0x06ED
| 0x0900..=0x0903 | 0x093A..=0x094F | 0x0951..=0x0957
| 0x0962..=0x0963
| 0x0E31 | 0x0E34..=0x0E3A | 0x0E47..=0x0E4E
| 0x1AB0..=0x1AFF | 0x20D0..=0x20F0 | 0xFE00..=0xFE0F | 0xFE20..=0xFE2F ) || ch == '\u{200D}' || ch == '\u{200C}' }
pub fn cluster_boundaries(text: &str) -> Vec<usize> {
let mut out = vec![0usize];
let mut prev_was_zwj = false;
for (i, ch) in text.char_indices() {
if i > 0 && !is_combining_mark(ch) && !prev_was_zwj {
out.push(i);
}
prev_was_zwj = ch == '\u{200D}';
}
out.push(text.len());
out.dedup();
out
}
pub fn snap(text: &str, byte: usize) -> usize {
let byte = byte.min(text.len());
if byte == 0 || byte >= text.len() {
return byte.min(text.len());
}
let bounds = cluster_boundaries(text);
match bounds.binary_search(&byte) {
Ok(_) => byte,
Err(idx) => bounds.get(idx.saturating_sub(1)).copied().unwrap_or(0),
}
}
pub fn prev_boundary(text: &str, byte: usize) -> Option<usize> {
let byte = snap(text, byte);
let bounds = cluster_boundaries(text);
bounds.iter().rev().copied().find(|b| *b < byte)
}
pub fn next_boundary(text: &str, byte: usize) -> Option<usize> {
let byte = snap(text, byte);
let bounds = cluster_boundaries(text);
bounds.iter().copied().find(|b| *b > byte)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ascii_has_one_boundary_per_char_plus_ends() {
assert_eq!(cluster_boundaries("abc"), vec![0, 1, 2, 3]);
}
#[test]
fn arabic_letters_are_individual_clusters() {
let text = "مرحبا";
let b = cluster_boundaries(text);
assert_eq!(b.len(), 6, "5 个字母 → 6 个边界,实得 {b:?}");
}
#[test]
fn devanagari_vowel_sign_stays_with_its_consonant() {
let text = "कि";
let b = cluster_boundaries(text);
assert_eq!(b, vec![0, text.len()], "实得 {b:?}");
}
#[test]
fn combining_accent_attaches_to_the_letter() {
let text = "e\u{0301}x";
let b = cluster_boundaries(text);
assert_eq!(b, vec![0, 3, 4], "e+重音应同群,x 单群;实得 {b:?}");
}
#[test]
fn snap_never_lands_inside_a_cluster() {
let text = "कि";
for i in 0..=text.len() {
let s = snap(text, i);
assert!(s == 0 || s == text.len(), "snap({i}) = {s}");
}
}
#[test]
fn snap_clamps_out_of_range_input() {
assert_eq!(snap("abc", 99), 3);
assert_eq!(snap("abc", 0), 0);
assert_eq!(snap("", 5), 0);
}
#[test]
fn prev_and_next_boundary_walk_over_whole_clusters() {
let text = "e\u{0301}x";
assert_eq!(
prev_boundary(text, 3),
Some(0),
"从 x 往左应跨过整个 e+重音"
);
assert_eq!(
next_boundary(text, 0),
Some(3),
"从 0 往右应跨过整个 e+重音"
);
assert_eq!(prev_boundary(text, 0), None);
assert_eq!(next_boundary(text, text.len()), None);
}
#[test]
fn empty_text_has_one_boundary_and_no_movement() {
assert_eq!(cluster_boundaries(""), vec![0]);
assert_eq!(prev_boundary("", 0), None);
assert_eq!(next_boundary("", 0), None);
}
#[test]
fn orbiting_spaces_create_boundaries() {
let b = cluster_boundaries("ab cd");
assert!(b.contains(&3), "空格处应成界:{b:?}");
}
}