summaryrefslogtreecommitdiffhomepage
path: root/words/words.go
blob: ebac1cc0a571ebc5c0fee189e33ca1dd4a1397a5 (plain)
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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

// 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:]
		} else {
			txt = nil
		}
		if word := strings.TrimSpace(string(word)); word != "" && word[0] != '#' {
			ret = append(ret, word)
		}
	}
	return ret
}