Skip to content

Commit

Permalink
Merge 53f051c into ab44ba0
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Jan 16, 2020
2 parents ab44ba0 + 53f051c commit 9a1e076
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/negotiate/mod.rs
Expand Up @@ -141,17 +141,23 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
let mut supported_locales = vec![];

let mut available_locales: Vec<&A> = available.iter().collect();
let requested_locales: Vec<&LanguageIdentifier> =
requested.iter().map(|r| r.as_ref()).collect();

for req in requested {
let mut req = req.as_ref().to_owned();
for (idx, req) in requested_locales.iter().enumerate() {
let mut req = (*req).to_owned();
macro_rules! test_strategy {
($self_as_range:expr, $other_as_range:expr) => {{
($self_as_range:expr, $other_as_range:expr, $exact_match:expr) => {{
let mut match_found = false;
available_locales.retain(|locale| {
if strategy != NegotiationStrategy::Filtering && match_found {
return true;
}

if !$exact_match && requested_locales[idx + 1..].contains(&locale.as_ref()) {
return true;
}

if locale
.as_ref()
.matches(&req, $self_as_range, $other_as_range)
Expand All @@ -174,10 +180,10 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
}

// 1) Try to find a simple (case-insensitive) string match for the request.
test_strategy!(false, false);
test_strategy!(false, false, true);

// 2) Try to match against the available locales treated as ranges.
test_strategy!(true, false);
test_strategy!(true, false, false);

// Per Unicode TR35, 4.4 Locale Matching, we don't add likely subtags to
// requested locales, so we'll skip it from the rest of the steps.
Expand All @@ -187,22 +193,22 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu

// 3) Try to match against a maximized version of the requested locale
if req.add_likely_subtags() {
test_strategy!(true, false);
test_strategy!(true, false, false);
}

// 4) Try to match against a variant as a range
req.clear_variants();
test_strategy!(true, true);
test_strategy!(true, true, false);

// 5) Try to match against the likely subtag without region
req.clear_region();
if req.add_likely_subtags() {
test_strategy!(true, false);
test_strategy!(true, false, false);
}

// 6) Try to match against a region as a range
req.clear_region();
test_strategy!(true, true);
test_strategy!(true, true, false);
}

supported_locales
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/negotiate/filtering/prioritize.json
Expand Up @@ -26,5 +26,9 @@
{
"input": [["fr-CA-macos", "de-DE"], ["de-DE", "fr-FR-windows"]],
"output": ["fr-FR-windows", "de-DE"]
},
{
"input": [["en-US", "fr", "en-CA"], ["en-US", "en-CA", "fr"]],
"output": ["en-US", "fr", "en-CA"]
}
]

0 comments on commit 9a1e076

Please sign in to comment.