From a07287acdbf2108a565ed1cea135b2af2ca0657f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez=20=28inkel=29?= Date: Thu, 27 Jun 2024 10:32:57 -0300 Subject: [PATCH] Replace regexp replace with strings replace These functions are faster than the regexp ones. --- codeowners.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/codeowners.go b/codeowners.go index a0f8ff6..7b27a7e 100644 --- a/codeowners.go +++ b/codeowners.go @@ -222,21 +222,14 @@ func (c *Codeowners) Owners(path string) []string { // precompile all regular expressions var ( - reCommentIgnore = regexp.MustCompile(`^(\\#|\\!)`) - rePrependSlash = regexp.MustCompile(`([^\/+])/.*\*\.`) - reEscapeDot = regexp.MustCompile(`\.`) - reDoubleStar1 = regexp.MustCompile(`/\*\*/`) - reDoubleStar2 = regexp.MustCompile(`\*\*/`) - reDoubleStar3 = regexp.MustCompile(`/\*\*`) - reEscapeStar1 = regexp.MustCompile(`\\\*`) - reEscapeStar2 = regexp.MustCompile(`\*`) + rePrependSlash = regexp.MustCompile(`([^\/+])/.*\*\.`) ) // based on github.com/sabhiram/go-gitignore // but modified so that 'dir/*' only matches files in 'dir/' func getPattern(line string) (*regexp.Regexp, error) { // when # or ! is escaped with a \ - if reCommentIgnore.MatchString(line) { + if strings.HasPrefix(line, `\#`) || strings.HasPrefix(line, `\!`) { line = line[1:] } @@ -246,7 +239,7 @@ func getPattern(line string) (*regexp.Regexp, error) { } // Handle escaping the "." char - line = reEscapeDot.ReplaceAllString(line, `\.`) + line = strings.ReplaceAll(line, ".", `\.`) magicStar := "#$~" @@ -254,13 +247,13 @@ func getPattern(line string) (*regexp.Regexp, error) { if strings.HasPrefix(line, "/**/") { line = line[1:] } - line = reDoubleStar1.ReplaceAllString(line, `(/|/.+/)`) - line = reDoubleStar2.ReplaceAllString(line, `(|.`+magicStar+`/)`) - line = reDoubleStar3.ReplaceAllString(line, `(|/.`+magicStar+`)`) + line = strings.ReplaceAll(line, `/**/`, `(/|/.+/)`) + line = strings.ReplaceAll(line, `**/`, `(|.`+magicStar+`/)`) + line = strings.ReplaceAll(line, `/**`, `(|/.`+magicStar+`)`) // Handle escaping the "*" char - line = reEscapeStar1.ReplaceAllString(line, `\`+magicStar) - line = reEscapeStar2.ReplaceAllString(line, `([^/]*)`) + line = strings.ReplaceAll(line, `\*`, `\`+magicStar) + line = strings.ReplaceAll(line, `*`, `([^/]*)`) // Handle escaping the "?" char line = strings.ReplaceAll(line, "?", `\?`)