From 850ac0831fcd50ea8ed8514ae42fd07acce897bb Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 25 Jul 2026 17:58:56 +0000 Subject: [PATCH] refactor: use the new regex! macro It was introduced in regex crate 1.13.0 --- deltachat-contact-tools/src/lib.rs | 7 ++----- deltachat-contact-tools/src/vcard.rs | 8 ++------ src/receive_imf.rs | 12 +++++------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/deltachat-contact-tools/src/lib.rs b/deltachat-contact-tools/src/lib.rs index 1a958d51f..678322b36 100644 --- a/deltachat-contact-tools/src/lib.rs +++ b/deltachat-contact-tools/src/lib.rs @@ -29,11 +29,10 @@ use std::fmt; use std::ops::Deref; -use std::sync::LazyLock; use anyhow::bail; use anyhow::Result; -use regex::Regex; +use regex::regex; mod vcard; pub use vcard::{make_vcard, parse_vcard, VcardContact}; @@ -88,9 +87,7 @@ impl rusqlite::types::ToSql for ContactAddress { /// - Removes special characters from the name, see [`sanitize_name()`] /// - Removes the name if it is equal to the address by setting it to "" pub fn sanitize_name_and_addr(name: &str, addr: &str) -> (String, String) { - static ADDR_WITH_NAME_REGEX: LazyLock = - LazyLock::new(|| Regex::new("(.*)<(.*)>").unwrap()); - let (name, addr) = if let Some(captures) = ADDR_WITH_NAME_REGEX.captures(addr.as_ref()) { + let (name, addr) = if let Some(captures) = regex!("(.*)<(.*)>").captures(addr.as_ref()) { ( if name.is_empty() { captures.get(1).map_or("", |m| m.as_str()) diff --git a/deltachat-contact-tools/src/vcard.rs b/deltachat-contact-tools/src/vcard.rs index 0b09e9727..be69ef4c1 100644 --- a/deltachat-contact-tools/src/vcard.rs +++ b/deltachat-contact-tools/src/vcard.rs @@ -1,10 +1,8 @@ -use std::sync::LazyLock; - use anyhow::Context as _; use anyhow::Result; use chrono::DateTime; use chrono::NaiveDateTime; -use regex::Regex; +use regex::regex; use crate::sanitize_name_and_addr; @@ -210,9 +208,7 @@ pub fn parse_vcard(vcard: &str) -> Vec { } // Remove line folding, see https://datatracker.ietf.org/doc/html/rfc6350#section-3.2 - static NEWLINE_AND_SPACE_OR_TAB: LazyLock = - LazyLock::new(|| Regex::new("\r?\n[\t ]").unwrap()); - let unfolded_lines = NEWLINE_AND_SPACE_OR_TAB.replace_all(vcard, ""); + let unfolded_lines = regex!("\r?\n[\t ]").replace_all(vcard, ""); let mut lines = unfolded_lines.lines().peekable(); let mut contacts = Vec::new(); diff --git a/src/receive_imf.rs b/src/receive_imf.rs index c4dbc87fb..4b8a17652 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -12,7 +12,7 @@ use deltachat_contact_tools::{ sanitize_single_line, }; use mailparse::SingleInfo; -use regex::Regex; +use regex::{Regex, regex}; use crate::chat::{ self, Chat, ChatId, ChatIdBlocked, ChatVisibility, is_contact_in_chat, save_broadcast_secret, @@ -3636,9 +3636,8 @@ fn compute_mailinglist_name( // (as that part is much more visible, we assume, that names is shorter and comes more to the point, // than the sometimes longer part from ListId) let subject = mime_parser.get_subject().unwrap_or_default(); - static SUBJECT: LazyLock = - LazyLock::new(|| Regex::new(r"^.{0,5}\[(.+?)\](\s*\[.+\])?").unwrap()); // remove square brackets around first name - if let Some(cap) = SUBJECT.captures(&subject) { + let subject_re: &Regex = regex!(r"^.{0,5}\[(.+?)\](\s*\[.+\])?"); // remove square brackets around first name + if let Some(cap) = subject_re.captures(&subject) { name = cap[1].to_string() + cap.get(2).map_or("", |m| m.as_str()); } @@ -3663,9 +3662,8 @@ fn compute_mailinglist_name( // but strip some known, long hash prefixes if name.is_empty() { // 51231231231231231231231232869f58.xing.com -> xing.com - static PREFIX_32_CHARS_HEX: LazyLock = - LazyLock::new(|| Regex::new(r"([0-9a-fA-F]{32})\.(.{6,})").unwrap()); - if let Some(cap) = PREFIX_32_CHARS_HEX + let prefix_32_chars_hex: &Regex = regex!(r"([0-9a-fA-F]{32})\.(.{6,})"); + if let Some(cap) = prefix_32_chars_hex .captures(listid) .and_then(|caps| caps.get(2)) {