MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / glob_match

Function glob_match

nodedb/src/engine/kv/scan.rs:115–143  ·  view source on GitHub ↗

Glob matching: `*` matches zero or more bytes, `?` matches exactly one byte.

(pattern: &[u8], input: &[u8])

Source from the content-addressed store, hash-verified

113
114/// Glob matching: `*` matches zero or more bytes, `?` matches exactly one byte.
115pub(crate) fn glob_match(pattern: &[u8], input: &[u8]) -> bool {
116 let mut pi = 0;
117 let mut ii = 0;
118 let mut star_pi = usize::MAX;
119 let mut star_ii = 0;
120
121 while ii < input.len() {
122 if pi < pattern.len() && (pattern[pi] == b'?' || pattern[pi] == input[ii]) {
123 pi += 1;
124 ii += 1;
125 } else if pi < pattern.len() && pattern[pi] == b'*' {
126 star_pi = pi;
127 star_ii = ii;
128 pi += 1;
129 } else if star_pi != usize::MAX {
130 pi = star_pi + 1;
131 star_ii += 1;
132 ii = star_ii;
133 } else {
134 return false;
135 }
136 }
137
138 while pi < pattern.len() && pattern[pi] == b'*' {
139 pi += 1;
140 }
141
142 pi == pattern.len()
143}
144
145#[cfg(test)]
146mod tests {

Callers 2

handle_psubscribeFunction · 0.85
matches_patternFunction · 0.85

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected