-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifier.go
61 lines (49 loc) · 1.04 KB
/
modifier.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
59
60
61
package main
import (
"fmt"
)
type modifier uint8
const (
modifierBold modifier = iota
modifierUnderline
modifierReverse
modifierBlink
)
const firstModifier = modifierBold
const lastModifier = modifierBlink
var itoaModifier = map[modifier]string{
modifierBold: "bold",
modifierUnderline: "underline",
modifierReverse: "reverse",
modifierBlink: "blink",
}
var atoiModifier = func() map[string]modifier {
m := map[string]modifier{}
for k, v := range itoaModifier {
m[v] = k
}
return m
}()
func parseModifier(s string) (modifier, error) {
if m, ok := atoiModifier[s]; ok {
return m, nil
}
return modifierBold, fmt.Errorf("unknown modifier %q", s)
}
func (m modifier) String() string {
return itoaModifier[m]
}
type modifierSet uint8
func (s *modifierSet) set(m modifier) {
*s |= modifierSet(1 << m)
}
func (s *modifierSet) test(m modifier) bool {
return *s&modifierSet(1<<m) != 0
}
func (s *modifierSet) foreach(f func(m modifier)) {
for m := firstModifier; m <= lastModifier; m++ {
if s.test(m) {
f(m)
}
}
}