use core::fmt;
use core::sync::atomic::{AtomicU32, Ordering};
const OTHER_LANGUAGE: u32 = 0x7FFF;
const FNV_OFFSET: u32 = 0x811C_9DC5;
const FNV_PRIME: u32 = 0x0100_0193;
const fn normalize(byte: u8) -> u8 {
match byte {
b'_' => b'-',
byte => byte.to_ascii_lowercase(),
}
}
const fn hash(bytes: &[u8], start: usize, end: usize) -> u32 {
if start >= end {
return 0;
}
let mut h = FNV_OFFSET;
let mut index = start;
while index < end {
h ^= normalize(bytes[index]) as u32;
h = h.wrapping_mul(FNV_PRIME);
index += 1;
}
let low = (h ^ (h >> 17)) & 0x1_FFFF;
if low == 0 { 1 } else { low }
}
const fn pack(tag: &str) -> u32 {
let bytes = tag.as_bytes();
let mut start = 0;
let mut end = bytes.len();
while start < end && bytes[start].is_ascii_whitespace() {
start += 1;
}
while end > start && bytes[end - 1].is_ascii_whitespace() {
end -= 1;
}
if start == end {
return 0;
}
let mut separator = start;
while separator < end && normalize(bytes[separator]) != b'-' {
separator += 1;
}
let length = separator - start;
let mut language = 0u32;
let mut is_language = length >= 1 && length <= 3;
let mut index = start;
while is_language && index < separator {
let byte = normalize(bytes[index]);
if byte.is_ascii_lowercase() {
language = language * 32 + (byte - b'a' + 1) as u32;
} else {
is_language = false;
}
index += 1;
}
if is_language {
let rest = if separator < end { separator + 1 } else { end };
(language << 17) | hash(bytes, rest, end)
} else {
(OTHER_LANGUAGE << 17) | hash(bytes, start, end)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Locale(u32);
impl Locale {
pub const NONE: Locale = Locale(0);
pub const fn new(tag: &str) -> Locale {
Locale(pack(tag))
}
pub const fn id(self) -> u32 {
self.0
}
}
impl From<&str> for Locale {
fn from(tag: &str) -> Locale {
Locale::new(tag)
}
}
static CURRENT: AtomicU32 = AtomicU32::new(0);
pub fn set_locale(locale: &str) {
CURRENT.store(Locale::new(locale).id(), Ordering::Relaxed);
}
#[doc(hidden)]
pub fn __current_locale() -> u32 {
CURRENT.load(Ordering::Relaxed)
}
#[doc(hidden)]
pub struct __FormatWith<F>(pub F);
impl<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Display for __FormatWith<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(self.0)(f)
}
}
#[doc(hidden)]
pub trait __PluralCount {
fn __plural_operand(&self) -> u64;
}
impl<T: __PluralCount + ?Sized> __PluralCount for &T {
fn __plural_operand(&self) -> u64 {
(**self).__plural_operand()
}
}
macro_rules! plural_count {
(unsigned: $($unsigned:ty),*; signed: $($signed:ty),*) => {
$(
impl __PluralCount for $unsigned {
fn __plural_operand(&self) -> u64 {
u64::try_from(*self).unwrap_or(u64::MAX)
}
}
)*
$(
impl __PluralCount for $signed {
fn __plural_operand(&self) -> u64 {
u64::try_from(self.unsigned_abs()).unwrap_or(u64::MAX)
}
}
)*
};
}
plural_count!(
unsigned: u8, u16, u32, u64, u128, usize;
signed: i8, i16, i32, i64, i128, isize
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn packs_reference_tags() {
assert_eq!(Locale::new("en").id(), 0x015C_0000);
assert_eq!(Locale::new("fr").id(), 0x01A4_0000);
assert_eq!(Locale::new("de").id(), 0x010A_0000);
assert_eq!(Locale::new("fr-CA").id(), 0x01A5_B85D);
assert_eq!(Locale::new("fr-FR").id(), 0x01A4_FD60);
assert_eq!(Locale::new("zh-Hant").id(), 0x0690_220D);
assert_eq!(Locale::new("zh-Hans").id(), 0x0690_3E88);
assert_eq!(Locale::new("pt-BR").id(), 0x0429_E320);
assert_eq!(Locale::new("pseudo").id(), 0xFFFE_7D15);
assert_eq!(Locale::new("x-a").id(), 0x0030_5B2A);
assert_eq!(Locale::new(""), Locale::NONE);
}
#[test]
fn normalizes_before_packing() {
assert_eq!(Locale::new("FR_ca"), Locale::new("fr-CA"));
assert_eq!(Locale::new(" fr-ca "), Locale::new("fr-CA"));
assert_eq!(Locale::from("pt_br"), Locale::new("pt-BR"));
}
#[test]
fn global_locale_round_trips() {
assert_eq!(__current_locale(), 0);
set_locale("fr_CA");
assert_eq!(__current_locale(), 0x01A5_B85D);
set_locale("");
assert_eq!(__current_locale(), 0);
}
#[test]
fn plural_operand_is_the_saturated_absolute_value() {
assert_eq!(3u8.__plural_operand(), 3);
assert_eq!((-1i32).__plural_operand(), 1);
assert_eq!(i64::MIN.__plural_operand(), 1 << 63);
assert_eq!(u64::MAX.__plural_operand(), u64::MAX);
assert_eq!(u128::MAX.__plural_operand(), u64::MAX);
assert_eq!(i128::MIN.__plural_operand(), u64::MAX);
assert_eq!((&&7usize).__plural_operand(), 7);
}
const _: () = assert!(Locale::new("en").id() == 0x015C_0000); }