internal/language/compact: implement parent generation
by modifying code copied earlier
Change-Id: I53a83ebc235675238973cfa93faba0d57260e846
Reviewed-on: https://go-review.googlesource.com/98437
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ross Light <light@google.com>
diff --git a/internal/language/compact/compact.go b/internal/language/compact/compact.go
index 7994984..1b36935 100644
--- a/internal/language/compact/compact.go
+++ b/internal/language/compact/compact.go
@@ -37,6 +37,11 @@
return ID(i), true
}
+// Parent returns the ID of the parent or the root ID if id is already the root.
+func (id ID) Parent() ID {
+ return parents[id]
+}
+
// Tag converts id to an internal language Tag.
func (id ID) Tag() language.Tag {
if int(id) >= len(coreTags) {
diff --git a/internal/language/compact/gen_parents.go b/internal/language/compact/gen_parents.go
index d7e97ed..9543d58 100644
--- a/internal/language/compact/gen_parents.go
+++ b/internal/language/compact/gen_parents.go
@@ -10,7 +10,8 @@
"log"
"golang.org/x/text/internal/gen"
- "golang.org/x/text/language"
+ "golang.org/x/text/internal/language"
+ "golang.org/x/text/internal/language/compact"
"golang.org/x/text/unicode/cldr"
)
@@ -25,28 +26,29 @@
}
w := gen.NewCodeWriter()
- defer w.WriteGoFile("tables.go", "internal")
+ defer w.WriteGoFile("parents.go", "compact")
// Create parents table.
- parents := make([]uint16, language.NumCompactTags)
+ type ID uint16
+ parents := make([]ID, compact.NumCompactTags)
for _, loc := range data.Locales() {
tag := language.MustParse(loc)
- index, ok := language.CompactIndex(tag)
+ index, ok := compact.FromTag(tag)
if !ok {
continue
}
- parentIndex := 0 // und
+ parentIndex := compact.ID(0) // und
for p := tag.Parent(); p != language.Und; p = p.Parent() {
- if x, ok := language.CompactIndex(p); ok {
+ if x, ok := compact.FromTag(p); ok {
parentIndex = x
break
}
}
- parents[index] = uint16(parentIndex)
+ parents[index] = ID(parentIndex)
}
w.WriteComment(`
- Parent maps a compact index of a tag to the compact index of the parent of
+ parents maps a compact index of a tag to the compact index of the parent of
this tag.`)
- w.WriteVar("Parent", parents)
+ w.WriteVar("parents", parents)
}
diff --git a/internal/language/compact/gen_test.go b/internal/language/compact/gen_test.go
index c934999..3b74252 100644
--- a/internal/language/compact/gen_test.go
+++ b/internal/language/compact/gen_test.go
@@ -2,13 +2,12 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package compact_test
+package compact
import (
"testing"
- "golang.org/x/text/internal/language/compact"
- "golang.org/x/text/language"
+ "golang.org/x/text/internal/language"
)
func TestParents(t *testing.T) {
@@ -24,15 +23,15 @@
{"ca-ES-valencia", "ca-ES"},
}
for _, tc := range testCases {
- tag, ok := language.CompactIndex(language.MustParse(tc.tag))
+ tag, ok := LanguageID(Make(language.MustParse(tc.tag)))
if !ok {
t.Fatalf("Could not get index of flag %s", tc.tag)
}
- want, ok := language.CompactIndex(language.MustParse(tc.parent))
+ want, ok := LanguageID(Make(language.MustParse(tc.parent)))
if !ok {
t.Fatalf("Could not get index of parent %s of tag %s", tc.parent, tc.tag)
}
- if got := int(compact.Parent[tag]); got != want {
+ if got := parents[tag]; got != want {
t.Errorf("Parent[%s] = %d; want %d (%s)", tc.tag, got, want, tc.parent)
}
}
diff --git a/internal/language/compact/language.go b/internal/language/compact/language.go
index ecae6dd..83816a7 100644
--- a/internal/language/compact/language.go
+++ b/internal/language/compact/language.go
@@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.
//go:generate go run gen.go gen_index.go -output tables.go
+//go:generate go run gen_parents.go
package compact
diff --git a/internal/language/compact/parents.go b/internal/language/compact/parents.go
index a07ed9d..8d81072 100644
--- a/internal/language/compact/parents.go
+++ b/internal/language/compact/parents.go
@@ -2,9 +2,9 @@
package compact
-// Parent maps a compact index of a tag to the compact index of the parent of
+// parents maps a compact index of a tag to the compact index of the parent of
// this tag.
-var Parent = []uint16{ // 775 elements
+var parents = []ID{ // 775 elements
// Entry 0 - 3F
0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0004, 0x0000, 0x0006,
0x0000, 0x0008, 0x0000, 0x000a, 0x000a, 0x000a, 0x000a, 0x000a,