63 lines
1.5 KiB
V
63 lines
1.5 KiB
V
// file_matcher.v
|
|
module file_matcher
|
|
|
|
// 匹配模式枚举
|
|
pub enum MatchMode {
|
|
exact // 精确匹配
|
|
wildcard // 通配符匹配 (* 和 _)
|
|
like // 模糊匹配,忽略大小写
|
|
}
|
|
|
|
// 通配符匹配函数
|
|
fn wildcard_match(pattern string, name string) bool {
|
|
mut i := 0
|
|
mut j := 0
|
|
mut star_idx := -1
|
|
mut match_idx := -1
|
|
|
|
for i < name.len {
|
|
if j < pattern.len && (pattern[j] == name[i] || pattern[j] == `_`) {
|
|
i++
|
|
j++
|
|
} else if j < pattern.len && pattern[j] == `*` {
|
|
star_idx = j
|
|
match_idx = i
|
|
j++
|
|
} else if star_idx != -1 {
|
|
j = star_idx + 1
|
|
match_idx++
|
|
i = match_idx
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// 处理模式末尾的 *
|
|
for j < pattern.len && pattern[j] == `*` {
|
|
j++
|
|
}
|
|
|
|
return j == pattern.len
|
|
}
|
|
|
|
// 主匹配函数
|
|
pub fn match_file(pattern string, filename string, mode MatchMode) bool {
|
|
match mode {
|
|
.exact {
|
|
return filename == pattern
|
|
}
|
|
.wildcard {
|
|
return wildcard_match(pattern, filename)
|
|
}
|
|
.like {
|
|
// 移除%符号,进行模糊匹配
|
|
clean_pattern := pattern.replace('%', '')
|
|
return filename.to_lower().contains(clean_pattern.to_lower())
|
|
}
|
|
}
|
|
}
|
|
|
|
// 检查是否为通配符模式
|
|
pub fn is_wildcard_pattern(pattern string) bool {
|
|
return pattern.contains('*') || pattern.contains('_')
|
|
} |