forked from multiformats/go-multiaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
129 lines (116 loc) · 2.97 KB
/
util.go
File metadata and controls
129 lines (116 loc) · 2.97 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package multiaddr
import (
"fmt"
"github.com/aperturerobotics/go-multiaddr/x/meg"
)
// Split returns the sub-address portions of a multiaddr.
func Split(m Multiaddr) []Component {
return m
}
// Join returns a combination of addresses.
// Note: This copies all the components from the input Multiaddrs. Depending on
// your use case, you may prefer to use `append(leftMA, rightMA...)` instead.
func Join(msInterfaces ...Multiaddrer) Multiaddr {
ms := make([]Multiaddr, len(msInterfaces))
for i, m := range msInterfaces {
if m == nil {
continue
}
ms[i] = m.Multiaddr()
}
size := 0
for _, m := range ms {
size += len(m)
}
if size == 0 {
return nil
}
out := make([]Component, 0, size)
for _, m := range ms {
out = append(out, m...)
}
return out
}
// Cast re-casts a byte slice as a multiaddr. will panic if it fails to parse.
func Cast(b []byte) Multiaddr {
m, err := NewMultiaddrBytes(b)
if err != nil {
panic(fmt.Errorf("multiaddr failed to parse: %s", err))
}
return m
}
// StringCast like Cast, but parses a string. Will also panic if it fails to parse.
func StringCast(s string) Multiaddr {
m, err := NewMultiaddr(s)
if err != nil {
panic(fmt.Errorf("multiaddr failed to parse: %s", err))
}
return m
}
// SplitFirst returns the first component and the rest of the multiaddr.
func SplitFirst(m Multiaddr) (*Component, Multiaddr) {
if len(m) == 0 {
return nil, nil
}
if len(m) == 1 {
return &m[0], nil
}
// defensive copy. Users can avoid by doing the split themselves.
copyC := m[0]
return ©C, m[1:].copy()
}
// SplitLast returns the rest of the multiaddr and the last component.
func SplitLast(m Multiaddr) (Multiaddr, *Component) {
if len(m) == 0 {
return nil, nil
}
if len(m) == 1 {
// We want to explicitly return a nil slice if the prefix is now empty.
return nil, &m[0]
}
// defensive copy. Users can avoid by doing the split themselves.
copyC := m[len(m)-1]
return m[:len(m)-1].copy(), ©C
}
// SplitFunc splits the multiaddr when the callback first returns true. The
// component on which the callback first returns will be included in the
// *second* multiaddr.
func SplitFunc(m Multiaddr, cb func(Component) bool) (Multiaddr, Multiaddr) {
if len(m) == 0 {
return nil, nil
}
idx := len(m)
for i, c := range m {
if cb(c) {
idx = i
break
}
}
pre, post := m[:idx], m[idx:]
if len(pre) == 0 {
pre = nil
}
if len(post) == 0 {
post = nil
}
// defensive copy. Users can avoid by doing the split themselves.
return pre.copy(), post.copy()
}
// ForEach walks over the multiaddr, component by component.
//
// This function iterates over components.
// Return true to continue iteration, false to stop.
//
// Prefer a standard for range loop instead
// e.g. `for _, c := range m { ... }`
func ForEach(m Multiaddr, cb func(c Component) bool) {
for _, c := range m {
if !cb(c) {
return
}
}
}
func (m Multiaddr) Match(p ...meg.Pattern) (bool, error) {
matcher := meg.PatternToMatcher(p...)
return meg.Match(matcher, m)
}