diff --git a/filter.go b/filter.go index bb67872..e784600 100644 --- a/filter.go +++ b/filter.go @@ -46,7 +46,7 @@ func (ef Filter) Matches(event *Event) bool { return false } - if ef.IDs != nil && !ContainsPrefixOf(ef.IDs, event.ID) { + if ef.IDs != nil && !containsPrefixOf(ef.IDs, event.ID) { return false } @@ -54,7 +54,7 @@ func (ef Filter) Matches(event *Event) bool { return false } - if ef.Authors != nil && !ContainsPrefixOf(ef.Authors, event.PubKey) { + if ef.Authors != nil && !containsPrefixOf(ef.Authors, event.PubKey) { return false } @@ -76,15 +76,15 @@ func (ef Filter) Matches(event *Event) bool { } func FilterEqual(a Filter, b Filter) bool { - if !Similar(a.Kinds, b.Kinds) { + if !similar(a.Kinds, b.Kinds) { return false } - if !Similar(a.IDs, b.IDs) { + if !similar(a.IDs, b.IDs) { return false } - if !Similar(a.Authors, b.Authors) { + if !similar(a.Authors, b.Authors) { return false } @@ -96,7 +96,7 @@ func FilterEqual(a Filter, b Filter) bool { if bv, ok := b.Tags[f]; !ok { return false } else { - if !Similar(av, bv) { + if !similar(av, bv) { return false } } diff --git a/helpers.go b/helpers.go index 1b94a8a..b8f6fcf 100644 --- a/helpers.go +++ b/helpers.go @@ -1,77 +1 @@ package nostr - -import ( - "strings" - - "golang.org/x/exp/constraints" -) - -func Similar[E constraints.Ordered](as, bs []E) 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 ContainsPrefixOf(haystack []string, needle string) bool { - for _, hay := range haystack { - if strings.HasPrefix(needle, hay) { - return true - } - } - return false -} - -// Escaping strings for JSON encoding according to RFC8259. -// Also encloses result in quotation marks "". -func escapeString(dst []byte, s string) []byte { - dst = append(dst, '"') - for i := 0; i < len(s); i++ { - c := s[i] - switch { - case c == '"': - // quotation mark - dst = append(dst, []byte{'\\', '"'}...) - case c == '\\': - // reverse solidus - dst = append(dst, []byte{'\\', '\\'}...) - case c >= 0x20: - // default, rest below are control chars - dst = append(dst, c) - case c == 0x08: - dst = append(dst, []byte{'\\', 'b'}...) - case c < 0x09: - dst = append(dst, []byte{'\\', 'u', '0', '0', '0', '0' + c}...) - case c == 0x09: - dst = append(dst, []byte{'\\', 't'}...) - case c == 0x0a: - dst = append(dst, []byte{'\\', 'n'}...) - case c == 0x0c: - dst = append(dst, []byte{'\\', 'f'}...) - case c == 0x0d: - dst = append(dst, []byte{'\\', 'r'}...) - case c < 0x10: - dst = append(dst, []byte{'\\', 'u', '0', '0', '0', 0x57 + c}...) - case c < 0x1a: - dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x20 + c}...) - case c < 0x20: - dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x47 + c}...) - } - } - dst = append(dst, '"') - return dst -} diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..d1adfee --- /dev/null +++ b/utils.go @@ -0,0 +1,77 @@ +package nostr + +import ( + "strings" + + "golang.org/x/exp/constraints" +) + +func similar[E constraints.Ordered](as, bs []E) 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 containsPrefixOf(haystack []string, needle string) bool { + for _, hay := range haystack { + if strings.HasPrefix(needle, hay) { + return true + } + } + return false +} + +// Escaping strings for JSON encoding according to RFC8259. +// Also encloses result in quotation marks "". +func escapeString(dst []byte, s string) []byte { + dst = append(dst, '"') + for i := 0; i < len(s); i++ { + c := s[i] + switch { + case c == '"': + // quotation mark + dst = append(dst, []byte{'\\', '"'}...) + case c == '\\': + // reverse solidus + dst = append(dst, []byte{'\\', '\\'}...) + case c >= 0x20: + // default, rest below are control chars + dst = append(dst, c) + case c == 0x08: + dst = append(dst, []byte{'\\', 'b'}...) + case c < 0x09: + dst = append(dst, []byte{'\\', 'u', '0', '0', '0', '0' + c}...) + case c == 0x09: + dst = append(dst, []byte{'\\', 't'}...) + case c == 0x0a: + dst = append(dst, []byte{'\\', 'n'}...) + case c == 0x0c: + dst = append(dst, []byte{'\\', 'f'}...) + case c == 0x0d: + dst = append(dst, []byte{'\\', 'r'}...) + case c < 0x10: + dst = append(dst, []byte{'\\', 'u', '0', '0', '0', 0x57 + c}...) + case c < 0x1a: + dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x20 + c}...) + case c < 0x20: + dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x47 + c}...) + } + } + dst = append(dst, '"') + return dst +}