gen.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 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. import (
  7. "log"
  8. "golang.org/x/text/internal/gen"
  9. "golang.org/x/text/language"
  10. "golang.org/x/text/unicode/cldr"
  11. )
  12. func main() {
  13. r := gen.OpenCLDRCoreZip()
  14. defer r.Close()
  15. d := &cldr.Decoder{}
  16. data, err := d.DecodeZip(r)
  17. if err != nil {
  18. log.Fatalf("DecodeZip: %v", err)
  19. }
  20. w := gen.NewCodeWriter()
  21. defer w.WriteGoFile("tables.go", "internal")
  22. // Create parents table.
  23. parents := make([]uint16, language.NumCompactTags)
  24. for _, loc := range data.Locales() {
  25. tag := language.MustParse(loc)
  26. index, ok := language.CompactIndex(tag)
  27. if !ok {
  28. continue
  29. }
  30. parentIndex := 0 // und
  31. for p := tag.Parent(); p != language.Und; p = p.Parent() {
  32. if x, ok := language.CompactIndex(p); ok {
  33. parentIndex = x
  34. break
  35. }
  36. }
  37. parents[index] = uint16(parentIndex)
  38. }
  39. w.WriteComment(`
  40. Parent maps a compact index of a tag to the compact index of the parent of
  41. this tag.`)
  42. w.WriteVar("Parent", parents)
  43. }