diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/unic-langid-impl/src/subtags | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/unic-langid-impl/src/subtags')
5 files changed, 331 insertions, 0 deletions
diff --git a/third_party/rust/unic-langid-impl/src/subtags/language.rs b/third_party/rust/unic-langid-impl/src/subtags/language.rs new file mode 100644 index 0000000000..8b25bb609f --- /dev/null +++ b/third_party/rust/unic-langid-impl/src/subtags/language.rs @@ -0,0 +1,108 @@ +use crate::parser::errors::ParserError; +use std::borrow::Borrow; +use std::convert::TryFrom; +use std::str::FromStr; +use tinystr::TinyStr8; + +#[derive(Default, Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)] +pub struct Language(Option<TinyStr8>); + +impl Language { + pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> { + let slen = v.len(); + + let s = TinyStr8::from_bytes(v).map_err(|_| ParserError::InvalidLanguage)?; + if slen < 2 || slen > 8 || slen == 4 || !s.is_ascii_alphabetic() { + return Err(ParserError::InvalidLanguage); + } + + let value = s.to_ascii_lowercase(); + + if value == "und" { + Ok(Self(None)) + } else { + Ok(Self(Some(value))) + } + } + + pub fn as_str(&self) -> &str { + self.0.as_deref().unwrap_or("und") + } + + /// # Safety + /// + /// This function accepts any u64 that is exected to be a valid + /// `TinyStr8` and a valid `Language` subtag. + pub const unsafe fn from_raw_unchecked(v: u64) -> Self { + Self(Some(TinyStr8::from_bytes_unchecked(v.to_le_bytes()))) + } + + pub fn matches<O: Borrow<Self>>( + self, + other: O, + self_as_range: bool, + other_as_range: bool, + ) -> bool { + (self_as_range && self.0.is_none()) + || (other_as_range && other.borrow().0.is_none()) + || self == *other.borrow() + } + + pub fn clear(&mut self) { + self.0 = None; + } + + pub fn is_empty(self) -> bool { + self.0.is_none() + } +} + +impl From<Language> for Option<u64> { + fn from(input: Language) -> Self { + input.0.map(|i| u64::from_le_bytes(*i.all_bytes())) + } +} + +impl From<&Language> for Option<u64> { + fn from(input: &Language) -> Self { + input.0.map(|i| u64::from_le_bytes(*i.all_bytes())) + } +} + +impl<T> TryFrom<Option<T>> for Language +where + T: AsRef<[u8]>, +{ + type Error = ParserError; + + fn try_from(v: Option<T>) -> Result<Self, Self::Error> { + match v { + Some(l) => Ok(Self::from_bytes(l.as_ref())?), + None => Ok(Self(None)), + } + } +} + +impl FromStr for Language { + type Err = ParserError; + + fn from_str(source: &str) -> Result<Self, Self::Err> { + Self::from_bytes(source.as_bytes()) + } +} + +impl std::fmt::Display for Language { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if let Some(ref lang) = self.0 { + f.write_str(lang) + } else { + f.write_str("und") + } + } +} + +impl PartialEq<&str> for Language { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} diff --git a/third_party/rust/unic-langid-impl/src/subtags/mod.rs b/third_party/rust/unic-langid-impl/src/subtags/mod.rs new file mode 100644 index 0000000000..2d0a7e3a42 --- /dev/null +++ b/third_party/rust/unic-langid-impl/src/subtags/mod.rs @@ -0,0 +1,9 @@ +mod language; +mod region; +mod script; +mod variant; + +pub use language::Language; +pub use region::Region; +pub use script::Script; +pub use variant::Variant; diff --git a/third_party/rust/unic-langid-impl/src/subtags/region.rs b/third_party/rust/unic-langid-impl/src/subtags/region.rs new file mode 100644 index 0000000000..b7febe2025 --- /dev/null +++ b/third_party/rust/unic-langid-impl/src/subtags/region.rs @@ -0,0 +1,74 @@ +use crate::parser::errors::ParserError; +use std::str::FromStr; +use tinystr::TinyStr4; + +#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)] +pub struct Region(TinyStr4); + +impl Region { + pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> { + let slen = v.len(); + + match slen { + 2 => { + let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; + if !s.is_ascii_alphabetic() { + return Err(ParserError::InvalidSubtag); + } + Ok(Self(s.to_ascii_uppercase())) + } + 3 => { + let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; + if !s.is_ascii_numeric() { + return Err(ParserError::InvalidSubtag); + } + Ok(Self(s)) + } + _ => Err(ParserError::InvalidSubtag), + } + } + + pub fn as_str(&self) -> &str { + self.0.as_str() + } + + /// # Safety + /// + /// This function accepts any u64 that is exected to be a valid + /// `TinyStr4` and a valid `Region` subtag. + pub const unsafe fn from_raw_unchecked(v: u32) -> Self { + Self(TinyStr4::from_bytes_unchecked(v.to_le_bytes())) + } +} + +impl From<Region> for u32 { + fn from(input: Region) -> Self { + u32::from_le_bytes(*input.0.all_bytes()) + } +} + +impl<'l> From<&'l Region> for &'l str { + fn from(input: &'l Region) -> Self { + input.0.as_str() + } +} + +impl FromStr for Region { + type Err = ParserError; + + fn from_str(source: &str) -> Result<Self, Self::Err> { + Self::from_bytes(source.as_bytes()) + } +} + +impl std::fmt::Display for Region { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +impl PartialEq<&str> for Region { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} diff --git a/third_party/rust/unic-langid-impl/src/subtags/script.rs b/third_party/rust/unic-langid-impl/src/subtags/script.rs new file mode 100644 index 0000000000..8cb38c8d52 --- /dev/null +++ b/third_party/rust/unic-langid-impl/src/subtags/script.rs @@ -0,0 +1,62 @@ +use crate::parser::errors::ParserError; +use std::str::FromStr; +use tinystr::TinyStr4; + +#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)] +pub struct Script(TinyStr4); + +impl Script { + pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> { + let slen = v.len(); + + let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; + if slen != 4 || !s.is_ascii_alphabetic() { + return Err(ParserError::InvalidSubtag); + } + Ok(Self(s.to_ascii_titlecase())) + } + + pub fn as_str(&self) -> &str { + self.0.as_str() + } + + /// # Safety + /// + /// This function accepts any u64 that is exected to be a valid + /// `TinyStr4` and a valid `Script` subtag. + pub const unsafe fn from_raw_unchecked(v: u32) -> Self { + Self(TinyStr4::from_bytes_unchecked(v.to_le_bytes())) + } +} + +impl From<Script> for u32 { + fn from(input: Script) -> Self { + u32::from_le_bytes(*input.0.all_bytes()) + } +} + +impl<'l> From<&'l Script> for &'l str { + fn from(input: &'l Script) -> Self { + input.0.as_str() + } +} + +impl FromStr for Script { + type Err = ParserError; + + fn from_str(source: &str) -> Result<Self, Self::Err> { + Self::from_bytes(source.as_bytes()) + } +} + +impl std::fmt::Display for Script { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +impl PartialEq<&str> for Script { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} diff --git a/third_party/rust/unic-langid-impl/src/subtags/variant.rs b/third_party/rust/unic-langid-impl/src/subtags/variant.rs new file mode 100644 index 0000000000..8b29a458ec --- /dev/null +++ b/third_party/rust/unic-langid-impl/src/subtags/variant.rs @@ -0,0 +1,78 @@ +use crate::parser::errors::ParserError; +use std::str::FromStr; +use tinystr::TinyStr8; + +#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)] +pub struct Variant(TinyStr8); + +impl Variant { + pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> { + let slen = v.len(); + + if slen < 4 || slen > 8 { + return Err(ParserError::InvalidSubtag); + } + + let s = TinyStr8::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; + + if (slen >= 5 && !s.is_ascii_alphanumeric()) + || (slen == 4 + && !v[0].is_ascii_digit() + && v[1..].iter().any(|c: &u8| !c.is_ascii_alphanumeric())) + { + return Err(ParserError::InvalidSubtag); + } + + Ok(Self(s.to_ascii_lowercase())) + } + + pub fn as_str(&self) -> &str { + self.0.as_str() + } + + /// # Safety + /// + /// This function accepts any u64 that is exected to be a valid + /// `TinyStr8` and a valid `Variant` subtag. + pub const unsafe fn from_raw_unchecked(v: u64) -> Self { + Self(TinyStr8::from_bytes_unchecked(v.to_le_bytes())) + } +} + +impl From<Variant> for u64 { + fn from(input: Variant) -> Self { + u64::from_le_bytes(*input.0.all_bytes()) + } +} + +impl From<&Variant> for u64 { + fn from(input: &Variant) -> Self { + u64::from_le_bytes(*input.0.all_bytes()) + } +} + +impl FromStr for Variant { + type Err = ParserError; + + fn from_str(source: &str) -> Result<Self, Self::Err> { + Self::from_bytes(source.as_bytes()) + } +} + +impl std::fmt::Display for Variant { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +impl PartialEq<&str> for Variant { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} + +impl PartialEq<str> for Variant { + fn eq(&self, other: &str) -> bool { + self.as_str() == other + } +} |