github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/x/text/encoding/charmap/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 // +build ignore 6 7 package main 8 9 import ( 10 "bufio" 11 "fmt" 12 "log" 13 "net/http" 14 "sort" 15 "strings" 16 "unicode/utf8" 17 18 "github.com/insionng/yougam/libraries/x/text/encoding" 19 "github.com/insionng/yougam/libraries/x/text/internal/gen" 20 ) 21 22 const ascii = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + 23 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + 24 ` !"#$%&'()*+,-./0123456789:;<=>?` + 25 `@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` + 26 "`abcdefghijklmnopqrstuvwxyz{|}~\u007f" 27 28 var encodings = []struct { 29 name string 30 mib string 31 comment string 32 varName string 33 replacement byte 34 mapping string 35 }{ 36 { 37 "IBM Code Page 437", 38 "PC8CodePage437", 39 "", 40 "CodePage437", 41 encoding.ASCIISub, 42 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM437-2.1.2.ucm", 43 }, 44 { 45 "IBM Code Page 850", 46 "PC850Multilingual", 47 "", 48 "CodePage850", 49 encoding.ASCIISub, 50 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM850-2.1.2.ucm", 51 }, 52 { 53 "IBM Code Page 852", 54 "PCp852", 55 "", 56 "CodePage852", 57 encoding.ASCIISub, 58 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM852-2.1.2.ucm", 59 }, 60 { 61 "IBM Code Page 855", 62 "IBM855", 63 "", 64 "CodePage855", 65 encoding.ASCIISub, 66 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM855-2.1.2.ucm", 67 }, 68 { 69 "Windows Code Page 858", // PC latin1 with Euro 70 "IBM00858", 71 "", 72 "CodePage858", 73 encoding.ASCIISub, 74 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/windows-858-2000.ucm", 75 }, 76 { 77 "IBM Code Page 862", 78 "PC862LatinHebrew", 79 "", 80 "CodePage862", 81 encoding.ASCIISub, 82 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM862-2.1.2.ucm", 83 }, 84 { 85 "IBM Code Page 866", 86 "IBM866", 87 "", 88 "CodePage866", 89 encoding.ASCIISub, 90 "http://encoding.spec.whatwg.org/index-ibm866.txt", 91 }, 92 { 93 "ISO 8859-1", 94 "ISOLatin1", 95 "", 96 "ISO8859_1", 97 encoding.ASCIISub, 98 "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/iso-8859_1-1998.ucm", 99 }, 100 { 101 "ISO 8859-2", 102 "ISOLatin2", 103 "", 104 "ISO8859_2", 105 encoding.ASCIISub, 106 "http://encoding.spec.whatwg.org/index-iso-8859-2.txt", 107 }, 108 { 109 "ISO 8859-3", 110 "ISOLatin3", 111 "", 112 "ISO8859_3", 113 encoding.ASCIISub, 114 "http://encoding.spec.whatwg.org/index-iso-8859-3.txt", 115 }, 116 { 117 "ISO 8859-4", 118 "ISOLatin4", 119 "", 120 "ISO8859_4", 121 encoding.ASCIISub, 122 "http://encoding.spec.whatwg.org/index-iso-8859-4.txt", 123 }, 124 { 125 "ISO 8859-5", 126 "ISOLatinCyrillic", 127 "", 128 "ISO8859_5", 129 encoding.ASCIISub, 130 "http://encoding.spec.whatwg.org/index-iso-8859-5.txt", 131 }, 132 { 133 "ISO 8859-6", 134 "ISOLatinArabic", 135 "", 136 "ISO8859_6,ISO8859_6E,ISO8859_6I", 137 encoding.ASCIISub, 138 "http://encoding.spec.whatwg.org/index-iso-8859-6.txt", 139 }, 140 { 141 "ISO 8859-7", 142 "ISOLatinGreek", 143 "", 144 "ISO8859_7", 145 encoding.ASCIISub, 146 "http://encoding.spec.whatwg.org/index-iso-8859-7.txt", 147 }, 148 { 149 "ISO 8859-8", 150 "ISOLatinHebrew", 151 "", 152 "ISO8859_8,ISO8859_8E,ISO8859_8I", 153 encoding.ASCIISub, 154 "http://encoding.spec.whatwg.org/index-iso-8859-8.txt", 155 }, 156 { 157 "ISO 8859-10", 158 "ISOLatin6", 159 "", 160 "ISO8859_10", 161 encoding.ASCIISub, 162 "http://encoding.spec.whatwg.org/index-iso-8859-10.txt", 163 }, 164 { 165 "ISO 8859-13", 166 "ISO885913", 167 "", 168 "ISO8859_13", 169 encoding.ASCIISub, 170 "http://encoding.spec.whatwg.org/index-iso-8859-13.txt", 171 }, 172 { 173 "ISO 8859-14", 174 "ISO885914", 175 "", 176 "ISO8859_14", 177 encoding.ASCIISub, 178 "http://encoding.spec.whatwg.org/index-iso-8859-14.txt", 179 }, 180 { 181 "ISO 8859-15", 182 "ISO885915", 183 "", 184 "ISO8859_15", 185 encoding.ASCIISub, 186 "http://encoding.spec.whatwg.org/index-iso-8859-15.txt", 187 }, 188 { 189 "ISO 8859-16", 190 "ISO885916", 191 "", 192 "ISO8859_16", 193 encoding.ASCIISub, 194 "http://encoding.spec.whatwg.org/index-iso-8859-16.txt", 195 }, 196 { 197 "KOI8-R", 198 "KOI8R", 199 "", 200 "KOI8R", 201 encoding.ASCIISub, 202 "http://encoding.spec.whatwg.org/index-koi8-r.txt", 203 }, 204 { 205 "KOI8-U", 206 "KOI8U", 207 "", 208 "KOI8U", 209 encoding.ASCIISub, 210 "http://encoding.spec.whatwg.org/index-koi8-u.txt", 211 }, 212 { 213 "Macintosh", 214 "Macintosh", 215 "", 216 "Macintosh", 217 encoding.ASCIISub, 218 "http://encoding.spec.whatwg.org/index-macintosh.txt", 219 }, 220 { 221 "Macintosh Cyrillic", 222 "MacintoshCyrillic", 223 "", 224 "MacintoshCyrillic", 225 encoding.ASCIISub, 226 "http://encoding.spec.whatwg.org/index-x-mac-cyrillic.txt", 227 }, 228 { 229 "Windows 874", 230 "Windows874", 231 "", 232 "Windows874", 233 encoding.ASCIISub, 234 "http://encoding.spec.whatwg.org/index-windows-874.txt", 235 }, 236 { 237 "Windows 1250", 238 "Windows1250", 239 "", 240 "Windows1250", 241 encoding.ASCIISub, 242 "http://encoding.spec.whatwg.org/index-windows-1250.txt", 243 }, 244 { 245 "Windows 1251", 246 "Windows1251", 247 "", 248 "Windows1251", 249 encoding.ASCIISub, 250 "http://encoding.spec.whatwg.org/index-windows-1251.txt", 251 }, 252 { 253 "Windows 1252", 254 "Windows1252", 255 "", 256 "Windows1252", 257 encoding.ASCIISub, 258 "http://encoding.spec.whatwg.org/index-windows-1252.txt", 259 }, 260 { 261 "Windows 1253", 262 "Windows1253", 263 "", 264 "Windows1253", 265 encoding.ASCIISub, 266 "http://encoding.spec.whatwg.org/index-windows-1253.txt", 267 }, 268 { 269 "Windows 1254", 270 "Windows1254", 271 "", 272 "Windows1254", 273 encoding.ASCIISub, 274 "http://encoding.spec.whatwg.org/index-windows-1254.txt", 275 }, 276 { 277 "Windows 1255", 278 "Windows1255", 279 "", 280 "Windows1255", 281 encoding.ASCIISub, 282 "http://encoding.spec.whatwg.org/index-windows-1255.txt", 283 }, 284 { 285 "Windows 1256", 286 "Windows1256", 287 "", 288 "Windows1256", 289 encoding.ASCIISub, 290 "http://encoding.spec.whatwg.org/index-windows-1256.txt", 291 }, 292 { 293 "Windows 1257", 294 "Windows1257", 295 "", 296 "Windows1257", 297 encoding.ASCIISub, 298 "http://encoding.spec.whatwg.org/index-windows-1257.txt", 299 }, 300 { 301 "Windows 1258", 302 "Windows1258", 303 "", 304 "Windows1258", 305 encoding.ASCIISub, 306 "http://encoding.spec.whatwg.org/index-windows-1258.txt", 307 }, 308 { 309 "X-User-Defined", 310 "XUserDefined", 311 "It is defined at http://encoding.spec.whatwg.org/#x-user-defined", 312 "XUserDefined", 313 encoding.ASCIISub, 314 ascii + 315 "\uf780\uf781\uf782\uf783\uf784\uf785\uf786\uf787" + 316 "\uf788\uf789\uf78a\uf78b\uf78c\uf78d\uf78e\uf78f" + 317 "\uf790\uf791\uf792\uf793\uf794\uf795\uf796\uf797" + 318 "\uf798\uf799\uf79a\uf79b\uf79c\uf79d\uf79e\uf79f" + 319 "\uf7a0\uf7a1\uf7a2\uf7a3\uf7a4\uf7a5\uf7a6\uf7a7" + 320 "\uf7a8\uf7a9\uf7aa\uf7ab\uf7ac\uf7ad\uf7ae\uf7af" + 321 "\uf7b0\uf7b1\uf7b2\uf7b3\uf7b4\uf7b5\uf7b6\uf7b7" + 322 "\uf7b8\uf7b9\uf7ba\uf7bb\uf7bc\uf7bd\uf7be\uf7bf" + 323 "\uf7c0\uf7c1\uf7c2\uf7c3\uf7c4\uf7c5\uf7c6\uf7c7" + 324 "\uf7c8\uf7c9\uf7ca\uf7cb\uf7cc\uf7cd\uf7ce\uf7cf" + 325 "\uf7d0\uf7d1\uf7d2\uf7d3\uf7d4\uf7d5\uf7d6\uf7d7" + 326 "\uf7d8\uf7d9\uf7da\uf7db\uf7dc\uf7dd\uf7de\uf7df" + 327 "\uf7e0\uf7e1\uf7e2\uf7e3\uf7e4\uf7e5\uf7e6\uf7e7" + 328 "\uf7e8\uf7e9\uf7ea\uf7eb\uf7ec\uf7ed\uf7ee\uf7ef" + 329 "\uf7f0\uf7f1\uf7f2\uf7f3\uf7f4\uf7f5\uf7f6\uf7f7" + 330 "\uf7f8\uf7f9\uf7fa\uf7fb\uf7fc\uf7fd\uf7fe\uf7ff", 331 }, 332 } 333 334 func getWHATWG(url string) string { 335 res, err := http.Get(url) 336 if err != nil { 337 log.Fatalf("%q: Get: %v", url, err) 338 } 339 defer res.Body.Close() 340 341 mapping := make([]rune, 128) 342 for i := range mapping { 343 mapping[i] = '\ufffd' 344 } 345 346 scanner := bufio.NewScanner(res.Body) 347 for scanner.Scan() { 348 s := strings.TrimSpace(scanner.Text()) 349 if s == "" || s[0] == '#' { 350 continue 351 } 352 x, y := 0, 0 353 if _, err := fmt.Sscanf(s, "%d\t0x%x", &x, &y); err != nil { 354 log.Fatalf("could not parse %q", s) 355 } 356 if x < 0 || 128 <= x { 357 log.Fatalf("code %d is out of range", x) 358 } 359 if 0x80 <= y && y < 0xa0 { 360 // We diverge from the WHATWG spec by mapping control characters 361 // in the range [0x80, 0xa0) to U+FFFD. 362 continue 363 } 364 mapping[x] = rune(y) 365 } 366 return ascii + string(mapping) 367 } 368 369 func getUCM(url string) string { 370 res, err := http.Get(url) 371 if err != nil { 372 log.Fatalf("%q: Get: %v", url, err) 373 } 374 defer res.Body.Close() 375 376 mapping := make([]rune, 256) 377 for i := range mapping { 378 mapping[i] = '\ufffd' 379 } 380 381 charsFound := 0 382 scanner := bufio.NewScanner(res.Body) 383 for scanner.Scan() { 384 s := strings.TrimSpace(scanner.Text()) 385 if s == "" || s[0] == '#' { 386 continue 387 } 388 var c byte 389 var r rune 390 if _, err := fmt.Sscanf(s, `<U%x> \x%x |0`, &r, &c); err != nil { 391 continue 392 } 393 mapping[c] = r 394 charsFound++ 395 } 396 397 if charsFound < 200 { 398 log.Fatalf("%q: only %d characters found (wrong page format?)", url, charsFound) 399 } 400 401 return string(mapping) 402 } 403 404 func main() { 405 mibs := map[string]bool{} 406 all := []string{} 407 408 w := gen.NewCodeWriter() 409 defer w.WriteGoFile("tables.go", "charmap") 410 411 printf := func(s string, a ...interface{}) { fmt.Fprintf(w, s, a...) } 412 413 printf("import (\n") 414 printf("\t\"github.com/insionng/yougam/libraries/x/text/encoding\"\n") 415 printf("\t\"github.com/insionng/yougam/libraries/x/text/encoding/internal/identifier\"\n") 416 printf(")\n\n") 417 for _, e := range encodings { 418 varNames := strings.Split(e.varName, ",") 419 all = append(all, varNames...) 420 varName := varNames[0] 421 switch { 422 case strings.HasPrefix(e.mapping, "http://encoding.spec.whatwg.org/"): 423 e.mapping = getWHATWG(e.mapping) 424 case strings.HasPrefix(e.mapping, "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/"): 425 e.mapping = getUCM(e.mapping) 426 } 427 428 asciiSuperset, low := strings.HasPrefix(e.mapping, ascii), 0x00 429 if asciiSuperset { 430 low = 0x80 431 } 432 lvn := 1 433 if strings.HasPrefix(varName, "ISO") || strings.HasPrefix(varName, "KOI") { 434 lvn = 3 435 } 436 lowerVarName := strings.ToLower(varName[:lvn]) + varName[lvn:] 437 printf("// %s is the %s encoding.\n", varName, e.name) 438 if e.comment != "" { 439 printf("//\n// %s\n", e.comment) 440 } 441 printf("var %s encoding.Encoding = &%s\n\nvar %s = charmap{\nname: %q,\n", 442 varName, lowerVarName, lowerVarName, e.name) 443 if mibs[e.mib] { 444 log.Fatalf("MIB type %q declared multiple times.", e.mib) 445 } 446 printf("mib: identifier.%s,\n", e.mib) 447 printf("asciiSuperset: %t,\n", asciiSuperset) 448 printf("low: 0x%02x,\n", low) 449 printf("replacement: 0x%02x,\n", e.replacement) 450 451 printf("decode: [256]utf8Enc{\n") 452 i, backMapping := 0, map[rune]byte{} 453 for _, c := range e.mapping { 454 if _, ok := backMapping[c]; !ok && c != utf8.RuneError { 455 backMapping[c] = byte(i) 456 } 457 var buf [8]byte 458 n := utf8.EncodeRune(buf[:], c) 459 if n > 3 { 460 panic(fmt.Sprintf("rune %q (%U) is too long", c, c)) 461 } 462 printf("{%d,[3]byte{0x%02x,0x%02x,0x%02x}},", n, buf[0], buf[1], buf[2]) 463 if i%2 == 1 { 464 printf("\n") 465 } 466 i++ 467 } 468 printf("},\n") 469 470 printf("encode: [256]uint32{\n") 471 encode := make([]uint32, 0, 256) 472 for c, i := range backMapping { 473 encode = append(encode, uint32(i)<<24|uint32(c)) 474 } 475 sort.Sort(byRune(encode)) 476 for len(encode) < cap(encode) { 477 encode = append(encode, encode[len(encode)-1]) 478 } 479 for i, enc := range encode { 480 printf("0x%08x,", enc) 481 if i%8 == 7 { 482 printf("\n") 483 } 484 } 485 printf("},\n}\n") 486 487 // Add an estimate of the size of a single charmap{} struct value, which 488 // includes two 256 elem arrays of 4 bytes and some extra fields, which 489 // align to 3 uint64s on 64-bit architectures. 490 w.Size += 2*4*256 + 3*8 491 } 492 // TODO: add proper line breaking. 493 printf("var listAll = []encoding.Encoding{\n%s,\n}\n\n", strings.Join(all, ",\n")) 494 } 495 496 type byRune []uint32 497 498 func (b byRune) Len() int { return len(b) } 499 func (b byRune) Less(i, j int) bool { return b[i]&0xffffff < b[j]&0xffffff } 500 func (b byRune) Swap(i, j int) { b[i], b[j] = b[j], b[i] }