From 91c0e032ee729df77775c46ea8875b586adf65b2 Mon Sep 17 00:00:00 2001 From: Zed Date: Mon, 27 Dec 2021 00:42:52 +0100 Subject: [PATCH] Avoid unnecessary string allocations in replaceUrl --- src/formatters.nim | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/formatters.nim b/src/formatters.nim index abb95c1..f56658c 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -41,17 +41,22 @@ proc shortLink*(text: string; length=28): string = proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string = result = url - if prefs.replaceYouTube.len > 0: + + if prefs.replaceYouTube.len > 0 and ytRegex in result: result = result.replace(ytRegex, prefs.replaceYouTube) if prefs.replaceYouTube in result: result = result.replace("/c/", "/") - if prefs.replaceInstagram.len > 0: - result = result.replace(igRegex, prefs.replaceInstagram) - if prefs.replaceTwitter.len > 0: + + if prefs.replaceTwitter.len > 0 and + (twRegex in result or tco in result): result = result.replace(tco, "https://" & prefs.replaceTwitter & "/t.co") result = result.replace(cards, prefs.replaceTwitter & "/cards") result = result.replace(twRegex, prefs.replaceTwitter) - if absolute.len > 0: + + if prefs.replaceInstagram.len > 0 and igRegex in result: + result = result.replace(igRegex, prefs.replaceInstagram) + + if absolute.len > 0 and "href" in result: result = result.replace("href=\"/", "href=\"" & absolute & "/") proc getM3u8Url*(content: string): string =