From 87f65f2bd2cab50ccd9c6771ec68b26f529c2098 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Tue, 14 Nov 2017 15:20:06 +0100 Subject: [PATCH] Updated StringArgumentType.escapeIfRequired to new string rules --- build.gradle | 2 +- src/main/java/com/mojang/brigadier/StringReader.java | 2 +- .../com/mojang/brigadier/arguments/StringArgumentType.java | 6 ++++-- .../mojang/brigadier/arguments/StringArgumentTypeTest.java | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 97b9fb0..eff18c0 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ import groovy.io.FileType apply plugin: 'java-library' apply plugin: 'maven' -version = '0.1.9' +version = '0.1.10' group = 'com.mojang' task wrapper(type: Wrapper) { diff --git a/src/main/java/com/mojang/brigadier/StringReader.java b/src/main/java/com/mojang/brigadier/StringReader.java index 007061f..abc62cb 100644 --- a/src/main/java/com/mojang/brigadier/StringReader.java +++ b/src/main/java/com/mojang/brigadier/StringReader.java @@ -137,7 +137,7 @@ public class StringReader implements ImmutableStringReader { } } - private static boolean isAllowedInUnquotedString(final char c) { + public static boolean isAllowedInUnquotedString(final char c) { return c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' diff --git a/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java index e8082d1..a494b14 100644 --- a/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/StringArgumentType.java @@ -52,8 +52,10 @@ public class StringArgumentType implements ArgumentType { } public static String escapeIfRequired(final String input) { - if (input.contains("\\") || input.contains("\"") || input.contains(CommandDispatcher.ARGUMENT_SEPARATOR)) { - return escape(input); + for (final char c : input.toCharArray()) { + if (!StringReader.isAllowedInUnquotedString(c)) { + return escape(input); + } } return input; } diff --git a/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java index 6e4eff1..5c74d19 100644 --- a/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java +++ b/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java @@ -58,7 +58,7 @@ public class StringArgumentTypeTest { @Test public void testEscapeIfRequired_notRequired() throws Exception { - assertThat(escapeIfRequired("hello!"), is(equalTo("hello!"))); + assertThat(escapeIfRequired("hello"), is(equalTo("hello"))); assertThat(escapeIfRequired(""), is(equalTo(""))); }