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