From 524d5a5363ee2cdd22f2dc8ce81ec4cebe356f5c Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Wed, 15 Nov 2023 01:01:10 +0100 Subject: [PATCH] Cleanup MqttTopicFilterImpl.validateWildcards --- .../mqtt/datatypes/MqttTopicFilterImpl.java | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/hivemq/client2/internal/mqtt/datatypes/MqttTopicFilterImpl.java b/src/main/java/com/hivemq/client2/internal/mqtt/datatypes/MqttTopicFilterImpl.java index a4cc3e85b..58b21b97d 100644 --- a/src/main/java/com/hivemq/client2/internal/mqtt/datatypes/MqttTopicFilterImpl.java +++ b/src/main/java/com/hivemq/client2/internal/mqtt/datatypes/MqttTopicFilterImpl.java @@ -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: @@ -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; @@ -170,7 +169,6 @@ static int validateWildcards(final byte @NotNull [] binary, final int start) { break; } } - return wildcardFlags; } @@ -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: @@ -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; @@ -233,7 +230,6 @@ static int validateWildcards(final @NotNull String string, final int start) { break; } } - return wildcardFlags; }