-
Notifications
You must be signed in to change notification settings - Fork 12
/
gen_testdata.go
56 lines (49 loc) · 1.21 KB
/
gen_testdata.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
// +build ignore
// gen_testdata clones the utf-8 tests data to the other
// unicode encodings and adds BOM variants of each.
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
)
func main() {
var xforms = []struct {
dir, bom string
enc encoding.Encoding
}{
{"testdata/utf-16be", "\xFE\xFF", unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)},
{"testdata/utf-16le", "\xFF\xFE", unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)},
}
paths, _ := filepath.Glob("testdata/utf-8/*")
for _, p := range paths {
src, err := ioutil.ReadFile(p)
if err != nil {
log.Fatal(err)
}
write("testdata/utf-8_bom", p, "\xEF\xBB\xBF", src)
for _, xform := range xforms {
dst, err := xform.enc.NewEncoder().Bytes(src)
if err != nil {
log.Fatal(err)
}
write(xform.dir, p, "", dst)
write(xform.dir+"_bom", p, xform.bom, dst)
}
}
}
func write(dir, orig, bom string, buf []byte) {
f, err := os.Create(filepath.Join(dir, filepath.Base(orig)))
if err != nil {
log.Fatal(err)
}
if _, err = f.Write([]byte(bom)); err != nil {
log.Fatal(err)
}
if _, err = f.Write(buf); err != nil {
log.Fatal(err)
}
}