-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <ydrml@hotmail.com>
- Loading branch information
Showing
5 changed files
with
124 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use regex::Regex; | ||
use std::{borrow::Cow, fmt, sync::OnceLock}; | ||
|
||
pub fn compile_patterns(patterns: &str) -> Regex { | ||
Regex::new(&format!("{}", Patterns(patterns))).unwrap() | ||
} | ||
|
||
struct Patterns<'a>(&'a str); | ||
|
||
impl fmt::Display for Patterns<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
static REGEX: OnceLock<Regex> = OnceLock::new(); | ||
// 匹配任何标识符、点、星号的组合 | ||
let patterns = REGEX | ||
.get_or_init(|| Regex::new(r"[\w*.]+").unwrap()) | ||
.captures_iter(self.0); | ||
|
||
let mut patterns = patterns.into_iter(); | ||
if let Some(pattern) = patterns.next() { | ||
write!(f, "{}", Pattern(&pattern[0]))?; | ||
} | ||
for pattern in patterns { | ||
write!(f, "|{}", Pattern(&pattern[0]))?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
struct Pattern<'a>(&'a str); | ||
|
||
impl fmt::Display for Pattern<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let mut iter = self.0.split('.').map(|s| { | ||
if s.is_empty() { | ||
Cow::Borrowed(r"\w+") | ||
} else if s.chars().all(|c| c == '*') { | ||
Cow::Borrowed(r"(\w+\.)*\w+") | ||
} else { | ||
static REGEX: OnceLock<Regex> = OnceLock::new(); | ||
// 消除任何连续 * | ||
REGEX | ||
.get_or_init(|| Regex::new(r"\*+").unwrap()) | ||
.replace_all(s, r"\w*") | ||
} | ||
}); | ||
|
||
write!(f, "^")?; | ||
if let Some(ele) = iter.next() { | ||
write!(f, "{ele}")?; | ||
} | ||
for ele in iter { | ||
write!(f, r"\.{ele}")?; | ||
} | ||
write!(f, "$") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters