๐Ÿš€ Grammar-Aware Code Formatter: Structure through separation (supports Go, JavaScript, TypeScript, JSX, and TSX)
go formatter code-formatter javascript typescript jsx tsx
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf(patterns): Replace remaining regular expressions with string functions

Fuwn b8f2c13b e72abfc4

+85 -13
+1 -1
.gitignore
··· 1 - iku 1 + iku* 2 2 result 3 3 .task
+79 -7
patterns.go
··· 1 1 package main 2 2 3 - import "regexp" 3 + func isWhitespace(character byte) bool { 4 + return character == ' ' || character == '\t' || character == '\n' || character == '\r' || character == '\f' 5 + } 6 + 7 + func isClosingBrace(sourceLine string) bool { 8 + for characterIndex := 0; characterIndex < len(sourceLine); characterIndex++ { 9 + character := sourceLine[characterIndex] 10 + 11 + if isWhitespace(character) { 12 + continue 13 + } 14 + 15 + return character == '}' || character == ')' 16 + } 17 + 18 + return false 19 + } 20 + 21 + func isOpeningBrace(sourceLine string) bool { 22 + for characterIndex := len(sourceLine) - 1; characterIndex >= 0; characterIndex-- { 23 + character := sourceLine[characterIndex] 24 + 25 + if isWhitespace(character) { 26 + continue 27 + } 28 + 29 + return character == '{' || character == '(' 30 + } 31 + 32 + return false 33 + } 34 + 35 + func isCaseLabel(sourceLine string) bool { 36 + firstNonWhitespaceIndex := 0 4 37 5 - var ( 6 - closingBracePattern = regexp.MustCompile(`^\s*[\}\)]`) 7 - openingBracePattern = regexp.MustCompile(`[\{\(]\s*$`) 8 - caseLabelPattern = regexp.MustCompile(`^\s*(case\s|default\s*:)|(^\s+.*:\s*$)`) 9 - ) 38 + for firstNonWhitespaceIndex < len(sourceLine) && isWhitespace(sourceLine[firstNonWhitespaceIndex]) { 39 + firstNonWhitespaceIndex++ 40 + } 41 + 42 + if firstNonWhitespaceIndex >= len(sourceLine) { 43 + return false 44 + } 45 + 46 + contentAfterWhitespace := sourceLine[firstNonWhitespaceIndex:] 47 + 48 + if len(contentAfterWhitespace) >= 5 && contentAfterWhitespace[:4] == "case" && isWhitespace(contentAfterWhitespace[4]) { 49 + return true 50 + } 51 + 52 + if len(contentAfterWhitespace) >= 7 && contentAfterWhitespace[:7] == "default" { 53 + for characterIndex := 7; characterIndex < len(contentAfterWhitespace); characterIndex++ { 54 + character := contentAfterWhitespace[characterIndex] 55 + 56 + if isWhitespace(character) { 57 + continue 58 + } 59 + 60 + if character == ':' { 61 + return true 62 + } 63 + 64 + break 65 + } 66 + } 67 + 68 + if firstNonWhitespaceIndex > 0 { 69 + lastNonWhitespaceIndex := len(sourceLine) - 1 70 + 71 + for lastNonWhitespaceIndex >= 0 && isWhitespace(sourceLine[lastNonWhitespaceIndex]) { 72 + lastNonWhitespaceIndex-- 73 + } 74 + 75 + if lastNonWhitespaceIndex >= 0 && sourceLine[lastNonWhitespaceIndex] == ':' { 76 + return true 77 + } 78 + } 79 + 80 + return false 81 + } 10 82 11 83 func isCommentOnly(sourceLine string) bool { 12 84 for characterIndex := range len(sourceLine) { 13 85 character := sourceLine[characterIndex] 14 86 15 - if character == ' ' || character == '\t' { 87 + if isWhitespace(character) { 16 88 continue 17 89 } 18 90
+5 -5
rewrite.go
··· 33 33 continue 34 34 } 35 35 36 - isClosingBrace := closingBracePattern.MatchString(currentLine) 37 - isOpeningBrace := openingBracePattern.MatchString(currentLine) 38 - isCaseLabel := caseLabelPattern.MatchString(currentLine) 36 + isClosingBraceLine := isClosingBrace(currentLine) 37 + isOpeningBraceLine := isOpeningBrace(currentLine) 38 + isCaseLabelLine := isCaseLabel(currentLine) 39 39 isCommentOnlyLine := isCommentOnly(currentLine) 40 40 isPackageDeclaration := isPackageLine(trimmedLine) 41 41 currentInformation := lineInformationMap[lineNumber] ··· 53 53 currentIsTopLevel := currentInformation != nil && currentInformation.isTopLevel 54 54 currentIsScoped := currentInformation != nil && currentInformation.isScoped 55 55 56 - if len(resultLines) > 0 && !previousWasOpenBrace && !isClosingBrace && !isCaseLabel { 56 + if len(resultLines) > 0 && !previousWasOpenBrace && !isClosingBraceLine && !isCaseLabelLine { 57 57 if currentIsTopLevel && previousWasTopLevel && currentStatementType != previousStatementType { 58 58 if f.CommentMode == CommentsFollow && previousWasComment { 59 59 } else { ··· 98 98 } 99 99 100 100 resultLines = append(resultLines, currentLine) 101 - previousWasOpenBrace = isOpeningBrace || isCaseLabel 101 + previousWasOpenBrace = isOpeningBraceLine || isCaseLabelLine 102 102 previousWasComment = isCommentOnlyLine 103 103 104 104 if currentInformation != nil {