/
name.go
64 lines (55 loc) · 1.3 KB
/
name.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
package ndn
import (
"bytes"
"github.com/go-ndn/lpm"
"github.com/go-ndn/tlv"
)
// Name is a hierarchical name for NDN content, which contains a sequence of name components.
type Name struct {
Components []lpm.Component `tlv:"8"`
ImplicitDigestSHA256 lpm.Component `tlv:"1?"`
}
// NewName creates a name by invoking lpm.NewComponents.
func NewName(s string) (n Name) {
n.Components = lpm.NewComponents(s)
return
}
// Compare compares two names according to http://named-data.net/doc/ndn-tlv/name.html#canonical-order.
//
// -1 if a < b; 0 if a == b; 1 if a > b
func (n *Name) Compare(n2 Name) int {
l1, l2 := n.Len(), n2.Len()
for i := 0; i < l1 && i < l2; i++ {
cmp := bytes.Compare(n.Components[i], n2.Components[i])
if cmp != 0 {
return cmp
}
}
if l1 < l2 {
return -1
}
if l1 > l2 {
return 1
}
return 0
}
// Len returns the number of components.
func (n *Name) Len() int {
return len(n.Components)
}
// WriteTo implements tlv.WriteTo
func (n *Name) WriteTo(w tlv.Writer) error {
return w.Write(n, 7)
}
// ReadFrom implements tlv.ReadFrom
func (n *Name) ReadFrom(r tlv.Reader) error {
return r.Read(n, 7)
}
func (n Name) String() string {
buf := new(bytes.Buffer)
for _, c := range n.Components {
buf.WriteByte('/')
buf.WriteString(c.String())
}
return buf.String()
}