maketables.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 traditionalchinese provides Traditional Chinese encodings such as Big5.\n")
  20. fmt.Printf(`package traditionalchinese // import "golang.org/x/text/encoding/traditionalchinese"` + "\n\n")
  21. res, err := http.Get("http://encoding.spec.whatwg.org/index-big5.txt")
  22. if err != nil {
  23. log.Fatalf("Get: %v", err)
  24. }
  25. defer res.Body.Close()
  26. mapping := [65536]uint32{}
  27. reverse := [65536 * 4]uint16{}
  28. scanner := bufio.NewScanner(res.Body)
  29. for scanner.Scan() {
  30. s := strings.TrimSpace(scanner.Text())
  31. if s == "" || s[0] == '#' {
  32. continue
  33. }
  34. x, y := uint16(0), uint32(0)
  35. if _, err := fmt.Sscanf(s, "%d 0x%x", &x, &y); err != nil {
  36. log.Fatalf("could not parse %q", s)
  37. }
  38. if x < 0 || 126*157 <= x {
  39. log.Fatalf("Big5 code %d is out of range", x)
  40. }
  41. mapping[x] = y
  42. // The WHATWG spec http://encoding.spec.whatwg.org/#indexes says that
  43. // "The index pointer for code point in index is the first pointer
  44. // corresponding to code point in index", which would normally mean
  45. // that the code below should be guarded by "if reverse[y] == 0", but
  46. // last instead of first seems to match the behavior of
  47. // "iconv -f UTF-8 -t BIG5". For example, U+8005 者 occurs twice in
  48. // http://encoding.spec.whatwg.org/index-big5.txt, as index 2148
  49. // (encoded as "\x8e\xcd") and index 6543 (encoded as "\xaa\xcc")
  50. // and "echo 者 | iconv -f UTF-8 -t BIG5 | xxd" gives "\xaa\xcc".
  51. c0, c1 := x/157, x%157
  52. if c1 < 0x3f {
  53. c1 += 0x40
  54. } else {
  55. c1 += 0x62
  56. }
  57. reverse[y] = (0x81+c0)<<8 | c1
  58. }
  59. if err := scanner.Err(); err != nil {
  60. log.Fatalf("scanner error: %v", err)
  61. }
  62. fmt.Printf("// decode is the decoding table from Big5 code to Unicode.\n")
  63. fmt.Printf("// It is defined at http://encoding.spec.whatwg.org/index-big5.txt\n")
  64. fmt.Printf("var decode = [...]uint32{\n")
  65. for i, v := range mapping {
  66. if v != 0 {
  67. fmt.Printf("\t%d: 0x%08X,\n", i, v)
  68. }
  69. }
  70. fmt.Printf("}\n\n")
  71. // Any run of at least separation continuous zero entries in the reverse map will
  72. // be a separate encode table.
  73. const separation = 1024
  74. intervals := []interval(nil)
  75. low, high := -1, -1
  76. for i, v := range reverse {
  77. if v == 0 {
  78. continue
  79. }
  80. if low < 0 {
  81. low = i
  82. } else if i-high >= separation {
  83. if high >= 0 {
  84. intervals = append(intervals, interval{low, high})
  85. }
  86. low = i
  87. }
  88. high = i + 1
  89. }
  90. if high >= 0 {
  91. intervals = append(intervals, interval{low, high})
  92. }
  93. sort.Sort(byDecreasingLength(intervals))
  94. fmt.Printf("const numEncodeTables = %d\n\n", len(intervals))
  95. fmt.Printf("// encodeX are the encoding tables from Unicode to Big5 code,\n")
  96. fmt.Printf("// sorted by decreasing length.\n")
  97. for i, v := range intervals {
  98. fmt.Printf("// encode%d: %5d entries for runes in [%6d, %6d).\n", i, v.len(), v.low, v.high)
  99. }
  100. fmt.Printf("\n")
  101. for i, v := range intervals {
  102. fmt.Printf("const encode%dLow, encode%dHigh = %d, %d\n\n", i, i, v.low, v.high)
  103. fmt.Printf("var encode%d = [...]uint16{\n", i)
  104. for j := v.low; j < v.high; j++ {
  105. x := reverse[j]
  106. if x == 0 {
  107. continue
  108. }
  109. fmt.Printf("\t%d-%d: 0x%04X,\n", j, v.low, x)
  110. }
  111. fmt.Printf("}\n\n")
  112. }
  113. }
  114. // interval is a half-open interval [low, high).
  115. type interval struct {
  116. low, high int
  117. }
  118. func (i interval) len() int { return i.high - i.low }
  119. // byDecreasingLength sorts intervals by decreasing length.
  120. type byDecreasingLength []interval
  121. func (b byDecreasingLength) Len() int { return len(b) }
  122. func (b byDecreasingLength) Less(i, j int) bool { return b[i].len() > b[j].len() }
  123. func (b byDecreasingLength) Swap(i, j int) { b[i], b[j] = b[j], b[i] }