mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
35 lines
451 B
Go
35 lines
451 B
Go
package intern
|
|
|
|
import "sync"
|
|
|
|
type Interner struct {
|
|
mu sync.RWMutex
|
|
data map[string]string
|
|
}
|
|
|
|
func New() *Interner {
|
|
return &Interner{data: map[string]string{}}
|
|
}
|
|
|
|
func (i *Interner) Flush() {
|
|
i.mu.Lock()
|
|
i.data = map[string]string{}
|
|
i.mu.Unlock()
|
|
}
|
|
|
|
func (i *Interner) String(s string) string {
|
|
i.mu.RLock()
|
|
interned, ok := i.data[s]
|
|
i.mu.RUnlock()
|
|
|
|
if ok {
|
|
return interned
|
|
}
|
|
|
|
i.mu.Lock()
|
|
i.data[s] = s
|
|
i.mu.Unlock()
|
|
|
|
return s
|
|
}
|