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

Use pattern in icu_decimal #5385

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion components/datetime/src/any/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use icu_provider::prelude::*;
use icu_provider::DataLocale;
use writeable::Writeable;

size_test!(DateFormatter, date_formatter_size, 4456);
size_test!(DateFormatter, date_formatter_size, 4408);

/// [`DateFormatter`] is a formatter capable of formatting
/// dates from any calendar, selected at runtime. For the difference between this and [`TypedDateFormatter`](crate::TypedDateFormatter),
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/any/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use icu_provider::prelude::*;
use icu_provider::DataLocale;
use writeable::Writeable;

size_test!(DateTimeFormatter, date_time_formatter_size, 5208);
size_test!(DateTimeFormatter, date_time_formatter_size, 5160);

/// [`DateTimeFormatter`] is a formatter capable of formatting
/// date/times from any calendar, selected at runtime. For the difference between this and [`TypedDateTimeFormatter`](crate::TypedDateTimeFormatter),
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/any/zoned_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use icu_decimal::provider::DecimalSymbolsV1Marker;
use icu_plurals::provider::OrdinalV1Marker;
use writeable::Writeable;

size_test!(ZonedDateTimeFormatter, zoned_date_time_formatter_size, 6224);
size_test!(ZonedDateTimeFormatter, zoned_date_time_formatter_size, 6176);

/// [`ZonedDateTimeFormatter`] is a formatter capable of formatting
/// date/times with time zones from any calendar, selected at runtime. For the difference between this and [`TypedZonedDateTimeFormatter`](crate::TypedZonedDateTimeFormatter),
Expand Down
6 changes: 3 additions & 3 deletions components/datetime/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
DateTimeError, FormattedDateTime,
};

size_test!(TimeFormatter, time_formatter_size, 1200);
size_test!(TimeFormatter, time_formatter_size, 1152);

/// [`TimeFormatter`] is a structure of the [`icu::datetime`] component that provides time formatting only.
/// When constructed, it uses data from the [data provider], selected locale and provided preferences to
Expand Down Expand Up @@ -186,7 +186,7 @@ impl TimeFormatter {
size_test!(
TypedDateFormatter<icu_calendar::Gregorian>,
typed_date_formatter_size,
4400
4352
);

/// [`TypedDateFormatter`] is a formatter capable of formatting
Expand Down Expand Up @@ -402,7 +402,7 @@ impl<C: CldrCalendar> TypedDateFormatter<C> {
size_test!(
TypedDateTimeFormatter<icu_calendar::Gregorian>,
typed_date_time_formatter_size,
5152
5104
);

/// [`TypedDateTimeFormatter`] is a formatter capable of formatting
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/format/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ where
size_test!(
TypedDateTimeNames<icu_calendar::Gregorian, DateTimeMarker>,
typed_date_time_names_size,
464
416
);

/// A low-level type that formats datetime patterns with localized symbols.
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ where
}
}

size_test!(TypedNeoFormatter<icu_calendar::Gregorian, crate::neo_marker::NeoYearMonthDayMarker>, typed_neo_year_month_day_formatter_size, 504);
size_test!(TypedNeoFormatter<icu_calendar::Gregorian, crate::neo_marker::NeoYearMonthDayMarker>, typed_neo_year_month_day_formatter_size, 456);

/// [`TypedNeoFormatter`] is a formatter capable of formatting dates and/or times from
/// a calendar selected at compile time.
Expand Down Expand Up @@ -648,7 +648,7 @@ where
size_test!(
NeoFormatter<crate::neo_marker::NeoYearMonthDayMarker>,
neo_year_month_day_formatter_size,
560
512
);

/// [`NeoFormatter`] is a formatter capable of formatting dates and/or times from
Expand Down
5 changes: 3 additions & 2 deletions components/decimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ all-features = true
displaydoc = { workspace = true }
fixed_decimal = { workspace = true }
icu_provider = { workspace = true, features = ["macros"] }
icu_pattern = { workspace = true, features = ["zerofrom"]}
writeable = { workspace = true }

databake = { workspace = true, features = ["derive"], optional = true}
Expand All @@ -46,8 +47,8 @@ criterion = { workspace = true }
[features]
default = ["compiled_data"]
std = ["fixed_decimal/std", "icu_locale_core/std", "icu_provider/std"]
serde = ["dep:serde", "icu_provider/serde"]
datagen = ["serde", "dep:databake"]
serde = ["dep:serde", "icu_provider/serde", "icu_pattern/serde"]
datagen = ["serde", "dep:databake", "icu_pattern/databake"]
bench = ["serde"]
compiled_data = ["dep:icu_decimal_data"]

