mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-10-11 05:13:47 +02:00
use generic functions for dealing with lists.
This commit is contained in:
48
helpers.go
48
helpers.go
@@ -2,12 +2,11 @@ package nostr
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
type StringList []string
|
||||
type IntList []int
|
||||
|
||||
func (as StringList) Equals(bs StringList) bool {
|
||||
func Similar[E constraints.Ordered](as, bs []E) bool {
|
||||
if len(as) != len(bs) {
|
||||
return false
|
||||
}
|
||||
@@ -28,37 +27,7 @@ func (as StringList) Equals(bs StringList) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (as IntList) Equals(bs IntList) bool {
|
||||
if len(as) != len(bs) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, a := range as {
|
||||
for _, b := range bs {
|
||||
if b == a {
|
||||
goto next
|
||||
}
|
||||
}
|
||||
// didn't find a B that corresponded to the current A
|
||||
return false
|
||||
|
||||
next:
|
||||
continue
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (haystack StringList) Contains(needle string) bool {
|
||||
for _, hay := range haystack {
|
||||
if hay == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (haystack StringList) ContainsPrefixOf(needle string) bool {
|
||||
func ContainsPrefixOf(haystack []string, needle string) bool {
|
||||
for _, hay := range haystack {
|
||||
if strings.HasPrefix(needle, hay) {
|
||||
return true
|
||||
@@ -66,12 +35,3 @@ func (haystack StringList) ContainsPrefixOf(needle string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (haystack IntList) Contains(needle int) bool {
|
||||
for _, hay := range haystack {
|
||||
if hay == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user