1 // Copyright 2014 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.
13 "golang.org/x/text/internal/triegen"
18 var genWriter = ioutil.Discard
20 func randomRunes() map[rune]uint8 {
21 rnd := rand.New(rand.NewSource(seed))
24 // Only set our random rune if it is a valid Unicode code point.
25 if r := rune(rnd.Int31n(unicode.MaxRune + 1)); []rune(string(r))[0] == r {
32 // Example_build shows how to build a simple trie. It assigns the value 1 to
33 // 100 random runes generated by randomRunes.
34 func Example_build() {
35 t := triegen.NewTrie("rand")
37 for r, _ := range randomRunes() {
40 sz, err := t.Gen(genWriter)
42 fmt.Printf("Trie size: %d bytes\n", sz)
43 fmt.Printf("Error: %v\n", err)
46 // Trie size: 9280 bytes
50 // Example_lookup demonstrates how to use the trie generated by Example_build.
51 func Example_lookup() {
52 trie := newRandTrie(0)
54 // The same set of runes used by Example_build.
55 runes := randomRunes()
57 // Verify the right value is returned for all runes.
58 for r := rune(0); r <= unicode.MaxRune; r++ {
59 // Note that the return type of lookup is uint8.
60 if v, _ := trie.lookupString(string(r)); v != runes[r] {
61 fmt.Println("FAILURE")
65 fmt.Println("SUCCESS")
71 // runeValues generates some random values for a set of interesting runes.
72 func runeValues() map[rune]uint64 {
73 rnd := rand.New(rand.NewSource(seed))
74 m := map[rune]uint64{}
75 for p := 4; p <= unicode.MaxRune; p <<= 1 {
76 for d := -1; d <= 1; d++ {
77 m[rune(p+d)] = uint64(rnd.Int63())
83 // ExampleGen_build demonstrates the creation of multiple tries sharing common
84 // blocks. ExampleGen_lookup demonstrates how to use the generated tries.
85 func ExampleGen_build() {
86 var tries []*triegen.Trie
89 for _, c := range []struct {
90 include func(rune) bool
93 {func(r rune) bool { return true }, "all"},
94 {func(r rune) bool { return r < 0x80 }, "ASCII only"},
95 {func(r rune) bool { return r < 0x80 }, "ASCII only 2"},
96 {func(r rune) bool { return r <= 0xFFFF }, "BMP only"},
97 {func(r rune) bool { return r > 0xFFFF }, "No BMP"},
99 t := triegen.NewTrie(c.name)
100 tries = append(tries, t)
102 for r, v := range rv {
108 sz, err := triegen.Gen(genWriter, "multi", tries)
110 fmt.Printf("Trie size: %d bytes\n", sz)
111 fmt.Printf("Error: %v\n", err)
114 // Trie size: 18250 bytes
118 // ExampleGen_lookup shows how to look up values in the trie generated by
120 func ExampleGen_lookup() {
122 for i, include := range []func(rune) bool{
123 func(r rune) bool { return true }, // all
124 func(r rune) bool { return r < 0x80 }, // ASCII only
125 func(r rune) bool { return r < 0x80 }, // ASCII only 2
126 func(r rune) bool { return r <= 0xFFFF }, // BMP only
127 func(r rune) bool { return r > 0xFFFF }, // No BMP
131 for r := rune(0); r <= unicode.MaxRune; r++ {
136 // As we convert from a valid rune, we know it is safe to use
137 // lookupStringUnsafe.
138 if v := t.lookupStringUnsafe(string(r)); x != v {
139 fmt.Println("FAILURE")
144 fmt.Println("SUCCESS")