helpers.go -> utils.go and lowercase util functions.

This commit is contained in:
fiatjaf
2023-02-27 16:30:48 -03:00
parent 916a6a6abb
commit de7179437e
3 changed files with 83 additions and 82 deletions

View File

@@ -46,7 +46,7 @@ func (ef Filter) Matches(event *Event) bool {
return false return false
} }
if ef.IDs != nil && !ContainsPrefixOf(ef.IDs, event.ID) { if ef.IDs != nil && !containsPrefixOf(ef.IDs, event.ID) {
return false return false
} }
@@ -54,7 +54,7 @@ func (ef Filter) Matches(event *Event) bool {
return false return false
} }
if ef.Authors != nil && !ContainsPrefixOf(ef.Authors, event.PubKey) { if ef.Authors != nil && !containsPrefixOf(ef.Authors, event.PubKey) {
return false return false
} }
@@ -76,15 +76,15 @@ func (ef Filter) Matches(event *Event) bool {
} }
func FilterEqual(a Filter, b Filter) bool { func FilterEqual(a Filter, b Filter) bool {
if !Similar(a.Kinds, b.Kinds) { if !similar(a.Kinds, b.Kinds) {
return false return false
} }
if !Similar(a.IDs, b.IDs) { if !similar(a.IDs, b.IDs) {
return false return false
} }
if !Similar(a.Authors, b.Authors) { if !similar(a.Authors, b.Authors) {
return false return false
} }
@@ -96,7 +96,7 @@ func FilterEqual(a Filter, b Filter) bool {
if bv, ok := b.Tags[f]; !ok { if bv, ok := b.Tags[f]; !ok {
return false return false
} else { } else {
if !Similar(av, bv) { if !similar(av, bv) {
return false return false
} }
} }

View File

@@ -1,77 +1 @@
package nostr 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
}

77
utils.go Normal file
View File

@@ -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
}