mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-04 20:09:03 +00:00
words: add accessors and tests for a few of our favorite words
Updates #1235 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
57
words/words.go
Normal file
57
words/words.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package words contains accessors for some nice words.
|
||||
package words
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
//go:embed tails.txt
|
||||
var tailsTxt []byte
|
||||
|
||||
//go:embed scales.txt
|
||||
var scalesTxt []byte
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
tails, scales []string
|
||||
)
|
||||
|
||||
// Tails returns words about tails.
|
||||
func Tails() []string {
|
||||
once.Do(initWords)
|
||||
return tails
|
||||
}
|
||||
|
||||
// Scales returns words about scales.
|
||||
func Scales() []string {
|
||||
once.Do(initWords)
|
||||
return scales
|
||||
}
|
||||
|
||||
func initWords() {
|
||||
tails = parseWords(tailsTxt)
|
||||
scales = parseWords(scalesTxt)
|
||||
}
|
||||
|
||||
func parseWords(txt []byte) []string {
|
||||
n := bytes.Count(txt, []byte{'\n'})
|
||||
ret := make([]string, 0, n)
|
||||
for len(txt) > 0 {
|
||||
word := txt
|
||||
i := bytes.IndexByte(txt, '\n')
|
||||
if i != -1 {
|
||||
word, txt = word[:i], txt[i+1:]
|
||||
}
|
||||
if word := strings.TrimSpace(string(word)); word != "" {
|
||||
ret = append(ret, word)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
Reference in New Issue
Block a user