ewts 1.0.0

Converter between EWTS (Extended Wylie Transliteration Scheme) and Tibetan Unicode symbols (lib)
Documentation
use crate::dict::{Con, PREFIXES, ROOT_FIRST, SUBSCRIPTS, SUFFIXES, SUFFIXES2, SUPERSCRIPTS, Syntax};

// "rk", "ky": a standard stack of two letters.
pub(crate) fn is_standard_pair(top: Con, bottom: Con) -> bool {
    SUPERSCRIPTS
        .iter()
        .any(|&(sup, lower, _)| sup == top && lower.contains(&bottom))
        || SUBSCRIPTS
            .iter()
            .any(|&(sub, upper, _)| sub == bottom && upper.contains(&top))
}

// "rky", "skr": a standard stack of three letters.
pub(crate) fn is_standard_triple(top: Con, middle: Con, bottom: Con) -> bool {
    SUPERSCRIPTS
        .iter()
        .any(|&(sup, _, two)| sup == top && two.contains(&(middle, bottom)))
        || SUBSCRIPTS
            .iter()
            .any(|&(sub, _, two)| sub == bottom && two.contains(&(top, middle)))
}

// The bottom letter of some standard stack.
pub(crate) fn is_standard_bottom(con: Con) -> bool {
    SUBSCRIPTS.iter().any(|&(sub, _, _)| sub == con)
        || SUPERSCRIPTS
            .iter()
            .any(|&(_, lower, two)| lower.contains(&con) || two.iter().any(|&(_, l)| l == con))
}

#[inline]
pub(crate) fn is_suffix(letter: Con) -> bool {
    SUFFIXES.contains(&letter)
}

#[inline]
pub(crate) fn can_suffix2(suffix2: Con, suffix: Con) -> bool {
    SUFFIXES2
        .iter()
        .any(|&(s2, after)| s2 == suffix2 && after.contains(&suffix))
}

// wa-zur is not counted in the root for the prefix check
#[inline]
pub(crate) fn can_prefix(prefix: Con, root: &[Con]) -> bool {
    let root = || {
        (root.iter().enumerate())
            .filter(|&(i, c)| i == 0 || *c != Con::W)
            .map(|(_, c)| *c)
    };
    PREFIXES
        .iter()
        .any(|&(p, roots)| p == prefix && roots.iter().any(|r| r.iter().copied().eq(root())))
}

#[inline]
pub(crate) fn is_root_first(letters: &[Con]) -> bool {
    ROOT_FIRST.iter().any(|word| word[..] == *letters)
}

// "u0f40" or "U00000f40" after a backslash: the char and the length of the escape without the backslash.
pub(crate) fn unicode_escape(rest: &[u8]) -> Option<(char, usize)> {
    let digits = Syntax::unicode_escape_digits(*rest.first()?)?;
    // Hex digits only: `from_str_radix` would also take a sign ("+f40").
    let mut code = 0;
    for &b in rest.get(1..=digits)? {
        code = code * 16 + char::from(b).to_digit(16)?;
    }
    Some((char::from_u32(code)?, 1 + digits))
}