From 6c44f3e6536f217f71a45cffdb97527815bc4801 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 31 Jan 2026 06:27:08 +0000 Subject: perf: Precompile regular expressions --- formatter.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/formatter.go b/formatter.go index 8d249cb..8c44547 100644 --- a/formatter.go +++ b/formatter.go @@ -10,6 +10,14 @@ import ( "strings" ) +var ( + closingBracePattern = regexp.MustCompile(`^\s*[\}\)]`) + openingBracePattern = regexp.MustCompile(`[\{\(]\s*$`) + caseLabelPattern = regexp.MustCompile(`^\s*(case\s+.*|default\s*):\s*$`) + commentOnlyPattern = regexp.MustCompile(`^\s*//`) + packageLinePattern = regexp.MustCompile(`^package\s+`) +) + type CommentMode int const ( @@ -176,11 +184,6 @@ func (f *Formatter) rewrite(source []byte, lineInfoMap map[int]*lineInfo) []byte var result []string - closingBracePattern := regexp.MustCompile(`^\s*[\}\)]`) - openingBracePattern := regexp.MustCompile(`[\{\(]\s*$`) - caseLabelPattern := regexp.MustCompile(`^\s*(case\s+.*|default\s*):\s*$`) - commentOnlyPattern := regexp.MustCompile(`^\s*//`) - packageLinePattern := regexp.MustCompile(`^package\s+`) previousWasOpenBrace := false previousType := "" previousWasComment := false @@ -295,8 +298,6 @@ func (f *Formatter) rewrite(source []byte, lineInfoMap map[int]*lineInfo) []byte } func (f *Formatter) findNextNonCommentLine(lines []string, startIndex int) int { - commentOnlyPattern := regexp.MustCompile(`^\s*//`) - for index := startIndex; index < len(lines); index++ { trimmed := strings.TrimSpace(lines[index]) -- cgit v1.2.3