From 4f9fe856a25ab29345b90e7725509e9ee38a37be Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:41 +0200 Subject: Adding upstream version 1.69.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/icu_locid/src/locale.rs | 106 +++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 57 deletions(-) (limited to 'vendor/icu_locid/src/locale.rs') 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::().unwrap()); +/// assert_eq!(loc.id.language, language!("en")); /// assert_eq!(loc.id.script, None); -/// assert_eq!(loc.id.region, "US".parse::().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::(), 3); assert_eq!(core::mem::size_of::(), 4); assert_eq!(core::mem::size_of::(), 3); @@ -99,12 +101,21 @@ fn test_sizes() { assert_eq!(core::mem::size_of::(), 24); assert_eq!(core::mem::size_of::(), 24); - assert_eq!(core::mem::size_of::(), 48); + assert_eq!( + core::mem::size_of::(), + if forced_nightly { 40 } else { 48 } + ); assert_eq!(core::mem::size_of::>(), 24); assert_eq!(core::mem::size_of::(), 24); - assert_eq!(core::mem::size_of::(), 192); + assert_eq!( + core::mem::size_of::(), + if forced_nightly { 184 } else { 192 } + ); - assert_eq!(core::mem::size_of::(), 240); + assert_eq!( + core::mem::size_of::(), + 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 { 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>(input: S) -> Result { 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::().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 for Locale { fn from(language: subtags::Language) -> Self { @@ -447,14 +449,10 @@ impl From 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> for Locale { fn from(script: Option) -> Self { @@ -468,14 +466,10 @@ impl From> 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> for Locale { fn from(region: Option) -> Self { @@ -491,20 +485,18 @@ impl From> 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<( -- cgit v1.2.3