Expand Down
28 changes: 20 additions & 8 deletions components/decimal/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use crate::grouper;
use crate::options::*;
use crate::provider::*;
use alloc::borrow::Cow;
use fixed_decimal::FixedDecimal;
use fixed_decimal::Sign;
use icu_pattern::SinglePlaceholderPattern;
use writeable::Writeable;

/// An intermediate structure returned by [`FixedDecimalFormatter`](crate::FixedDecimalFormatter).
Expand All @@ -21,11 +23,21 @@ pub struct FormattedFixedDecimal<'l> {
}

impl<'l> FormattedFixedDecimal<'l> {
fn get_affixes(&self) -> Option<&AffixesV1> {
fn get_pattern(&self) -> Option<&SinglePlaceholderPattern<Cow<'l, str>>> {
match self.value.sign() {
Sign::None => None,
Sign::Negative => Some(&self.symbols.minus_sign_affixes),
Sign::Positive => Some(&self.symbols.plus_sign_affixes),
Sign::Negative => Some(
self.symbols
.minus_sign_pattern
.as_ref()
.unwrap_or(&NEGATIVE_DEFAULT),
),
Sign::Positive => Some(
self.symbols
.plus_sign_pattern
.as_ref()
.unwrap_or(&POSITIVE_DEFAULT),
),
}
}
}
Expand All @@ -35,9 +47,9 @@ impl<'l> Writeable for FormattedFixedDecimal<'l> {
where
W: core::fmt::Write + ?Sized,
{
let affixes = self.get_affixes();
if let Some(affixes) = affixes {
sink.write_str(&affixes.prefix)?;
let pattern = self.get_pattern();
if let Some(pattern) = pattern {
sink.write_str(pattern.prefix())?;
}
let range = self.value.magnitude_range();
let upper_magnitude = *range.end();
Expand All @@ -56,8 +68,8 @@ impl<'l> Writeable for FormattedFixedDecimal<'l> {
sink.write_str(&self.symbols.grouping_separator)?;
}
}
if let Some(affixes) = affixes {
sink.write_str(&affixes.suffix)?;
if let Some(pattern) = pattern {
sink.write_str(pattern.suffix())?;
}
Ok(())
}
Expand Down
50 changes: 14 additions & 36 deletions components/decimal/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#![allow(clippy::exhaustive_enums)]

use alloc::borrow::Cow;
use icu_pattern::SinglePlaceholderPattern;
use icu_provider::prelude::*;

#[cfg(feature = "compiled_data")]
Expand Down Expand Up @@ -46,30 +47,6 @@ const _: () = {
/// The latest minimum set of markers required by this component.
pub const MARKERS: &[DataMarkerInfo] = &[DecimalSymbolsV1Marker::INFO];

/// A collection of strings to affix to a decimal number.
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed
/// to be stable, their Rust representation might not be. Use with caution.
/// </div>
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_decimal::provider),
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct AffixesV1<'data> {
/// String to prepend before the decimal number.
#[cfg_attr(feature = "serde", serde(borrow))]
pub prefix: Cow<'data, str>,

/// String to append after the decimal number.
#[cfg_attr(feature = "serde", serde(borrow))]
pub suffix: Cow<'data, str>,
}

/// A collection of settings expressing where to put grouping separators in a decimal number.
/// For example, `1,000,000` has two grouping separators, positioned along every 3 digits.
///
Expand Down Expand Up @@ -117,13 +94,13 @@ pub struct GroupingSizesV1 {
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct DecimalSymbolsV1<'data> {
/// Prefix and suffix to apply when a negative sign is needed.
/// Pattern to apply when a negative sign is needed.
#[cfg_attr(feature = "serde", serde(borrow))]
pub minus_sign_affixes: AffixesV1<'data>,
pub minus_sign_pattern: Option<SinglePlaceholderPattern<Cow<'data, str>>>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strictly speaking this should use a pattern type that requires exactly one placeholder

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you make .suffix() return an Option, you can debug_assert that it is Some.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: why did you make it an Option? The value is only 2 bytes long. Seems like unnecessary indirection? You already have a Cow so the niche is already being used.


/// Prefix and suffix to apply when a plus sign is needed.
/// Pattern to apply when a plus sign is needed.
#[cfg_attr(feature = "serde", serde(borrow))]
pub plus_sign_affixes: AffixesV1<'data>,
pub plus_sign_pattern: Option<SinglePlaceholderPattern<Cow<'data, str>>>,

/// Character used to separate the integer and fraction parts of the number.
#[cfg_attr(feature = "serde", serde(borrow))]
Expand All @@ -141,17 +118,18 @@ pub struct DecimalSymbolsV1<'data> {
pub digits: [char; 10],
}

/// The value used if [`DecimalSymbolsV1::minus_sign_pattern`] is empty.
pub static NEGATIVE_DEFAULT: SinglePlaceholderPattern<Cow<'static, str>> =
SinglePlaceholderPattern::from_store_unchecked(Cow::Borrowed("\x02-"));
/// The value used if [`DecimalSymbolsV1::plus_sign_pattern`] is empty.
pub static POSITIVE_DEFAULT: SinglePlaceholderPattern<Cow<'static, str>> =
SinglePlaceholderPattern::from_store_unchecked(Cow::Borrowed("\x02+"));

impl Default for DecimalSymbolsV1<'static> {
fn default() -> Self {
Self {
minus_sign_affixes: AffixesV1 {
prefix: Cow::Borrowed("-"),
suffix: Cow::Borrowed(""),
},
plus_sign_affixes: AffixesV1 {
prefix: Cow::Borrowed("+"),
suffix: Cow::Borrowed(""),
},
minus_sign_pattern: None,
plus_sign_pattern: None,
decimal_separator: ".".into(),
grouping_separator: ",".into(),
grouping_sizes: GroupingSizesV1 {
Expand Down
3 changes: 2 additions & 1 deletion ffi/capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ casemap = ["dep:icu_casemap"]
collator = ["dep:icu_collator"]
# collections = ["dep:icu_collections"] # Not useful on its own: use properties
datetime = ["dep:icu_datetime", "dep:icu_calendar", "dep:icu_timezone", "dep:icu_decimal", "dep:icu_plurals"]
decimal = ["dep:icu_decimal", "dep:fixed_decimal"]
decimal = ["dep:icu_decimal", "dep:fixed_decimal", "dep:icu_pattern"]
experimental = ["dep:icu_experimental"]
list = ["dep:icu_list"]
locale = ["dep:icu_locale"]
Expand Down Expand Up @@ -144,6 +144,7 @@ icu_properties = { workspace = true, features = ["bidi"], optional = true }
icu_segmenter = { workspace = true, features = ["auto"], optional = true }
icu_timezone = { workspace = true, optional = true }
icu_experimental = { workspace = true, optional = true }
icu_pattern = { workspace = true, features = ["alloc"], optional = true }

# Optional ICU4X features (not components)
icu_provider_blob = { workspace = true, optional = true }
Expand Down
33 changes: 24 additions & 9 deletions ffi/capi/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
use alloc::boxed::Box;
use icu_pattern::SinglePlaceholderPattern;

use crate::{
errors::ffi::DataError, fixed_decimal::ffi::FixedDecimal, locale_core::ffi::Locale,
Expand Down Expand Up @@ -88,7 +89,7 @@ pub mod ffi {
}

use icu_decimal::provider::{
AffixesV1, DecimalSymbolsV1, DecimalSymbolsV1Marker, GroupingSizesV1,
DecimalSymbolsV1, DecimalSymbolsV1Marker, GroupingSizesV1,
};
let mut new_digits = ['\0'; 10];
for (old, new) in digits
Expand All @@ -100,13 +101,27 @@ pub mod ffi {
*new = char::from_u32(old).unwrap_or(char::REPLACEMENT_CHARACTER);
}
let digits = new_digits;
let plus_sign_affixes = AffixesV1 {
prefix: str_to_cow(plus_sign_prefix),
suffix: str_to_cow(plus_sign_suffix),
let plus_sign_pattern = if plus_sign_prefix != b"+" || plus_sign_suffix != b"" {
Some(SinglePlaceholderPattern::from_store_unchecked(Cow::Owned(
SinglePlaceholderPattern::encode_store(
core::str::from_utf8(plus_sign_prefix).unwrap_or_default(),
core::str::from_utf8(plus_sign_suffix).unwrap_or_default(),
)
.into_string(),
)))
} else {
None
};
let minus_sign_affixes = AffixesV1 {
prefix: str_to_cow(minus_sign_prefix),
suffix: str_to_cow(minus_sign_suffix),
let minus_sign_pattern = if minus_sign_prefix != b"-" || minus_sign_suffix != b"" {
Some(SinglePlaceholderPattern::from_store_unchecked(Cow::Owned(
SinglePlaceholderPattern::encode_store(
core::str::from_utf8(minus_sign_prefix).unwrap_or_default(),
core::str::from_utf8(minus_sign_suffix).unwrap_or_default(),
)
.into_string(),
)))
} else {
None
};
let grouping_sizes = GroupingSizesV1 {
primary: primary_group_size,
Expand All @@ -133,8 +148,8 @@ pub mod ffi {
>(
icu_provider::DataPayload::<DecimalSymbolsV1Marker>::from_owned(
DecimalSymbolsV1 {
plus_sign_affixes,
minus_sign_affixes,
plus_sign_pattern,
minus_sign_pattern,
decimal_separator: str_to_cow(decimal_separator),
grouping_separator: str_to_cow(grouping_separator),
grouping_sizes,
Expand Down

Large diffs are not rendered by default.

Loading
Loading