Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c34f84

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ross Light <[email protected]>
1 parent 71a41d8 commit 8c34f84

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

internal/language/compact/compact.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ func getCoreIndex(t language.Tag) (id ID, ok bool) {
3737
return ID(i), true
3838
}
3939

40+
// Parent returns the ID of the parent or the root ID if id is already the root.
41+
func (id ID) Parent() ID {
42+
return parents[id]
43+
}
44+
4045
// Tag converts id to an internal language Tag.
4146
func (id ID) Tag() language.Tag {
4247
if int(id) >= len(coreTags) {

internal/language/compact/gen_parents.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
"log"
1111

1212
"golang.org/x/text/internal/gen"
13-
"golang.org/x/text/language"
13+
"golang.org/x/text/internal/language"
14+
"golang.org/x/text/internal/language/compact"
1415
"golang.org/x/text/unicode/cldr"
1516
)
1617

@@ -25,28 +26,29 @@ func main() {
2526
}
2627

2728
w := gen.NewCodeWriter()
28-
defer w.WriteGoFile("tables.go", "internal")
29+
defer w.WriteGoFile("parents.go", "compact")
2930

3031
// Create parents table.
31-
parents := make([]uint16, language.NumCompactTags)
32+
type ID uint16
33+
parents := make([]ID, compact.NumCompactTags)
3234
for _, loc := range data.Locales() {
3335
tag := language.MustParse(loc)
34-
index, ok := language.CompactIndex(tag)
36+
index, ok := compact.FromTag(tag)
3537
if !ok {
3638
continue
3739
}
38-
parentIndex := 0 // und
40+
parentIndex := compact.ID(0) // und
3941
for p := tag.Parent(); p != language.Und; p = p.Parent() {
40-
if x, ok := language.CompactIndex(p); ok {
42+
if x, ok := compact.FromTag(p); ok {
4143
parentIndex = x
4244
break
4345
}
4446
}
45-
parents[index] = uint16(parentIndex)
47+
parents[index] = ID(parentIndex)
4648
}
4749

4850
w.WriteComment(`
49-
Parent maps a compact index of a tag to the compact index of the parent of
51+
parents maps a compact index of a tag to the compact index of the parent of
5052
this tag.`)
51-
w.WriteVar("Parent", parents)
53+
w.WriteVar("parents", parents)
5254
}

internal/language/compact/gen_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package compact_test
5+
package compact
66

77
import (
88
"testing"
99

10-
"golang.org/x/text/internal/language/compact"
11-
"golang.org/x/text/language"
10+
"golang.org/x/text/internal/language"
1211
)
1312

1413
func TestParents(t *testing.T) {
@@ -24,15 +23,15 @@ func TestParents(t *testing.T) {
2423
{"ca-ES-valencia", "ca-ES"},
2524
}
2625
for _, tc := range testCases {
27-
tag, ok := language.CompactIndex(language.MustParse(tc.tag))
26+
tag, ok := LanguageID(Make(language.MustParse(tc.tag)))
2827
if !ok {
2928
t.Fatalf("Could not get index of flag %s", tc.tag)
3029
}
31-
want, ok := language.CompactIndex(language.MustParse(tc.parent))
30+
want, ok := LanguageID(Make(language.MustParse(tc.parent)))
3231
if !ok {
3332
t.Fatalf("Could not get index of parent %s of tag %s", tc.parent, tc.tag)
3433
}
35-
if got := int(compact.Parent[tag]); got != want {
34+
if got := parents[tag]; got != want {
3635
t.Errorf("Parent[%s] = %d; want %d (%s)", tc.tag, got, want, tc.parent)
3736
}
3837
}

internal/language/compact/language.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
//go:generate go run gen.go gen_index.go -output tables.go
6+
//go:generate go run gen_parents.go
67

78
package compact
89

internal/language/compact/parents.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)