-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.go
More file actions
172 lines (148 loc) · 3.85 KB
/
code.go
File metadata and controls
172 lines (148 loc) · 3.85 KB
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
package main
import (
"errors"
"go/ast"
"go/parser"
"go/token"
"html"
"io/ioutil"
"regexp"
"strings"
"unicode"
)
const (
testSuiteStartPattern = "<div class='pane pane-2 test-suite'>"
testSuiteEndPattern = "</div>"
codeStartPattern = "<code class='language-go'>"
codeEndPattern = "</code>"
solutionCodeStartPattern = "<pre class='line-numbers solution-code'>" + codeStartPattern
solutionCodeEndPattern = codeEndPattern + "</pre>"
testFileNameStartPattern = "<h3>"
testFileNameEndPattern = "</h3>"
)
var (
authorRE = regexp.MustCompile("Avatar of (([[:word:]]|-)+)")
)
var (
errNoSolutionCode = errors.New("no solution code")
errNoAuthorName = errors.New("no author name")
errNoTestSuite = errors.New("no test suite")
)
type codeRange struct {
start int
end int
}
type codeRanges []*codeRange
func (ranges *codeRanges) include(offset int) bool {
for _, r := range *ranges {
if offset >= r.start && offset < r.end {
return true
}
}
return false
}
func (ranges *codeRanges) add(start, end int) {
*ranges = append(*ranges, &codeRange{
start: start,
end: end,
})
}
// getCodeSize returns number of symbols in code w/o white spaces and comments.
func getCodeSize(sourceFilePath string) (size uint, err error) {
bs, err := ioutil.ReadFile(sourceFilePath)
if err != nil {
return
}
// exclude comments and ignore white spaces in string and char literals
var (
exclude, ignore codeRanges
)
// parse source code
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, sourceFilePath, bs, parser.ParseComments)
if err != nil {
return
}
// find all comments, string and char literals
ast.Inspect(f, func(n ast.Node) bool {
switch v := n.(type) {
case *ast.Comment:
exclude.add(fs.Position(v.Pos()).Offset, fs.Position(v.End()).Offset)
case *ast.BasicLit:
if v.Kind == token.STRING || v.Kind == token.CHAR {
ignore.add(fs.Position(v.Pos()).Offset, fs.Position(v.End()).Offset)
}
}
return true
})
// count only relevant code symbols
for i, r := range string(bs) {
if exclude.include(i) {
continue
}
if ignore.include(i) || !unicode.IsSpace(r) {
size++
}
}
return size, nil
}
func extractSolutionCode(solutionPage string) (code, author string, err error) {
// extract author name
ms := authorRE.FindStringSubmatch(solutionPage)
if ms == nil {
return "", "", errNoAuthorName
}
author = html.UnescapeString(ms[1])
// extract code
m, _ := getFirstMatch(solutionPage, solutionCodeStartPattern, solutionCodeEndPattern)
if m == "" {
return "", "", errNoSolutionCode
}
code = html.UnescapeString(m)
return code, author, nil
}
func extractTestSuite(solutionPage string) (suite map[string]string, err error) {
// locate test suite
ts, _ := getFirstMatch(solutionPage, testSuiteStartPattern, testSuiteEndPattern)
if ts == "" {
return nil, errNoTestSuite
}
// extract test files
suite = make(map[string]string)
for {
// locate file name
var m string
m, ts = getFirstMatch(ts, testFileNameStartPattern, testFileNameEndPattern)
if m == "" {
if len(suite) == 0 {
return nil, errNoTestSuite
}
break
}
name := html.UnescapeString(m)
// locate code
m, ts = getFirstMatch(ts, codeStartPattern, codeEndPattern)
if m == "" {
return nil, errNoTestSuite
}
code := html.UnescapeString(m)
// fill in suite
suite[name] = code
}
return suite, nil
}
// getFirstMatch looks for a substring with given start and end patterns.
// match contains the substring excluding patterns or empty string if nothing has been found.
// out gets the remaining input string after the chunk and the end pattern.
func getFirstMatch(in, sp, ep string) (match, out string) {
sind := strings.Index(in, sp)
if sind == -1 {
return
}
sind += len(sp)
eind := strings.Index(in[sind:], ep)
if eind == -1 {
return
}
return in[sind : sind+eind], in[sind+eind+len(ep):]
}