/
generate.go
74 lines (66 loc) · 1.52 KB
/
generate.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
package binpack
import (
"fmt"
"io"
)
func appendHex(dst []byte, v byte) []byte {
n1 := v >> 4
if n1 >= 10 {
n1 += 'a' - 10
} else {
n1 += '0'
}
n2 := v & 0x0f
if n2 >= 10 {
n2 += 'a' - 10
} else {
n2 += '0'
}
return append(dst, '0', 'x', n1, n2, ',')
}
func MakeCpp(w io.Writer, namespace string, sources []*Source) {
fmt.Fprintf(w, "// DO NOT EDIT: Automatically-generated file\n")
fmt.Fprintf(w, "// clang-format off\n\n")
fmt.Fprintf(w, "#include <array>\n")
fmt.Fprintf(w, "#include <cstddef>\n\n")
if namespace != "" {
fmt.Fprintf(w, "namespace %s {\n", namespace)
}
for _, source := range sources {
fmt.Fprintf(w, "\nconst std::array<std::byte, %d> %s = {", len(source.Content), source.Name)
eol := []byte("\n ")
buf := []byte{}
for i, b := range source.Content {
if i%32 == 0 {
w.Write(buf)
w.Write(eol)
buf = buf[:0]
}
buf = appendHex(buf, b)
}
w.Write(buf)
fmt.Fprintf(w, "\n};\n")
}
if namespace != "" {
fmt.Fprintf(w, "\n} // namespace %s\n", namespace)
}
}
func MakeGolang(w io.Writer, packagename string, sources []*Source) {
fmt.Fprintf(w, "package %s\n\n", packagename)
fmt.Fprintf(w, "// DO NOT EDIT: Automatically-generated file\n")
for _, source := range sources {
fmt.Fprintf(w, "\nvar %s = []byte{", source.Name)
eol := []byte("\n ")
buf := []byte{}
for i, b := range source.Content {
if i%32 == 0 {
w.Write(buf)
w.Write(eol)
buf = buf[:0]
}
buf = appendHex(buf, b)
}
w.Write(buf)
fmt.Fprintf(w, "\n};\n")
}
}