Glob Wildcard Matcher
Match strings against star and question-mark patterns with backtracking that avoids exponential blowup on repeated stars.
Code
fn glob_match(pattern: &str, text: &str) -> bool {
let pattern: Vec<char> = pattern.chars().collect();
let text: Vec<char> = text.chars().collect();
let (mut p, mut t) = (0usize, 0usize);
let mut star: Option<usize> = None;
let mut resume = 0usize;
while t < text.len() {
if p < pattern.len() && (pattern[p] == '?' || pattern[p] == text[t]) {
p += 1;
t += 1;
} else if p < pattern.len() && pattern[p] == '*' {
star = Some(p);
p += 1;
resume = t;
} else if let Some(star_index) = star {
p = star_index + 1;
resume += 1;
t = resume;
} else {
return false;
}
}
while p < pattern.len() && pattern[p] == '*' {
p += 1;
}
p == pattern.len()
}
fn main() {
let cases = [
("*.rs", "main.rs", true),
("*.rs", "main.py", false),
("file-?.txt", "file-a.txt", true),
("file-?.txt", "file-ab.txt", false),
("logs/*.log", "logs/app.log", true),
("a*b*c", "aXXbYYc", true),
("*", "", true),
];
for (pattern, text, expected) in cases {
let actual = glob_match(pattern, text);
println!("{pattern:<12} {text:<14} => {actual} (expected {expected})");
assert_eq!(actual, expected);
}
}How it works
The working surface is the functions glob_match and main — everything else is wiring.
Iteration happens inside while t < text.len() — everything outside it runs once.
Putting it to use
Entry signature: fn glob_match(pattern: &str, text: &str) -> bool
The entry point wires glob_match together — run it once and the whole demo flows.
Core lines
let pattern: Vec<char> = pattern.chars().collect();
let text: Vec<char> = text.chars().collect();
let mut star: Option<usize> = None;
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.CSP Header BuilderBuild a Content-Security-Policy header from per-directive sources, skipping empty ones and rejecting a blanket wildcard.
FAQ
- How do you run Glob Wildcard Matcher?
- Run it as a complete program — compile or interpret the file directly; there is nothing to install first.
Report a problem with this exampleOpens a pre-filled issue on GitHub.