diff --git a/components/locale_core/src/extensions/unicode/attributes.rs b/components/locale_core/src/extensions/unicode/attributes.rs index 357ae8a2667..53152bbbc29 100644 --- a/components/locale_core/src/extensions/unicode/attributes.rs +++ b/components/locale_core/src/extensions/unicode/attributes.rs @@ -148,6 +148,29 @@ impl Attributes { { self.deref().iter().map(|t| t.as_str()).try_for_each(f) } + + /// Extends the `Attributes` with values from another `Attributes`. + /// + /// # Example + /// + /// ``` + /// use icu::locale::extensions::unicode::Attributes; + /// + /// let mut attrs: Attributes = "foobar-foobaz".parse().unwrap(); + /// let attrs2: Attributes = "foobar-fooqux".parse().unwrap(); + /// + /// attrs.extend(attrs2); + /// + /// assert_eq!(attrs, "foobar-foobaz-fooqux".parse().unwrap()); + /// ``` + #[cfg(feature = "alloc")] + pub fn extend(&mut self, other: Attributes) { + for attr in other.0 { + if let Err(idx) = self.binary_search(&attr) { + self.0.insert(idx, attr); + } + } + } } /// ✨ *Enabled with the `alloc` Cargo feature.* diff --git a/components/locale_core/src/extensions/unicode/keywords.rs b/components/locale_core/src/extensions/unicode/keywords.rs index f3ecd4cad72..c31921a318b 100644 --- a/components/locale_core/src/extensions/unicode/keywords.rs +++ b/components/locale_core/src/extensions/unicode/keywords.rs @@ -386,6 +386,27 @@ impl Keywords { Ok(()) } + /// Extends the `Keywords` with values from another `Keywords`. + /// + /// # Example + /// + /// ``` + /// use icu::locale::extensions::unicode::Keywords; + /// + /// let mut kw: Keywords = "ab-cd-ca-buddhist".parse().unwrap(); + /// let kw2: Keywords = "ca-gregory-hc-h12".parse().unwrap(); + /// + /// kw.extend(kw2); + /// + /// assert_eq!(kw, "ab-cd-ca-gregory-hc-h12".parse().unwrap()); + /// ``` + #[cfg(feature = "alloc")] + pub fn extend(&mut self, other: Keywords) { + for (key, value) in other.0 { + self.0.insert(key, value); + } + } + /// This needs to be its own method to help with type inference in helpers.rs #[cfg(test)] pub(crate) fn from_tuple_vec(v: Vec<(Key, Value)>) -> Self { diff --git a/components/locale_core/src/extensions/unicode/mod.rs b/components/locale_core/src/extensions/unicode/mod.rs index 9577d397df3..0b60f779f6d 100644 --- a/components/locale_core/src/extensions/unicode/mod.rs +++ b/components/locale_core/src/extensions/unicode/mod.rs @@ -214,6 +214,26 @@ impl Unicode { } Ok(()) } + + /// Extends the `Unicode` with values from another `Unicode`. + /// + /// # Example + /// + /// ``` + /// use icu::locale::extensions::unicode::Unicode; + /// + /// let mut ue: Unicode = "u-foobar-ca-buddhist".parse().unwrap(); + /// let ue2: Unicode = "u-ca-gregory-hc-h12".parse().unwrap(); + /// + /// ue.extend(ue2); + /// + /// assert_eq!(ue, "u-foobar-ca-gregory-hc-h12".parse().unwrap()); + /// ``` + #[cfg(feature = "alloc")] + pub fn extend(&mut self, other: Unicode) { + self.keywords.extend(other.keywords); + self.attributes.extend(other.attributes); + } } /// ✨ *Enabled with the `alloc` Cargo feature.* diff --git a/components/locale_core/src/preferences/mod.rs b/components/locale_core/src/preferences/mod.rs index fe47b1e2b6f..e74ffb04362 100644 --- a/components/locale_core/src/preferences/mod.rs +++ b/components/locale_core/src/preferences/mod.rs @@ -37,7 +37,7 @@ //! //! # Preferences Merging //! -//! In traditional internatonalization APIs, the argument passed to constructors is a locale. +//! In traditional internationalization APIs, the argument passed to constructors is a locale. //! ICU4X changes this paradigm by accepting a `Preferences`, which can be extracted from a [`Locale`] and combined with //! other `Preferences`s provided by the environment. //!