-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstringer.go
61 lines (54 loc) · 1.27 KB
/
stringer.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 elf : stringer.go implements various string related utilites
// and stringer interface implementation for all custom types.
package elf
import "strconv"
// flagName pairs integer flags and a corresponding string.
type flagName struct {
flag uint32
name string
}
// stringify matches various elf flags against their naming maps.
func stringify(flag uint32, names []flagName, goSyntax bool) string {
for _, n := range names {
if n.flag == flag {
if goSyntax {
return "elf." + n.name
}
return n.name
}
}
for j := len(names) - 1; j >= 0; j-- {
n := names[j]
if n.flag < flag {
name := n.name
if goSyntax {
name = "elf." + name
}
return name + "+" + strconv.FormatUint(uint64(flag-n.flag), 10)
}
}
return strconv.FormatUint(uint64(flag), 10)
}
// matchFlagName matches a given integer flag against it's corresponding flagname.
func matchFlagName(flag uint32, names []flagName, goSyntax bool) string {
s := ""
for _, n := range names {
if n.flag&flag == n.flag {
if len(s) > 0 {
s += "+"
}
if goSyntax {
s += "elf."
}
s += n.name
flag -= n.flag
}
}
if len(s) == 0 {
return "0x" + strconv.FormatUint(uint64(flag), 16)
}
if flag != 0 {
s += "+0x" + strconv.FormatUint(uint64(flag), 16)
}
return s
}