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