Skip to content

Commit

Permalink
Make transliterate crate work with case-folded aux keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Oct 12, 2023
1 parent d27c86b commit 848ec59
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions experimental/transliterate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ icu_provider = { workspace = true, features = ["macros", "experimental"] }
icu_locid = { workspace = true }
icu_collections = { workspace = true }
icu_normalizer = { workspace = true }
writeable = { workspace = true }

icu_properties = { workspace = true, optional = true }
icu_unicodeset_parse = { workspace = true, optional = true }
Expand Down
21 changes: 18 additions & 3 deletions experimental/transliterate/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use alloc::string::ToString;
use icu_locid::Locale;
use icu_provider::prelude::*;
use writeable::Writeable;

pub fn bcp47_to_data_locale(locale: &Locale) -> DataLocale {
let mut data_locale = DataLocale::default();
#[allow(clippy::unwrap_used)] // any BCP-47 locale is a valid aux key
data_locale.set_aux(locale.to_string().parse().unwrap());
data_locale.set_aux(
AuxiliaryKeys::try_from_iter(
locale
.write_to_string()
.split('-')
.map(str::parse)
.filter_map(Result::ok),
)
.unwrap(),
);
data_locale
}

pub fn unparsed_bcp47_to_data_locale(dep: &str) -> Result<DataLocale, DataError> {
let mut data_locale = DataLocale::default();
data_locale.set_aux(dep.parse()?);
data_locale.set_aux(AuxiliaryKeys::try_from_iter(
dep.split('-')
.map(str::parse)
// TODO: Bubble a potential subtag parse error out. Currently
// we ignore it inside of the `filter_map`.
.filter_map(Result::ok),
)?);
Ok(data_locale)
}
10 changes: 9 additions & 1 deletion provider/core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,15 @@ impl AuxiliaryKeys {
pub fn iter(&self) -> impl Iterator<Item = Subtag> + '_ {
self.value
.split(Self::separator())
.flat_map(|x| x.parse().ok())
.filter_map(|x| {
match x.parse() {
Ok(x) => Some(x),
Err(_) => {
debug_assert!(false, "failed to convert to subtag: {x}");
None
}
}
})
}

pub(crate) fn strict_cmp_iter<'l, I>(&self, mut subtags: I) -> SubtagOrderingResult<I>
Expand Down

0 comments on commit 848ec59

Please sign in to comment.