maketables.go 3.6 KB

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