-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
53 lines (49 loc) · 1.07 KB
/
match.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package strings
import (
"strings"
)
// MqttMatch answers true if the topic matches the pattern according
// to MQTT rules:
// * Pattern is a slash-separated hierarchy.
// Wildcard "+" matches a single level in the hierarchy.
// Wildcard "#" matches all further levels in the hierarchy.
//
// Examples:
// Pattern a/b matches topic a/b
// a/# matches a/b, a/c, a/b/c etc
// a/+/c matches a/b/c, a/c/c, but not a/b, not a/b/c/c, etc.
func MqttMatch(pattern, topic string) bool {
pattern, nextPattern := mqttChunk(pattern)
nextTopic := topic
for len(pattern) > 0 {
topic, nextTopic = mqttChunk(nextTopic)
switch pattern {
case "#":
return true
case "+":
default:
if pattern != topic {
return false
}
}
pattern, nextPattern = mqttChunk(nextPattern)
}
return nextTopic == ""
}
func mqttChunk(s string) (string, string) {
idx := strings.Index(s, "/")
if idx < 0 {
return s, ""
} else if idx == 0 {
if len(s) < 1 {
return "", ""
}
return mqttChunk(s[1:])
} else {
next := ""
if idx < len(s) {
next = s[idx+1:]
}
return s[0:idx], next
}
}