common_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
  2. package width
  3. // This code is shared between the main code generator and the test code.
  4. import (
  5. "flag"
  6. "log"
  7. "strconv"
  8. "strings"
  9. "golang.org/x/text/internal/gen"
  10. "golang.org/x/text/internal/ucd"
  11. )
  12. var (
  13. outputFile = flag.String("out", "tables.go", "output file")
  14. )
  15. var typeMap = map[string]elem{
  16. "A": tagAmbiguous,
  17. "N": tagNeutral,
  18. "Na": tagNarrow,
  19. "W": tagWide,
  20. "F": tagFullwidth,
  21. "H": tagHalfwidth,
  22. }
  23. // getWidthData calls f for every entry for which it is defined.
  24. //
  25. // f may be called multiple times for the same rune. The last call to f is the
  26. // correct value. f is not called for all runes. The default tag type is
  27. // Neutral.
  28. func getWidthData(f func(r rune, tag elem, alt rune)) {
  29. // Set the default values for Unified Ideographs. In line with Annex 11,
  30. // we encode full ranges instead of the defined runes in Unified_Ideograph.
  31. for _, b := range []struct{ lo, hi rune }{
  32. {0x4E00, 0x9FFF}, // the CJK Unified Ideographs block,
  33. {0x3400, 0x4DBF}, // the CJK Unified Ideographs Externsion A block,
  34. {0xF900, 0xFAFF}, // the CJK Compatibility Ideographs block,
  35. {0x20000, 0x2FFFF}, // the Supplementary Ideographic Plane,
  36. {0x30000, 0x3FFFF}, // the Tertiary Ideographic Plane,
  37. } {
  38. for r := b.lo; r <= b.hi; r++ {
  39. f(r, tagWide, 0)
  40. }
  41. }
  42. inverse := map[rune]rune{}
  43. maps := map[string]bool{
  44. "<wide>": true,
  45. "<narrow>": true,
  46. }
  47. // We cannot reuse package norm's decomposition, as we need an unexpanded
  48. // decomposition. We make use of the opportunity to verify that the
  49. // decomposition type is as expected.
  50. ucd.Parse(gen.OpenUCDFile("UnicodeData.txt"), func(p *ucd.Parser) {
  51. r := p.Rune(0)
  52. s := strings.SplitN(p.String(ucd.DecompMapping), " ", 2)
  53. if !maps[s[0]] {
  54. return
  55. }
  56. x, err := strconv.ParseUint(s[1], 16, 32)
  57. if err != nil {
  58. log.Fatalf("Error parsing rune %q", s[1])
  59. }
  60. if inverse[r] != 0 || inverse[rune(x)] != 0 {
  61. log.Fatalf("Circular dependency in mapping between %U and %U", r, x)
  62. }
  63. inverse[r] = rune(x)
  64. inverse[rune(x)] = r
  65. })
  66. // <rune range>;<type>
  67. ucd.Parse(gen.OpenUCDFile("EastAsianWidth.txt"), func(p *ucd.Parser) {
  68. tag, ok := typeMap[p.String(1)]
  69. if !ok {
  70. log.Fatalf("Unknown width type %q", p.String(1))
  71. }
  72. r := p.Rune(0)
  73. alt, ok := inverse[r]
  74. if tag == tagFullwidth || tag == tagHalfwidth && r != wonSign {
  75. tag |= tagNeedsFold
  76. if !ok {
  77. log.Fatalf("Narrow or wide rune %U has no decomposition", r)
  78. }
  79. }
  80. f(r, tag, alt)
  81. })
  82. }