use std::rc::Rc;
pub trait Unit: Copy + Into<u32> + 'static {}
impl Unit for u8 {}
impl Unit for u16 {}
#[derive(Clone, Debug)]
pub enum CodeUnits {
Bytes(Rc<[u8]>),
Utf16(Rc<[u16]>),
}
impl CodeUnits {
pub fn from_str(text: &str) -> Self {
if text.is_ascii() {
CodeUnits::Bytes(Rc::from(text.as_bytes()))
} else {
CodeUnits::Utf16(text.encode_utf16().collect::<Vec<u16>>().into())
}
}
pub fn from_utf16(units: &[u16]) -> Self {
if units.iter().all(|&u| u < 0x80) {
CodeUnits::Bytes(units.iter().map(|&u| u as u8).collect::<Vec<u8>>().into())
} else {
CodeUnits::Utf16(Rc::from(units))
}
}
#[inline]
pub fn len(&self) -> usize {
match self {
CodeUnits::Bytes(b) => b.len(),
CodeUnits::Utf16(u) => u.len(),
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn at(&self, index: usize) -> u32 {
match self {
CodeUnits::Bytes(b) => b.get(index).map_or(u32::MAX, |&c| c as u32),
CodeUnits::Utf16(u) => u.get(index).map_or(u32::MAX, |&c| c as u32),
}
}
pub fn slice(&self, start: usize, end: usize) -> std::borrow::Cow<'_, str> {
match self {
CodeUnits::Bytes(b) => std::borrow::Cow::Borrowed(unsafe { std::str::from_utf8_unchecked(&b[start..end]) }),
CodeUnits::Utf16(u) => std::borrow::Cow::Owned(String::from_utf16_lossy(&u[start..end])),
}
}
pub fn hash_range(&self, start: usize, end: usize) -> i32 {
match self {
CodeUnits::Bytes(b) => hash_range(b, start, end),
CodeUnits::Utf16(u) => hash_range(u, start, end),
}
}
pub fn hash_range_lower(&self, start: usize, end: usize) -> i32 {
match self {
CodeUnits::Bytes(b) => hash_range_lower(b, start, end),
CodeUnits::Utf16(u) => hash_range_lower(u, start, end),
}
}
}
const FNV_OFFSET: u32 = 0x811c9dc5;
const FNV_PRIME: u32 = 0x01000193;
const HASH_MASK: u32 = 0x3fffffff;
const KELVIN_SIGN: u32 = 0x212a;
pub fn hash_string(str: &str) -> i32 {
let mut hash = FNV_OFFSET;
for code in str.encode_utf16() {
hash = (hash ^ code as u32).wrapping_mul(FNV_PRIME);
}
(hash & HASH_MASK) as i32
}
pub fn hash_units<T: Unit>(codes: &[T]) -> i32 {
hash_range(codes, 0, codes.len())
}
#[inline]
pub fn hash_range<T: Unit>(codes: &[T], start: usize, end: usize) -> i32 {
let mut hash = FNV_OFFSET;
for &code in &codes[start..end] {
hash = (hash ^ code.into()).wrapping_mul(FNV_PRIME);
}
(hash & HASH_MASK) as i32
}
#[inline]
pub fn lower_ascii(code: u32) -> i32 {
if (0x41..=0x5a).contains(&code) {
return (code + 32) as i32;
}
if code > 0x7f {
return if code == KELVIN_SIGN { 0x6b } else { -1 };
}
code as i32
}
pub fn hash_range_lower<T: Unit>(codes: &[T], start: usize, end: usize) -> i32 {
let mut hash = FNV_OFFSET;
for &code in &codes[start..end] {
let code = lower_ascii(code.into());
if code == -1 {
return -1;
}
hash = (hash ^ code as u32).wrapping_mul(FNV_PRIME);
}
(hash & HASH_MASK) as i32
}
pub fn hash_string_lower(str: &str) -> i32 {
let mut hash = FNV_OFFSET;
for code in str.encode_utf16() {
let code = lower_ascii(code as u32);
if code == -1 {
return -1;
}
hash = (hash ^ code as u32).wrapping_mul(FNV_PRIME);
}
(hash & HASH_MASK) as i32
}