Skip to content

Commit

Permalink
Use static initialisation for regular expressions to optimize (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
hloeung authored Sep 29, 2023
1 parent be85b89 commit 62f6234
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
5 changes: 3 additions & 2 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,8 @@ func (m *Mattermost) addParentMsg(parentID string, msg string, newLen int, uncou
return strings.TrimRight(msg, "\n") + replyMessage, nil
}

var validIRCNickRegExp = regexp.MustCompile("^[a-zA-Z0-9_]*$")

//nolint:funlen,gocognit,gocyclo,cyclop,forcetypeassert
func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) {
var data model.Post
Expand Down Expand Up @@ -890,8 +892,7 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) {
if overrideUsername != "" {
logger.Debugf("found override username %s", overrideUsername)
// only allow valid irc nicks
re := regexp.MustCompile("^[a-zA-Z0-9_]*$")
if re.MatchString(overrideUsername) {
if validIRCNickRegExp.MatchString(overrideUsername) {
ghost.Nick = overrideUsername
ghost.Me = false
}
Expand Down
15 changes: 9 additions & 6 deletions mm-go-irckit/server_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,10 @@ func CmdPrivMsg(s Server, u *User, msg *irc.Message) error {
return s.EncodeMessage(u, irc.ERR_NOSUCHNICK, msg.Params, "No such nick/channel")
}

var parseReactionToMsgRegExp = regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26})\s+([\-\+]):(\S+):\s*$`)

func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool {
re := regexp.MustCompile(`^\@\@([0-9a-f]{3}|[0-9a-z]{26})\s+([\-\+]):(\S+):\s*$`)
matches := re.FindStringSubmatch(msg.Trailing)
matches := parseReactionToMsgRegExp.FindStringSubmatch(msg.Trailing)
if len(matches) != 4 {
return false
}
Expand Down Expand Up @@ -503,9 +504,10 @@ func parseReactionToMsg(u *User, msg *irc.Message, channelID string) bool {
return true
}

var parseModifyMsgRegExp = regexp.MustCompile(`^s(\/(?:[0-9a-f]{3}|[0-9a-z]{26}|!!)?\/)(.*)`)

func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool {
re := regexp.MustCompile(`^s(\/(?:[0-9a-f]{3}|[0-9a-z]{26}|!!)?\/)(.*)`)
matches := re.FindStringSubmatch(msg.Trailing)
matches := parseModifyMsgRegExp.FindStringSubmatch(msg.Trailing)
text := msg.Trailing

// only two so s/xxx/ which means a delete
Expand Down Expand Up @@ -576,9 +578,10 @@ func parseModifyMsg(u *User, msg *irc.Message, channelID string) bool {
return true
}

var parseThreadIDRegExp = regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26})\s)(.*)`)

func parseThreadID(u *User, msg *irc.Message, channelID string) (string, string) {
re := regexp.MustCompile(`(?s)^\@\@(?:(!!|[0-9a-f]{3}|[0-9a-z]{26})\s)(.*)`)
matches := re.FindStringSubmatch(msg.Trailing)
matches := parseThreadIDRegExp.FindStringSubmatch(msg.Trailing)
if len(matches) == 0 {
return "", ""
}
Expand Down
9 changes: 6 additions & 3 deletions mm-go-irckit/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func (u *User) Encode(msgs ...*irc.Message) (err error) {
return nil
}

var (
replyRegExp = regexp.MustCompile(`\@\@(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)\s`)
modifyRegExp = regexp.MustCompile(`^s/(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)?/`)
)

// Decode will receive and return a decoded message, or an error.
// nolint:funlen,gocognit,gocyclo
func (u *User) Decode() {
Expand Down Expand Up @@ -181,9 +186,7 @@ func (u *User) Decode() {
// start timer now
t.Reset(time.Duration(bufferTimeout) * time.Millisecond)
} else {
replyRe := regexp.MustCompile(`\@\@(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)\s`)
modifyRe := regexp.MustCompile(`^s/(?:[0-9a-z]{26}|[0-9a-f]{3}|!!)?/`)
if strings.HasPrefix(msg.Trailing, "\x01ACTION") || replyRe.MatchString(msg.Trailing) || modifyRe.MatchString(msg.Trailing) {
if strings.HasPrefix(msg.Trailing, "\x01ACTION") || replyRegExp.MatchString(msg.Trailing) || modifyRegExp.MatchString(msg.Trailing) {
// flush buffer
logger.Debug("flushing buffer because of /me, replies to threads, and message modifications")
u.BufferedMsg.Trailing = strings.TrimSpace(u.BufferedMsg.Trailing)
Expand Down

0 comments on commit 62f6234

Please sign in to comment.