mirror of
https://github.com/RGBCube/dix
synced 2025-07-28 12:17:45 +00:00
main: strip nix store paths
the paths we match with regex are always prefixed with /nix/store/<...>-, which amounts to 44 chars. Since this is always the same, we can just strip these 44 chars before matching with regex. This reduces both complexity of the regex and performance.
This commit is contained in:
parent
c180c26bf5
commit
d0e538b1fb
1 changed files with 8 additions and 2 deletions
10
src/main.rs
10
src/main.rs
|
@ -254,7 +254,7 @@ fn main() {
|
|||
fn store_path_regex() -> &'static Regex {
|
||||
static REGEX: OnceLock<Regex> = OnceLock::new();
|
||||
REGEX.get_or_init(|| {
|
||||
Regex::new(r"^/nix/store/[a-z0-9]+-(.+?)-([0-9].*?)$")
|
||||
Regex::new(r"(.+?)-([0-9].*?)$")
|
||||
.expect("Failed to compile regex pattern for nix store paths")
|
||||
})
|
||||
}
|
||||
|
@ -263,8 +263,14 @@ fn store_path_regex() -> &'static Regex {
|
|||
fn get_version<'a>(pack: impl Into<&'a str>) -> Result<(&'a str, &'a str)> {
|
||||
let path = pack.into();
|
||||
|
||||
// We can strip the path since it _always_ starts with
|
||||
// /nix/store/....-
|
||||
// This part is exactly 44 chars long, so we just remove it.
|
||||
let stripped_path = &path[44..];
|
||||
debug!("Stripped path: {stripped_path}");
|
||||
|
||||
// Match the regex against the input
|
||||
if let Some(cap) = store_path_regex().captures(path) {
|
||||
if let Some(cap) = store_path_regex().captures(stripped_path) {
|
||||
// Handle potential missing captures safely
|
||||
let name = cap.get(1).map_or("", |m| m.as_str());
|
||||
let version = cap.get(2).map_or("", |m| m.as_str());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue