~edwargix/msc-link-bot

362b76f6656a64a5116f523b4c7182ff30649312 — Sumner Evans 3 years ago 77c012f
Use regexp.MustCompile and make the regex constant
1 files changed, 2 insertions(+), 8 deletions(-)

M main.go
M main.go => main.go +2 -8
@@ 16,7 16,7 @@ import (
	"maunium.net/go/mautrix/id"
)

var mscRegex *regexp.Regexp
var MSC_REGEX *regexp.Regexp = regexp.MustCompile("\\b(?:MSC|msc)(\\d+)\\b")

func main() {
	store := NewMSCBotStore()


@@ 48,12 48,6 @@ func main() {
		log.Fatalf("couldn't load olm machine: %v", err)
	}

	mscRegex, err = regexp.Compile("\\b(?:MSC|msc)(\\d+)\\b")
	if err != nil {
		// should never happen
		log.Fatalf("couldn't compile regex: %v", err)
	}

	syncer := client.Syncer.(*mautrix.DefaultSyncer)
	syncer.OnSync(olmMachine.ProcessSyncResponse)
	syncer.OnEventType(event.StateMember, func(_ mautrix.EventSource, evt *event.Event) {


@@ 154,7 148,7 @@ func getMsgResponse(client *mautrix.Client, evt *event.Event) *event.MessageEven
}

func getMSCs(body string) (mscs []string) {
	matches := mscRegex.FindAllStringSubmatch(body, -1)
	matches := MSC_REGEX.FindAllStringSubmatch(body, -1)
	for _, match := range matches {
		mscs = append(mscs, match[1])
	}