Skip to content

Migrate PercentEssentials to VarZeroCow; add test for #4662 #6716

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 5 additions & 19 deletions components/experimental/src/dimension/provider/percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use alloc::borrow::Cow;
use icu_pattern::{DoublePlaceholderPattern, SinglePlaceholderPattern};
use icu_provider::prelude::*;

#[cfg(feature = "serde")]
use icu_pattern::{DoublePlaceholder, SinglePlaceholder};
use zerovec::VarZeroCow;

#[cfg(feature = "compiled_data")]
/// Baked data
Expand Down Expand Up @@ -42,27 +40,15 @@ icu_provider::data_marker!(
/// <https://www.unicode.org/reports/tr35/tr35-numbers.html#approximate-number-formatting>
/// <https://www.unicode.org/reports/tr35/tr35-numbers.html#explicit-plus-signs>
pub struct PercentEssentials<'data> {
#[cfg_attr(
feature = "serde",
serde(
borrow,
deserialize_with = "icu_pattern::deserialize_borrowed_cow::<DoublePlaceholder, _>"
)
)]
#[cfg_attr(feature = "serde", serde(borrow))]
/// Represents the standard pattern for signed percents.
/// NOTE: place holder 0 is the place of the percent value.
/// place holder 1 is the place of the plus, minus, or approximate signs.
pub signed_pattern: Cow<'data, DoublePlaceholderPattern>,
pub signed_pattern: VarZeroCow<'data, DoublePlaceholderPattern>,

#[cfg_attr(
feature = "serde",
serde(
borrow,
deserialize_with = "icu_pattern::deserialize_borrowed_cow::<SinglePlaceholder, _>"
)
)]
#[cfg_attr(feature = "serde", serde(borrow))]
/// Represents the standard pattern for unsigned percents.
pub unsigned_pattern: Cow<'data, SinglePlaceholderPattern>,
pub unsigned_pattern: VarZeroCow<'data, SinglePlaceholderPattern>,

#[cfg_attr(feature = "serde", serde(borrow))]
/// The localize approximate sign.
Expand Down
1 change: 1 addition & 0 deletions components/pattern/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ yoke = { workspace = true, features = ["derive"], optional = true }
zerovec = { workspace = true, default-features = false, optional = true }

[dev-dependencies]
litemap = { workspace = true, features = ["alloc"] }
zerofrom = { workspace = true, features = ["alloc"] }
zerovec = { workspace = true, features = ["databake", "serde"] }
rmp-serde = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions components/pattern/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
mod databake;
#[cfg(feature = "serde")]
pub(crate) mod serde;
#[cfg(all(feature = "zerovec", feature = "alloc"))]
mod zerovec;

use crate::common::*;
#[cfg(feature = "alloc")]
use crate::Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,22 @@ where
Self::from_ref_store_unchecked(store)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::SinglePlaceholderPattern;
use litemap::LiteMap;
use zerovec::ZeroMap;

#[test]
fn test_zeromap() {
let pattern =
SinglePlaceholderPattern::try_from_str("Hello, {0}!", Default::default()).unwrap();
let mut litemap = LiteMap::<u32, Box<SinglePlaceholderPattern>>::new_vec();
litemap.insert(0, pattern.clone());
let zeromap = ZeroMap::<u32, SinglePlaceholderPattern>::from_iter(litemap);
let recovered_pattern = zeromap.get(&0).unwrap();
assert_eq!(&*pattern, recovered_pattern);
}
}
2 changes: 0 additions & 2 deletions components/pattern/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ mod common;
mod double;
mod error;
mod frontend;
#[cfg(all(feature = "zerovec", feature = "alloc"))]
mod implementations;
mod multi_named;
#[cfg(feature = "alloc")]
mod parser;
Expand Down

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions provider/source/src/percent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use icu_pattern::SinglePlaceholder;
use icu_pattern::SinglePlaceholderPattern;
use icu_provider::prelude::*;
use icu_provider::DataProvider;
use zerovec::VarZeroCow;

impl DataProvider<PercentEssentialsV1> for SourceDataProvider {
fn load(&self, req: DataRequest) -> Result<DataResponse<PercentEssentialsV1>, DataError> {
Expand Down Expand Up @@ -102,7 +103,7 @@ fn extract_percent_essentials<'data>(
fn create_signed_pattern<'a>(
pattern: &str,
localized_percent_sign: &str,
) -> Result<Cow<'a, Pattern<DoublePlaceholder>>, DataError> {
) -> Result<VarZeroCow<'a, Pattern<DoublePlaceholder>>, DataError> {
// While all locales use the `%`, some include non-breaking spaces.
// Hence using the literal `%` char here.
let percent_pattern_index = pattern.find('%').unwrap();
Expand Down Expand Up @@ -170,15 +171,15 @@ fn create_signed_pattern<'a>(
let pattern = DoublePlaceholderPattern::try_from_str(&pattern_vec.concat(), Default::default())
.map_err(|e| DataError::custom("Could not parse pattern").with_display_context(&e))?;

Ok(Cow::Owned(pattern))
Ok(VarZeroCow::new_owned(pattern))
}

/// Used only for positive percents.
/// If you need an approximate, explicit plus, or negative percent, use the negative pattern.
fn create_unsigned_pattern<'a>(
pattern: &str,
localized_percent_sign: &str,
) -> Result<Cow<'a, Pattern<SinglePlaceholder>>, DataError> {
) -> Result<VarZeroCow<'a, Pattern<SinglePlaceholder>>, DataError> {
// While all locales use the `%`, some include non-breaking spaces.
// Hence using the literal `%` char here.
let percent_sign_index = pattern.find('%').unwrap();
Expand Down Expand Up @@ -218,7 +219,7 @@ fn create_unsigned_pattern<'a>(
)
.map_err(|e| DataError::custom("Could not parse pattern").with_display_context(&e))?;

Ok(Cow::Owned(pattern))
Ok(VarZeroCow::new_owned(pattern))
}

#[test]
Expand Down