From 362b76f6656a64a5116f523b4c7182ff30649312 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sun, 31 Oct 2021 16:55:47 -0600 Subject: [PATCH] Use regexp.MustCompile and make the regex constant --- main.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 84aa7e9..46a71ea 100644 --- a/main.go +++ b/main.go @@ -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]) } -- 2.38.4