Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a Contains function #172

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions multiaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,13 @@ addrloop:
}
return b
}

// Contains reports whether addr is contained in addrs.
func Contains(addrs []Multiaddr, addr Multiaddr) bool {
for _, a := range addrs {
if addr.Equal(a) {
return true
}
}
return false
}
13 changes: 13 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,16 @@ func TestFilterAddrs(t *testing.T) {
require.ElementsMatch(t, FilterAddrs(good, filter), good)
require.ElementsMatch(t, FilterAddrs(goodAndBad, filter), good)
}

func TestContains(t *testing.T) {
a1 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")
a2 := newMultiaddr(t, "/ip4/1.1.1.1/tcp/999")
a3 := newMultiaddr(t, "/ip4/1.2.3.4/udp/443/quic")
addrs := []Multiaddr{a1, a2, a3}

require.True(t, Contains(addrs, a1))
require.True(t, Contains(addrs, a2))
require.True(t, Contains(addrs, a3))
require.False(t, Contains(addrs, newMultiaddr(t, "/ip4/4.3.2.1/udp/1234/utp")))
require.False(t, Contains(nil, a1))
}