-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
58 lines (45 loc) · 1.72 KB
/
string.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
package gopii
import "strings"
// String is a representation of a string whose parts can be obscured.
type String struct {
obscureFuncs map[PartType]map[Level]ObscureFunc
parts []part
}
// PartType is the type of a part of a string. Different part types can be obscured differently.
type PartType int
type part struct {
s string
typ PartType
}
// NewString returns a new String containing the given parts. More parts can be added using Append or AppendParts.
// obscureFuncs determines how each part type is obscured at each output level.
func NewString(obscureFuncs map[PartType]map[Level]ObscureFunc, parts ...any) *String {
s := &String{obscureFuncs: obscureFuncs}
s.AppendParts(parts...)
return s
}
// Append appends a new part to the String.
func (s *String) Append(str string, typ PartType) {
s.parts = append(s.parts, part{s: str, typ: typ})
}
// AppendParts appends new parts to the String. parts must be in pairs of string and PartType.
func (s *String) AppendParts(parts ...any) {
for i := 0; i < len(parts); i += 2 {
s.Append(parts[i].(string), parts[i+1].(PartType)) //nolint:forcetypeassert // must conform to these types
}
}
// String returns the string with its parts obscured according to outLevel and the String's obscureFuncs.
// For each part, if no ObscureFunc is found for outLevel, the ObscureFunc for the next higher level is used.
// If there is no ObscureFunc for a higher level, Obscured() is used.
func (s *String) String(outLevel Level) string {
buf := strings.Builder{}
for _, part := range s.parts {
obscureFuncs, ok := s.obscureFuncs[part.typ]
if !ok {
buf.WriteString(Obscured()(part.s))
continue
}
buf.WriteString(obscure(part.s, outLevel, obscureFuncs))
}
return buf.String()
}