maketables.go 4.1 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. import (
  10. "bufio"
  11. "fmt"
  12. "log"
  13. "net/http"
  14. "sort"
  15. "strings"
  16. )
  17. func main() {
  18. fmt.Printf("// generated by go run maketables.go; DO NOT EDIT\n\n")
  19. fmt.Printf("// Package simplifiedchinese provides Simplified Chinese encodings such as GBK.\n")
  20. fmt.Printf(`package simplifiedchinese // import "golang.org/x/text/encoding/simplifiedchinese"` + "\n\n")
  21. printGB18030()
  22. printGBK()
  23. }
  24. func printGB18030() {
  25. res, err := http.Get("http://encoding.spec.whatwg.org/index-gb18030.txt")
  26. if err != nil {
  27. log.Fatalf("Get: %v", err)
  28. }
  29. defer res.Body.Close()
  30. fmt.Printf("// gb18030 is the table from http://encoding.spec.whatwg.org/index-gb18030.txt\n")
  31. fmt.Printf("var gb18030 = [...][2]uint16{\n")
  32. scanner := bufio.NewScanner(res.Body)
  33. for scanner.Scan() {
  34. s := strings.TrimSpace(scanner.Text())
  35. if s == "" || s[0] == '#' {
  36. continue
  37. }
  38. x, y := uint32(0), uint32(0)
  39. if _, err := fmt.Sscanf(s, "%d 0x%x", &x, &y); err != nil {
  40. log.Fatalf("could not parse %q", s)
  41. }
  42. if x < 0x10000 && y < 0x10000 {
  43. fmt.Printf("\t{0x%04x, 0x%04x},\n", x, y)
  44. }
  45. }
  46. fmt.Printf("}\n\n")
  47. }
  48. func printGBK() {
  49. res, err := http.Get("http://encoding.spec.whatwg.org/index-gbk.txt")
  50. if err != nil {
  51. log.Fatalf("Get: %v", err)
  52. }
  53. defer res.Body.Close()
  54. mapping := [65536]uint16{}
  55. reverse := [65536]uint16{}
  56. scanner := bufio.NewScanner(res.Body)
  57. for scanner.Scan() {
  58. s := strings.TrimSpace(scanner.Text())
  59. if s == "" || s[0] == '#' {
  60. continue
  61. }
  62. x, y := uint16(0), uint16(0)
  63. if _, err := fmt.Sscanf(s, "%d 0x%x", &x, &y); err != nil {
  64. log.Fatalf("could not parse %q", s)
  65. }
  66. if x < 0 || 126*190 <= x {
  67. log.Fatalf("GBK code %d is out of range", x)
  68. }
  69. mapping[x] = y
  70. if reverse[y] == 0 {
  71. c0, c1 := x/190, x%190
  72. if c1 >= 0x3f {
  73. c1++
  74. }
  75. reverse[y] = (0x81+c0)<<8 | (0x40 + c1)
  76. }
  77. }
  78. if err := scanner.Err(); err != nil {
  79. log.Fatalf("scanner error: %v", err)
  80. }
  81. fmt.Printf("// decode is the decoding table from GBK code to Unicode.\n")
  82. fmt.Printf("// It is defined at http://encoding.spec.whatwg.org/index-gbk.txt\n")
  83. fmt.Printf("var decode = [...]uint16{\n")
  84. for i, v := range mapping {
  85. if v != 0 {
  86. fmt.Printf("\t%d: 0x%04X,\n", i, v)
  87. }
  88. }
  89. fmt.Printf("}\n\n")
  90. // Any run of at least separation continuous zero entries in the reverse map will
  91. // be a separate encode table.
  92. const separation = 1024
  93. intervals := []interval(nil)
  94. low, high := -1, -1
  95. for i, v := range reverse {
  96. if v == 0 {
  97. continue
  98. }
  99. if low < 0 {
  100. low = i
  101. } else if i-high >= separation {
  102. if high >= 0 {
  103. intervals = append(intervals, interval{low, high})
  104. }
  105. low = i
  106. }
  107. high = i + 1
  108. }
  109. if high >= 0 {
  110. intervals = append(intervals, interval{low, high})
  111. }
  112. sort.Sort(byDecreasingLength(intervals))
  113. fmt.Printf("const numEncodeTables = %d\n\n", len(intervals))
  114. fmt.Printf("// encodeX are the encoding tables from Unicode to GBK code,\n")
  115. fmt.Printf("// sorted by decreasing length.\n")
  116. for i, v := range intervals {
  117. fmt.Printf("// encode%d: %5d entries for runes in [%5d, %5d).\n", i, v.len(), v.low, v.high)
  118. }
  119. fmt.Printf("\n")
  120. for i, v := range intervals {
  121. fmt.Printf("const encode%dLow, encode%dHigh = %d, %d\n\n", i, i, v.low, v.high)
  122. fmt.Printf("var encode%d = [...]uint16{\n", i)
  123. for j := v.low; j < v.high; j++ {
  124. x := reverse[j]
  125. if x == 0 {
  126. continue
  127. }
  128. fmt.Printf("\t%d-%d: 0x%04X,\n", j, v.low, x)
  129. }
  130. fmt.Printf("}\n\n")
  131. }
  132. }
  133. // interval is a half-open interval [low, high).
  134. type interval struct {
  135. low, high int
  136. }
  137. func (i interval) len() int { return i.high - i.low }
  138. // byDecreasingLength sorts intervals by decreasing length.
  139. type byDecreasingLength []interval
  140. func (b byDecreasingLength) Len() int { return len(b) }
  141. func (b byDecreasingLength) Less(i, j int) bool { return b[i].len() > b[j].len() }
  142. func (b byDecreasingLength) Swap(i, j int) { b[i], b[j] = b[j], b[i] }