Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not validating tinystr keys #3406

Merged
merged 3 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion components/locid/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ macro_rules! impl_tinystr_subtag {
self.0.as_str()
}

#[doc(hidden)]
pub const fn into_tinystr(&self) -> tinystr::TinyAsciiStr<$len_end> {
self.0
}

/// Compare with BCP-47 bytes.
///
/// The return value is equivalent to what would happen if you first converted
Expand Down Expand Up @@ -425,7 +430,7 @@ macro_rules! impl_tinystr_subtag {

impl From<$name> for tinystr::TinyAsciiStr<$len_end> {
fn from(input: $name) -> Self {
input.0
input.into_tinystr()
}
}

Expand Down
47 changes: 36 additions & 11 deletions components/locid_transform/src/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ fn uts35_check_language_rules(
if !locale.id.language.is_empty() {
let lang: TinyAsciiStr<3> = locale.id.language.into();
let replacement = if lang.len() == 2 {
alias_data.get().language_len2.get(&lang.resize())
alias_data.get().language_len2.get(&lang.resize().raw())
} else {
alias_data.get().language_len3.get(&lang)
alias_data.get().language_len3.get(&lang.raw())
};

if let Some(replacement) = replacement {
Expand Down Expand Up @@ -366,7 +366,12 @@ impl LocaleCanonicalizer {
// If the region is specified, check sgn-region rules first
if let Some(region) = locale.id.region {
if locale.id.language == language!("sgn") {
if let Some(&sgn_lang) = self.aliases.get().sgn_region.get(&region.into()) {
if let Some(&sgn_lang) = self
.aliases
.get()
.sgn_region
.get(&region.into_tinystr().raw())
{
uts35_replacement::<core::iter::Empty<&str>>(
locale,
true,
Expand All @@ -388,7 +393,9 @@ impl LocaleCanonicalizer {
}

if let Some(script) = locale.id.script {
if let Some(&replacement) = self.aliases.get().script.get(&script.into()) {
if let Some(&replacement) =
self.aliases.get().script.get(&script.into_tinystr().raw())
{
locale.id.script = Some(replacement);
result = TransformResult::Modified;
continue;
Expand All @@ -397,18 +404,28 @@ impl LocaleCanonicalizer {

if let Some(region) = locale.id.region {
let replacement = if region.is_alphabetic() {
let region: TinyAsciiStr<3> = region.into();
self.aliases.get().region_alpha.get(&region.resize())
self.aliases
.get()
.region_alpha
.get(&region.into_tinystr().resize().raw())
} else {
self.aliases.get().region_num.get(&region.into())
self.aliases
.get()
.region_num
.get(&region.into_tinystr().raw())
};
if let Some(&replacement) = replacement {
locale.id.region = Some(replacement);
result = TransformResult::Modified;
continue;
}

if let Some(regions) = self.aliases.get().complex_region.get(&region.into()) {
if let Some(regions) = self
.aliases
.get()
.complex_region
.get(&region.into_tinystr().raw())
{
// Skip if regions are empty
if let Some(default_region) = regions.get(0) {
let mut maximized = LanguageIdentifier {
Expand Down Expand Up @@ -438,7 +455,12 @@ impl LocaleCanonicalizer {
let mut modified = Vec::new();
let mut unmodified = Vec::new();
for &variant in locale.id.variants.iter() {
if let Some(&updated) = self.aliases.get().variant.get(&variant.into()) {
if let Some(&updated) = self
.aliases
.get()
.variant
.get(&variant.into_tinystr().raw())
{
modified.push(updated);
} else {
unmodified.push(variant);
Expand Down Expand Up @@ -486,8 +508,11 @@ impl LocaleCanonicalizer {
for key in &[key!("rg"), key!("sd")] {
if let Some(value) = locale.extensions.unicode.keywords.get_mut(key) {
if let &[only_value] = value.as_tinystr_slice() {
if let Some(modified_value) =
self.aliases.get().subdivision.get(&only_value.resize())
if let Some(modified_value) = self
.aliases
.get()
.subdivision
.get(&only_value.resize().raw())
{
if let Ok(modified_value) = modified_value.parse() {
*value = modified_value;
Expand Down
12 changes: 6 additions & 6 deletions components/locid_transform/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ struct LocaleExpanderBorrowed<'a> {

impl LocaleExpanderBorrowed<'_> {
fn get_l(&self, l: Language) -> Option<(Script, Region)> {
let key = &l.into();
let key = &l.into_tinystr().raw();
self.likely_subtags_l.language.get_copied(key).or_else(|| {
self.likely_subtags_ext
.and_then(|ext| ext.language.get_copied(key))
})
}

fn get_ls(&self, l: Language, s: Script) -> Option<Region> {
let key = &(l.into(), s.into());
let key = &(l.into_tinystr().raw(), s.into_tinystr().raw());
self.likely_subtags_l
.language_script
.get_copied(key)
Expand All @@ -111,7 +111,7 @@ impl LocaleExpanderBorrowed<'_> {
}

fn get_lr(&self, l: Language, r: Region) -> Option<Script> {
let key = &(l.into(), r.into());
let key = &(l.into_tinystr().raw(), r.into_tinystr().raw());
self.likely_subtags_l
.language_region
.get_copied(key)
Expand All @@ -122,15 +122,15 @@ impl LocaleExpanderBorrowed<'_> {
}

fn get_s(&self, s: Script) -> Option<(Language, Region)> {
let key = &s.into();
let key = &s.into_tinystr().raw();
self.likely_subtags_sr.script.get_copied(key).or_else(|| {
self.likely_subtags_ext
.and_then(|ext| ext.script.get_copied(key))
})
}

fn get_sr(&self, s: Script, r: Region) -> Option<Language> {
let key = &(s.into(), r.into());
let key = &(s.into_tinystr().raw(), r.into_tinystr().raw());
self.likely_subtags_sr
.script_region
.get_copied(key)
Expand All @@ -141,7 +141,7 @@ impl LocaleExpanderBorrowed<'_> {
}

fn get_r(&self, r: Region) -> Option<(Language, Script)> {
let key = &r.into();
let key = &r.into_tinystr().raw();
self.likely_subtags_sr.region.get_copied(key).or_else(|| {
self.likely_subtags_ext
.and_then(|ext| ext.region.get_copied(key))
Expand Down
19 changes: 10 additions & 9 deletions components/locid_transform/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
use alloc::borrow::Cow;
use icu_locid::subtags::{Language, Region, Script, Variant};
use icu_provider::prelude::*;
use tinystr::TinyAsciiStr;
use tinystr::{TinyAsciiStr, UnvalidatedTinyAsciiStr};
use zerovec::{VarZeroVec, ZeroMap, ZeroSlice};

// We use raw TinyAsciiStrs for map keys, as we then don't have to
// validate them as subtags on deserialization. Map lookup can be
// done even if they are not valid tags (an invalid key will just
// become inaccessible).
type UnvalidatedLanguage = TinyAsciiStr<3>;
type UnvalidatedScript = TinyAsciiStr<4>;
type UnvalidatedRegion = TinyAsciiStr<3>;
type UnvalidatedVariant = TinyAsciiStr<8>;
type UnvalidatedSubdivision = TinyAsciiStr<7>;
type UnvalidatedLanguage = UnvalidatedTinyAsciiStr<3>;
type UnvalidatedScript = UnvalidatedTinyAsciiStr<4>;
type UnvalidatedRegion = UnvalidatedTinyAsciiStr<3>;
type UnvalidatedVariant = UnvalidatedTinyAsciiStr<8>;
type UnvalidatedSubdivision = UnvalidatedTinyAsciiStr<7>;
type SemivalidatedSubdivision = TinyAsciiStr<7>;

// LanguageIdentifier doesn't have an AsULE implementation, so we have
// to store strs and parse when needed.
Expand Down Expand Up @@ -101,7 +102,7 @@ pub struct AliasesV1<'data> {
pub sgn_region: ZeroMap<'data, UnvalidatedRegion, Language>,
/// `[language{2}] -> [langid]`
#[cfg_attr(feature = "serde", serde(borrow))]
pub language_len2: ZeroMap<'data, TinyAsciiStr<2>, UnvalidatedLanguageIdentifier>,
pub language_len2: ZeroMap<'data, UnvalidatedTinyAsciiStr<2>, UnvalidatedLanguageIdentifier>,
/// `[language{3}] -> [langid]`
#[cfg_attr(feature = "serde", serde(borrow))]
pub language_len3: ZeroMap<'data, UnvalidatedLanguage, UnvalidatedLanguageIdentifier>,
Expand All @@ -116,7 +117,7 @@ pub struct AliasesV1<'data> {

/// `[region{2}] -> [region]`
#[cfg_attr(feature = "serde", serde(borrow))]
pub region_alpha: ZeroMap<'data, TinyAsciiStr<2>, Region>,
pub region_alpha: ZeroMap<'data, UnvalidatedTinyAsciiStr<2>, Region>,
/// `[region{3}] -> [region]`
#[cfg_attr(feature = "serde", serde(borrow))]
pub region_num: ZeroMap<'data, UnvalidatedRegion, Region>,
Expand All @@ -131,7 +132,7 @@ pub struct AliasesV1<'data> {

/// `[value{7}] -> [value{7}]`
#[cfg_attr(feature = "serde", serde(borrow))]
pub subdivision: ZeroMap<'data, UnvalidatedSubdivision, UnvalidatedSubdivision>,
pub subdivision: ZeroMap<'data, UnvalidatedSubdivision, SemivalidatedSubdivision>,
}

#[icu_provider::data_struct(LikelySubtagsV1Marker = "locid_transform/likelysubtags@1")]
Expand Down
37 changes: 23 additions & 14 deletions experimental/displaynames/src/displaynames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ impl RegionDisplayNames {
pub fn of(&self, region: Region) -> Option<&str> {
let data = self.region_data.get();
match self.options.style {
Some(Style::Short) => data.short_names.get(&region.into()),
Some(Style::Short) => data.short_names.get(&region.into_tinystr().raw()),
_ => None,
}
.or_else(|| data.names.get(&region.into()))
.or_else(|| data.names.get(&region.into_tinystr().raw()))
// TODO: Respect options.fallback
}
}
Expand Down Expand Up @@ -149,10 +149,10 @@ impl ScriptDisplayNames {
pub fn of(&self, script: Script) -> Option<&str> {
let data = self.script_data.get();
match self.options.style {
Some(Style::Short) => data.short_names.get(&script.into()),
Some(Style::Short) => data.short_names.get(&script.into_tinystr().raw()),
_ => None,
}
.or_else(|| data.names.get(&script.into()))
.or_else(|| data.names.get(&script.into_tinystr().raw()))
// TODO: Respect options.fallback
}
}
Expand Down Expand Up @@ -222,12 +222,12 @@ impl LanguageDisplayNames {
pub fn of(&self, language: Language) -> Option<&str> {
let data = self.language_data.get();
match self.options.style {
Some(Style::Short) => data.short_names.get(&language.into()),
Some(Style::Long) => data.long_names.get(&language.into()),
Some(Style::Menu) => data.menu_names.get(&language.into()),
Some(Style::Short) => data.short_names.get(&language.into_tinystr().raw()),
Some(Style::Long) => data.long_names.get(&language.into_tinystr().raw()),
Some(Style::Menu) => data.menu_names.get(&language.into_tinystr().raw()),
_ => None,
}
.or_else(|| data.names.get(&language.into()))
.or_else(|| data.names.get(&language.into_tinystr().raw()))
// TODO: Respect options.fallback
}
}
Expand Down Expand Up @@ -356,32 +356,41 @@ impl LocaleDisplayNamesFormatter {
.language_data
.get()
.short_names
.get(&locale.id.language.into()),
.get(&locale.id.language.into_tinystr().raw()),
Some(Style::Long) => self
.language_data
.get()
.long_names
.get(&locale.id.language.into()),
.get(&locale.id.language.into_tinystr().raw()),
Some(Style::Menu) => self
.language_data
.get()
.menu_names
.get(&locale.id.language.into()),
.get(&locale.id.language.into_tinystr().raw()),
_ => None,
}
.or_else(|| {
self.language_data
.get()
.names
.get(&locale.id.language.into())
.get(&locale.id.language.into_tinystr().raw())
});

if let Some(region) = locale.id.region {
let regiondisplay = match self.options.style {
Some(Style::Short) => self.region_data.get().short_names.get(&region.into()),
Some(Style::Short) => self
.region_data
.get()
.short_names
.get(&region.into_tinystr().raw()),
_ => None,
}
.or_else(|| self.region_data.get().names.get(&region.into()));
.or_else(|| {
self.region_data
.get()
.names
.get(&region.into_tinystr().raw())
});
// TODO: Use data patterns
Cow::Owned(alloc::format!(
"{} ({})",
Expand Down
8 changes: 4 additions & 4 deletions experimental/displaynames/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
//! Read more about data providers: [`icu_provider`]

use icu_provider::prelude::*;
use tinystr::TinyAsciiStr;
use tinystr::UnvalidatedTinyAsciiStr;
use zerovec::ule::UnvalidatedStr;
use zerovec::ZeroMap;

// We use raw TinyAsciiStrs for map keys, as we then don't have to
// validate them as subtags on deserialization. Map lookup can be
// done even if they are not valid tags (an invalid key will just
// become inaccessible).
type UnvalidatedRegion = TinyAsciiStr<3>;
type UnvalidatedLanguage = TinyAsciiStr<3>;
type UnvalidatedScript = TinyAsciiStr<4>;
type UnvalidatedRegion = UnvalidatedTinyAsciiStr<3>;
type UnvalidatedLanguage = UnvalidatedTinyAsciiStr<3>;
type UnvalidatedScript = UnvalidatedTinyAsciiStr<4>;
type UnvalidatedLocale = UnvalidatedStr;

#[icu_provider::data_struct(RegionDisplayNamesV1Marker = "displaynames/regions@1")]
Expand Down
15 changes: 10 additions & 5 deletions provider/adapters/src/fallback/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,33 @@ impl<'a> LocaleFallbackerWithConfig<'a> {
locale.set_region(
self.likely_subtags
.ls2r
.get_2d(&language.into(), &script.into())
.get_2d(&language.into_tinystr().raw(), &script.into_tinystr().raw())
.copied(),
);
}
// 1b. If that fails, try language only
if locale.region().is_none() {
locale.set_region(self.likely_subtags.l2r.get(&language.into()).copied());
locale.set_region(
self.likely_subtags
.l2r
.get(&language.into_tinystr().raw())
.copied(),
);
}
}
// 2. Remove the script if it is implied by the other subtags
if let Some(script) = locale.script() {
let default_script = self
.likely_subtags
.l2s
.get_copied(&language.into())
.get_copied(&language.into_tinystr().raw())
.unwrap_or(DEFAULT_SCRIPT);
if let Some(region) = locale.region() {
if script
== self
.likely_subtags
.lr2s
.get_copied_2d(&language.into(), &region.into())
.get_copied_2d(&language.into_tinystr().raw(), &region.into_tinystr().raw())
.unwrap_or(default_script)
{
locale.set_script(None);
Expand Down Expand Up @@ -122,7 +127,7 @@ impl<'a, 'b> LocaleFallbackIteratorInner<'a, 'b> {
if let Some(script) = self
.likely_subtags
.lr2s
.get_copied_2d(&language.into(), &region.into())
.get_copied_2d(&language.into_tinystr().raw(), &region.into_tinystr().raw())
{
locale.set_script(Some(script));
self.restore_extensions_variants(locale);
Expand Down
Loading