-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic.go
More file actions
34 lines (29 loc) · 781 Bytes
/
topic.go
File metadata and controls
34 lines (29 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package catbird
import "strings"
// matchTopic reports whether topic matches the given pattern.
// Patterns use the same syntax as Bind:
// - "." separates tokens
// - "*" matches exactly one token
// - "#" matches zero or more tokens (must be the final token)
//
// Both pattern and topic must be non-empty.
func matchTopic(pattern, topic string) bool {
if pattern == "#" {
return true
}
patternTokens := strings.Split(pattern, ".")
topicTokens := strings.Split(topic, ".")
for i, pt := range patternTokens {
if pt == "#" {
// # must be last token (validated at registration)
return true
}
if i >= len(topicTokens) {
return false
}
if pt != "*" && pt != topicTokens[i] {
return false
}
}
return len(patternTokens) == len(topicTokens)
}