Skip to content

Commit

Permalink
go: Performance: Don't compile regex on matcher create (#107)
Browse files Browse the repository at this point in the history
Co-authored-by: Tighearnán Carroll <tighearnan.carroll@gamil.com>
Co-authored-by: M.P. Korstanje <rien.korstanje@gmail.com>
  • Loading branch information
3 people authored Aug 10, 2023
1 parent 4bede30 commit b107924
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
## [Unreleased]
### Changed
- [Go] Upgraded messages to v22
- [Go] Improve performance - don't compile regex on matcher create

### Added
- (i18n) Add Malayalam localization
Expand Down
14 changes: 9 additions & 5 deletions go/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const (
DocstringAlternativeSeparator = "```"
)

var (
defaultLanguagePattern = regexp.MustCompile("^\\s*#\\s*language\\s*:\\s*([a-zA-Z\\-_]+)\\s*$")
commentDelimiterPattern = regexp.MustCompile(`\s+` + CommentPrefix)
)

type matcher struct {
gdp DialectProvider
defaultLang string
Expand All @@ -35,7 +40,7 @@ func NewMatcher(gdp DialectProvider) Matcher {
defaultLang: DefaultDialect,
lang: DefaultDialect,
dialect: gdp.GetDialect(DefaultDialect),
languagePattern: regexp.MustCompile("^\\s*#\\s*language\\s*:\\s*([a-zA-Z\\-_]+)\\s*$"),
languagePattern: defaultLanguagePattern,
}
}

Expand All @@ -45,7 +50,7 @@ func NewLanguageMatcher(gdp DialectProvider, language string) Matcher {
defaultLang: language,
lang: language,
dialect: gdp.GetDialect(language),
languagePattern: regexp.MustCompile("^\\s*#\\s*language\\s*:\\s*([a-zA-Z\\-_]+)\\s*$"),
languagePattern: defaultLanguagePattern,
}
}

Expand Down Expand Up @@ -95,8 +100,7 @@ func (m *matcher) MatchTagLine(line *Line) (ok bool, token *Token, err error) {
if !line.StartsWith(TagPrefix) {
return
}
commentDelimiter := regexp.MustCompile(`\s+` + CommentPrefix)
uncommentedLine := commentDelimiter.Split(line.TrimmedLineText, 2)[0]
uncommentedLine := commentDelimiterPattern.Split(line.TrimmedLineText, 2)[0]
var tags []*LineSpan
var column = line.Indent() + 1

Expand All @@ -108,7 +112,7 @@ func (m *matcher) MatchTagLine(line *Line) (ok bool, token *Token, err error) {
if len(txt) == 0 {
continue
}
if !regexp.MustCompile(`^\S+$`).MatchString(txt) {
if strings.ContainsAny(txt, "\t\n\f\r ") {
location := &Location{line.LineNumber, column}
msg := "A tag may not contain whitespace"
err = &parseError{msg, location}
Expand Down

0 comments on commit b107924

Please sign in to comment.