summaryrefslogtreecommitdiffstats
path: root/vendor/icu_locid/src/locale.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/icu_locid/src/locale.rs')
-rw-r--r--vendor/icu_locid/src/locale.rs106
1 files changed, 49 insertions, 57 deletions
diff --git a/vendor/icu_locid/src/locale.rs b/vendor/icu_locid/src/locale.rs
index d7040d31a..5d9109fee 100644
--- a/vendor/icu_locid/src/locale.rs
+++ b/vendor/icu_locid/src/locale.rs
@@ -4,16 +4,15 @@
use crate::ordering::SubtagOrderingResult;
use crate::parser::{
- get_subtag_iterator, parse_locale,
- parse_locale_with_single_variant_single_keyword_unicode_keyword_extension, ParserError,
- ParserMode,
+ parse_locale, parse_locale_with_single_variant_single_keyword_unicode_keyword_extension,
+ ParserError, ParserMode, SubtagIterator,
};
use crate::{extensions, subtags, LanguageIdentifier};
use alloc::string::String;
-use alloc::string::ToString;
use core::cmp::Ordering;
use core::str::FromStr;
use tinystr::TinyAsciiStr;
+use writeable::Writeable;
/// A core struct representing a [`Unicode Locale Identifier`].
///
@@ -28,20 +27,21 @@ use tinystr::TinyAsciiStr;
/// # Examples
///
/// ```
-/// use icu::locid::extensions::unicode::{Key, Value};
-/// use icu::locid::{subtags::*, Locale};
+/// use icu_locid::{
+/// extensions_unicode_key as key, extensions_unicode_value as value,
+/// locale, subtags_language as language, subtags_region as region,
+/// };
///
-/// let loc: Locale = "en-US-u-ca-buddhist".parse().expect("Failed to parse.");
+/// let loc = locale!("en-US-u-ca-buddhist");
///
-/// assert_eq!(loc.id.language, "en".parse::<Language>().unwrap());
+/// assert_eq!(loc.id.language, language!("en"));
/// assert_eq!(loc.id.script, None);
-/// assert_eq!(loc.id.region, "US".parse::<Region>().ok());
+/// assert_eq!(loc.id.region, Some(region!("US")));
/// assert_eq!(loc.id.variants.len(), 0);
-/// assert_eq!(loc.to_string(), "en-US-u-ca-buddhist");
-///
-/// let key: Key = "ca".parse().expect("Parsing key failed.");
-/// let value: Value = "buddhist".parse().expect("Parsing value failed.");
-/// assert_eq!(loc.extensions.unicode.keywords.get(&key), Some(&value));
+/// assert_eq!(
+/// loc.extensions.unicode.keywords.get(&key!("ca")),
+/// Some(&value!("buddhist"))
+/// );
/// ```
///
/// # Parsing
@@ -87,6 +87,8 @@ pub struct Locale {
#[test]
fn test_sizes() {
+ // Remove when we upgrade to a compiler where the new sizes are default
+ let forced_nightly = std::env::var("ICU4X_BUILDING_WITH_FORCED_NIGHTLY").is_ok();
assert_eq!(core::mem::size_of::<subtags::Language>(), 3);
assert_eq!(core::mem::size_of::<subtags::Script>(), 4);
assert_eq!(core::mem::size_of::<subtags::Region>(), 3);
@@ -99,12 +101,21 @@ fn test_sizes() {
assert_eq!(core::mem::size_of::<extensions::transform::Fields>(), 24);
assert_eq!(core::mem::size_of::<extensions::unicode::Attributes>(), 24);
- assert_eq!(core::mem::size_of::<extensions::unicode::Keywords>(), 48);
+ assert_eq!(
+ core::mem::size_of::<extensions::unicode::Keywords>(),
+ if forced_nightly { 40 } else { 48 }
+ );
assert_eq!(core::mem::size_of::<Vec<extensions::other::Other>>(), 24);
assert_eq!(core::mem::size_of::<extensions::private::Private>(), 24);
- assert_eq!(core::mem::size_of::<extensions::Extensions>(), 192);
+ assert_eq!(
+ core::mem::size_of::<extensions::Extensions>(),
+ if forced_nightly { 184 } else { 192 }
+ );
- assert_eq!(core::mem::size_of::<Locale>(), 240);
+ assert_eq!(
+ core::mem::size_of::<Locale>(),
+ if forced_nightly { 232 } else { 240 }
+ );
}
impl Locale {
@@ -116,10 +127,7 @@ impl Locale {
/// ```
/// use icu::locid::Locale;
///
- /// let loc = Locale::try_from_bytes("en-US-u-hc-h12".as_bytes())
- /// .expect("Parsing failed.");
- ///
- /// assert_eq!(loc.to_string(), "en-US-u-hc-h12");
+ /// Locale::try_from_bytes(b"en-US-u-hc-h12").unwrap();
/// ```
pub fn try_from_bytes(v: &[u8]) -> Result<Self, ParserError> {
parse_locale(v)
@@ -133,7 +141,6 @@ impl Locale {
/// use icu::locid::Locale;
///
/// assert_eq!(Locale::default(), Locale::UND);
- /// assert_eq!("und", Locale::UND.to_string());
/// ```
pub const UND: Self = Self {
id: LanguageIdentifier::UND,
@@ -151,13 +158,13 @@ impl Locale {
/// use icu::locid::Locale;
///
/// assert_eq!(
- /// Locale::canonicalize("pL_latn_pl-U-HC-H12"),
- /// Ok("pl-Latn-PL-u-hc-h12".to_string())
+ /// Locale::canonicalize("pL_latn_pl-U-HC-H12").as_deref(),
+ /// Ok("pl-Latn-PL-u-hc-h12")
/// );
/// ```
pub fn canonicalize<S: AsRef<[u8]>>(input: S) -> Result<String, ParserError> {
let locale = Self::try_from_bytes(input.as_ref())?;
- Ok(locale.to_string())
+ Ok(locale.write_to_string().into_owned())
}
/// Compare this [`Locale`] with BCP-47 bytes.
@@ -189,7 +196,6 @@ impl Locale {
/// let b = ab[1];
/// assert!(a.cmp(b) == Ordering::Less);
/// let a_loc = a.parse::<Locale>().unwrap();
- /// assert_eq!(a, a_loc.to_string());
/// assert!(a_loc.strict_cmp(a.as_bytes()) == Ordering::Equal);
/// assert!(a_loc.strict_cmp(b.as_bytes()) == Ordering::Less);
/// }
@@ -286,7 +292,7 @@ impl Locale {
};
}
- let mut iter = get_subtag_iterator(other.as_bytes());
+ let mut iter = SubtagIterator::new(other.as_bytes());
if !subtag_matches!(subtags::Language, iter, self.id.language) {
return false;
}
@@ -391,7 +397,7 @@ impl core::fmt::Debug for Locale {
}
}
-impl_writeable_for_each_subtag_str_no_test!(Locale);
+impl_writeable_for_each_subtag_str_no_test!(Locale, selff, selff.extensions.is_empty() => selff.id.write_to_string());
#[test]
fn test_writeable() {
@@ -426,14 +432,10 @@ fn test_writeable() {
/// # Examples
///
/// ```
-/// use icu::locid::subtags_language as language;
/// use icu::locid::Locale;
+/// use icu::locid::{locale, subtags_language as language};
///
-/// let language = language!("en");
-/// let loc = Locale::from(language);
-///
-/// assert_eq!(loc.id.language, language);
-/// assert_eq!(loc.to_string(), "en");
+/// assert_eq!(Locale::from(language!("en")), locale!("en"));
/// ```
impl From<subtags::Language> for Locale {
fn from(language: subtags::Language) -> Self {
@@ -447,14 +449,10 @@ impl From<subtags::Language> for Locale {
/// # Examples
///
/// ```
-/// use icu::locid::subtags_script as script;
/// use icu::locid::Locale;
+/// use icu::locid::{locale, subtags_script as script};
///
-/// let script = script!("latn");
-/// let loc = Locale::from(Some(script));
-///
-/// assert_eq!(loc.id.script.unwrap(), script);
-/// assert_eq!(loc.to_string(), "und-Latn");
+/// assert_eq!(Locale::from(Some(script!("latn"))), locale!("und-Latn"));
/// ```
impl From<Option<subtags::Script>> for Locale {
fn from(script: Option<subtags::Script>) -> Self {
@@ -468,14 +466,10 @@ impl From<Option<subtags::Script>> for Locale {
/// # Examples
///
/// ```
-/// use icu::locid::subtags_region as region;
/// use icu::locid::Locale;
+/// use icu::locid::{locale, subtags_region as region};
///
-/// let region = region!("US");
-/// let loc = Locale::from(Some(region));
-///
-/// assert_eq!(loc.id.region.unwrap(), region);
-/// assert_eq!(loc.to_string(), "und-US");
+/// assert_eq!(Locale::from(Some(region!("US"))), locale!("und-US"));
/// ```
impl From<Option<subtags::Region>> for Locale {
fn from(region: Option<subtags::Region>) -> Self {
@@ -491,20 +485,18 @@ impl From<Option<subtags::Region>> for Locale {
/// ```
/// use icu::locid::Locale;
/// use icu::locid::{
-/// subtags_language as language, subtags_region as region,
+/// locale, subtags_language as language, subtags_region as region,
/// subtags_script as script,
/// };
///
-/// let lang = language!("en");
-/// let script = script!("Latn");
-/// let region = region!("US");
-/// let loc = Locale::from((lang, Some(script), Some(region)));
-///
-/// assert_eq!(loc.id.language, lang);
-/// assert_eq!(loc.id.script.unwrap(), script);
-/// assert_eq!(loc.id.region.unwrap(), region);
-/// assert_eq!(loc.id.variants.len(), 0);
-/// assert_eq!(loc.to_string(), "en-Latn-US");
+/// assert_eq!(
+/// Locale::from((
+/// language!("en"),
+/// Some(script!("Latn")),
+/// Some(region!("US"))
+/// )),
+/// locale!("en-Latn-US")
+/// );
/// ```
impl
From<(