-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.go
227 lines (195 loc) · 5.54 KB
/
file.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package ctoc
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strings"
"unicode"
)
// ClocFile is collecting to line count result.
type ClocFile struct {
Code int32 `xml:"code,attr" json:"code"`
Comments int32 `xml:"comment,attr" json:"comment"`
Blanks int32 `xml:"blank,attr" json:"blank"`
Name string `xml:"name,attr" json:"name"`
Lang string `xml:"language,attr" json:"language"`
Tokens int32 `xml:"tokens,attr" json:"tokens"`
}
// ClocFiles is gocloc result set.
type ClocFiles []ClocFile
func (cf ClocFiles) SortByName() {
sortFunc := func(i, j int) bool {
return cf[i].Name < cf[j].Name
}
sort.Slice(cf, sortFunc)
}
func (cf ClocFiles) SortByComments() {
sortFunc := func(i, j int) bool {
if cf[i].Comments == cf[j].Comments {
return cf[i].Code > cf[j].Code
}
return cf[i].Comments > cf[j].Comments
}
sort.Slice(cf, sortFunc)
}
func (cf ClocFiles) SortByBlanks() {
sortFunc := func(i, j int) bool {
if cf[i].Blanks == cf[j].Blanks {
return cf[i].Code > cf[j].Code
}
return cf[i].Blanks > cf[j].Blanks
}
sort.Slice(cf, sortFunc)
}
func (cf ClocFiles) SortByCode() {
sortFunc := func(i, j int) bool {
return cf[i].Code > cf[j].Code
}
sort.Slice(cf, sortFunc)
}
func (cf ClocFiles) SortByTokens() {
sortFunc := func(i, j int) bool {
return cf[i].Tokens > cf[j].Tokens
}
sort.Slice(cf, sortFunc)
}
// AnalyzeFile is analyzing file, this function calls AnalyzeReader() inside.
func AnalyzeFile(filename string, language *Language, opts *ClocOptions) *ClocFile {
fp, err := os.Open(filename)
if err != nil {
// ignore error
return &ClocFile{Name: filename}
}
defer fp.Close()
return AnalyzeReader(filename, language, fp, opts)
}
// AnalyzeReader is analyzing file for io.Reader.
func AnalyzeReader(filename string, language *Language, file io.Reader, opts *ClocOptions) *ClocFile {
if opts.Debug {
fmt.Printf("filename=%v\n", filename)
}
clocFile := &ClocFile{
Name: filename,
Lang: language.Name,
}
isFirstLine := true
var inComments [][2]string
buf := getByteSlice()
defer putByteSlice(buf)
scanner := bufio.NewScanner(file)
scanner.Buffer(buf.Bytes(), 1024*1024)
scannerloop:
for scanner.Scan() {
lineOrg := scanner.Text()
tokens := opts.Tokenizer.Encode(lineOrg, nil, nil)
clocFile.Tokens += int32(len(tokens))
line := strings.TrimSpace(lineOrg)
if len(strings.TrimSpace(line)) == 0 {
onBlank(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue
}
// shebang line is 'code'
if isFirstLine && strings.HasPrefix(line, "#!") {
onCode(clocFile, opts, len(inComments) > 0, line, lineOrg)
isFirstLine = false
continue
}
if len(inComments) == 0 {
if isFirstLine {
line = trimBOM(line)
}
singleloop:
for _, singleComment := range language.lineComments {
if strings.HasPrefix(line, singleComment) {
// check if single comment is a prefix of multi comment
for _, ml := range language.multiLines {
if ml[0] != "" && strings.HasPrefix(line, ml[0]) {
break singleloop
}
}
onComment(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue scannerloop
}
}
if len(language.multiLines) == 0 {
onCode(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue scannerloop
}
}
if len(inComments) == 0 && !containsComment(line, language.multiLines) {
onCode(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue scannerloop
}
lenLine := len(line)
if len(language.multiLines) == 1 && len(language.multiLines[0]) == 2 && language.multiLines[0][0] == "" {
onCode(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue
}
codeFlags := make([]bool, len(language.multiLines))
for pos := 0; pos < lenLine; {
for idx, ml := range language.multiLines {
begin, end := ml[0], ml[1]
lenBegin := len(begin)
if pos+lenBegin <= lenLine && strings.HasPrefix(line[pos:], begin) && (begin != end || len(inComments) == 0) {
pos += lenBegin
inComments = append(inComments, [2]string{begin, end})
continue
}
if n := len(inComments); n > 0 {
last := inComments[n-1]
if pos+len(last[1]) <= lenLine && strings.HasPrefix(line[pos:], last[1]) {
inComments = inComments[:n-1]
pos += len(last[1])
}
} else if pos < lenLine && !unicode.IsSpace(nextRune(line[pos:])) {
codeFlags[idx] = true
}
}
pos++
}
isCode := true
for _, b := range codeFlags {
if !b {
isCode = false
}
}
if isCode {
onCode(clocFile, opts, len(inComments) > 0, line, lineOrg)
} else {
onComment(clocFile, opts, len(inComments) > 0, line, lineOrg)
}
}
return clocFile
}
func onBlank(clocFile *ClocFile, opts *ClocOptions, isInComments bool, line, lineOrg string) {
clocFile.Blanks++
if opts.OnBlank != nil {
opts.OnBlank(line)
}
if opts.Debug {
fmt.Printf("[BLNK, cd:%d, cm:%d, bk:%d, iscm:%v] %s\n",
clocFile.Code, clocFile.Comments, clocFile.Blanks, isInComments, lineOrg)
}
}
func onComment(clocFile *ClocFile, opts *ClocOptions, isInComments bool, line, lineOrg string) {
clocFile.Comments++
if opts.OnComment != nil {
opts.OnComment(line)
}
if opts.Debug {
fmt.Printf("[COMM, cd:%d, cm:%d, bk:%d, iscm:%v] %s\n",
clocFile.Code, clocFile.Comments, clocFile.Blanks, isInComments, lineOrg)
}
}
func onCode(clocFile *ClocFile, opts *ClocOptions, isInComments bool, line, lineOrg string) {
clocFile.Code++
if opts.OnCode != nil {
opts.OnCode(line)
}
if opts.Debug {
fmt.Printf("[CODE, cd:%d, cm:%d, bk:%d, iscm:%v] %s\n",
clocFile.Code, clocFile.Comments, clocFile.Blanks, isInComments, lineOrg)
}
}