gopl-zh.github.com/tools/mktable.go
2015-12-09 15:45:11 +08:00

63 lines
1.2 KiB
Go

// Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ingore
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"unicode/utf8"
)
func main() {
f, err := os.Open("./TSCharacters.txt")
if err != nil {
log.Fatal("open failed:", err)
}
defer f.Close()
br := bufio.NewReader(f)
var out bytes.Buffer
fmt.Fprintf(&out, `
// Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Auto generated by go generate, DO NOT EDIT !!!
package main
var _TSCharactersMap = map[rune]rune{
`[1:])
for i := 0; i < 1<<20; i++ {
data, isPrefix, err := br.ReadLine()
if err != nil || isPrefix {
break
}
if !utf8.ValidString(string(data)) {
continue
}
line := strings.Replace(string(data), "\t", " ", -1)
ss := strings.Split(string(line), " ")
if len(ss) >= 2 {
tw := strings.TrimSpace(ss[0])
zh := strings.TrimSpace(ss[1])
fmt.Fprintf(&out, "\t'%s': '%s',\n", tw, zh)
}
}
fmt.Fprintf(&out, "}\n")
ioutil.WriteFile("z_TSCharacters.go", []byte(out.Bytes()), 0666)
}