1 // Package intern interns strings.
2 // Interning is best effort only.
3 // Interned strings may be removed automatically
4 // at any time without notification.
5 // All functions may be called concurrently
6 // with themselves and each other.
7 package intern
8 9 import "sync"
10 11 var (
12 pool sync.Pool = sync.Pool{
13 New: func() interface{} {
14 return make(map[string]string)
15 },
16 }
17 )
18 19 // String returns s, interned.
20 func String(s string) string {
21 m := pool.Get().(map[string]string)
22 c, ok := m[s]
23 if ok {
24 pool.Put(m)
25 return c
26 }
27 m[s] = s
28 pool.Put(m)
29 return s
30 }
31 32 // Bytes returns b converted to a string, interned.
33 func Bytes(b []byte) string {
34 m := pool.Get().(map[string]string)
35 c, ok := m[string(b)]
36 if ok {
37 pool.Put(m)
38 return c
39 }
40 s := string(b)
41 m[s] = s
42 pool.Put(m)
43 return s
44 }
45