1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-24 15:29:28 +02:00

Merge pull request #96 from zhouwfang/simplify-solution-to-prefix-matching

Simplify the solution to prefix matching
This commit is contained in:
Martin Geisler 2023-01-04 09:11:58 +01:00 committed by GitHub
commit c2c11531a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,25 +15,20 @@
// ANCHOR: prefix_matches // ANCHOR: prefix_matches
pub fn prefix_matches(prefix: &str, request_path: &str) -> bool { pub fn prefix_matches(prefix: &str, request_path: &str) -> bool {
// ANCHOR_END: prefix_matches // ANCHOR_END: prefix_matches
let mut prefixes = prefix let prefixes = prefix.split('/');
.split('/') let request_paths = request_path
.map(|p| Some(p))
.chain(std::iter::once(None));
let mut request_paths = request_path
.split('/') .split('/')
.map(|p| Some(p)) .map(|p| Some(p))
.chain(std::iter::once(None)); .chain(std::iter::once(None));
for (prefix, request_path) in prefixes.by_ref().zip(&mut request_paths) { for (prefix, request_path) in prefixes.zip(request_paths) {
match (prefix, request_path) { match request_path {
(Some(prefix), Some(request_path)) => { Some(request_path) => {
if (prefix != "*") && (prefix != request_path) { if (prefix != "*") && (prefix != request_path) {
return false; return false;
} }
} }
(Some(_), None) => return false, None => return false,
(None, None) => break,
(None, Some(_)) => break,
} }
} }
true true