-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstringer.go
91 lines (79 loc) · 2.09 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vnet
import (
"fmt"
"sort"
"strings"
)
type MaskedStringer interface {
MaskedString(mask MaskedStringer) string
}
type masked_string_pair struct{ v, m MaskedStringer }
type MaskedStrings struct {
m map[string]masked_string_pair
}
func (x *MaskedStrings) Add(key string, v, m MaskedStringer) {
if x.m == nil {
x.m = make(map[string]masked_string_pair)
}
x.m[key] = masked_string_pair{v: v, m: m}
}
func (x *MaskedStrings) Strings() (s []string) {
type t struct{ k, v string }
var ts []t
for k, v := range x.m {
ts = append(ts, t{k: k, v: v.v.MaskedString(v.m)})
}
sort.Slice(ts, func(i, j int) bool { return ts[i].k < ts[j].k })
for i := range ts {
s = append(s, ts[i].k+": "+ts[i].v)
}
return
}
func (x *MaskedStrings) String() (s string) {
lines := x.Strings()
return strings.Join(lines, ",")
}
type Bool bool
func (v Bool) MaskedString(r MaskedStringer) (s string) {
m := r.(Bool)
s = fmt.Sprintf("%v", v)
if m != true {
s += fmt.Sprintf("/%v", m)
}
return
}
func (v Uint8) MaskedString(r MaskedStringer) string {
m := r.(Uint8)
return fmt.Sprintf("0x%x/%x", v, m)
}
func (v Uint16) MaskedString(r MaskedStringer) string {
m := r.(Uint16)
return fmt.Sprintf("0x%x/%x", v.ToHost(), m.ToHost())
}
func (v Uint32) MaskedString(r MaskedStringer) string {
m := r.(Uint32)
return fmt.Sprintf("0x%x/%x", v.ToHost(), m.ToHost())
}
func (v Uint64) MaskedString(r MaskedStringer) string {
m := r.(Uint64)
return fmt.Sprintf("0x%x/%x", v.ToHost(), m.ToHost())
}
// As above but host byte order.
type HostUint16 uint16
type HostUint32 uint32
type HostUint64 uint64
func (v HostUint16) MaskedString(r MaskedStringer) string {
m := r.(HostUint16)
return fmt.Sprintf("0x%x/%x", v, m)
}
func (v HostUint32) MaskedString(r MaskedStringer) string {
m := r.(HostUint32)
return fmt.Sprintf("0x%x/%x", v, m)
}
func (v HostUint64) MaskedString(r MaskedStringer) string {
m := r.(HostUint64)
return fmt.Sprintf("0x%x/%x", v, m)
}