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

Implement dummy sorting for LanguageIdentifier's variants #78

Open
wants to merge 1 commit into
base: master
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
11 changes: 3 additions & 8 deletions unic-langid-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ impl LanguageIdentifier {
variants: &[subtags::Variant],
) -> Self {
let variants = if !variants.is_empty() {
let mut v = variants.to_vec();
v.sort_unstable();
v.dedup();
let v = subtags::Variant::sort_and_deduplicate(variants);
Some(v.into_boxed_slice())
} else {
None
Expand Down Expand Up @@ -298,13 +296,10 @@ impl LanguageIdentifier {
/// assert_eq!(li.to_string(), "ca-ES-valencia");
/// ```
pub fn set_variants(&mut self, variants: &[subtags::Variant]) {
let mut v = variants.to_vec();

if v.is_empty() {
if variants.is_empty() {
self.variants = None;
} else {
v.sort_unstable();
v.dedup();
let v = subtags::Variant::sort_and_deduplicate(variants);
self.variants = Some(v.into_boxed_slice());
}
}
Expand Down
4 changes: 1 addition & 3 deletions unic-langid-impl/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn parse_language_identifier_from_iter<'a>(
} else {
// Variants
if let Ok(v) = subtags::Variant::from_bytes(subtag) {
variants.push(v);
subtags::Variant::push_sorted_and_deduplicated(v, &mut variants);
} else {
break;
}
Expand All @@ -64,8 +64,6 @@ pub fn parse_language_identifier_from_iter<'a>(
let variants = if variants.is_empty() {
None
} else {
variants.sort_unstable();
variants.dedup();
Some(variants.into_boxed_slice())
};

Expand Down
28 changes: 28 additions & 0 deletions unic-langid-impl/src/subtags/variant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::parser::errors::ParserError;
use std::str::FromStr;
use core::cmp::Ordering;
use tinystr::TinyStr8;

#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)]
Expand Down Expand Up @@ -37,6 +38,33 @@ impl Variant {
pub const unsafe fn from_raw_unchecked(v: u64) -> Self {
Self(TinyStr8::from_bytes_unchecked(v.to_le_bytes()))
}

// Utility function to push a variant to a vector, keeping it sorted and deduplicated.
#[inline]
pub(crate) fn push_sorted_and_deduplicated(
item: Variant,
vec: &mut Vec<Variant>,
) {
let mut i = 0;
while i < vec.len() {
match vec[i].cmp(&item) {
Ordering::Less => i += 1,
Ordering::Equal => return,
Ordering::Greater => break,
}
}
vec.insert(i, item);
}

// Utility function to sort and deduplicate a vector of variants.
#[inline]
pub(crate) fn sort_and_deduplicate(vec: &[Variant]) -> Vec<Variant> {
let mut new_vec = Vec::with_capacity(vec.len());
for item in vec {
Self::push_sorted_and_deduplicated(*item, &mut new_vec);
}
new_vec
}
}

impl From<Variant> for u64 {
Expand Down