Skip to content

Commit

Permalink
allow mixing regex and glob patterns in hash list filenames.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenchou committed Aug 31, 2024
1 parent adfa758 commit 6a47e80
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/pmatcher.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::path::{Path,PathBuf};
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

use fancy_regex::Regex;
use md5::{Digest, Md5};

use crate::pconfig;
use crate::fnmatch_regex;
use crate::pconfig;

#[derive(Debug)]
pub struct PatternMatcher {
Expand Down Expand Up @@ -79,31 +79,27 @@ impl PatternMatcher {
fn create_patterns_with_hash(patterns: HashMap<String, Vec<String>>) -> Vec<(Regex, Vec<String>)> {
patterns
.into_iter()
.map(|(key, value)| {
// println!("hash --> {}", key);
(
Regex::new(fnmatch_regex::glob_to_regex_string(&key).as_str()).unwrap(),
value,
)
})
.map(|(key, value)| (parse_mixed_regex(&key), value))
.collect()
}

fn parse_mixed_regex(pattern: &str) -> Regex {
let pattern = pattern.trim();
// println!(">>> {:#?}", pattern);
if let Some(stripped) = pattern.strip_prefix('/') {
Regex::new(stripped).unwrap()
} else {
Regex::new(fnmatch_regex::glob_to_regex_string(pattern).as_str()).unwrap()
}
}

/**
* 创建正则表达式列表,通配符形式转为正则表达式
*/
fn create_mixed_regex_list(patterns: Vec<&str>) -> Vec<Regex> {
patterns
.iter()
.map(|pattern| {
let pattern = pattern.trim();
// println!(">>> {:#?}", pattern);
if let Some(stripped) = pattern.strip_prefix('/') {
Regex::new(stripped).unwrap()
} else {
Regex::new(fnmatch_regex::glob_to_regex_string(pattern).as_str()).unwrap()
}
})
.map(|pattern| parse_mixed_regex(pattern))
.collect()
}

Expand Down

0 comments on commit 6a47e80

Please sign in to comment.