From e544ef6ca51c2b3a05b3b8ecca6220a16193d845 Mon Sep 17 00:00:00 2001 From: Sergio <77530549+sergi0g@users.noreply.github.com> Date: Fri, 6 Dec 2024 17:47:31 +0200 Subject: [PATCH] Slightly optimize version regex --- src/structs/version.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/structs/version.rs b/src/structs/version.rs index b090cc4..bc32363 100644 --- a/src/structs/version.rs +++ b/src/structs/version.rs @@ -5,12 +5,6 @@ use regex::Regex; use super::status::Status; -/// Heavily modified version of the official semver regex based on common tagging schemes for container images. Sometimes it matches more than once, but we'll try to select the best match. -static SEMVER_REGEX: Lazy = Lazy::new(|| { - Regex::new(r#"(?P0|[1-9]\d*)(?:\.(?P0|[1-9]\d*))?(?:\.(?P0|[1-9]\d*)+)?"#) - .unwrap() -}); - /// Semver-like version struct #[derive(Debug, PartialEq, Eq, Clone)] pub struct Version { @@ -22,7 +16,14 @@ pub struct Version { impl Version { /// Tries to parse the tag into semver-like parts. pub fn from_tag(tag: &str) -> Option { - let captures = SEMVER_REGEX.captures_iter(tag); + /// Heavily modified version of the official semver regex based on common tagging schemes for container images. Sometimes it matches more than once, but we'll try to select the best match. + static VERSION_REGEX: Lazy = Lazy::new(|| { + Regex::new( + r"(?P0|[1-9][0-9]*)(?:\.(?P0|[1-9][0-9]*))?(?:\.(?P0|[1-9][0-9]*))?", + ) + .unwrap() + }); + let captures = VERSION_REGEX.captures_iter(tag); // And now... terrible best match selection for everyone! let mut max_matches = 0; let mut best_match = None;