use super::decode_rune;
const RANGES: [u32; 1302] = include!("grapheme_table.in");
mod prop {
pub(super) const ANY: u8 = 0;
pub(super) const PREPEND: u8 = 1;
pub(super) const CR: u8 = 2;
pub(super) const LF: u8 = 3;
pub(super) const CONTROL: u8 = 4;
pub(super) const EXTEND: u8 = 5;
pub(super) const REGIONAL_INDICATOR: u8 = 6;
pub(super) const SPACING_MARK: u8 = 7;
pub(super) const L: u8 = 8;
pub(super) const V: u8 = 9;
pub(super) const T: u8 = 10;
pub(super) const LV: u8 = 11;
pub(super) const LVT: u8 = 12;
pub(super) const ZWJ: u8 = 13;
pub(super) const PICTOGRAPHIC: u8 = 14;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
Any,
Cr,
ControlLf,
L,
Lvv,
Lvtt,
Prepend,
Pictographic,
PictographicZwj,
RiOdd,
RiEven,
}
type Transition = (State, u8, State, bool, u16);
const TRANSITIONS: [Transition; 30] = {
use State::{
Any, ControlLf, Cr, L, Lvtt, Lvv, Pictographic, PictographicZwj, Prepend, RiEven, RiOdd,
};
use prop as p;
[
(Any, p::CR, Cr, true, 50),
(Any, p::LF, ControlLf, true, 50),
(Any, p::CONTROL, ControlLf, true, 50),
(Cr, p::ANY, Any, true, 40),
(ControlLf, p::ANY, Any, true, 40),
(Cr, p::LF, ControlLf, false, 30),
(Any, p::L, L, true, 9990),
(L, p::L, L, false, 60),
(L, p::V, Lvv, false, 60),
(L, p::LV, Lvv, false, 60),
(L, p::LVT, Lvtt, false, 60),
(Any, p::LV, Lvv, true, 9990),
(Any, p::V, Lvv, true, 9990),
(Lvv, p::V, Lvv, false, 70),
(Lvv, p::T, Lvtt, false, 70),
(Any, p::LVT, Lvtt, true, 9990),
(Any, p::T, Lvtt, true, 9990),
(Lvtt, p::T, Lvtt, false, 80),
(Any, p::EXTEND, Any, false, 90),
(Any, p::ZWJ, Any, false, 90),
(Any, p::SPACING_MARK, Any, false, 91),
(Any, p::PREPEND, Prepend, true, 9990),
(Prepend, p::ANY, Any, false, 92),
(Any, p::PICTOGRAPHIC, Pictographic, true, 9990),
(Pictographic, p::EXTEND, Pictographic, false, 110),
(Pictographic, p::ZWJ, PictographicZwj, false, 110),
(PictographicZwj, p::PICTOGRAPHIC, Pictographic, false, 110),
(Any, p::REGIONAL_INDICATOR, RiOdd, true, 9990),
(RiOdd, p::REGIONAL_INDICATOR, RiEven, false, 120),
(RiEven, p::REGIONAL_INDICATOR, RiOdd, true, 120),
]
};
fn find(state: State, property: u8) -> Option<(State, bool, u16)> {
TRANSITIONS
.iter()
.find(|t| t.0 == state && t.1 == property)
.map(|t| (t.2, t.3, t.4))
}
fn transition(state: State, property: u8) -> (State, bool) {
if let Some((next, boundary, _)) = find(state, property) {
return (next, boundary);
}
match (find(state, prop::ANY), find(State::Any, property)) {
(Some(any_prop), Some(any_state)) => {
let boundary = if any_prop.2 < any_state.2 {
any_prop.1
} else {
any_state.1
};
(any_state.0, boundary)
}
(Some((next, boundary, _)), None) | (None, Some((next, boundary, _))) => (next, boundary),
(None, None) => (State::Any, true),
}
}
fn property(c: char) -> u8 {
let r = u32::from(c);
if (0xAC00..=0xD7A3).contains(&r) {
return if (r - 0xAC00) % 28 == 0 {
prop::LV
} else {
prop::LVT
};
}
let ranges = RANGES.len() / 2;
let (mut from, mut to) = (0, ranges);
while from < to {
let middle = usize::midpoint(from, to);
let (first, last) = (RANGES[middle * 2], RANGES[middle * 2 + 1]);
if r < first & 0xFF_FFFF {
to = middle;
} else if r > last {
from = middle + 1;
} else {
return u8::try_from(first >> 24).unwrap_or(prop::ANY);
}
}
prop::ANY
}
pub(crate) fn first_cluster(bytes: &[u8]) -> usize {
let (first, mut length) = decode_rune(bytes);
if bytes.len() <= length {
return bytes.len();
}
let mut state = transition(State::Any, property(first)).0;
loop {
let (c, width) = decode_rune(&bytes[length..]);
let (next, boundary) = transition(state, property(c));
if boundary {
return length;
}
state = next;
length += width;
if bytes.len() <= length {
return bytes.len();
}
}
}