forked from apaxa-go/helper
/
main.go
154 lines (133 loc) · 3.14 KB
/
main.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
package main
import (
"bytes"
"github.com/bobyang007/helper/unicodeh/internal/ucd"
"fmt"
"go/format"
"io/ioutil"
"os"
"strconv"
)
const (
breakStr = "÷"
noBreakStr = "×"
)
/*type ClusterBoundary struct {
From, To int
}*/
/*func (b ClusterBoundary) ToSource() string {
return "{" + strconvh.FormatInt(b.From) + "," + strconvh.FormatInt(b.To) + "}"
}*/
/*func GraphemesToSource(gs []ClusterBoundary) string {
r := "[]Boundary{"
for _, g := range gs {
r += g.ToSource() + ","
}
return r[:len(r)-1] + "}"
}*/
func RunesToSource(runes []rune) string {
r := "[]rune{"
for _, char := range runes {
r += "0x" + strconv.FormatInt(int64(char), 16) + ","
}
return r[:len(r)-1] + "}"
}
func isBreak(str string) bool {
switch str {
case breakStr:
return true
case noBreakStr:
return false
default:
panic("Invalid runes separator " + str)
}
}
func parseLine(strs []string) (runes []rune, breaks []int) {
if isBreak(strs[0]) {
breaks = []int{0}
}
runeCount := (len(strs) - 1) / 2
runes = make([]rune, runeCount)
for runeI := 0; runeI < runeCount; runeI++ {
//
// Rune
//
runeStr := strs[1+runeI*2]
var err error
tmp, err := strconv.ParseInt(runeStr, 16, 32)
if err != nil {
panic(err.Error())
}
runes[runeI] = rune(tmp)
//
// Break
//
breakStr := strs[2+runeI*2]
if isBreak(breakStr) {
breaks = append(breaks, runeI+1)
}
}
return
}
func saveFile(fileName string, packageName string, imports []string, data []byte) {
start := "package " + packageName + "\n\n"
for _, i := range imports {
start += "import \"" + i + "\"\n"
}
start += "\n"
data = append([]byte(start), data...)
data, err := format.Source(data)
if err != nil {
panic("Syntax error in file " + fileName + ": " + err.Error())
}
if err := ioutil.WriteFile(fileName, data, 0); err != nil {
panic(err)
}
}
func parseTests(srcDir, testFilename, codeSuffix string) []byte {
srcFile := srcDir + string(os.PathSeparator) + "auxiliary" + string(os.PathSeparator) + testFilename
src, err := os.Open(srcFile)
defer src.Close()
if err != nil {
panic(err)
}
data := bytes.NewBuffer(nil)
data.WriteString("var ucd" + codeSuffix + "Tests = []ucd" /*+ codeSuffix*/ + "Test{\n")
p := ucd.New(src)
for p.Next() {
runes, breaks := parseLine(p.Strings(0))
data.WriteString(fmt.Sprintf("{%v, %#v},\n", RunesToSource(runes), breaks))
}
data.WriteString("}\n")
return data.Bytes()
}
func main() {
//
// Init
//
const usage = "Bad usage. Usage: \"maketables path-to-ucd-directory\"" // TODO
if len(os.Args) != 2 {
panic(usage)
}
srcDir := os.Args[1]
//
// Define settings
//
type test struct {
targetFilename string
codeSuffix string
}
tests := map[string]test{
"GraphemeBreakTest.txt": {"grapheme-ucd-data-gen_test.go", "GraphemeCluster"},
"WordBreakTest.txt": {"word-ucd-data-gen_test.go", "Word"},
"SentenceBreakTest.txt": {"sentence-ucd-data-gen_test.go", "Sentence"},
"LineBreakTest.txt": {"line-ucd-data-gen_test.go", "Line"},
}
//
// Execute
//
for testFilename, test := range tests {
data := parseTests(srcDir, testFilename, test.codeSuffix)
saveFile(test.targetFilename, "boundaryh", nil, data)
}
}