1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 // This program generates tables.go:
10 // go run maketables.go | gofmt > tables.go
12 // TODO: Emoji extensions?
13 // http://www.unicode.org/faq/emoji_dingbats.html
14 // http://www.unicode.org/Public/UNIDATA/EmojiSources.txt
30 fmt.Printf("// generated by go run maketables.go; DO NOT EDIT\n\n")
31 fmt.Printf("// Package japanese provides Japanese encodings such as EUC-JP and Shift JIS.\n")
32 fmt.Printf(`package japanese // import "golang.org/x/text/encoding/japanese"` + "\n\n")
34 reverse := [65536]entry{}
35 for i := range reverse {
43 {"http://encoding.spec.whatwg.org/index-jis0208.txt", "0208"},
44 {"http://encoding.spec.whatwg.org/index-jis0212.txt", "0212"},
46 for i, table := range tables {
47 res, err := http.Get(table.url)
49 log.Fatalf("%q: Get: %v", table.url, err)
51 defer res.Body.Close()
53 mapping := [65536]uint16{}
55 scanner := bufio.NewScanner(res.Body)
57 s := strings.TrimSpace(scanner.Text())
58 if s == "" || s[0] == '#' {
62 if _, err := fmt.Sscanf(s, "%d 0x%x", &x, &y); err != nil {
63 log.Fatalf("%q: could not parse %q", table.url, s)
65 if x < 0 || 120*94 <= x {
66 log.Fatalf("%q: JIS code %d is out of range", table.url, x)
69 if reverse[y].table == -1 {
70 reverse[y] = entry{jisCode: x, table: i}
73 if err := scanner.Err(); err != nil {
74 log.Fatalf("%q: scanner error: %v", table.url, err)
77 fmt.Printf("// jis%sDecode is the decoding table from JIS %s code to Unicode.\n// It is defined at %s\n",
78 table.name, table.name, table.url)
79 fmt.Printf("var jis%sDecode = [...]uint16{\n", table.name)
80 for i, m := range mapping {
82 fmt.Printf("\t%d: 0x%04X,\n", i, m)
88 // Any run of at least separation continuous zero entries in the reverse map will
89 // be a separate encode table.
90 const separation = 1024
92 intervals := []interval(nil)
94 for i, v := range reverse {
100 } else if i-high >= separation {
102 intervals = append(intervals, interval{low, high})
109 intervals = append(intervals, interval{low, high})
111 sort.Sort(byDecreasingLength(intervals))
113 fmt.Printf("const (\n")
114 fmt.Printf("\tjis0208 = 1\n")
115 fmt.Printf("\tjis0212 = 2\n")
116 fmt.Printf("\tcodeMask = 0x7f\n")
117 fmt.Printf("\tcodeShift = 7\n")
118 fmt.Printf("\ttableShift = 14\n")
121 fmt.Printf("const numEncodeTables = %d\n\n", len(intervals))
122 fmt.Printf("// encodeX are the encoding tables from Unicode to JIS code,\n")
123 fmt.Printf("// sorted by decreasing length.\n")
124 for i, v := range intervals {
125 fmt.Printf("// encode%d: %5d entries for runes in [%5d, %5d).\n", i, v.len(), v.low, v.high)
128 fmt.Printf("// The high two bits of the value record whether the JIS code comes from the\n")
129 fmt.Printf("// JIS0208 table (high bits == 1) or the JIS0212 table (high bits == 2).\n")
130 fmt.Printf("// The low 14 bits are two 7-bit unsigned integers j1 and j2 that form the\n")
131 fmt.Printf("// JIS code (94*j1 + j2) within that table.\n")
134 for i, v := range intervals {
135 fmt.Printf("const encode%dLow, encode%dHigh = %d, %d\n\n", i, i, v.low, v.high)
136 fmt.Printf("var encode%d = [...]uint16{\n", i)
137 for j := v.low; j < v.high; j++ {
142 fmt.Printf("\t%d - %d: jis%s<<14 | 0x%02X<<7 | 0x%02X,\n",
143 j, v.low, tables[x.table].name, x.jisCode/94, x.jisCode%94)
149 // interval is a half-open interval [low, high).
150 type interval struct {
154 func (i interval) len() int { return i.high - i.low }
156 // byDecreasingLength sorts intervals by decreasing length.
157 type byDecreasingLength []interval
159 func (b byDecreasingLength) Len() int { return len(b) }
160 func (b byDecreasingLength) Less(i, j int) bool { return b[i].len() > b[j].len() }
161 func (b byDecreasingLength) Swap(i, j int) { b[i], b[j] = b[j], b[i] }