Newer
Older
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# bool SubscriberMeta::matches(const std::string &other_topic, const std::vector<std::string> &other_topic_split) {
# // TODO: block subscriptions for invalid topics
#
# if(this->topic == "#")
# return true;
#
# // if(other_topic_split.size() == this->topic_split.size() - 1)
# // if((other_topic + "/#") == this->topic)
# // return true;
#
# for(u32 i = 0; i < this->topic_split.size(); i++) {
# auto &p = this->topic_split.at(i);
#
# if(p == "#")
# return true;
#
# if(p == "+")
# continue;
#
# if(p != other_topic_split.at(i))
# return false;
# }
#
# if(this->topic_split.size() == other_topic_split.size())
# return true;
#
# if(this->topic_split.at(this->topic_split.size() - 1) == "#")
# return true;
#
# return false;
# }
def get(l: list, idx: int):
if idx >= 0 and idx < len(l):
return l[idx]
else:
return None
def matches(topic, otherTopic) -> bool:
if topic == '#':
return True
topicSplit = topic.split('/')
otherTopicSplit = otherTopic.split('/')
for i in range(len(topicSplit)):
p = get(topicSplit, i)
if p == '#':
return True
if p == '+':
continue
if p != get(otherTopicSplit, i):
return False
if len(topicSplit) == len(otherTopicSplit):
return True
# if get(topicSplit, len(topicSplit) - 2) == '#':
# return True
return False