Skip to content

Commit

Permalink
Switch numeric overrides to starting at 17 instead of 64 (#4312)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth authored Nov 18, 2023
1 parent 4d54f71 commit c4a3dde
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 50 deletions.
24 changes: 17 additions & 7 deletions components/datetime/src/fields/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ pub enum FieldLength {
NumericOverride(FieldNumericOverrides),
}

/// First index used for numeric overrides in compact FieldLength representation
///
/// Currently 17 due to decision in <https://unicode-org.atlassian.net/browse/CLDR-17217>,
/// may become 16 if the `> 16` is updated to a ` >= 16`
const FIRST_NUMERIC_OVERRIDE: u8 = 17;
/// First index used for fixed size formats in compact FieldLength representation
const FIRST_FIXED: u8 = 128;

impl FieldLength {
#[inline]
pub(crate) fn idx(&self) -> u8 {
Expand All @@ -69,8 +77,10 @@ impl FieldLength {
FieldLength::Wide => 4,
FieldLength::Narrow => 5,
FieldLength::Six => 6,
FieldLength::NumericOverride(o) => 64 + (*o as u8).min(63),
FieldLength::Fixed(p) => 128 + p.min(&127), /* truncate to at most 127 digits to avoid overflow */
FieldLength::NumericOverride(o) => FIRST_NUMERIC_OVERRIDE
.saturating_add(*o as u8)
.min(FIRST_FIXED - 1),
FieldLength::Fixed(p) => FIRST_FIXED.saturating_add(*p), /* truncate to at most 127 digits to avoid overflow */
}
}

Expand All @@ -84,13 +94,13 @@ impl FieldLength {
5 => Self::Narrow,
6 => Self::Six,
idx => {
if idx < 64 {
if idx < FIRST_NUMERIC_OVERRIDE {
return Err(LengthError::InvalidLength);
}
if idx < 128 {
Self::NumericOverride((idx - 64).try_into()?)
if idx < FIRST_FIXED {
Self::NumericOverride((idx - FIRST_NUMERIC_OVERRIDE).try_into()?)
} else {
Self::Fixed(idx - 128)
Self::Fixed(idx - FIRST_FIXED)
}
}
})
Expand All @@ -106,7 +116,7 @@ impl FieldLength {
FieldLength::Wide => 4,
FieldLength::Narrow => 5,
FieldLength::Six => 6,
FieldLength::NumericOverride(o) => 64 + o as usize,
FieldLength::NumericOverride(o) => FIRST_NUMERIC_OVERRIDE as usize + o as usize,
FieldLength::Fixed(p) => p as usize,
}
}
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

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

Loading

0 comments on commit c4a3dde

Please sign in to comment.