Skip to content

Commit

Permalink
add a Contains function
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jan 10, 2022
1 parent f901192 commit 1c6ed28
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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))
}

0 comments on commit 1c6ed28

Please sign in to comment.