This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/dev by this push:
     new a15dd415 refactor(internal): compile regex once while clearing text
a15dd415 is described below

commit a15dd41550c282a4de44259c69a57dd52eeb0e9c
Author: ferhat elmas <[email protected]>
AuthorDate: Sat Nov 22 12:43:55 2025 +0100

    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 <[email protected]>
---
 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 49049109..0b84cda4 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.*?[^<]>(.*)?<\/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.*?[^<]>(.*)?<\/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) {

Reply via email to