aboutsummaryrefslogtreecommitdiff
path: root/patterns.go
blob: 5cd35caf28337bf9a942079a1b97e9a1279e227f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main

import "regexp"

var (
	closingBracePattern = regexp.MustCompile(`^\s*[\}\)]`)
	openingBracePattern = regexp.MustCompile(`[\{\(]\s*$`)
	caseLabelPattern    = regexp.MustCompile(`^\s*(case\s|default\s*:)|(^\s+.*:\s*$)`)
)

func isCommentOnly(sourceLine string) bool {
	for characterIndex := range len(sourceLine) {
		character := sourceLine[characterIndex]

		if character == ' ' || character == '\t' {
			continue
		}

		return len(sourceLine) > characterIndex+1 && sourceLine[characterIndex] == '/' && sourceLine[characterIndex+1] == '/'
	}

	return false
}

func isPackageLine(trimmedLine string) bool {
	return len(trimmedLine) > 8 && trimmedLine[:8] == "package "
}

func countRawStringDelimiters(sourceLine string) int {
	delimiterCount := 0
	insideDoubleQuotedString := false
	insideCharacterLiteral := false

	for characterIndex := 0; characterIndex < len(sourceLine); characterIndex++ {
		character := sourceLine[characterIndex]

		if insideCharacterLiteral {
			if character == '\\' && characterIndex+1 < len(sourceLine) {
				characterIndex++

				continue
			}

			if character == '\'' {
				insideCharacterLiteral = false
			}

			continue
		}

		if insideDoubleQuotedString {
			if character == '\\' && characterIndex+1 < len(sourceLine) {
				characterIndex++

				continue
			}

			if character == '"' {
				insideDoubleQuotedString = false
			}

			continue
		}

		if character == '\'' {
			insideCharacterLiteral = true

			continue
		}

		if character == '"' {
			insideDoubleQuotedString = true

			continue
		}

		if character == '`' {
			delimiterCount++
		}
	}

	return delimiterCount
}