Struct icu4x_ecma402::DataLocale
source · pub struct DataLocale(/* private fields */);
Expand description
An adapter between DataLocale
and ecma402_traits::Locale
.
Methods from Deref<Target = DataLocale>§
sourcepub fn strict_cmp(&self, other: &[u8]) -> Ordering
pub fn strict_cmp(&self, other: &[u8]) -> Ordering
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 icu_locid::Locale;
use icu_provider::DataLocale;
use std::cmp::Ordering;
let bcp47_strings: &[&str] = &[
"ca",
"ca+EUR",
"ca-ES",
"ca-ES+GBP",
"ca-ES+GBP+short",
"ca-ES+USD",
"ca-ES-u-ca-buddhist",
"ca-ES-valencia",
"cat",
"pl-Latn-PL",
"und",
"und+MXN",
"und-fonipa",
"und-u-ca-hebrew",
"und-u-ca-japanese",
"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+GBP+FOO",
// Greater than "ca-ES+GBP"
"ca_ES",
"ca-ES+GBP+FOO",
];
let data_locale = "ca-ES+GBP".parse::<DataLocale>().unwrap();
for s in invalid_strings.iter() {
let expected_ordering = "ca-ES+GBP".cmp(s);
let actual_ordering = data_locale.strict_cmp(s.as_bytes());
assert_eq!(expected_ordering, actual_ordering, "{}", s);
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether this DataLocale
has all empty fields (no components).
See also:
Examples
use icu_provider::DataLocale;
assert!("und".parse::<DataLocale>().unwrap().is_empty());
assert!(!"und-u-ca-buddhist"
.parse::<DataLocale>()
.unwrap()
.is_empty());
assert!(!"und+auxiliary".parse::<DataLocale>().unwrap().is_empty());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_empty());
sourcepub fn is_und(&self) -> bool
pub fn is_und(&self) -> bool
Returns whether this DataLocale
is und
in the locale and extensions portion.
This ignores auxiliary keys.
See also:
Examples
use icu_provider::DataLocale;
assert!("und".parse::<DataLocale>().unwrap().is_und());
assert!(!"und-u-ca-buddhist".parse::<DataLocale>().unwrap().is_und());
assert!("und+auxiliary".parse::<DataLocale>().unwrap().is_und());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_und());
sourcepub fn is_langid_und(&self) -> bool
pub fn is_langid_und(&self) -> bool
Returns whether the [LanguageIdentifier
] associated with this request is und
.
This ignores extension keywords and auxiliary keys.
See also:
Examples
use icu_provider::DataLocale;
assert!("und".parse::<DataLocale>().unwrap().is_langid_und());
assert!("und-u-ca-buddhist"
.parse::<DataLocale>()
.unwrap()
.is_langid_und());
assert!("und+auxiliary"
.parse::<DataLocale>()
.unwrap()
.is_langid_und());
assert!(!"ca-ES".parse::<DataLocale>().unwrap().is_langid_und());
sourcepub fn get_langid(&self) -> LanguageIdentifier
pub fn get_langid(&self) -> LanguageIdentifier
Gets the [LanguageIdentifier
] for this DataLocale
.
This may allocate memory if there are variant subtags. If you need only the language, script, and/or region subtag, use the specific getters for those subtags:
If you have ownership over the DataLocale
, use DataLocale::into_locale()
and then access the id
field.
Examples
use icu_locid::langid;
use icu_provider::prelude::*;
const FOO_BAR: DataKey = icu_provider::data_key!("foo/bar@1");
let req_no_langid = DataRequest {
locale: &Default::default(),
metadata: Default::default(),
};
let req_with_langid = DataRequest {
locale: &langid!("ar-EG").into(),
metadata: Default::default(),
};
assert_eq!(req_no_langid.locale.get_langid(), langid!("und"));
assert_eq!(req_with_langid.locale.get_langid(), langid!("ar-EG"));
sourcepub fn language(&self) -> Language
pub fn language(&self) -> Language
Returns the [Language
] for this DataLocale
.
sourcepub fn script(&self) -> Option<Script>
pub fn script(&self) -> Option<Script>
Returns the [Script
] for this DataLocale
.
sourcepub fn region(&self) -> Option<Region>
pub fn region(&self) -> Option<Region>
Returns the [Region
] for this DataLocale
.
sourcepub fn has_variants(&self) -> bool
pub fn has_variants(&self) -> bool
Returns whether there are any [Variant
] subtags in this DataLocale
.
sourcepub fn get_unicode_ext(&self, key: &Key) -> Option<Value>
pub fn get_unicode_ext(&self, key: &Key) -> Option<Value>
Gets the value of the specified Unicode extension keyword for this DataLocale
.
sourcepub fn has_unicode_ext(&self) -> bool
pub fn has_unicode_ext(&self) -> bool
Returns whether there are any Unicode extension keywords in this DataLocale
.
sourcepub fn contains_unicode_ext(&self, key: &Key) -> bool
pub fn contains_unicode_ext(&self, key: &Key) -> bool
Returns whether a specific Unicode extension keyword is present in this DataLocale
.
sourcepub fn matches_unicode_ext(&self, key: &Key, value: &Value) -> bool
pub fn matches_unicode_ext(&self, key: &Key, value: &Value) -> bool
Returns whether this DataLocale
contains a Unicode extension keyword
with the specified key and value.
Examples
use icu_locid::{
extensions::unicode::{key, value},
Locale,
};
use icu_provider::prelude::*;
let locale: Locale = "it-IT-u-ca-coptic".parse().expect("Valid BCP-47");
let locale: DataLocale = locale.into();
assert_eq!(locale.get_unicode_ext(&key!("hc")), None);
assert_eq!(locale.get_unicode_ext(&key!("ca")), Some(value!("coptic")));
assert!(locale.matches_unicode_ext(&key!("ca"), &value!("coptic"),));
Trait Implementations§
source§impl Clone for DataLocale
impl Clone for DataLocale
source§fn clone(&self) -> DataLocale
fn clone(&self) -> DataLocale
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for DataLocale
impl Debug for DataLocale
source§impl Deref for DataLocale
impl Deref for DataLocale
source§impl Display for DataLocale
impl Display for DataLocale
source§impl Hash for DataLocale
impl Hash for DataLocale
source§impl PartialEq for DataLocale
impl PartialEq for DataLocale
source§fn eq(&self, other: &DataLocale) -> bool
fn eq(&self, other: &DataLocale) -> bool
self
and other
values to be equal, and is used
by ==
.