Skip to content

Commit

Permalink
Update Locale and LanguageIdentifier comparison names (#2020)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Jun 8, 2022
1 parent a8c8d2c commit 34ca520
Show file tree
Hide file tree
Showing 111 changed files with 306 additions and 279 deletions.
11 changes: 7 additions & 4 deletions components/locid/benches/helpers/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ macro_rules! overview {
let value: Result<$struct, _> = black_box(s).parse();
values.push(value.expect("Parsing failed"));
}
let _ = values.iter().filter(|v| *v == $compare).count();
let _ = values
.iter()
.filter(|&v| v.normalizing_eq($compare))
.count();

let mut strings = vec![];
for value in &values {
Expand Down Expand Up @@ -76,14 +79,14 @@ macro_rules! compare_str {
$c.bench_function(BenchmarkId::new("str", $struct_name), |b| {
b.iter(|| {
for (lid, s) in $data1.iter().zip($data2.iter()) {
let _ = black_box(lid) == &black_box(s).as_str();
let _ = black_box(lid).normalizing_eq(&black_box(s));
}
})
});
$c.bench_function(BenchmarkId::new("cmp_bytes", $struct_name), |b| {
$c.bench_function(BenchmarkId::new("strict_cmp", $struct_name), |b| {
b.iter(|| {
for (lid, s) in $data1.iter().zip($data2.iter()) {
let _ = black_box(lid).cmp_bytes(&black_box(s).as_str().as_bytes());
let _ = black_box(lid).strict_cmp(&black_box(s).as_str().as_bytes());
}
})
});
Expand Down
8 changes: 4 additions & 4 deletions components/locid/benches/iai_langid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ fn bench_langid_compare_components_str() {
assert_eq!(result, 2);
}

fn bench_langid_cmp_bytes() {
fn bench_langid_strict_cmp() {
// Tests the cost of comparing a langid against byte strings.
use core::cmp::Ordering;

let lid = langid!("en_us");

let result = LIDS_STR
.iter()
.filter(|s| lid.cmp_bytes(s.as_bytes()) == Ordering::Equal)
.filter(|s| lid.strict_cmp(s.as_bytes()) == Ordering::Equal)
.count();

assert_eq!(result, 1);
Expand All @@ -83,7 +83,7 @@ fn bench_langid_matching_str() {

let lid = langid!("en_us");

let count = LIDS_STR.iter().filter(|l| lid == **l).count();
let count = LIDS_STR.iter().filter(|&l| lid.normalizing_eq(l)).count();
assert_eq!(count, 1);
}

Expand All @@ -106,7 +106,7 @@ iai::main!(
bench_langid_constr,
bench_langid_compare_components,
bench_langid_compare_components_str,
bench_langid_cmp_bytes,
bench_langid_strict_cmp,
bench_langid_matching,
bench_langid_matching_str,
bench_langid_serialize,
Expand Down
6 changes: 0 additions & 6 deletions components/locid/benches/langid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ fn langid_benches(c: &mut Criterion) {
#[cfg(feature = "bench")]
{
use criterion::BenchmarkId;
use icu_locid::Locale;

// Construct
{
let mut group = c.benchmark_group("langid/construct");

construct!(group, LanguageIdentifier, "langid", &data.canonicalized);
construct!(group, Locale, "locale", &data.canonicalized);

group.finish();
}
Expand All @@ -48,7 +46,6 @@ fn langid_benches(c: &mut Criterion) {
.collect();

to_string!(group, LanguageIdentifier, "langid", &langids);
to_string!(group, Locale, "locale", &langids);

group.finish();
}
Expand All @@ -69,7 +66,6 @@ fn langid_benches(c: &mut Criterion) {
.collect();

compare_struct!(group, LanguageIdentifier, "langid", &langids, &langids2);
compare_struct!(group, Locale, "locale", &langids, &langids2);

compare_str!(
group,
Expand All @@ -78,7 +74,6 @@ fn langid_benches(c: &mut Criterion) {
&langids,
&data.canonicalized
);
compare_str!(group, Locale, "locale", &langids, &data.canonicalized);

group.finish();
}
Expand All @@ -88,7 +83,6 @@ fn langid_benches(c: &mut Criterion) {
let mut group = c.benchmark_group("langid/canonicalize");

canonicalize!(group, LanguageIdentifier, "langid", &data.casing);
canonicalize!(group, Locale, "locale", &data.casing);

group.finish();
}
Expand Down
4 changes: 2 additions & 2 deletions components/locid/src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ impl Extensions {
/// only_unicode
/// .extensions
/// .retain_by_type(|t| t == ExtensionType::Unicode);
/// assert_eq!(only_unicode, "und-u-world");
/// assert_eq!(only_unicode, "und-u-world".parse().unwrap());
///
/// let mut only_t_z = loc.clone();
/// only_t_z
/// .extensions
/// .retain_by_type(|t| t == ExtensionType::Transform || t == ExtensionType::Other(b'z'));
/// assert_eq!(only_t_z, "und-t-mul-z-zzz");
/// assert_eq!(only_t_z, "und-t-mul-z-zzz".parse().unwrap());
/// ```
pub fn retain_by_type<F>(&mut self, mut predicate: F)
where
Expand Down
6 changes: 3 additions & 3 deletions components/locid/src/extensions/transform/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Fields {
/// let old_value = loc.extensions.transform.fields.set(D0_KEY, lower);
///
/// assert_eq!(old_value, Some(casefold));
/// assert_eq!(loc, "en-t-hi-d0-lower");
/// assert_eq!(loc, "en-t-hi-d0-lower".parse().unwrap());
/// ```
pub fn set(&mut self, key: Key, value: Value) -> Option<Value> {
self.0.insert(key, value)
Expand All @@ -177,10 +177,10 @@ impl Fields {
/// let mut loc: Locale = "und-t-h0-hybrid-d0-hex-m0-xml".parse().unwrap();
///
/// loc.extensions.transform.fields.retain_by_key(|k| k == "h0");
/// assert_eq!(loc, "und-t-h0-hybrid");
/// assert_eq!(loc, "und-t-h0-hybrid".parse().unwrap());
///
/// loc.extensions.transform.fields.retain_by_key(|k| k == "d0");
/// assert_eq!(loc, "und");
/// assert_eq!(loc, Locale::UND);
/// ```
pub fn retain_by_key<F>(&mut self, mut predicate: F)
where
Expand Down
2 changes: 1 addition & 1 deletion components/locid/src/extensions/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Transform {
///
/// let mut loc: Locale = "en-US-t-es-AR".parse().unwrap();
/// loc.extensions.transform.clear();
/// assert_eq!(loc, "en-US");
/// assert_eq!(loc, "en-US".parse().unwrap());
/// ```
pub fn clear(&mut self) {
self.lang = None;
Expand Down
10 changes: 5 additions & 5 deletions components/locid/src/extensions/unicode/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl Keywords {
/// let old_value = loc.extensions.unicode.keywords.set(CA_KEY, japanese);
///
/// assert_eq!(old_value, Some(buddhist));
/// assert_eq!(loc, "und-u-hello-ca-japanese-hc-h12");
/// assert_eq!(loc, "und-u-hello-ca-japanese-hc-h12".parse().unwrap());
/// ```
pub fn set(&mut self, key: Key, value: Value) -> Option<Value> {
self.0.insert(key, value)
Expand All @@ -219,7 +219,7 @@ impl Keywords {
/// .parse()
/// .expect("valid BCP-47 identifier");
/// loc.extensions.unicode.keywords.remove(&CA_KEY);
/// assert_eq!(loc, "und-u-hello-hc-h12");
/// assert_eq!(loc, "und-u-hello-hc-h12".parse().unwrap());
/// ```
pub fn remove<Q>(&mut self, key: &Q) -> Option<Value>
where
Expand All @@ -239,7 +239,7 @@ impl Keywords {
///
/// let mut loc: Locale = "und-u-hello-ca-buddhist-hc-h12".parse().unwrap();
/// loc.extensions.unicode.keywords.clear();
/// assert_eq!(loc, "und-u-hello");
/// assert_eq!(loc, "und-u-hello".parse().unwrap());
/// ```
pub fn clear(&mut self) {
self.0.clear();
Expand All @@ -256,10 +256,10 @@ impl Keywords {
/// let mut loc: Locale = "und-u-ca-buddhist-hc-h12-ms-metric".parse().unwrap();
///
/// loc.extensions.unicode.keywords.retain_by_key(|k| k == "hc");
/// assert_eq!(loc, "und-u-hc-h12");
/// assert_eq!(loc, "und-u-hc-h12".parse().unwrap());
///
/// loc.extensions.unicode.keywords.retain_by_key(|k| k == "ms");
/// assert_eq!(loc, "und");
/// assert_eq!(loc, Locale::UND);
/// ```
pub fn retain_by_key<F>(&mut self, mut predicate: F)
where
Expand Down
2 changes: 1 addition & 1 deletion components/locid/src/extensions/unicode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Unicode {
///
/// let mut loc: Locale = "und-t-mul-u-hello-ca-buddhist-hc-h12".parse().unwrap();
/// loc.extensions.unicode.clear();
/// assert_eq!(loc, "und-t-mul");
/// assert_eq!(loc, "und-t-mul".parse().unwrap());
/// ```
pub fn clear(&mut self) {
self.keywords.clear();
Expand Down
Loading

0 comments on commit 34ca520

Please sign in to comment.