use core::cmp::Ordering;
use crate::error::{Error, Result};
use crate::spec::{MysqlCollation, MysqlUcaVersion, Pad};
mod table_0400;
mod table_0900;
const SPACE: u16 = 0x0209;
const LONG_RUNE_U16: u16 = 0xFFFD;
const DICP_RANGES: &[(u32, u32)] = &[
(0x00AD, 0x00AD),
(0x034F, 0x034F),
(0x061C, 0x061C),
(0x115F, 0x1160),
(0x17B4, 0x17B5),
(0x180B, 0x180E),
(0x200B, 0x200F),
(0x202A, 0x202E),
(0x2060, 0x2064),
(0x2066, 0x206F),
(0x3164, 0x3164),
(0xFE00, 0xFE0F),
(0xFEFF, 0xFEFF),
(0xFFA0, 0xFFA0),
(0xFFF9, 0xFFFB),
(0x1BCA0, 0x1BCA3),
(0x1D173, 0x1D17A),
(0xE0001, 0xE0001),
(0xE0020, 0xE007F),
(0xE0100, 0xE01EF),
];
fn is_default_ignorable(cp: u32) -> bool {
DICP_RANGES.iter().any(|&(lo, hi)| (lo..=hi).contains(&cp))
}
pub(crate) fn validate(spec: &MysqlCollation) -> Result<()> {
let expected = match spec.name.as_str() {
"utf8mb4_0900_ai_ci" => Some((MysqlUcaVersion::Uca900, Pad::NoPad)),
"utf8mb4_unicode_ci" => Some((MysqlUcaVersion::Uca400, Pad::Space)),
_ => None,
};
match expected {
Some((uca, pad)) if uca == spec.uca_version && pad == spec.pad => Ok(()),
Some(_) => Err(Error::unsupported(
"mysql",
&spec.name,
"collation name/version/pad combination is inconsistent",
)),
None => Err(Error::unsupported(
"mysql",
&spec.name,
"not one of the exactly reproducible utf8mb4 UCA collations",
)),
}
}
const MAX_WEIGHTS: usize = 8;
struct WeightBuf {
buf: [u16; MAX_WEIGHTS],
len: usize,
}
impl WeightBuf {
const fn new() -> Self {
Self {
buf: [0; MAX_WEIGHTS],
len: 0,
}
}
const fn push(&mut self, w: u16) {
if self.len < MAX_WEIGHTS {
self.buf[self.len] = w;
self.len += 1;
}
}
#[cfg(test)]
fn as_slice(&self) -> &[u16] {
&self.buf[..self.len]
}
}
fn push_weight(collation: &MysqlCollation, ch: char, out: &mut WeightBuf) {
let cp = ch as u32;
match collation.uca_version {
MysqlUcaVersion::Uca900 => {
if let Some(u) = table_0900::weight(cp) {
push_u64(u, out);
} else if is_default_ignorable(cp) {
} else {
push_implicit(cp, out);
}
}
MysqlUcaVersion::Uca400 => {
if cp > 0xFFFF {
out.push(LONG_RUNE_U16);
} else if let Some(u) = table_0400::weight(cp) {
push_u64(u, out);
} else {
out.push(LONG_RUNE_U16);
}
}
}
}
const fn push_u64(mut u: u128, out: &mut WeightBuf) {
while u != 0 {
out.push((u & 0xFFFF) as u16);
u >>= 16;
}
}
fn implicit_page(cp: u32) -> u32 {
if (0x17000..=0x18AFF).contains(&cp) {
0xFB00
} else if (0x3400..=0x4DB5).contains(&cp)
|| (0x20000..=0x2A6D6).contains(&cp)
|| (0x2A700..=0x2B734).contains(&cp)
|| (0x2B740..=0x2B81D).contains(&cp)
|| (0x2B820..=0x2CEA1).contains(&cp)
{
(cp >> 15) + 0xFB80
} else if (0x4E00..=0x9FD5).contains(&cp) || (0xFA0E..=0xFA29).contains(&cp) {
(cp >> 15) + 0xFB40
} else {
(cp >> 15) + 0xFBC0
}
}
fn decompose_hangul(cp: u32) -> Option<[u32; 3]> {
const SYLLABLE_BASE: u32 = 0xAC00;
const LEADING_BASE: u32 = 0x1100;
const VOWEL_BASE: u32 = 0x1161;
const TRAILING_BASE: u32 = 0x11A7;
const VOWEL_CNT: u32 = 21;
const TRAILING_CNT: u32 = 28;
if !(SYLLABLE_BASE..=0xD7AF).contains(&cp) {
return None;
}
let index = cp - SYLLABLE_BASE;
let v_t = VOWEL_CNT * TRAILING_CNT;
let leading = LEADING_BASE + index / v_t;
let vowel = VOWEL_BASE + (index % v_t) / TRAILING_CNT;
let trailing_index = index % TRAILING_CNT;
let trailing = if trailing_index == 0 {
0
} else {
TRAILING_BASE + trailing_index
};
Some([leading, vowel, trailing])
}
fn push_implicit(cp: u32, out: &mut WeightBuf) {
if let Some([l, v, t]) = decompose_hangul(cp) {
for jamo in [l, v, t] {
if jamo == 0 {
continue;
}
if let Some(u) = table_0900::weight(jamo) {
push_u64(u, out);
}
}
return;
}
let tangut = (0x17000..=0x18AFF).contains(&cp);
let page = if tangut { 0xFB00 } else { implicit_page(cp) };
let low = if tangut {
(cp - 0x17000) | 0x8000
} else {
(cp & 0x7FFF) | 0x8000
};
out.push(u16::try_from(page).unwrap_or(u16::MAX));
out.push(u16::try_from(low).unwrap_or(u16::MAX));
}
#[cfg(test)]
fn weights(collation: &MysqlCollation, s: &str) -> Vec<u16> {
let mut out = Vec::with_capacity(s.len());
for ch in s.chars() {
let mut buf = WeightBuf::new();
push_weight(collation, ch, &mut buf);
out.extend_from_slice(buf.as_slice());
}
out
}
struct WeightIter<'a> {
collation: &'a MysqlCollation,
chars: std::str::Chars<'a>,
buf: WeightBuf,
pos: usize,
}
impl<'a> WeightIter<'a> {
fn new(collation: &'a MysqlCollation, s: &'a str) -> Self {
Self {
collation,
chars: s.chars(),
buf: WeightBuf::new(),
pos: 0,
}
}
fn next_weight(&mut self) -> Option<u16> {
loop {
if self.pos < self.buf.len {
let w = self.buf.buf[self.pos];
self.pos += 1;
return Some(w);
}
let ch = self.chars.next()?;
self.buf = WeightBuf::new();
push_weight(self.collation, ch, &mut self.buf);
self.pos = 0;
}
}
}
pub(crate) fn compare(collation: &MysqlCollation, a: &str, b: &str) -> Ordering {
let pad = collation.pad == Pad::Space;
let mut ia = WeightIter::new(collation, a);
let mut ib = WeightIter::new(collation, b);
loop {
let (xw, yw) = match (ia.next_weight(), ib.next_weight()) {
(None, None) => return Ordering::Equal,
(Some(_), None) if !pad => return Ordering::Greater,
(None, Some(_)) if !pad => return Ordering::Less,
(Some(xw), None) => (xw, SPACE),
(None, Some(yw)) => (SPACE, yw),
(Some(xw), Some(yw)) => (xw, yw),
};
match xw.cmp(&yw) {
Ordering::Equal => {}
ord => return ord,
}
}
}
#[cfg(test)]
mod tests {
use core::cmp::Ordering;
use super::*;
fn ai_ci() -> MysqlCollation {
MysqlCollation::utf8mb4_0900_ai_ci()
}
fn unicode_ci() -> MysqlCollation {
MysqlCollation::utf8mb4_unicode_ci()
}
#[test]
fn test_case_and_accent_insensitive() {
let c = ai_ci();
assert_eq!(compare(&c, "a", "A"), Ordering::Equal);
assert_eq!(compare(&c, "e", "\u{e9}"), Ordering::Equal);
assert_eq!(compare(&c, "e", "e\u{301}"), Ordering::Equal);
}
#[test]
fn test_no_pad_treats_trailing_space_as_significant() {
let c = ai_ci();
assert_ne!(compare(&c, "a", "a "), Ordering::Equal);
}
#[test]
fn test_pad_space_ignores_trailing_space() {
let c = unicode_ci();
assert_eq!(compare(&c, "a", "a "), Ordering::Equal);
assert_eq!(compare(&c, "a ", "a "), Ordering::Equal);
}
#[test]
fn test_pad_space_shorter_can_be_greater() {
let c = unicode_ci();
assert_eq!(compare(&c, "a", "a\t"), Ordering::Greater);
assert_eq!(compare(&c, "a", "ab"), Ordering::Less);
assert_eq!(compare(&c, "a", "a "), Ordering::Equal);
}
#[test]
fn test_supplementary_default_ignorable_has_zero_weight() {
let c = ai_ci();
assert_eq!(compare(&c, "", "\u{e0001}"), Ordering::Equal);
assert_eq!(compare(&c, "", "\u{e0100}"), Ordering::Equal);
assert_ne!(compare(&c, "", "\u{e0002}"), Ordering::Equal);
}
#[test]
fn test_ligature_expands() {
let c = ai_ci();
assert_eq!(compare(&c, "\u{df}", "ss"), Ordering::Equal);
}
#[test]
fn test_multi_element_weights_match_mysql_order() {
let c = ai_ci();
assert_eq!(weights(&c, "\u{bd}"), vec![0x1C3E, 0x0626, 0x1C3F]); assert_eq!(weights(&c, "\u{e6}"), vec![0x1C47, 0x1CAA]); assert_eq!(
weights(&c, "\u{33ae}"),
vec![0x1E33, 0x1C47, 0x1C8F, 0x0625, 0x1E71]
);
assert_eq!(compare(&c, "\u{bd}", "2"), Ordering::Less);
}
#[test]
fn test_implicit_weight_ranges_beyond_table() {
let c = ai_ci();
assert_eq!(weights(&c, "\u{2CEA1}"), vec![0xFB85, 0xCEA1]);
assert_eq!(compare(&c, "\u{2CEA1}", "\u{0378}"), Ordering::Less);
assert_eq!(compare(&c, "\u{2CEA1}", "\u{2CEA0}"), Ordering::Greater);
}
#[test]
fn test_uca400_unassigned_uses_generic_implicit() {
let c = unicode_ci();
assert_eq!(weights(&c, "\u{4DB6}"), vec![0xFBC0, 0xCDB6]);
assert_eq!(weights(&c, "\u{FA2E}"), vec![0xFBC1, 0xFA2E]);
assert_eq!(weights(&c, "\u{9FA6}"), vec![0xFBC1, 0x9FA6]);
assert_eq!(weights(&c, "\u{3400}"), vec![0xFB80, 0xB400]);
assert_eq!(weights(&c, "\u{FA29}"), vec![0xFB41, 0xFA29]);
assert_eq!(compare(&c, "\u{3400}", "\u{FA2E}"), Ordering::Less);
}
#[test]
fn test_uca400_overlong_expansion_packs_overflow_page_first() {
let c = unicode_ci();
assert_eq!(weights(&c, "\u{FDFA}"), vec![0xFBC1, 0xFDFA]);
assert_eq!(compare(&c, "\u{FDFA}", "\u{FDFE}"), Ordering::Less);
}
#[test]
fn test_default_ignorable_set_is_unicode_9_0_0() {
let c = ai_ci();
for cp in ['\u{061C}', '\u{2064}', '\u{2066}', '\u{2069}', '\u{180E}'] {
assert_eq!(
compare(&c, "", &cp.to_string()),
Ordering::Equal,
"U+{:04X}",
cp as u32
);
}
for cp in ['\u{180F}', '\u{2065}', '\u{FFFD}'] {
assert_ne!(
compare(&c, "", &cp.to_string()),
Ordering::Equal,
"U+{:04X}",
cp as u32
);
}
let u = unicode_ci();
assert_ne!(compare(&u, "", "\u{061C}"), Ordering::Equal);
assert_eq!(compare(&c, "", "\u{034F}"), Ordering::Equal);
assert_eq!(compare(&u, "", "\u{034F}"), Ordering::Equal);
}
#[test]
fn test_hangul_syllable_decomposes() {
let c = ai_ci();
assert_eq!(weights(&c, "\u{AC00}"), vec![0x3BF5, 0x3C73]);
}
#[test]
fn test_validate_rejects_inconsistent_spec() {
let bad = MysqlCollation {
name: "utf8mb4_0900_ai_ci".to_owned(),
uca_version: MysqlUcaVersion::Uca400,
pad: Pad::NoPad,
};
assert!(validate(&bad).is_err());
}
}