mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-01-02 13:03:11 +00:00
This adds a `Component` helper type and a `ForEach` helper method. The first attempt used an interface but interfaces imply allocation. We really can't afford to allocate here.
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package multiaddr
|
|
|
|
import "fmt"
|
|
|
|
// Split returns the sub-address portions of a multiaddr.
|
|
func Split(m Multiaddr) []Multiaddr {
|
|
var addrs []Multiaddr
|
|
ForEach(m, func(c Component) bool {
|
|
addrs = append(addrs, &c)
|
|
return true
|
|
})
|
|
return addrs
|
|
}
|
|
|
|
// Join returns a combination of addresses.
|
|
func Join(ms ...Multiaddr) Multiaddr {
|
|
switch len(ms) {
|
|
case 0:
|
|
// empty multiaddr, unfortunately, we have callers that rely on
|
|
// this contract.
|
|
return multiaddr{}
|
|
case 1:
|
|
return ms[0]
|
|
}
|
|
|
|
length := 0
|
|
bs := make([][]byte, len(ms))
|
|
for i, m := range ms {
|
|
bs[i] = m.Bytes()
|
|
length += len(bs[i])
|
|
}
|
|
|
|
bidx := 0
|
|
b := make([]byte, length)
|
|
for _, mb := range bs {
|
|
bidx += copy(b[bidx:], mb)
|
|
}
|
|
return multiaddr{bytes: b}
|
|
}
|
|
|
|
// 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
|
|
}
|