Skip to content

Commit

Permalink
Replace regex with custom logic for MQTT topic matching
Browse files Browse the repository at this point in the history
  • Loading branch information
epieffe committed Dec 29, 2024
1 parent def41f5 commit 74de979
Showing 1 changed file with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,31 @@ private boolean topicMatches(String topic, String pattern, long authorization)
pattern = pattern.replace(String.format("{guarded[%s].identity}", g.name), identity);
}
}
return topic.matches(pattern
.replace("{", "\\{")
.replace("}", "\\}")
.replace("[", "\\[")
.replace("]", "\\]")
.replace(".", "\\.")
.replace("$", "\\$")
.replace("+", "[^/]*")
.replace("#", ".*"));
int topicIndex = 0;
for (int i = 0; i < pattern.length(); ++i)
{
char patternChar = pattern.charAt(i);
if (patternChar == '#')
{
return true;
}
else if (patternChar == '+')
{
while (topicIndex < topic.length())
{
if (topic.charAt(topicIndex) == '/')
{
break;
}
topicIndex++;
}
}
else if (topicIndex == topic.length() || topic.charAt(topicIndex++) != patternChar)
{
return false;
}
}
return topicIndex == topic.length();
}

private static List<Matcher> asWildcardMatcher(
Expand Down

0 comments on commit 74de979

Please sign in to comment.