From 92c4b29a4ca2986e5f85630478be628428980096 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Mon, 4 Dec 2023 12:59:16 +0300 Subject: [PATCH] feat(topic): match empty topic levels more loosely So that the result of `emqx_topic:tokens/1` would be perfectly matchable with the result of `emqx_topic:words/1` of a topic filter with empty levels. --- apps/emqx/src/emqx_topic.erl | 8 +++----- apps/emqx/test/emqx_topic_SUITE.erl | 6 ++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/emqx/src/emqx_topic.erl b/apps/emqx/src/emqx_topic.erl index 76c6ef34e..bc6946a43 100644 --- a/apps/emqx/src/emqx_topic.erl +++ b/apps/emqx/src/emqx_topic.erl @@ -91,13 +91,11 @@ match([H | T1], [H | T2]) -> match(T1, T2); match([_H | T1], ['+' | T2]) -> match(T1, T2); +match([<<>> | T1], ['' | T2]) -> + match(T1, T2); match(_, ['#']) -> true; -match([_H1 | _], [_H2 | _]) -> - false; -match([_H1 | _], []) -> - false; -match([], [_H | _T2]) -> +match(_, _) -> false. -spec match_share(Name, Filter) -> boolean() when diff --git a/apps/emqx/test/emqx_topic_SUITE.erl b/apps/emqx/test/emqx_topic_SUITE.erl index 4761ea17d..7a2130992 100644 --- a/apps/emqx/test/emqx_topic_SUITE.erl +++ b/apps/emqx/test/emqx_topic_SUITE.erl @@ -115,6 +115,12 @@ t_sys_match(_) -> true = match(<<"a/b/$c">>, <<"a/b/#">>), true = match(<<"a/b/$c">>, <<"a/#">>). +t_match_tokens(_) -> + true = match(emqx_topic:tokens(<<"a/b/c">>), words(<<"a/+/c">>)), + true = match(emqx_topic:tokens(<<"a//c">>), words(<<"a/+/c">>)), + false = match(emqx_topic:tokens(<<"a//c/">>), words(<<"a/+/c">>)), + true = match(emqx_topic:tokens(<<"a//c/">>), words(<<"a/+/c/#">>)). + t_match_perf(_) -> true = match(<<"a/b/ccc">>, <<"a/#">>), Name = <<"/abkc/19383/192939/akakdkkdkak/xxxyyuya/akakak">>,