From 02541a4090231859c5d3ad4b80a480b1602dcc2c Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Sat, 22 Nov 2025 12:43:55 +0100 Subject: [PATCH] refactor(internal): compile regex once while clearing text Regex and Replacer can be reused. No need to recreate for each invocation. Signed-off-by: ferhat elmas --- pkg/htmltext/htmltext.go | 48 ++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.go index 49049109d..0b84cda49 100644 --- a/pkg/htmltext/htmltext.go +++ b/pkg/htmltext/htmltext.go @@ -34,38 +34,34 @@ import ( "github.com/mozillazg/go-pinyin" ) +var ( + reCode = regexp.MustCompile(`(?ism)<(pre)>.*<\/pre>`) + reCodeReplace = "{code...}" + reLink = regexp.MustCompile(`(?ism)(.*)?<\/a>`) + reLinkReplace = " [$1] " + reSpace = regexp.MustCompile(` +`) + reSpaceReplace = " " + + spaceReplacer = strings.NewReplacer( + "\n", " ", + "\r", " ", + "\t", " ", + ) +) + // ClearText clear HTML, get the clear text -func ClearText(html string) (text string) { - if len(html) == 0 { - text = html - return +func ClearText(html string) string { + if html == "" { + return html } - var ( - re *regexp.Regexp - codeReg = `(?ism)<(pre)>.*<\/pre>` - codeRepl = "{code...}" - linkReg = `(?ism)(.*)?<\/a>` - linkRepl = " [$1] " - spaceReg = ` +` - spaceRepl = " " - ) - re = regexp.MustCompile(codeReg) - html = re.ReplaceAllString(html, codeRepl) + html = reCode.ReplaceAllString(html, reCodeReplace) + html = reLink.ReplaceAllString(html, reLinkReplace) - re = regexp.MustCompile(linkReg) - html = re.ReplaceAllString(html, linkRepl) - - text = strings.NewReplacer( - "\n", " ", - "\r", " ", - "\t", " ", - ).Replace(strip.StripTags(html)) + text := spaceReplacer.Replace(strip.StripTags(html)) // replace multiple spaces to one space - re = regexp.MustCompile(spaceReg) - text = strings.TrimSpace(re.ReplaceAllString(text, spaceRepl)) - return + return strings.TrimSpace(reSpace.ReplaceAllString(text, reSpaceReplace)) } func UrlTitle(title string) (text string) {