Skip to content

Commit

Permalink
Cleanup MqttTopicFilterImpl.validateWildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Nov 15, 2023
1 parent 2a678a7 commit 524d5a5
Showing 1 changed file with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,18 @@ public class MqttTopicFilterImpl extends MqttUtf8StringImpl implements MqttTopic
*/
static int validateWildcards(final byte @NotNull [] binary, final int start) {
int wildcardFlags = 0;

int state = WILDCARD_CHECK_STATE_BEFORE;

for (int i = start; i < binary.length; i++) {
final byte b = binary[i];
switch (state) {
case WILDCARD_CHECK_STATE_NOT_BEFORE:
if ((b == SINGLE_LEVEL_WILDCARD) || (b == MULTI_LEVEL_WILDCARD)) {
return WILDCARD_CHECK_FAILURE;
}
if (b == MqttTopicImpl.TOPIC_LEVEL_SEPARATOR) {
state = WILDCARD_CHECK_STATE_BEFORE;
switch (b) {
case MULTI_LEVEL_WILDCARD:
case SINGLE_LEVEL_WILDCARD:
return WILDCARD_CHECK_FAILURE;
case MqttTopicImpl.TOPIC_LEVEL_SEPARATOR:
state = WILDCARD_CHECK_STATE_BEFORE;
break;
}
break;
case WILDCARD_CHECK_STATE_BEFORE:
Expand All @@ -154,7 +154,6 @@ static int validateWildcards(final byte @NotNull [] binary, final int start) {
state = WILDCARD_CHECK_STATE_SINGLE_LEVEL;
break;
case MqttTopicImpl.TOPIC_LEVEL_SEPARATOR:
state = WILDCARD_CHECK_STATE_BEFORE;
break;
default:
state = WILDCARD_CHECK_STATE_NOT_BEFORE;
Expand All @@ -170,7 +169,6 @@ static int validateWildcards(final byte @NotNull [] binary, final int start) {
break;
}
}

return wildcardFlags;
}

Expand All @@ -185,20 +183,20 @@ static int validateWildcards(final byte @NotNull [] binary, final int start) {
*/
static int validateWildcards(final @NotNull String string, final int start) {
int wildcardFlags = 0;

int state = WILDCARD_CHECK_STATE_BEFORE;

for (int i = start; i < string.length(); i++) {
final char c = string.charAt(i);
switch (state) {
case WILDCARD_CHECK_STATE_NOT_BEFORE:
if ((c == SINGLE_LEVEL_WILDCARD) || (c == MULTI_LEVEL_WILDCARD)) {
throw new IllegalArgumentException("Topic filter [" + string.substring(start) +
"] contains misplaced wildcard characters. Wildcard (" + c + ") at index " +
(i - start) + " must follow a topic level separator.");
}
if (c == MqttTopicImpl.TOPIC_LEVEL_SEPARATOR) {
state = WILDCARD_CHECK_STATE_BEFORE;
switch (c) {
case MULTI_LEVEL_WILDCARD:
case SINGLE_LEVEL_WILDCARD:
throw new IllegalArgumentException("Topic filter [" + string.substring(start) +
"] contains misplaced wildcard characters. Wildcard (" + c + ") at index " +
(i - start) + " must follow a topic level separator.");
case MqttTopicImpl.TOPIC_LEVEL_SEPARATOR:
state = WILDCARD_CHECK_STATE_BEFORE;
break;
}
break;
case WILDCARD_CHECK_STATE_BEFORE:
Expand All @@ -212,7 +210,6 @@ static int validateWildcards(final @NotNull String string, final int start) {
state = WILDCARD_CHECK_STATE_SINGLE_LEVEL;
break;
case MqttTopicImpl.TOPIC_LEVEL_SEPARATOR:
state = WILDCARD_CHECK_STATE_BEFORE;
break;
default:
state = WILDCARD_CHECK_STATE_NOT_BEFORE;
Expand All @@ -233,7 +230,6 @@ static int validateWildcards(final @NotNull String string, final int start) {
break;
}
}

return wildcardFlags;
}

Expand Down

0 comments on commit 524d5a5

Please sign in to comment.