-
Notifications
You must be signed in to change notification settings - Fork 1
/
other_example_test.go
106 lines (96 loc) · 1.91 KB
/
other_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package intern_test
import (
"bufio"
"fmt"
"io"
"os"
"github.com/spakin/intern"
)
// Find all duplicates in a list of strings.
func ExampleNewEq() {
// Define some strings. Note that these aren't unique.
sList := []string{
"Gunnar",
"Högni",
"Gjúki",
"Gudrún",
"Gotthorm",
"Gjúki",
"Óttar",
"Sigurd",
"Svanhild",
"Jörmunrek",
"Jónakr",
"Hamdir",
"Sörli",
"Jónakr",
"Kostbera",
"Snævar",
"Atli",
}
// Intern all symbols into a set. Report any duplicates
// encountered.
seen := make(map[intern.Eq]struct{}, len(sList))
for _, s := range sList {
sym := intern.NewEq(s)
if _, ok := seen[sym]; ok {
fmt.Println(sym)
}
seen[sym] = struct{}{}
}
// Output:
// Gjúki
// Jónakr
}
// Maintain a long list of symbols, remapping as necessary.
func ExampleRemapAllLGEs() {
syms := make([]intern.LGE, 0, 10)
rb := bufio.NewReader(os.Stdin)
ReadInput:
for {
// Read a line from standard input.
s, err := rb.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
s = s[:len(s)-1]
// Map the symbol if we can. For this example,
// pretend we need the symbol right away and therefore
// wouldn't benefit by pre-allocating it with
// intern.PreLGE.
sy, err := intern.NewLGE(s)
switch e := err.(type) {
case nil:
// No error: Continue with the next line.
syms = append(syms, sy)
continue ReadInput
case *intern.PkgError:
// Package error: Ensure it's a full table.
if e.Code != intern.ErrTableFull {
panic(err)
}
default:
// Anything else: Abort.
panic(err)
}
// The LGE symbol table is full. Remap all existing
// symbols and try again.
intern.PreLGE(s)
m, err := intern.RemapAllLGEs()
if err != nil {
panic(err)
}
for i, sy := range syms {
syms[i] = m[sy]
}
sy, err = intern.NewLGE(s)
if err != nil {
panic(err)
}
syms = append(syms, sy)
}
fmt.Printf("%v\n", syms)
}