maketables.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.
  4. //go:build ignore
  5. // +build ignore
  6. package main
  7. // This program generates tables.go:
  8. // go run maketables.go | gofmt > tables.go
  9. // TODO: Emoji extensions?
  10. // https://www.unicode.org/faq/emoji_dingbats.html
  11. // https://www.unicode.org/Public/UNIDATA/EmojiSources.txt
  12. import (
  13. "bufio"
  14. "fmt"
  15. "log"
  16. "net/http"
  17. "sort"
  18. "strings"
  19. )
  20. type entry struct {
  21. jisCode, table int
  22. }
  23. func main() {
  24. fmt.Printf("// generated by go run maketables.go; DO NOT EDIT\n\n")
  25. fmt.Printf("// Package japanese provides Japanese encodings such as EUC-JP and Shift JIS.\n")
  26. fmt.Printf(`package japanese // import "golang.org/x/text/encoding/japanese"` + "\n\n")
  27. reverse := [65536]entry{}
  28. for i := range reverse {
  29. reverse[i].table = -1
  30. }
  31. tables := []struct {
  32. url string
  33. name string
  34. }{
  35. {"http://encoding.spec.whatwg.org/index-jis0208.txt", "0208"},
  36. {"http://encoding.spec.whatwg.org/index-jis0212.txt", "0212"},
  37. }
  38. for i, table := range tables {
  39. res, err := http.Get(table.url)
  40. if err != nil {
  41. log.Fatalf("%q: Get: %v", table.url, err)
  42. }
  43. defer res.Body.Close()
  44. mapping := [65536]uint16{}
  45. scanner := bufio.NewScanner(res.Body)
  46. for scanner.Scan() {
  47. s := strings.TrimSpace(scanner.Text())
  48. if s == "" || s[0] == '#' {
  49. continue
  50. }
  51. x, y := 0, uint16(0)
  52. if _, err := fmt.Sscanf(s, "%d 0x%x", &x, &y); err != nil {
  53. log.Fatalf("%q: could not parse %q", table.url, s)
  54. }
  55. if x < 0 || 120*94 <= x {
  56. log.Fatalf("%q: JIS code %d is out of range", table.url, x)
  57. }
  58. mapping[x] = y
  59. if reverse[y].table == -1 {
  60. reverse[y] = entry{jisCode: x, table: i}
  61. }
  62. }
  63. if err := scanner.Err(); err != nil {
  64. log.Fatalf("%q: scanner error: %v", table.url, err)
  65. }
  66. fmt.Printf("// jis%sDecode is the decoding table from JIS %s code to Unicode.\n// It is defined at %s\n",
  67. table.name, table.name, table.url)
  68. fmt.Printf("var jis%sDecode = [...]uint16{\n", table.name)
  69. for i, m := range mapping {
  70. if m != 0 {
  71. fmt.Printf("\t%d: 0x%04X,\n", i, m)
  72. }
  73. }
  74. fmt.Printf("}\n\n")
  75. }
  76. // Any run of at least separation continuous zero entries in the reverse map will
  77. // be a separate encode table.
  78. const separation = 1024
  79. intervals := []interval(nil)
  80. low, high := -1, -1
  81. for i, v := range reverse {
  82. if v.table == -1 {
  83. continue
  84. }
  85. if low < 0 {
  86. low = i
  87. } else if i-high >= separation {
  88. if high >= 0 {
  89. intervals = append(intervals, interval{low, high})
  90. }
  91. low = i
  92. }
  93. high = i + 1
  94. }
  95. if high >= 0 {
  96. intervals = append(intervals, interval{low, high})
  97. }
  98. sort.Sort(byDecreasingLength(intervals))
  99. fmt.Printf("const (\n")
  100. fmt.Printf("\tjis0208 = 1\n")
  101. fmt.Printf("\tjis0212 = 2\n")
  102. fmt.Printf("\tcodeMask = 0x7f\n")
  103. fmt.Printf("\tcodeShift = 7\n")
  104. fmt.Printf("\ttableShift = 14\n")
  105. fmt.Printf(")\n\n")
  106. fmt.Printf("const numEncodeTables = %d\n\n", len(intervals))
  107. fmt.Printf("// encodeX are the encoding tables from Unicode to JIS code,\n")
  108. fmt.Printf("// sorted by decreasing length.\n")
  109. for i, v := range intervals {
  110. fmt.Printf("// encode%d: %5d entries for runes in [%5d, %5d).\n", i, v.len(), v.low, v.high)
  111. }
  112. fmt.Printf("//\n")
  113. fmt.Printf("// The high two bits of the value record whether the JIS code comes from the\n")
  114. fmt.Printf("// JIS0208 table (high bits == 1) or the JIS0212 table (high bits == 2).\n")
  115. fmt.Printf("// The low 14 bits are two 7-bit unsigned integers j1 and j2 that form the\n")
  116. fmt.Printf("// JIS code (94*j1 + j2) within that table.\n")
  117. fmt.Printf("\n")
  118. for i, v := range intervals {
  119. fmt.Printf("const encode%dLow, encode%dHigh = %d, %d\n\n", i, i, v.low, v.high)
  120. fmt.Printf("var encode%d = [...]uint16{\n", i)
  121. for j := v.low; j < v.high; j++ {
  122. x := reverse[j]
  123. if x.table == -1 {
  124. continue
  125. }
  126. fmt.Printf("\t%d - %d: jis%s<<14 | 0x%02X<<7 | 0x%02X,\n",
  127. j, v.low, tables[x.table].name, x.jisCode/94, x.jisCode%94)
  128. }
  129. fmt.Printf("}\n\n")
  130. }
  131. }
  132. // interval is a half-open interval [low, high).
  133. type interval struct {
  134. low, high int
  135. }
  136. func (i interval) len() int { return i.high - i.low }
  137. // byDecreasingLength sorts intervals by decreasing length.
  138. type byDecreasingLength []interval
  139. func (b byDecreasingLength) Len() int { return len(b) }
  140. func (b byDecreasingLength) Less(i, j int) bool { return b[i].len() > b[j].len() }
  141. func (b byDecreasingLength) Swap(i, j int) { b[i], b[j] = b[j], b[i] }