petitcarnets 0.0.10

Terminal user interfaces in the Elm architecture (model, update, view), with no dependencies.
Documentation
//! The first grapheme cluster of some bytes, split as `rivo/uniseg`
//! v0.4.7 splits it (Unicode 15.0.0 rules, state machine included), so a
//! typed emoji or accented letter is one key, as in Bubble Tea.

use super::decode_rune;

/// Grapheme break properties in code point order, two words a range:
/// the first code point with the property in the top byte, then the
/// last code point. Generated by scripts/gen-grapheme-table.py.
const RANGES: [u32; 1302] = include!("grapheme_table.in");

/// Grapheme break properties, numbered as in the generated table.
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;
}

/// Parser states.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    Any,
    Cr,
    ControlLf,
    L,
    Lvv,
    Lvtt,
    Prepend,
    Pictographic,
    PictographicZwj,
    RiOdd,
    RiEven,
}

/// A transition: from a state on a property, to a state, with or
/// without a boundary before the code point, and the rule's number.
type Transition = (State, u8, State, bool, u16);

/// uniseg's `grTransitions`, row for row.
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),
    ]
};

/// The row for exactly `state` and `property`.
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))
}

/// uniseg's `transitionGraphemeState`: the next state, and whether there
/// is a boundary before a code point with `property`.
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),
    }
}

/// The grapheme break property of `c`.
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
}

/// The length of the first grapheme cluster in `bytes`, uniseg's
/// `FirstGraphemeCluster`: a cluster that runs to the end of `bytes` ends
/// there. `bytes` must not be empty.
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();
        }
    }
}