From 83ec663929f55ca0a2a03f10e900aac21db1e810 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 16 Mar 2025 19:53:29 +0200 Subject: [PATCH] skip regex operations on tag mismatch subject --- src/line/code.rs | 2 ++ src/line/code/inline.rs | 7 +++++++ src/line/code/multiline.rs | 3 ++- src/line/link.rs | 7 +++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/line/code.rs b/src/line/code.rs index 4d5e21b..60345bb 100644 --- a/src/line/code.rs +++ b/src/line/code.rs @@ -3,3 +3,5 @@ pub mod multiline; pub use inline::Inline; pub use multiline::Multiline; + +pub const TAG: &str = "```"; diff --git a/src/line/code/inline.rs b/src/line/code/inline.rs index 1148966..3569a29 100644 --- a/src/line/code/inline.rs +++ b/src/line/code/inline.rs @@ -1,3 +1,4 @@ +use super::TAG; use glib::{Regex, RegexCompileFlags, RegexMatchFlags}; /// Inline [preformatted](https://geminiprotocol.net/docs/gemtext-specification.gmi#in-pre-formatted-mode) entity holder @@ -10,6 +11,12 @@ impl Inline { /// Parse `Self` from line string pub fn from(line: &str) -> Option { + // Skip next operations on prefix and postfix mismatch `TAG` + // * replace regex implementation @TODO + if !line.starts_with(TAG) && !line.ends_with(TAG) { + return None; + } + // Parse line let regex = Regex::split_simple( r"^`{3}([^`]+)`{3}$", diff --git a/src/line/code/multiline.rs b/src/line/code/multiline.rs index a046445..2e1ef32 100644 --- a/src/line/code/multiline.rs +++ b/src/line/code/multiline.rs @@ -1,10 +1,11 @@ +use super::TAG; + pub mod error; pub use error::Error; // Shared defaults pub const NEW_LINE: char = '\n'; -pub const TAG: &str = "```"; /// Multi-line [preformatted](https://geminiprotocol.net/docs/gemtext-specification.gmi#in-pre-formatted-mode) entity holder pub struct Multiline { diff --git a/src/line/link.rs b/src/line/link.rs index 29bbab5..b193032 100644 --- a/src/line/link.rs +++ b/src/line/link.rs @@ -1,5 +1,7 @@ use glib::{DateTime, Regex, RegexCompileFlags, RegexMatchFlags, TimeZone, Uri, UriFlags}; +pub const TAG: &str = "=>"; + /// [Link](https://geminiprotocol.net/docs/gemtext-specification.gmi#link-lines) entity holder pub struct Link { pub alt: Option, // [optional] alternative link description @@ -12,6 +14,11 @@ impl Link { /// Parse `Self` from line string pub fn from(line: &str, base: Option<&Uri>, timezone: Option<&TimeZone>) -> Option { + // Skip next operations on prefix mismatch + // * replace regex implementation @TODO + if !line.starts_with(TAG) { + return None; + } // Define initial values let mut alt = None; let mut timestamp = None;