From 9b0e10ddaeacda3da9d589f7577bcf364609eb9c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 21 Jan 2026 14:50:53 +0000 Subject: fix(html,url): Resolve clippy pedantic lints and reduce allocations --- src/html.rs | 31 ++++++++++++++++--------------- src/url.rs | 33 ++++++++++++++------------------- 2 files changed, 30 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/html.rs b/src/html.rs index 341286c..ae79489 100644 --- a/src/html.rs +++ b/src/html.rs @@ -76,14 +76,18 @@ pub fn from_gemini( let align_adjacent_links = |html: &str| { if previous_link_count > 0 { - html - .chars() - .rev() - .collect::() - .replacen(&GEMINI_FRAGMENT.chars().rev().collect::(), "", 1) - .chars() - .rev() - .collect::() + html.rfind(GEMINI_FRAGMENT).map_or_else( + || html.to_string(), + |position| { + let mut result = + String::with_capacity(html.len() - GEMINI_FRAGMENT.len()); + + result.push_str(&html[..position]); + result.push_str(&html[position + GEMINI_FRAGMENT.len()..]); + + result + }, + ) } else { html.to_string() } @@ -121,7 +125,7 @@ pub fn from_gemini( let _ = write!(&mut html, "

{}

", safe(text)); } Node::Link { to, text } => { - let mut href = to.to_string(); + let mut href = to.clone(); let mut surface = false; if href.starts_with("./") || href.starts_with("../") { @@ -133,7 +137,7 @@ pub fn from_gemini( if href.contains("://") && !href.starts_with("gemini://") { surface = true; } else if !href.contains("://") && href.contains(':') { - href = href.to_string(); + // href contains a scheme-like pattern (e.g., mailto:), keep as-is } else if !href.starts_with("gemini://") && !href.starts_with('/') { href = format!( "{}/{}", @@ -255,7 +259,7 @@ pub fn from_gemini( } if title.is_empty() && *level == 1 { - title = safe(text).to_string(); + title = safe(text); } let _ = write!( @@ -285,10 +289,7 @@ pub fn from_gemini( let _ = write!(&mut html, "
{}
", safe(text)); } Node::PreformattedText { text, .. } => { - let mut new_text = text.to_string(); - - new_text.pop(); - + let new_text = text.strip_suffix('\n').unwrap_or(text); let _ = write!(&mut html, "
{new_text}
"); } Node::Whitespace => {} diff --git a/src/url.rs b/src/url.rs index 5289d28..56858e6 100644 --- a/src/url.rs +++ b/src/url.rs @@ -63,11 +63,11 @@ pub fn matches_pattern(pattern: &str, path: &str) -> bool { return path == pattern; } - let parts: Vec<&str> = pattern.split('*').collect(); + let mut parts = pattern.split('*').peekable(); let mut position = if pattern.starts_with('*') { 0 } else { - let first = parts.first().unwrap(); + let first = parts.next().unwrap_or(""); if !path.starts_with(first) { return false; @@ -75,25 +75,20 @@ pub fn matches_pattern(pattern: &str, path: &str) -> bool { first.len() }; - let before_last = parts.len().saturating_sub(1); - for part in &parts[1..before_last] { - if part.is_empty() { - continue; - } + while let Some(part) = parts.next() { + let is_last = parts.peek().is_none(); - if let Some(found) = path[position..].find(part) { - position += found + part.len(); - } else { - return false; - } - } - - if !pattern.ends_with('*') { - let last = parts.last().unwrap(); - - if !path[position..].ends_with(last) { - return false; + if is_last { + if !pattern.ends_with('*') && !path[position..].ends_with(part) { + return false; + } + } else if !part.is_empty() { + if let Some(found) = path[position..].find(part) { + position += found + part.len(); + } else { + return false; + } } } -- cgit v1.2.3