diff options
| author | Fuwn <[email protected]> | 2026-01-31 06:27:08 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-31 06:27:08 +0000 |
| commit | 6c44f3e6536f217f71a45cffdb97527815bc4801 (patch) | |
| tree | c0749022bb78ad478dca9e45f04b6556ee0f21b3 | |
| parent | chore: Add Taskfile and tasks (diff) | |
| download | iku-6c44f3e6536f217f71a45cffdb97527815bc4801.tar.xz iku-6c44f3e6536f217f71a45cffdb97527815bc4801.zip | |
perf: Precompile regular expressions
| -rw-r--r-- | formatter.go | 15 |
1 files 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]) |