From f0a11cac5d2184a65ea944b1101ecff759a90343 Mon Sep 17 00:00:00 2001 From: Jack <40802798+Jack073@users.noreply.github.com> Date: Fri, 29 Oct 2021 20:29:01 +0100 Subject: [PATCH] Optimise regexp usage (#1502) * Optimise regexp usage * Revert regexp removal * Atleast use the semioptimised version --- core/chat/events/events.go | 9 +++++++-- core/rtmp/utils.go | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/core/chat/events/events.go b/core/chat/events/events.go index 43dd8af1b..6b944fc30 100644 --- a/core/chat/events/events.go +++ b/core/chat/events/events.go @@ -124,6 +124,11 @@ func RenderMarkdown(raw string) string { return buf.String() } +var ( + _sanitizeReSrcMatch = regexp.MustCompile(`(?i)^/img/emoji`) + _sanitizeReAltTitleMatch = regexp.MustCompile(`:\S+:`) +) + func sanitize(raw string) string { p := bluemonday.StrictPolicy() @@ -146,8 +151,8 @@ func sanitize(raw string) string { p.AllowElementsContent("p") // Allow img tags from the the local emoji directory only - p.AllowAttrs("src").Matching(regexp.MustCompile(`(?i)^/img/emoji`)).OnElements("img") - p.AllowAttrs("alt", "title").Matching(regexp.MustCompile(`:\S+:`)).OnElements("img") + p.AllowAttrs("src").Matching(_sanitizeReSrcMatch).OnElements("img") + p.AllowAttrs("alt", "title").Matching(_sanitizeReAltTitleMatch).OnElements("img") p.AllowAttrs("class").OnElements("img") // Allow bold diff --git a/core/rtmp/utils.go b/core/rtmp/utils.go index 0fa59894e..8e0a67f32 100644 --- a/core/rtmp/utils.go +++ b/core/rtmp/utils.go @@ -14,13 +14,15 @@ import ( const unknownString = "Unknown" +var _getInboundDetailsFromMetadataRE = regexp.MustCompile(`\{(.*?)\}`) + func getInboundDetailsFromMetadata(metadata []interface{}) (models.RTMPStreamMetadata, error) { metadataComponentsString := fmt.Sprintf("%+v", metadata) if !strings.Contains(metadataComponentsString, "onMetaData") { return models.RTMPStreamMetadata{}, errors.New("Not a onMetaData message") } - re := regexp.MustCompile(`\{(.*?)\}`) - submatchall := re.FindAllString(metadataComponentsString, 1) + + submatchall := _getInboundDetailsFromMetadataRE.FindAllString(metadataComponentsString, 1) if len(submatchall) == 0 { return models.RTMPStreamMetadata{}, errors.New("unable to parse inbound metadata")