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 a higher numeric value for Strength::Identical #1942

Merged
merged 1 commit into from
May 26, 2022
Merged
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
15 changes: 10 additions & 5 deletions experimental/collator/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use crate::elements::{CASE_MASK, TERTIARY_MASK};
/// If an earlier level isn't equal, the earlier level is decisive.
/// If the result is equal on a level, but the strength is higher,
/// the comparison proceeds to the next level.
///
/// Note: The bit layout of `CollatorOptions` requires `Strength`
/// to fit in 3 bits.
#[derive(Eq, PartialEq, Debug, PartialOrd, Ord)]
#[repr(u8)]
pub enum Strength {
Expand Down Expand Up @@ -185,7 +188,7 @@ pub enum Strength {
/// assert_eq!(collator.compare("דחי", "דחי֭"),
/// core::cmp::Ordering::Less);
/// ```
Identical = 4,
Identical = 7,
}

/// What to do about characters whose comparison level can be
Expand Down Expand Up @@ -335,11 +338,13 @@ impl CollatorOptions {
/// This is the BCP47 key `ks`.
pub fn strength(&self) -> Strength {
let mut bits = self.0 & CollatorOptions::STRENGTH_MASK;
if bits > 4 {
debug_assert!(false, "Bad value for strength");
bits = 4;
if !(bits <= 3 || bits == 7) {
debug_assert!(false, "Bad value for strength.");
// If the bits say higher than `Quaternary` but
// lower than `Identical`, clamp to `Quaternary`.
bits = 3;
}
// By construction in range and, therefore,
// By construction above in range and, therefore,
// never UB.
unsafe { core::mem::transmute(bits as u8) }
}
Expand Down