1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-22 07:47:44 +02:00

Simplify solution to prefix matching.

This commit is contained in:
Zhou Fang 2023-01-02 22:27:40 -08:00 committed by Zhou Fang
parent 2a92487bbd
commit 1fb0b0a46d

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