usecrate::extensions::unicode as unicode_ext; usecrate::subtags::{Language, Region, Script, Subtag, Variant}; #[cfg(feature = "alloc")] usecrate::ParseError; usecrate::{LanguageIdentifier, Locale}; use core::cmp::Ordering; use core::default::Default; use core::fmt; use core::hash::Hash; #[cfg(feature = "alloc")] use core::str::FromStr;
/// A locale type optimized for use in fallbacking and the ICU4X data pipeline. /// /// [`DataLocale`] contains less functionality than [`Locale`] but more than /// [`LanguageIdentifier`] for better size and performance while still meeting /// the needs of the ICU4X data pipeline. /// /// You can create a [`DataLocale`] from a borrowed [`Locale`], which is more /// efficient than cloning the [`Locale`], but less efficient than converting an owned /// [`Locale`]: /// /// ``` /// use icu_locale_core::locale; /// use icu_provider::DataLocale; /// /// let locale1 = locale!("en-u-ca-buddhist"); /// let data_locale = DataLocale::from(&locale1); /// ``` /// /// [`DataLocale`] only supports `-u-sd` keywords, to reflect the current state of CLDR data /// lookup and fallback. This may change in the future. /// /// ``` /// use icu_locale_core::{locale, Locale}; /// use icu_provider::DataLocale; /// /// let locale = "hi-IN-t-en-h0-hybrid-u-attr-ca-buddhist-sd-inas" /// .parse::<Locale>() /// .unwrap(); /// /// assert_eq!( /// DataLocale::from(locale), /// DataLocale::from(locale!("hi-IN-u-sd-inas")) /// ); /// ``` #[derive(Clone, Copy, PartialEq, Hash, Eq)] #[non_exhaustive] pubstruct DataLocale { /// Language subtag pub language: Language, /// Script subtag pub script: Option<Script>, /// Region subtag pub region: Option<Region>, /// Variant subtag pub variant: Option<Variant>, /// Subivision (-u-sd-) subtag pub subdivision: Option<Subtag>,
}
/// Returns an ordering suitable for use in [`BTreeSet`]. /// /// [`BTreeSet`]: alloc::collections::BTreeSet pubfn total_cmp(&self, other: &Self) -> Ordering { self.as_tuple().cmp(&other.as_tuple())
}
/// Compare this [`DataLocale`] with BCP-47 bytes. /// /// The return value is equivalent to what would happen if you first converted this /// [`DataLocale`] to a BCP-47 string and then performed a byte comparison. /// /// This function is case-sensitive and results in a *total order*, so it is appropriate for /// binary search. The only argument producing [`Ordering::Equal`] is `self.to_string()`. /// /// # Examples /// /// ``` /// use core::cmp::Ordering; /// use icu_provider::DataLocale; /// /// let bcp47_strings: &[&str] = &[ /// "ca", /// "ca-ES", /// "ca-ES-u-sd-esct", /// "ca-ES-valencia", /// "cat", /// "pl-Latn-PL", /// "und", /// "und-fonipa", /// "zh", /// ]; /// /// for ab in bcp47_strings.windows(2) { /// let a = ab[0]; /// let b = ab[1]; /// assert_eq!(a.cmp(b), Ordering::Less, "strings: {} < {}", a, b); /// let a_loc: DataLocale = a.parse().unwrap(); /// assert_eq!( /// a_loc.strict_cmp(a.as_bytes()), /// Ordering::Equal, /// "strict_cmp: {} == {}", /// a_loc, /// a /// ); /// assert_eq!( /// a_loc.strict_cmp(b.as_bytes()), /// Ordering::Less, /// "strict_cmp: {} < {}", /// a_loc, /// b /// ); /// let b_loc: DataLocale = b.parse().unwrap(); /// assert_eq!( /// b_loc.strict_cmp(b.as_bytes()), /// Ordering::Equal, /// "strict_cmp: {} == {}", /// b_loc, /// b /// ); /// assert_eq!( /// b_loc.strict_cmp(a.as_bytes()), /// Ordering::Greater, /// "strict_cmp: {} > {}", /// b_loc, /// a /// ); /// } /// ``` /// /// Comparison against invalid strings: /// /// ``` /// use icu_provider::DataLocale; /// /// let invalid_strings: &[&str] = &[ /// // Less than "ca-ES" /// "CA", /// "ar-x-gbp-FOO", /// // Greater than "ca-AR" /// "ca_ES", /// "ca-ES-x-gbp-FOO", /// ]; /// /// let data_locale = "ca-ES".parse::<DataLocale>().unwrap(); /// /// for s in invalid_strings.iter() { /// let expected_ordering = "ca-AR".cmp(s); /// let actual_ordering = data_locale.strict_cmp(s.as_bytes()); /// assert_eq!(expected_ordering, actual_ordering, "{}", s); /// } /// ``` pubfn strict_cmp(&self, other: &[u8]) -> Ordering {
writeable::cmp_utf8(self, other)
}
/// Returns whether this [`DataLocale`] is `und` in the locale and extensions portion. /// /// # Examples /// /// ``` /// use icu_provider::DataLocale; /// /// assert!("und".parse::<DataLocale>().unwrap().is_unknown()); /// assert!(!"de-u-sd-denw".parse::<DataLocale>().unwrap().is_unknown()); /// assert!(!"und-ES".parse::<DataLocale>().unwrap().is_unknown()); /// ``` pubfn is_unknown(&self) -> bool { self.language.is_unknown()
&& self.script.is_none()
&& self.region.is_none()
&& self.variant.is_none()
&& self.subdivision.is_none()
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.