Skip to content

Commit

Permalink
switch to ascii85
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Aug 12, 2022
1 parent 906e7bb commit 80c3f7e
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions subscriber_list.go
@@ -1,7 +1,7 @@
package mercure

import (
"encoding/base64"
"encoding/ascii85"
"sort"
"strings"

Expand All @@ -17,23 +17,27 @@ func NewSubscriberList(size int) *SubscriberList {
skipfilter: skipfilter.New(func(s interface{}, filter interface{}) bool {
var private bool

encodedTopics := strings.Split(filter.(string), ":")
encodedTopics := strings.Split(filter.(string), "~")
topics := make([]string, len(encodedTopics))
for i, encodedTopic := range encodedTopics {
decoded, err := base64.StdEncoding.DecodeString(encodedTopic)
if err != nil {
return false
}

p := strings.SplitN(string(decoded), "_", 2)
p := strings.SplitN(encodedTopic, ">", 2)
if len(p) < 2 {
return false
}

if p[0] == "p" {
private = true
}
topics[i] = p[1]

decodedTopic := make([]byte, len(p[1]))
ndst, _, err := ascii85.Decode(decodedTopic, []byte(p[1]), true)
if err != nil {
return false
}

str := string(decodedTopic[:ndst])

topics[i] = str
}

return s.(*Subscriber).MatchTopics(topics, private)
Expand All @@ -44,16 +48,20 @@ func NewSubscriberList(size int) *SubscriberList {
func (sc *SubscriberList) MatchAny(u *Update) (res []*Subscriber) {
encodedTopics := make([]string, len(u.Topics))
for i, t := range u.Topics {
encodedTopic := make([]byte, ascii85.MaxEncodedLen(len(t)))
nb := ascii85.Encode(encodedTopic, []byte(t))
encodedTopic = encodedTopic[:nb]

if u.Private {
encodedTopics[i] = base64.StdEncoding.EncodeToString([]byte("p_" + t))
encodedTopics[i] = "p>" + string(encodedTopic)
} else {
encodedTopics[i] = base64.StdEncoding.EncodeToString([]byte("_" + t))
encodedTopics[i] = ">" + string(encodedTopic)
}
}

sort.Strings(encodedTopics)

for _, m := range sc.skipfilter.MatchAny(strings.Join(encodedTopics, ":")) {
for _, m := range sc.skipfilter.MatchAny(strings.Join(encodedTopics, "~")) {
res = append(res, m.(*Subscriber))
}

Expand Down

0 comments on commit 80c3f7e

Please sign in to comment.