mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2026-01-07 00:13:15 -06:00
update regexes
This commit is contained in:
parent
3cb6729f1b
commit
eb08ebe179
1 changed files with 15 additions and 4 deletions
|
|
@ -20,6 +20,8 @@ package gtsmodel
|
|||
import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
// Filter stores a filter created by a local account.
|
||||
|
|
@ -61,14 +63,23 @@ type FilterKeyword struct {
|
|||
|
||||
// Compile will compile this FilterKeyword as a prepared regular expression.
|
||||
func (k *FilterKeyword) Compile() (err error) {
|
||||
var wordBreak string
|
||||
if k.WholeWord != nil && *k.WholeWord {
|
||||
wordBreak = `\b`
|
||||
var (
|
||||
wordBreakStart string
|
||||
wordBreakEnd string
|
||||
)
|
||||
|
||||
if util.PtrOrZero(k.WholeWord) {
|
||||
// Either word boundary or
|
||||
// whitespace or start of line.
|
||||
wordBreakStart = `(?:\b|\s|^)`
|
||||
// Either word boundary or
|
||||
// whitespace or end of line.
|
||||
wordBreakEnd = `(?:\b|\s|$)`
|
||||
}
|
||||
|
||||
// Compile keyword filter regexp.
|
||||
quoted := regexp.QuoteMeta(k.Keyword)
|
||||
k.Regexp, err = regexp.Compile(`(?i)` + wordBreak + quoted + wordBreak)
|
||||
k.Regexp, err = regexp.Compile(`(?i)` + wordBreakStart + quoted + wordBreakEnd)
|
||||
return // caller is expected to wrap this error
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue