mcut 0.1.0

Zero-copy, low-memory multilingual word segmenter using pointwise linear classification and minimal FST
Documentation
use core::str::CharIndices;

/// Deterministic Thai Character Cluster (TCC) boundary finder based on
/// Theeramunkong et al. (2000).
///
/// Always advances by at least one UTF-8 character from `start` so callers
/// make guaranteed progress even on malformed or orphan combining marks.
pub fn next_tcc_end(text: &str, start: usize) -> usize {
  if start >= text.len() {
    return text.len();
  }
  let mut iter = text[start..].char_indices();
  let Some((_, first)) = iter.next() else {
    return text.len();
  };

  let mut end = start + first.len_utf8();
  let mut has_leading_e = first == '\u{0E40}';

  if is_leading_vowel(first) {
    let Some((rel, next_ch)) = iter.clone().next() else {
      return end;
    };
    if is_consonant(next_ch) {
      let _ = iter.next();
      end = start + rel + next_ch.len_utf8();
    } else {
      return end;
    }
  } else if is_consonant(first) {
    has_leading_e = false;
  } else {
    return end;
  }

  let mut has_han_akat = false;
  let mut has_sara_ii = false;
  let mut has_sara_uee = false;
  let mut has_trailing_vowel = false;

  // Absorb above/below dependent vowels, tone marks and thanthakhat.
  while let Some((rel, ch)) = iter.clone().next() {
    if is_combining_mark(ch) {
      let _ = iter.next();
      end = start + rel + ch.len_utf8();
      if ch == '\u{0E31}' {
        has_han_akat = true;
      } else if ch == '\u{0E35}' {
        has_sara_ii = true;
      } else if ch == '\u{0E37}' {
        has_sara_uee = true;
      }
    } else {
      break;
    }
  }

  // Handle vowel-frame patterns starting with `เ` (U+0E40) or `C + ั + C2`.
  if has_leading_e && let Some((rel, c2)) = iter.clone().next() {
    if c2 == '\u{0E2D}' {
      let mut probe = iter.clone();
      let _ = probe.next();
      if has_sara_uee {
        // `เ + C + ื + อ`
        let _ = iter.next();
        end = start + rel + c2.len_utf8();
        absorb_mark(&mut iter, start, &mut end);
        return end;
      }
      if let Some((r3, c3)) = probe.next()
        && c3 == '\u{0E30}'
      {
        // `เ + C + อ + ะ`
        let _ = iter.next();
        let _ = iter.next();
        end = start + r3 + c3.len_utf8();
        return end;
      }
    } else if has_sara_ii && c2 == '\u{0E22}' {
      // `เ + C + ี + ย` (when `ย` has no dependent vowel of its own)
      let mut probe = iter.clone();
      let _ = probe.next();
      if probe
        .next()
        .is_none_or(|(_, after)| !is_combining_mark(after) && !is_trailing_vowel(after))
      {
        let _ = iter.next();
        end = start + rel + c2.len_utf8();
        return end;
      }
    } else if c2 == '\u{0E32}' {
      let mut probe = iter.clone();
      let _ = probe.next();
      if let Some((r3, c3)) = probe.next()
        && c3 == '\u{0E30}'
      {
        // `เ + C + า + ะ`
        let _ = iter.next();
        let _ = iter.next();
        end = start + r3 + c3.len_utf8();
        return end;
      }
    }
  }

  // Absorb trailing Sara A / Aa / Am / Lakkhangyao plus optional tone/thanthakhat.
  if let Some((rel, ch)) = iter.clone().next()
    && is_trailing_vowel(ch)
  {
    let _ = iter.next();
    end = start + rel + ch.len_utf8();
    has_trailing_vowel = true;
    absorb_mark(&mut iter, start, &mut end);
    if ch == '\u{0E32}'
      && let Some((r2, c2)) = iter.clone().next()
      && c2 == '\u{0E30}'
    {
      let _ = iter.next();
      end = start + r2 + c2.len_utf8();
    }
  }

  // `C + ั` (Mai Han-Akat) always binds a following final consonant (unless followed
  // by a trailing vowel like `า`/`ะ` or a consonant carrying its own vowel).
  if has_han_akat
    && !has_trailing_vowel
    && let Some((rel, c2)) = iter.clone().next()
    && is_consonant(c2)
  {
    let mut probe = iter.clone();
    let _ = probe.next();
    let has_own_vowel = probe
      .next()
      .is_some_and(|(_, after)| is_dependent_vowel(after) || is_trailing_vowel(after));
    if !has_own_vowel {
      let _ = iter.next();
      end = start + rel + c2.len_utf8();
      absorb_mark(&mut iter, start, &mut end);
    }
  }

  end
}

fn absorb_mark(iter: &mut CharIndices<'_>, start: usize, end: &mut usize) {
  while let Some((rel, ch)) = iter.clone().next() {
    if is_tone_or_thanthakhat(ch) {
      let _ = iter.next();
      *end = start + rel + ch.len_utf8();
    } else {
      break;
    }
  }
}

#[inline]
const fn is_consonant(ch: char) -> bool {
  matches!(ch as u32, 0x0E01..=0x0E2E)
}

#[inline]
const fn is_leading_vowel(ch: char) -> bool {
  matches!(ch as u32, 0x0E40..=0x0E44)
}

#[inline]
const fn is_dependent_vowel(ch: char) -> bool {
  matches!(ch as u32, 0x0E31 | 0x0E34..=0x0E3A | 0x0E47)
}

#[inline]
const fn is_tone_or_thanthakhat(ch: char) -> bool {
  matches!(ch as u32, 0x0E48..=0x0E4E)
}

#[inline]
const fn is_combining_mark(ch: char) -> bool {
  is_dependent_vowel(ch) || is_tone_or_thanthakhat(ch)
}

#[inline]
const fn is_trailing_vowel(ch: char) -> bool {
  matches!(ch as u32, 0x0E30 | 0x0E32 | 0x0E33 | 0x0E45)
}