use core::fmt;
use core::sync::atomic::{AtomicU8, Ordering};
const fn embedded_locales() -> &'static str {
match option_env!("EMBEDDED_I18N_LOCALES") {
Some(locales) => locales,
None => "",
}
}
const fn entry(list: &'static str, wanted: usize) -> Option<&'static str> {
let bytes = list.as_bytes();
let mut start = 0;
let mut index = 0;
let mut current = 0;
while index <= bytes.len() {
if index == bytes.len() || bytes[index] == b',' {
let (head, _) = bytes.split_at(index);
let (_, entry) = head.split_at(start);
let entry = entry.trim_ascii();
if !entry.is_empty() {
if current == wanted {
return match core::str::from_utf8(entry) {
Ok(entry) => Some(entry),
Err(_) => None,
};
}
current += 1;
}
start = index + 1;
}
index += 1;
}
None
}
const fn count(list: &'static str) -> usize {
let mut count = 0;
while entry(list, count).is_some() {
count += 1;
}
count
}
pub const LOCALE_COUNT: usize = match count(embedded_locales()) {
0 => 1,
count => count,
};
const _: () = assert!(LOCALE_COUNT <= u8::MAX as usize);
pub const LOCALES: [&str; LOCALE_COUNT] = {
let mut locales = [crate::get_locale(); LOCALE_COUNT];
let mut index = 0;
while index < LOCALE_COUNT {
if let Some(locale) = entry(embedded_locales(), index) {
locales[index] = locale;
}
index += 1;
}
locales
};
const fn same_locale(left: &str, right: &str) -> bool {
let left = left.trim_ascii().as_bytes();
let right = right.trim_ascii().as_bytes();
if left.len() != right.len() {
return false;
}
let mut index = 0;
while index < left.len() {
let a = if left[index] == b'_' {
b'-'
} else {
left[index]
};
let b = if right[index] == b'_' {
b'-'
} else {
right[index]
};
if !a.eq_ignore_ascii_case(&b) {
return false;
}
index += 1;
}
true
}
pub const fn find_locale(locale: &str) -> Option<usize> {
let mut index = 0;
while index < LOCALE_COUNT {
if same_locale(LOCALES[index], locale) {
return Some(index);
}
index += 1;
}
None
}
pub const DEFAULT_LOCALE_INDEX: usize = match find_locale(crate::get_locale()) {
Some(index) => index,
None => 0,
};
static CURRENT: AtomicU8 = AtomicU8::new(DEFAULT_LOCALE_INDEX as u8);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UnknownLocale;
impl fmt::Display for UnknownLocale {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("locale is not embedded")
}
}
#[inline]
pub fn locale_index() -> usize {
let index = usize::from(CURRENT.load(Ordering::Relaxed));
if index < LOCALE_COUNT { index } else { 0 }
}
pub fn locale() -> &'static str {
LOCALES[locale_index()]
}
pub fn set_locale_index(index: usize) -> Result<(), UnknownLocale> {
if index >= LOCALE_COUNT {
return Err(UnknownLocale);
}
CURRENT.store(index as u8, Ordering::Relaxed);
Ok(())
}
pub fn set_locale(locale: &str) -> Result<(), UnknownLocale> {
set_locale_index(find_locale(locale).ok_or(UnknownLocale)?)
}
#[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)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_locale_lists() {
assert_eq!(entry(" en, fr_CA ,,de ", 0), Some("en"));
assert_eq!(entry(" en, fr_CA ,,de ", 1), Some("fr_CA"));
assert_eq!(entry(" en, fr_CA ,,de ", 2), Some("de"));
assert_eq!(entry(" en, fr_CA ,,de ", 3), None);
assert_eq!(count(" en, fr_CA ,,de "), 3);
assert_eq!(count(""), 0);
assert_eq!(count(" , "), 0);
}
#[test]
fn compares_locales_loosely() {
assert!(same_locale("fr_CA", "FR-ca"));
assert!(!same_locale("fr", "fr-CA"));
}
#[test]
fn single_locale_by_default() {
if LOCALE_COUNT != 1 {
return;
}
assert_eq!(LOCALES[..], ["en"]);
assert_eq!(locale_index(), 0);
assert_eq!(locale(), "en");
assert_eq!(set_locale("EN"), Ok(()));
assert_eq!(set_locale("fr"), Err(UnknownLocale));
assert_eq!(set_locale_index(1), Err(UnknownLocale));
assert_eq!(locale(), "en");
}
}