diff options
| author | Fuwn <[email protected]> | 2026-02-01 07:30:28 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-01 07:30:28 +0000 |
| commit | b8f2c13ba733fe3dbf84534d062e49b297304e8d (patch) | |
| tree | 59d2b8c011bee2ca7b2eb84425bf6234c05420fb /patterns.go | |
| parent | refactor: Merge benchmarks into formatter_test.go (diff) | |
| download | iku-b8f2c13ba733fe3dbf84534d062e49b297304e8d.tar.xz iku-b8f2c13ba733fe3dbf84534d062e49b297304e8d.zip | |
perf(patterns): Replace remaining regular expressions with string functions
Diffstat (limited to 'patterns.go')
| -rw-r--r-- | patterns.go | 86 |
1 files changed, 79 insertions, 7 deletions
diff --git a/patterns.go b/patterns.go index 5cd35ca..4bd2b57 100644 --- a/patterns.go +++ b/patterns.go @@ -1,18 +1,90 @@ package main -import "regexp" +func isWhitespace(character byte) bool { + return character == ' ' || character == '\t' || character == '\n' || character == '\r' || character == '\f' +} + +func isClosingBrace(sourceLine string) bool { + for characterIndex := 0; characterIndex < len(sourceLine); characterIndex++ { + character := sourceLine[characterIndex] + + if isWhitespace(character) { + continue + } + + return character == '}' || character == ')' + } + + return false +} -var ( - closingBracePattern = regexp.MustCompile(`^\s*[\}\)]`) - openingBracePattern = regexp.MustCompile(`[\{\(]\s*$`) - caseLabelPattern = regexp.MustCompile(`^\s*(case\s|default\s*:)|(^\s+.*:\s*$)`) -) +func isOpeningBrace(sourceLine string) bool { + for characterIndex := len(sourceLine) - 1; characterIndex >= 0; characterIndex-- { + character := sourceLine[characterIndex] + + if isWhitespace(character) { + continue + } + + return character == '{' || character == '(' + } + + return false +} + +func isCaseLabel(sourceLine string) bool { + firstNonWhitespaceIndex := 0 + + for firstNonWhitespaceIndex < len(sourceLine) && isWhitespace(sourceLine[firstNonWhitespaceIndex]) { + firstNonWhitespaceIndex++ + } + + if firstNonWhitespaceIndex >= len(sourceLine) { + return false + } + + contentAfterWhitespace := sourceLine[firstNonWhitespaceIndex:] + + if len(contentAfterWhitespace) >= 5 && contentAfterWhitespace[:4] == "case" && isWhitespace(contentAfterWhitespace[4]) { + return true + } + + if len(contentAfterWhitespace) >= 7 && contentAfterWhitespace[:7] == "default" { + for characterIndex := 7; characterIndex < len(contentAfterWhitespace); characterIndex++ { + character := contentAfterWhitespace[characterIndex] + + if isWhitespace(character) { + continue + } + + if character == ':' { + return true + } + + break + } + } + + if firstNonWhitespaceIndex > 0 { + lastNonWhitespaceIndex := len(sourceLine) - 1 + + for lastNonWhitespaceIndex >= 0 && isWhitespace(sourceLine[lastNonWhitespaceIndex]) { + lastNonWhitespaceIndex-- + } + + if lastNonWhitespaceIndex >= 0 && sourceLine[lastNonWhitespaceIndex] == ':' { + return true + } + } + + return false +} func isCommentOnly(sourceLine string) bool { for characterIndex := range len(sourceLine) { character := sourceLine[characterIndex] - if character == ' ' || character == '\t' { + if isWhitespace(character) { continue } |