mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-18 00:11:08 +08:00
perf: optimize filename matching logic in IProfiles by using a static regex
This commit is contained in:
parent
12c55421d0
commit
782e8d3352
@ -5,12 +5,16 @@ use crate::utils::{
|
|||||||
};
|
};
|
||||||
use anyhow::{Context as _, Result, bail};
|
use anyhow::{Context as _, Result, bail};
|
||||||
use clash_verge_logging::{Type, logging};
|
use clash_verge_logging::{Type, logging};
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
|
use regex::Regex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_yaml_ng::Mapping;
|
use serde_yaml_ng::Mapping;
|
||||||
use smartstring::alias::String;
|
use smartstring::alias::String;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
||||||
|
static PROFILE_FILE_RE: OnceCell<Regex> = OnceCell::new();
|
||||||
|
|
||||||
/// Define the `profiles.yaml` schema
|
/// Define the `profiles.yaml` schema
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct IProfiles {
|
pub struct IProfiles {
|
||||||
@ -497,18 +501,10 @@ impl IProfiles {
|
|||||||
// p12345678.yaml (proxies)
|
// p12345678.yaml (proxies)
|
||||||
// g12345678.yaml (groups)
|
// g12345678.yaml (groups)
|
||||||
|
|
||||||
let patterns = [
|
#[allow(clippy::unwrap_used)]
|
||||||
r"^[RL][a-zA-Z0-9]+\.yaml$", // Remote/Local profiles
|
let re = PROFILE_FILE_RE
|
||||||
r"^m[a-zA-Z0-9]+\.yaml$", // Merge files
|
.get_or_init(|| Regex::new(r"^(?:[RLmprg][a-zA-Z0-9_-]+\.yaml|s[a-zA-Z0-9_-]+\.js)$").unwrap());
|
||||||
r"^s[a-zA-Z0-9]+\.js$", // Script files
|
re.is_match(filename)
|
||||||
r"^[rpg][a-zA-Z0-9]+\.yaml$", // Rules/Proxies/Groups files
|
|
||||||
];
|
|
||||||
|
|
||||||
patterns.iter().any(|pattern| {
|
|
||||||
regex::Regex::new(pattern)
|
|
||||||
.map(|re| re.is_match(filename))
|
|
||||||
.unwrap_or(false)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user