don't word bound regexp that don't have word endings

This commit is contained in:
Ted Unangst 2021-01-11 21:12:19 -05:00
parent ebd7173e5b
commit 6ac73102e4
1 changed files with 23 additions and 4 deletions

27
hfcs.go
View File

@ -21,6 +21,7 @@ import (
"regexp" "regexp"
"sort" "sort"
"time" "time"
"unicode"
"humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/cache"
) )
@ -108,15 +109,33 @@ func filtcachefiller(userid int64) (afiltermap, bool) {
expflush = filt.Expiration expflush = filt.Expiration
} }
} }
if filt.Text != "" { if t := filt.Text; t != "" {
filt.re_text, err = regexp.Compile("\\b(?i:" + filt.Text + ")\\b") wordfront := unicode.IsLetter(rune(t[0]))
wordtail := unicode.IsLetter(rune(t[len(t)-1]))
t = "(?i:" + t + ")"
if wordfront {
t = "\\b" + t
}
if wordtail {
t = t + "\\b"
}
filt.re_text, err = regexp.Compile(t)
if err != nil { if err != nil {
log.Printf("error compiling filter text: %s", err) log.Printf("error compiling filter text: %s", err)
continue continue
} }
} }
if filt.Rewrite != "" { if t := filt.Rewrite; t != "" {
filt.re_rewrite, err = regexp.Compile("\\b(?i:" + filt.Rewrite + ")\\b") wordfront := unicode.IsLetter(rune(t[0]))
wordtail := unicode.IsLetter(rune(t[len(t)-1]))
t = "(?i:" + t + ")"
if wordfront {
t = "\\b" + t
}
if wordtail {
t = t + "\\b"
}
filt.re_rewrite, err = regexp.Compile(t)
if err != nil { if err != nil {
log.Printf("error compiling filter rewrite: %s", err) log.Printf("error compiling filter rewrite: %s", err)
continue continue