Skip to content

Commit c1b8ff7

Browse files
committed
Fix hoisting in filtering
1 parent 26b0037 commit c1b8ff7

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/negotiate/mod.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,39 +167,40 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
167167

168168
let mut available_locales: Vec<&A> = available.iter().collect();
169169

170-
for req in requested {
171-
let req = req.as_ref();
172-
macro_rules! test_strategy {
173-
($self_as_range:expr, $other_as_range:expr) => {{
174-
let mut match_found = false;
175-
available_locales.retain(|locale| {
176-
if strategy != NegotiationStrategy::Filtering && match_found {
177-
return true;
178-
}
170+
macro_rules! test_strategy {
171+
($req:ident, $self_as_range:expr, $other_as_range:expr) => {{
172+
let mut match_found = false;
173+
available_locales.retain(|locale| {
174+
if strategy != NegotiationStrategy::Filtering && match_found {
175+
return true;
176+
}
179177

180-
if matches(locale.as_ref(), &req, $self_as_range, $other_as_range) {
181-
match_found = true;
182-
supported_locales.push(*locale);
183-
return false;
184-
}
185-
true
186-
});
178+
if matches(locale.as_ref(), &$req, $self_as_range, $other_as_range) {
179+
match_found = true;
180+
supported_locales.push(*locale);
181+
return false;
182+
}
183+
true
184+
});
187185

188-
if match_found {
189-
match strategy {
190-
NegotiationStrategy::Filtering => {}
191-
NegotiationStrategy::Matching => continue,
192-
NegotiationStrategy::Lookup => break,
193-
}
186+
if match_found {
187+
match strategy {
188+
NegotiationStrategy::Filtering => {}
189+
NegotiationStrategy::Matching => continue,
190+
NegotiationStrategy::Lookup => break,
194191
}
195-
}};
196-
}
192+
}
193+
}};
194+
}
195+
196+
for req in requested {
197+
let req = req.as_ref();
197198

198199
// 1) Try to find a simple (case-insensitive) string match for the request.
199-
test_strategy!(false, false);
200+
test_strategy!(req, false, false);
200201

201202
// 2) Try to match against the available locales treated as ranges.
202-
test_strategy!(true, false);
203+
test_strategy!(req, true, false);
203204

204205
// Per Unicode TR35, 4.4 Locale Matching, we don't add likely subtags to
205206
// requested locales, so we'll skip it from the rest of the steps.
@@ -210,22 +211,22 @@ pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<Langu
210211
let mut req = req.to_owned();
211212
// 3) Try to match against a maximized version of the requested locale
212213
if req.maximize() {
213-
test_strategy!(true, false);
214+
test_strategy!(req, true, false);
214215
}
215216

216217
// 4) Try to match against a variant as a range
217218
req.variants.clear();
218-
test_strategy!(true, true);
219+
test_strategy!(req, true, true);
219220

220221
// 5) Try to match against the likely subtag without region
221222
req.region = None;
222223
if req.maximize() {
223-
test_strategy!(true, false);
224+
test_strategy!(req, true, false);
224225
}
225226

226227
// 6) Try to match against a region as a range
227228
req.region = None;
228-
test_strategy!(true, true);
229+
test_strategy!(req, true, true);
229230
}
230231

231232
supported_locales

0 commit comments

Comments
 (0)