-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptimizer.go
45 lines (35 loc) · 999 Bytes
/
Optimizer.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
package pixy
import (
"regexp"
"strings"
)
var compactCode = regexp.MustCompile(`\n{2,}`)
const (
writeStringCall = "_b.WriteString("
)
// optimize combines multiple WriteString calls to one.
func optimize(code string) (optimizedCode string, inlined string) {
lines := strings.Split(code, "\n")
lastString := strings.Builder{}
// Count the actual code lines
lineCount := 0
for index, line := range lines {
// Find WriteString call
pos := strings.Index(line, writeStringCall)
if pos != -1 {
if line[pos+len(writeStringCall)] == '"' {
// Delete this line and save it in a buffer "lastString"
lastString.WriteString(line[pos+len(writeStringCall)+1 : len(line)-2])
lines[index] = ""
continue
}
}
if lastString.Len() > 0 {
lines[index] = "\t" + writeStringCall + "\"" + lastString.String() + "\")\n" + line
lastString.Reset()
}
lineCount++
}
compact := compactCode.ReplaceAllString(strings.Join(lines, "\n"), "\n")
return compact, inlined
}