#![allow(clippy::cast_possible_truncation)]
use core::cmp::Ordering;
use crate::spec::OracleUcaVersion;
use super::table_uca0700;
use super::table_uca1210;
#[derive(Debug, Clone, Copy)]
pub(super) struct Ce {
primary: u16,
secondary: u16,
tertiary: u16,
variable: bool,
}
impl Ce {
const fn unpack(packed: u64) -> Self {
Self {
primary: ((packed >> 32) & 0xFFFF) as u16,
secondary: ((packed >> 16) & 0xFFFF) as u16,
tertiary: (packed & 0xFFFF) as u16,
variable: (packed >> 48) & 1 != 0,
}
}
}
const MAX_CONTRACTION_LEN: usize = 3;
pub(super) struct UcaTable {
pub(super) ces: &'static [u64],
pub(super) singles: &'static [(u32, u32, u8)],
pub(super) contractions: &'static [(u8, &'static [u32], u32, u8)],
pub(super) implicit: &'static [(u32, u32, u16)],
pub(super) unified_ideograph: &'static [(u32, u32, bool)],
pub(super) decomp: &'static [(u32, u32, u8)],
pub(super) decomp_blob: &'static [u32],
pub(super) ccc: &'static [(u32, u8)],
}
impl UcaTable {
fn decompose(&self, cp: u32) -> Option<&[u32]> {
self.decomp
.binary_search_by_key(&cp, |&(c, _, _)| c)
.ok()
.map(|i| {
let (_, off, len) = self.decomp[i];
&self.decomp_blob[off as usize..off as usize + len as usize]
})
}
fn combining_class(&self, cp: u32) -> u8 {
self.ccc
.binary_search_by_key(&cp, |&(c, _)| c)
.map_or(0, |i| self.ccc[i].1)
}
fn nfd(&self, s: &str) -> Vec<u32> {
let mut out: Vec<u32> = Vec::with_capacity(s.len());
for ch in s.chars() {
self.decompose_into(ch as u32, &mut out);
}
self.canonical_order(&mut out);
out
}
fn decompose_into(&self, cp: u32, out: &mut Vec<u32>) {
if (0xAC00..=0xD7A3).contains(&cp) {
const S_BASE: u32 = 0xAC00;
const L_BASE: u32 = 0x1100;
const V_BASE: u32 = 0x1161;
const T_BASE: u32 = 0x11A7;
const T_COUNT: u32 = 28;
const N_COUNT: u32 = 21 * T_COUNT;
let s_index = cp - S_BASE;
let l = L_BASE + s_index / N_COUNT;
let v = V_BASE + (s_index % N_COUNT) / T_COUNT;
let t = T_BASE + s_index % T_COUNT;
out.push(l);
out.push(v);
if s_index % T_COUNT != 0 {
out.push(t);
}
return;
}
if let Some(parts) = self.decompose(cp) {
for &p in parts {
self.decompose_into(p, out);
}
} else {
out.push(cp);
}
}
fn canonical_order(&self, cps: &mut [u32]) {
let n = cps.len();
let mut i = 0;
while i < n {
if self.combining_class(cps[i]) == 0 {
i += 1;
continue;
}
let start = i;
let mut end = i;
while end < n && self.combining_class(cps[end]) != 0 {
end += 1;
}
for a in (start + 1)..end {
let mut b = a;
while b > start && self.combining_class(cps[b - 1]) > self.combining_class(cps[b]) {
cps.swap(b - 1, b);
b -= 1;
}
}
i = end;
}
}
fn ces_at(&self, offset: u32, count: u8) -> &[u64] {
&self.ces[offset as usize..offset as usize + count as usize]
}
fn single(&self, cp: u32) -> Option<&[u64]> {
self.singles
.binary_search_by_key(&cp, |&(c, _, _)| c)
.ok()
.map(|i| {
let (_, off, cnt) = self.singles[i];
self.ces_at(off, cnt)
})
}
fn contraction_for(&self, key: &[u32]) -> Option<&[u64]> {
self.contractions
.binary_search_by(|(cl, k, _, _)| (*cl as usize, *k).cmp(&(key.len(), key)))
.ok()
.map(|j| {
let (_, _, off, cnt) = self.contractions[j];
self.ces_at(off, cnt)
})
}
fn implicit(&self, cp: u32) -> [u64; 2] {
for &(lo, hi, base) in self.implicit {
if (lo..=hi).contains(&cp) {
return implicit_pair(base, ((cp - lo) as u16) | 0x8000);
}
}
let mut han = None;
for &(lo, hi, core) in self.unified_ideograph {
if (lo..=hi).contains(&cp) {
han = Some(core);
break;
}
}
let (aaaa, bbbb) = match han {
None => (0xFBC0 + (cp >> 15) as u16, ((cp & 0x7FFF) | 0x8000) as u16),
Some(true) => (0xFB40 + (cp >> 15) as u16, ((cp & 0x7FFF) | 0x8000) as u16),
Some(false) => (0xFB80 + (cp >> 15) as u16, ((cp & 0x7FFF) | 0x8000) as u16),
};
implicit_pair(aaaa, bbbb)
}
fn element_array(&self, cps: &[u32]) -> Vec<Ce> {
let mut out = Vec::with_capacity(cps.len() + 2);
let mut work: Vec<u32> = cps.to_vec();
let mut cursor = 0usize;
while cursor < work.len() {
let rest = &work[cursor..];
let mut key: Vec<u32>;
let mut base: Option<&[u64]>;
let contig_len;
if let Some((len, els)) = self.longest_contraction(rest) {
key = rest[..len].to_vec();
base = Some(els);
contig_len = len;
} else if let Some(els) = self.single(rest[0]) {
key = vec![rest[0]];
base = Some(els);
contig_len = 1;
} else {
out.extend(self.implicit(rest[0]).iter().map(|&p| Ce::unpack(p)));
cursor += 1;
continue;
}
let mut scan = cursor + contig_len;
let mut max_skipped_ccc = 0u8;
while scan < work.len() {
let c = work[scan];
let ccc_c = self.combining_class(c);
if ccc_c == 0 {
break;
}
if ccc_c <= max_skipped_ccc {
scan += 1;
continue;
}
key.push(c);
if let Some(els) = self.contraction_for(&key) {
base = Some(els);
work.remove(scan);
} else {
key.pop();
max_skipped_ccc = max_skipped_ccc.max(ccc_c);
scan += 1;
}
}
out.extend(base.unwrap().iter().map(|&p| Ce::unpack(p)));
cursor += contig_len;
}
out
}
fn longest_contraction(&self, cps: &[u32]) -> Option<(usize, &[u64])> {
for len in (2..=MAX_CONTRACTION_LEN).rev() {
if len > cps.len() {
continue;
}
if let Some(els) = self.contraction_for(&cps[..len]) {
return Some((len, els));
}
}
None
}
}
fn pack(primary: u16, secondary: u16, tertiary: u16, variable: bool) -> u64 {
(u64::from(variable) << 48)
| (u64::from(primary) << 32)
| (u64::from(secondary) << 16)
| u64::from(tertiary)
}
fn implicit_pair(aaaa: u16, bbbb: u16) -> [u64; 2] {
[pack(aaaa, 0x0020, 0x0002, false), pack(bbbb, 0, 0, false)]
}
fn table_for(version: OracleUcaVersion) -> &'static UcaTable {
match version {
OracleUcaVersion::Uca700 => &table_uca0700::TABLE,
OracleUcaVersion::Uca1210 => &table_uca1210::TABLE,
}
}
pub(super) fn compare(version: OracleUcaVersion, a: &str, b: &str) -> Option<Ordering> {
let table = table_for(version);
let left = table.element_array(&table.nfd(a));
let right = table.element_array(&table.nfd(b));
if exceeds_key_limit(left.len()) || exceeds_key_limit(right.len()) {
return None;
}
for level in 1u8..=4 {
match compare_level(&left, &right, level) {
Ordering::Equal => {}
other => return Some(other),
}
}
Some(Ordering::Equal)
}
const ORACLE_MAX_KEY: usize = 2000;
const MAX_BYTES_PER_CE: usize = 8;
const KEY_OVERHEAD: usize = 8;
fn exceeds_key_limit(count: usize) -> bool {
let threshold = (ORACLE_MAX_KEY - KEY_OVERHEAD) / MAX_BYTES_PER_CE;
count > threshold
}
fn compare_level(a_ces: &[Ce], b_ces: &[Ce], level: u8) -> Ordering {
let mut wa = WeightIter::new(a_ces, level);
let mut wb = WeightIter::new(b_ces, level);
loop {
match (wa.next_weight(), wb.next_weight()) {
(None, None) => return Ordering::Equal,
(None, Some(_)) => return Ordering::Less,
(Some(_), None) => return Ordering::Greater,
(Some(x), Some(y)) => match x.cmp(&y) {
Ordering::Equal => {}
other => return other,
},
}
}
}
struct WeightIter<'a> {
ces: &'a [Ce],
idx: usize,
level: u8,
after_variable: bool,
}
impl<'a> WeightIter<'a> {
fn new(ces: &'a [Ce], level: u8) -> Self {
Self {
ces,
idx: 0,
level,
after_variable: false,
}
}
fn weight_of(&self, ce: Ce, after_variable: bool) -> Option<u16> {
if after_variable && ce.primary == 0 {
return None;
}
match self.level {
1 => (!ce.variable && ce.primary != 0).then_some(ce.primary),
2 => (!ce.variable && ce.secondary != 0).then_some(ce.secondary),
3 => (!ce.variable && ce.tertiary != 0).then_some(ce.tertiary),
_ => {
if ce.variable {
(ce.primary != 0).then_some(ce.primary)
} else if ce.primary != 0 || ce.secondary != 0 || ce.tertiary != 0 {
Some(0xFFFF)
} else {
None
}
}
}
}
fn next_weight(&mut self) -> Option<u16> {
loop {
let ce = *self.ces.get(self.idx)?;
self.idx += 1;
let w = self.weight_of(ce, self.after_variable);
self.after_variable = ce.variable || (self.after_variable && ce.primary == 0);
if let Some(w) = w {
return Some(w);
}
}
}
}