gen_parents.go 1.2 KB

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