From 9b1fabb74e3f3de15366e825328aec2984ee6d36 Mon Sep 17 00:00:00 2001 From: Zelaux Date: Tue, 22 Aug 2023 13:17:08 +0300 Subject: [PATCH 1/7] Better CommandHandler --- arc-core/src/arc/util/CommandHandler.java | 106 +++----------- .../command/CommandParamParseException.java | 44 ++++++ .../arc/util/command/CommandParamParser.java | 137 ++++++++++++++++++ .../util/command/CommandParamSplitter.java | 82 +++++++++++ .../src/arc/util/command/CommandParams.java | 18 +++ arc-core/test/utils/command/ParserTest.java | 48 ++++++ arc-core/test/utils/command/SplitterTest.java | 39 +++++ 7 files changed, 386 insertions(+), 88 deletions(-) create mode 100644 arc-core/src/arc/util/command/CommandParamParseException.java create mode 100644 arc-core/src/arc/util/command/CommandParamParser.java create mode 100644 arc-core/src/arc/util/command/CommandParamSplitter.java create mode 100644 arc-core/src/arc/util/command/CommandParams.java create mode 100644 arc-core/test/utils/command/ParserTest.java create mode 100644 arc-core/test/utils/command/SplitterTest.java diff --git a/arc-core/src/arc/util/CommandHandler.java b/arc-core/src/arc/util/CommandHandler.java index a125235cc..ce19d391d 100644 --- a/arc-core/src/arc/util/CommandHandler.java +++ b/arc-core/src/arc/util/CommandHandler.java @@ -4,6 +4,9 @@ import arc.struct.Seq; import arc.struct.ObjectMap; import arc.func.Cons; +import arc.util.command.CommandParamParser; +import arc.util.command.CommandParamSplitter; +import arc.util.command.CommandParams; /** Parses command syntax. */ public class CommandHandler{ @@ -20,9 +23,9 @@ public CommandHandler(String prefix){ public void setPrefix(String prefix){ this.prefix = prefix; } - + public String getPrefix(){ - return prefix; + return prefix; } /** Handles a message with no additional parameters.*/ @@ -36,55 +39,23 @@ public CommandResponse handleMessage(String message, Object params){ if(message == null || (!message.startsWith(prefix))) return new CommandResponse(ResponseType.noCommand, null, null); - message = message.substring(prefix.length()); - - String commandstr = message.contains(" ") ? message.substring(0, message.indexOf(" ")) : message; - String argstr = message.contains(" ") ? message.substring(commandstr.length() + 1) : ""; - - Seq result = new Seq<>(); - + String commandstr = message.contains(" ") ? message.substring(prefix.length(), message.indexOf(" ")) : message.substring(prefix.length()); Command command = commands.get(commandstr); if(command != null){ - int index = 0; - boolean satisfied = false; - - while(true){ - if(index >= command.params.length && !argstr.isEmpty()){ - return new CommandResponse(ResponseType.manyArguments, command, commandstr); - }else if(argstr.isEmpty()) break; - - if(command.params[index].optional || index >= command.params.length - 1 || command.params[index + 1].optional){ - satisfied = true; - } - - if(command.params[index].variadic){ - result.add(argstr); - break; - } - - int next = argstr.indexOf(" "); - if(next == -1){ - if(!satisfied){ - return new CommandResponse(ResponseType.fewArguments, command, commandstr); - } - result.add(argstr); - break; - }else{ - String arg = argstr.substring(0, next); - argstr = argstr.substring(arg.length() + 1); - result.add(arg); - } - - index++; + CommandParamSplitter.SplitResponse splitResponse; + if(message.contains(" ")){ + splitResponse = CommandParamSplitter.split("", 0, 0, command.params); + }else{ + splitResponse = CommandParamSplitter.split(message, commandstr.length() + 1, 0, command.params); } - - if(!satisfied && command.params.length > 0 && !command.params[0].optional){ + if(splitResponse.tooMany){ + return new CommandResponse(ResponseType.manyArguments, command, commandstr); + }else if(splitResponse.tooFew){ return new CommandResponse(ResponseType.fewArguments, command, commandstr); } - command.runner.accept(result.toArray(String.class), params); - + command.runner.accept(splitResponse.args, params); return new CommandResponse(ResponseType.valid, command, commandstr); }else{ return new CommandResponse(ResponseType.unknownCommand, null, commandstr); @@ -108,7 +79,7 @@ public Command register(String text, String description, CommandRunner ru * <mandatory-arg-1> <mandatory-arg-2> ... <mandatory-arg-n> [optional-arg-1] [optional-arg-2]
* Angle brackets indicate mandatory arguments. Square brackets to indicate optional arguments.
* All mandatory arguments must come before optional arguments. Arg names must not have spaces in them.
- * You may also use the ... syntax after the arg name to designate that everything after it will not be split into extra arguments. + * You may also use the ... syntax after the arg name to designate that everything after it will not be split into extra arguments. * There may only be one such argument, and it must be at the end. For example, the syntax * <arg1> [arg2...] will require a first argument, and then take any text after that and put it in the second argument, optionally.*/ public Command register(String text, String params, String description, CommandRunner runner){ @@ -141,7 +112,7 @@ public static class Command{ public final String text; public final String paramText; public final String description; - public final CommandParam[] params; + public final CommandParams params; final CommandRunner runner; public Command(String text, String paramText, String description, CommandRunner runner){ @@ -149,48 +120,7 @@ public Command(String text, String paramText, String description, CommandRunner this.paramText = paramText; this.runner = runner; this.description = description; - - String[] psplit = paramText.split(" "); - if(paramText.length() == 0){ - params = new CommandParam[0]; - }else{ - params = new CommandParam[psplit.length]; - - boolean hadOptional = false; - - for(int i = 0; i < params.length; i++){ - String param = psplit[i]; - - if(param.length() <= 2) throw new IllegalArgumentException("Malformed param '" + param + "'"); - - char l = param.charAt(0), r = param.charAt(param.length() - 1); - boolean optional, variadic = false; - - if(l == '<' && r == '>'){ - if(hadOptional) - throw new IllegalArgumentException("Can't have non-optional param after optional param!"); - optional = false; - }else if(l == '[' && r == ']'){ - optional = true; - }else{ - throw new IllegalArgumentException("Malformed param '" + param + "'"); - } - - if(optional) hadOptional = true; - - String fname = param.substring(1, param.length() - 1); - if(fname.endsWith("...")){ - if(i != params.length - 1) - throw new IllegalArgumentException("A variadic parameter should be the last parameter!"); - - fname = fname.substring(0, fname.length() - 3); - variadic = true; - } - - params[i] = new CommandParam(fname, optional, variadic); - - } - } + params = CommandParamParser.parse(paramText); } } diff --git a/arc-core/src/arc/util/command/CommandParamParseException.java b/arc-core/src/arc/util/command/CommandParamParseException.java new file mode 100644 index 000000000..465ed493b --- /dev/null +++ b/arc-core/src/arc/util/command/CommandParamParseException.java @@ -0,0 +1,44 @@ +package arc.util.command; + +public class CommandParamParseException extends RuntimeException { + public final int startIndex; + public final int endIndex; + public final String rawText; + + public CommandParamParseException(String message, int startIndex, int endIndex, String rawText) { + super(calculateMessage(message, rawText, startIndex, endIndex)); + this.startIndex = startIndex; + this.endIndex = endIndex; + this.rawText = rawText; + + } + + + private static String calculateMessage(String message, String rawText, int startIndex, int endIndex) { + StringBuilder builder = new StringBuilder(message); + appendRanges(builder, startIndex, endIndex); + builder.append("\n").append(rawText).append("\n"); + for (int i = 0; i < startIndex; i++) { + builder.append(" "); + } + for (int i = startIndex; i < endIndex; i++) { + builder.append("^"); + } + builder.append("\n"); + for (int i = 0; i < startIndex; i++) { + builder.append(" "); + } + builder.append(message); +// appendRanges(builder, startIndex, endIndex); + return builder.toString(); + } + + private static void appendRanges(StringBuilder builder, int startIndex, int endIndex) { + builder.append(" at "); + builder.append('['); + builder.append(startIndex); + builder.append(":"); + builder.append(endIndex); + builder.append(']'); + } +} diff --git a/arc-core/src/arc/util/command/CommandParamParser.java b/arc-core/src/arc/util/command/CommandParamParser.java new file mode 100644 index 000000000..27d87e69c --- /dev/null +++ b/arc-core/src/arc/util/command/CommandParamParser.java @@ -0,0 +1,137 @@ +package arc.util.command; + +import arc.struct.Seq; +import arc.util.CommandHandler; +import arc.util.pooling.Pool; + +public class CommandParamParser { + private static final byte SEARCH_PARAM = 0; + private static final byte PARSING_REQUIRED = 1; + private static final byte PARSING_OPTIONAL = 2; + private static final Seq tmpRegions = new Seq<>(); + private static final Pool textRegionPool = new Pool() { + @Override + protected TextRegion newObject() { + return new TextRegion(-1, -1) { + }; + } + }; + + public static CommandParams parse(String text) throws CommandParamParseException { + byte state = SEARCH_PARAM; + int begin = -1; + clear(); + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); + switch (state) { + case SEARCH_PARAM: + if (c != ' ' && c != '<' && c != '[') + throwException("Unexpected char '" + c + "'", i, text); + if (c == '<' || c == '[') { + state = c == '<' ? PARSING_REQUIRED : PARSING_OPTIONAL; + begin = i; + } + break; + + case PARSING_REQUIRED: + if (c == '>') { + state = completeParam(text, begin, i + 1); + } + break; + + case PARSING_OPTIONAL: + if (c == ']') { + state = completeParam(text, begin, i + 1); + } + break; + + } + } + CommandHandler.CommandParam[] params = new CommandHandler.CommandParam[tmpRegions.size]; + boolean wasVariadic = false; + for (int i = 0; i < tmpRegions.size; i++) { + TextRegion region = tmpRegions.get(i); + boolean isVariadic = false; + int nameOffset = 0; + if (region.length() > 5) { + for (int j = 0; ; j++) { + if (text.charAt(region.end - i - 1) != '.') break; + if (j == 2) { + if (wasVariadic) { + + throwException("Cannot be more than one variadic parameter!", region, text); + } + isVariadic = wasVariadic = true; + nameOffset = 3; + break; + } + } + } + params[i] = new CommandHandler.CommandParam( + text.substring(region.start + 1, region.end - 1 - nameOffset), + text.charAt(region.start) == '[', + isVariadic + ); + } + clear(); + return new CommandParams(params); + } + + private static void clear() { + textRegionPool.freeAll(tmpRegions); + tmpRegions.clear(); + } + + private static byte completeParam(String text, int begin, int end) { + if (end - begin <= 2) { + throwException("Malformed param '" + text.substring(begin, end) + "'", + begin, end, text + + ); + } + tmpRegions.add(textRegion(begin, end)); + return SEARCH_PARAM; + } + + private static TextRegion textRegion(int begin, int end) { + + return textRegionPool.obtain().set(begin, end); + } + + static void throwException(String message, int startIndex, int endIndex, String rawText) { + throw new CommandParamParseException(message, startIndex, endIndex, rawText); + } + + static void throwException(String message, int symbolIndex, String rawText) { + throw new CommandParamParseException(message, symbolIndex, symbolIndex + 1, rawText); + } + + static void throwException(String message, TextRegion region, String rawText) { + throw new CommandParamParseException(message, region.start, region.end, rawText); + } + + + private static abstract class TextRegion { + public int start; + public int end; + + private TextRegion(int start, int end) { + this.start = start; + this.end = end; + } + + public TextRegion set(int start, int end) { + this.start = start; + this.end = end; + return this; + } + + public int length() { + return end - start; + } + + public String substring(String text) { + return text.substring(start, end); + } + } +} diff --git a/arc-core/src/arc/util/command/CommandParamSplitter.java b/arc-core/src/arc/util/command/CommandParamSplitter.java new file mode 100644 index 000000000..da14a4553 --- /dev/null +++ b/arc-core/src/arc/util/command/CommandParamSplitter.java @@ -0,0 +1,82 @@ +package arc.util.command; + +import arc.struct.IntSeq; + +public class CommandParamSplitter { + private static final SplitResponse response = new SplitResponse(); + private static IntSeq tmpSeq = new IntSeq(); + + public static SplitResponse split(String text, CommandParams pattern) { + return split(text, 0, text.length(), pattern); + } + + public static SplitResponse split(String text, int startIndex, int endIndex, CommandParams pattern) { + int spaces = 0; + tmpSeq.clear(); + tmpSeq.add(startIndex - 1); + for (int i = startIndex; i < endIndex; i++) { + if (text.charAt(i) == ' ') { + tmpSeq.add(i); + spaces++; + if(spaces%5==0) { + if (spaces + 1 > pattern.params.length && pattern.variadicIndex == -1) return response.tooMany(); + } + } + } + + if (spaces + 1 < pattern.requiredAmount) return response.tooFew(); + + int expandVariadic = spaces + 1 - pattern.params.length; + if (expandVariadic > 0 && pattern.variadicIndex == -1) return response.tooMany(); + int givenParams = Math.min(pattern.params.length, spaces + 1); + int optionalLeft = givenParams - pattern.requiredAmount; + String[] resultArgs = new String[givenParams]; + tmpSeq.add(endIndex); + for (int paramIndex = 0, spaceIndex = 0, argIndex = 0; paramIndex < pattern.params.length; paramIndex++) { + + if (pattern.params[paramIndex].optional) { + if (optionalLeft <= 0) { + continue; + } else optionalLeft--; + } + int begin = tmpSeq.get(spaceIndex) + 1; + if (pattern.variadicIndex == paramIndex && expandVariadic > 0) { + spaceIndex += expandVariadic; + } + int end = tmpSeq.get(spaceIndex + 1); + resultArgs[argIndex] = text.substring(begin, end); + argIndex++; + spaceIndex++; + } + return response.args(resultArgs); + } + + public static class SplitResponse { + public boolean tooMany; + public boolean tooFew; + public String[] args; + + public SplitResponse tooMany() { + reset(); + this.tooMany = true; + return this; + } + + private void reset() { + tooFew = tooMany = false; + args = null; + } + + public SplitResponse tooFew() { + reset(); + this.tooFew = true; + return this; + } + + public SplitResponse args(String[] args) { + reset(); + this.args = args; + return this; + } + } +} diff --git a/arc-core/src/arc/util/command/CommandParams.java b/arc-core/src/arc/util/command/CommandParams.java new file mode 100644 index 000000000..71d5f3e3a --- /dev/null +++ b/arc-core/src/arc/util/command/CommandParams.java @@ -0,0 +1,18 @@ +package arc.util.command; + +import arc.util.CommandHandler; +import arc.util.Structs; + +public class CommandParams { + public final CommandHandler.CommandParam[] params; + public final int variadicIndex; + public final int requiredAmount; + + public CommandParams(CommandHandler.CommandParam[] params) { + this.params = params; + variadicIndex= Structs.indexOf(params, it->it.variadic); + requiredAmount= Structs.count(params, it->!it.optional); + + + } +} diff --git a/arc-core/test/utils/command/ParserTest.java b/arc-core/test/utils/command/ParserTest.java new file mode 100644 index 000000000..d446bb8af --- /dev/null +++ b/arc-core/test/utils/command/ParserTest.java @@ -0,0 +1,48 @@ +package utils.command; + +import arc.util.command.CommandParamParseException; +import arc.util.command.CommandParamParser; +import org.junit.Assert; +import org.junit.Test; + +public class ParserTest { + + static void checkError(String message, Runnable runnable) { + try { + runnable.run(); + if (message != null) Assert.fail("Expected error with message: '" + message + "'"); + } catch (CommandParamParseException e) { + Assert.assertEquals(message, e.getMessage()); + } + } + + @Test() + public void testUnexpectedChar() { + + checkError("Unexpected char 'd' at [11:12]\n" + + "[a] d[c] \n" + + " ^\n" + + " Unexpected char 'd'", () -> { + //noinspection ParameterOrder,VariadicParamPosition + CommandParamParser.parse("[a] d[c] "); + }); + checkError("Cannot be more than one variadic parameter! at [11:17]\n" + + "[a] [c...] \n" + + " ^^^^^^\n" + + " Cannot be more than one variadic parameter!", () -> { + //noinspection ParameterOrder,VariadicParamPosition + CommandParamParser.parse("[a] [c...] "); + }); + checkError("Malformed param '<>' at [15:17]\n" + + "[a] [c] <>\n" + + " ^^\n" + + " Malformed param '<>'", () -> { + //noinspection ParameterOrder,VariadicParamPosition + CommandParamParser.parse("[a] [c] <>"); + }); + checkError(null, () -> { + //noinspection ParameterOrder,VariadicParamPosition + CommandParamParser.parse("[a][c]"); + }); + } +} diff --git a/arc-core/test/utils/command/SplitterTest.java b/arc-core/test/utils/command/SplitterTest.java new file mode 100644 index 000000000..6e55ac34c --- /dev/null +++ b/arc-core/test/utils/command/SplitterTest.java @@ -0,0 +1,39 @@ +package utils.command; + +import arc.util.command.CommandParamParser; +import arc.util.command.CommandParamSplitter; +import arc.util.command.CommandParams; +import org.junit.Assert; +import org.junit.Test; + +public class SplitterTest { + static T[] array(T... array) { + return array; + } + + @Test + public void test() { + //noinspection ParameterOrder,VariadicParamPosition + CommandParams params = CommandParamParser.parse("[a] [c] "); + Assert.assertTrue(CommandParamSplitter.split("1", params).tooFew); + + //noinspection ParameterOrder,VariadicParamPosition + Assert.assertTrue(CommandParamSplitter.split("1 2 3 4", CommandParamParser.parse(" ")).tooMany); + Assert.assertArrayEquals( + array("1", "2"), + CommandParamSplitter.split("1 2", params).args + ); + Assert.assertArrayEquals( + array("1", "2", "3"), + CommandParamSplitter.split("1 2 3", params).args + ); + Assert.assertArrayEquals( + array("1", "2", "3", "4"), + CommandParamSplitter.split("1 2 3 4", params).args + ); + Assert.assertArrayEquals( + array("1", "2 3", "4", "5"), + CommandParamSplitter.split("1 2 3 4 5", params).args + ); + } +} From bdcfcd3dc988ea817a0e933758e550ce1ef2699b Mon Sep 17 00:00:00 2001 From: Zelaux Date: Tue, 22 Aug 2023 14:13:59 +0300 Subject: [PATCH 2/7] Fixed CommandHandler.java --- arc-core/src/arc/util/CommandHandler.java | 107 ++++++++++++---------- 1 file changed, 60 insertions(+), 47 deletions(-) diff --git a/arc-core/src/arc/util/CommandHandler.java b/arc-core/src/arc/util/CommandHandler.java index ce19d391d..ef071c02d 100644 --- a/arc-core/src/arc/util/CommandHandler.java +++ b/arc-core/src/arc/util/CommandHandler.java @@ -1,88 +1,101 @@ package arc.util; -import arc.struct.Seq; -import arc.struct.ObjectMap; import arc.func.Cons; +import arc.struct.ObjectMap; +import arc.struct.Seq; import arc.util.command.CommandParamParser; import arc.util.command.CommandParamSplitter; import arc.util.command.CommandParams; -/** Parses command syntax. */ -public class CommandHandler{ - public String prefix = ""; - +/** + * Parses command syntax. + */ +public class CommandHandler { private final ObjectMap commands = new ObjectMap<>(); private final Seq orderedCommands = new Seq<>(); + public String prefix = ""; - /** Creates a command handler with a specific command prefix.*/ - public CommandHandler(String prefix){ + /** + * Creates a command handler with a specific command prefix. + */ + public CommandHandler(String prefix) { this.prefix = prefix; } - public void setPrefix(String prefix){ - this.prefix = prefix; + public String getPrefix() { + return prefix; } - public String getPrefix(){ - return prefix; + public void setPrefix(String prefix) { + this.prefix = prefix; } - /** Handles a message with no additional parameters.*/ - public CommandResponse handleMessage(String message){ + /** + * Handles a message with no additional parameters. + */ + public CommandResponse handleMessage(String message) { return handleMessage(message, null); } - /** Handles a message with optional extra parameters. Runs the command if successful. - * @return a response detailing whether or not the command was handled, and what went wrong, if applicable. */ - public CommandResponse handleMessage(String message, Object params){ - if(message == null || (!message.startsWith(prefix))) + /** + * Handles a message with optional extra parameters. Runs the command if successful. + * + * @return a response detailing whether or not the command was handled, and what went wrong, if applicable. + */ + public CommandResponse handleMessage(String message, Object params) { + if (message == null || (!message.startsWith(prefix))) return new CommandResponse(ResponseType.noCommand, null, null); - String commandstr = message.contains(" ") ? message.substring(prefix.length(), message.indexOf(" ")) : message.substring(prefix.length()); + int spaceIndex = message.indexOf(" "); + String commandstr = spaceIndex != -1 ? message.substring(prefix.length(), spaceIndex) : message.substring(prefix.length()); Command command = commands.get(commandstr); - if(command != null){ + if (command != null) { CommandParamSplitter.SplitResponse splitResponse; - if(message.contains(" ")){ + if (spaceIndex == -1) { splitResponse = CommandParamSplitter.split("", 0, 0, command.params); - }else{ - splitResponse = CommandParamSplitter.split(message, commandstr.length() + 1, 0, command.params); + } else { + splitResponse = CommandParamSplitter.split(message, spaceIndex + 1, 0, command.params); } - if(splitResponse.tooMany){ + if (splitResponse.tooMany) { return new CommandResponse(ResponseType.manyArguments, command, commandstr); - }else if(splitResponse.tooFew){ + } else if (splitResponse.tooFew) { return new CommandResponse(ResponseType.fewArguments, command, commandstr); } command.runner.accept(splitResponse.args, params); return new CommandResponse(ResponseType.valid, command, commandstr); - }else{ + } else { return new CommandResponse(ResponseType.unknownCommand, null, commandstr); } } - public void removeCommand(String text){ + public void removeCommand(String text) { Command c = commands.get(text); - if(c == null) return; + if (c == null) return; commands.remove(text); orderedCommands.remove(c); } - /** Register a command which handles a zero-sized list of arguments and one parameter.*/ - public Command register(String text, String description, CommandRunner runner){ + /** + * Register a command which handles a zero-sized list of arguments and one parameter. + */ + public Command register(String text, String description, CommandRunner runner) { return register(text, "", description, runner); } - /** Register a command which handles a list of arguments and one handler-specific parameter.
+ /** + * Register a command which handles a list of arguments and one handler-specific parameter.
* argeter syntax is as follows:
* <mandatory-arg-1> <mandatory-arg-2> ... <mandatory-arg-n> [optional-arg-1] [optional-arg-2]
* Angle brackets indicate mandatory arguments. Square brackets to indicate optional arguments.
* All mandatory arguments must come before optional arguments. Arg names must not have spaces in them.
* You may also use the ... syntax after the arg name to designate that everything after it will not be split into extra arguments. * There may only be one such argument, and it must be at the end. For example, the syntax - * <arg1> [arg2...] will require a first argument, and then take any text after that and put it in the second argument, optionally.*/ - public Command register(String text, String params, String description, CommandRunner runner){ + * <arg1> [arg2...] will require a first argument, and then take any text after that and put it in the second argument, optionally. + */ + public Command register(String text, String params, String description, CommandRunner runner) { //remove previously registered commands orderedCommands.remove(c -> c.text.equals(text)); @@ -92,30 +105,34 @@ public Command register(String text, String params, String description, Comm return cmd; } - public Command register(String text, String description, Cons runner){ + public Command register(String text, String description, Cons runner) { return register(text, description, (args, p) -> runner.get(args)); } - public Command register(String text, String params, String description, Cons runner){ + public Command register(String text, String params, String description, Cons runner) { return register(text, params, description, (args, p) -> runner.get(args)); } - public Seq getCommandList(){ + public Seq getCommandList() { return orderedCommands; } - public enum ResponseType{ + public enum ResponseType { noCommand, unknownCommand, fewArguments, manyArguments, valid } - public static class Command{ + public interface CommandRunner { + void accept(String[] args, T parameter); + } + + public static class Command { public final String text; public final String paramText; public final String description; public final CommandParams params; final CommandRunner runner; - public Command(String text, String paramText, String description, CommandRunner runner){ + public Command(String text, String paramText, String description, CommandRunner runner) { this.text = text; this.paramText = paramText; this.runner = runner; @@ -124,28 +141,24 @@ public Command(String text, String paramText, String description, CommandRunner } } - public interface CommandRunner{ - void accept(String[] args, T parameter); - } - - public static class CommandParam{ + public static class CommandParam { public final String name; public final boolean optional; public final boolean variadic; - public CommandParam(String name, boolean optional, boolean variadic){ + public CommandParam(String name, boolean optional, boolean variadic) { this.name = name; this.optional = optional; this.variadic = variadic; } } - public static class CommandResponse{ + public static class CommandResponse { public final ResponseType type; public final Command command; public final String runCommand; - public CommandResponse(ResponseType type, Command command, String runCommand){ + public CommandResponse(ResponseType type, Command command, String runCommand) { this.type = type; this.command = command; this.runCommand = runCommand; From bcfd8bc7e71318f3645df365f4fce4dc64da99f6 Mon Sep 17 00:00:00 2001 From: Zelaux Date: Tue, 22 Aug 2023 14:15:19 +0300 Subject: [PATCH 3/7] Better naming --- arc-core/src/arc/util/CommandHandler.java | 4 ++-- .../src/arc/util/command/CommandParamSplitter.java | 10 +++++----- arc-core/test/utils/command/SplitterTest.java | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/arc-core/src/arc/util/CommandHandler.java b/arc-core/src/arc/util/CommandHandler.java index ef071c02d..88d95f6a3 100644 --- a/arc-core/src/arc/util/CommandHandler.java +++ b/arc-core/src/arc/util/CommandHandler.java @@ -58,9 +58,9 @@ public CommandResponse handleMessage(String message, Object params) { } else { splitResponse = CommandParamSplitter.split(message, spaceIndex + 1, 0, command.params); } - if (splitResponse.tooMany) { + if (splitResponse.many) { return new CommandResponse(ResponseType.manyArguments, command, commandstr); - } else if (splitResponse.tooFew) { + } else if (splitResponse.few) { return new CommandResponse(ResponseType.fewArguments, command, commandstr); } diff --git a/arc-core/src/arc/util/command/CommandParamSplitter.java b/arc-core/src/arc/util/command/CommandParamSplitter.java index da14a4553..30ab179d8 100644 --- a/arc-core/src/arc/util/command/CommandParamSplitter.java +++ b/arc-core/src/arc/util/command/CommandParamSplitter.java @@ -52,24 +52,24 @@ public static SplitResponse split(String text, int startIndex, int endIndex, Com } public static class SplitResponse { - public boolean tooMany; - public boolean tooFew; + public boolean many; + public boolean few; public String[] args; public SplitResponse tooMany() { reset(); - this.tooMany = true; + this.many = true; return this; } private void reset() { - tooFew = tooMany = false; + few = many = false; args = null; } public SplitResponse tooFew() { reset(); - this.tooFew = true; + this.few = true; return this; } diff --git a/arc-core/test/utils/command/SplitterTest.java b/arc-core/test/utils/command/SplitterTest.java index 6e55ac34c..ee08ab610 100644 --- a/arc-core/test/utils/command/SplitterTest.java +++ b/arc-core/test/utils/command/SplitterTest.java @@ -15,10 +15,10 @@ static T[] array(T... array) { public void test() { //noinspection ParameterOrder,VariadicParamPosition CommandParams params = CommandParamParser.parse("[a] [c] "); - Assert.assertTrue(CommandParamSplitter.split("1", params).tooFew); + Assert.assertTrue(CommandParamSplitter.split("1", params).few); //noinspection ParameterOrder,VariadicParamPosition - Assert.assertTrue(CommandParamSplitter.split("1 2 3 4", CommandParamParser.parse("
")).tooMany); + Assert.assertTrue(CommandParamSplitter.split("1 2 3 4", CommandParamParser.parse(" ")).many); Assert.assertArrayEquals( array("1", "2"), CommandParamSplitter.split("1 2", params).args From 61201e0d5f6e60121b67c1458d96a9bceaf3404e Mon Sep 17 00:00:00 2001 From: Zelaux Date: Tue, 22 Aug 2023 14:40:55 +0300 Subject: [PATCH 4/7] Fixed splitting empty strings --- .../util/command/CommandParamSplitter.java | 30 +++++++++++-------- arc-core/test/utils/command/SplitterTest.java | 10 +++++++ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/arc-core/src/arc/util/command/CommandParamSplitter.java b/arc-core/src/arc/util/command/CommandParamSplitter.java index 30ab179d8..ad60790e6 100644 --- a/arc-core/src/arc/util/command/CommandParamSplitter.java +++ b/arc-core/src/arc/util/command/CommandParamSplitter.java @@ -4,6 +4,7 @@ public class CommandParamSplitter { private static final SplitResponse response = new SplitResponse(); + private static final String[] EMPTY_ARRAY = {}; private static IntSeq tmpSeq = new IntSeq(); public static SplitResponse split(String text, CommandParams pattern) { @@ -11,36 +12,39 @@ public static SplitResponse split(String text, CommandParams pattern) { } public static SplitResponse split(String text, int startIndex, int endIndex, CommandParams pattern) { + if(endIndex - startIndex == 0) { + return pattern.requiredAmount == 0 ? response.args(EMPTY_ARRAY) : response.few(); + } int spaces = 0; tmpSeq.clear(); tmpSeq.add(startIndex - 1); - for (int i = startIndex; i < endIndex; i++) { - if (text.charAt(i) == ' ') { + for(int i = startIndex; i < endIndex; i++) { + if(text.charAt(i) == ' ') { tmpSeq.add(i); spaces++; - if(spaces%5==0) { - if (spaces + 1 > pattern.params.length && pattern.variadicIndex == -1) return response.tooMany(); - } + if(spaces % 5 == 0) { + if(spaces + 1 > pattern.params.length && pattern.variadicIndex == -1) return response.many(); + } } } - if (spaces + 1 < pattern.requiredAmount) return response.tooFew(); + if(spaces + 1 < pattern.requiredAmount) return response.few(); int expandVariadic = spaces + 1 - pattern.params.length; - if (expandVariadic > 0 && pattern.variadicIndex == -1) return response.tooMany(); + if(expandVariadic > 0 && pattern.variadicIndex == -1) return response.many(); int givenParams = Math.min(pattern.params.length, spaces + 1); int optionalLeft = givenParams - pattern.requiredAmount; String[] resultArgs = new String[givenParams]; tmpSeq.add(endIndex); - for (int paramIndex = 0, spaceIndex = 0, argIndex = 0; paramIndex < pattern.params.length; paramIndex++) { + for(int paramIndex = 0, spaceIndex = 0, argIndex = 0; paramIndex < pattern.params.length; paramIndex++) { - if (pattern.params[paramIndex].optional) { - if (optionalLeft <= 0) { + if(pattern.params[paramIndex].optional) { + if(optionalLeft <= 0) { continue; } else optionalLeft--; } int begin = tmpSeq.get(spaceIndex) + 1; - if (pattern.variadicIndex == paramIndex && expandVariadic > 0) { + if(pattern.variadicIndex == paramIndex && expandVariadic > 0) { spaceIndex += expandVariadic; } int end = tmpSeq.get(spaceIndex + 1); @@ -56,7 +60,7 @@ public static class SplitResponse { public boolean few; public String[] args; - public SplitResponse tooMany() { + public SplitResponse many() { reset(); this.many = true; return this; @@ -67,7 +71,7 @@ private void reset() { args = null; } - public SplitResponse tooFew() { + public SplitResponse few() { reset(); this.few = true; return this; diff --git a/arc-core/test/utils/command/SplitterTest.java b/arc-core/test/utils/command/SplitterTest.java index ee08ab610..e1cfed5fd 100644 --- a/arc-core/test/utils/command/SplitterTest.java +++ b/arc-core/test/utils/command/SplitterTest.java @@ -19,6 +19,12 @@ public void test() { //noinspection ParameterOrder,VariadicParamPosition Assert.assertTrue(CommandParamSplitter.split("1 2 3 4", CommandParamParser.parse(" ")).many); + + Assert.assertTrue( + CommandParamSplitter.split("", CommandParamParser.parse("<1>")).few + ); + + Assert.assertArrayEquals( array("1", "2"), CommandParamSplitter.split("1 2", params).args @@ -35,5 +41,9 @@ public void test() { array("1", "2 3", "4", "5"), CommandParamSplitter.split("1 2 3 4 5", params).args ); + Assert.assertArrayEquals( + array(), + CommandParamSplitter.split("", CommandParamParser.parse("")).args + ); } } From 71340753388e3a59bc1db35027da3e09c8c82583 Mon Sep 17 00:00:00 2001 From: Zelaux Date: Tue, 22 Aug 2023 14:50:08 +0300 Subject: [PATCH 5/7] Fixed index out of bounds --- arc-core/src/arc/util/CommandHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arc-core/src/arc/util/CommandHandler.java b/arc-core/src/arc/util/CommandHandler.java index 88d95f6a3..9048a2e25 100644 --- a/arc-core/src/arc/util/CommandHandler.java +++ b/arc-core/src/arc/util/CommandHandler.java @@ -56,7 +56,7 @@ public CommandResponse handleMessage(String message, Object params) { if (spaceIndex == -1) { splitResponse = CommandParamSplitter.split("", 0, 0, command.params); } else { - splitResponse = CommandParamSplitter.split(message, spaceIndex + 1, 0, command.params); + splitResponse = CommandParamSplitter.split(message, spaceIndex + 1, message.length(), command.params); } if (splitResponse.many) { return new CommandResponse(ResponseType.manyArguments, command, commandstr); From 8db384d8896099d876aec5eec18d8ce3e2c53d89 Mon Sep 17 00:00:00 2001 From: Zelaux Date: Tue, 22 Aug 2023 17:39:57 +0300 Subject: [PATCH 6/7] Changed code formatting --- arc-core/src/arc/util/CommandHandler.java | 66 ++++++++--------- .../command/CommandParamParseException.java | 14 ++-- .../arc/util/command/CommandParamParser.java | 72 +++++++++---------- .../util/command/CommandParamSplitter.java | 36 +++++----- .../src/arc/util/command/CommandParams.java | 21 +++--- 5 files changed, 102 insertions(+), 107 deletions(-) diff --git a/arc-core/src/arc/util/CommandHandler.java b/arc-core/src/arc/util/CommandHandler.java index 9048a2e25..222a7f282 100644 --- a/arc-core/src/arc/util/CommandHandler.java +++ b/arc-core/src/arc/util/CommandHandler.java @@ -1,17 +1,14 @@ package arc.util; -import arc.func.Cons; -import arc.struct.ObjectMap; -import arc.struct.Seq; -import arc.util.command.CommandParamParser; -import arc.util.command.CommandParamSplitter; -import arc.util.command.CommandParams; +import arc.func.*; +import arc.struct.*; +import arc.util.command.*; /** * Parses command syntax. */ -public class CommandHandler { +public class CommandHandler{ private final ObjectMap commands = new ObjectMap<>(); private final Seq orderedCommands = new Seq<>(); public String prefix = ""; @@ -19,61 +16,60 @@ public class CommandHandler { /** * Creates a command handler with a specific command prefix. */ - public CommandHandler(String prefix) { + public CommandHandler(String prefix){ this.prefix = prefix; } - public String getPrefix() { + public String getPrefix(){ return prefix; } - public void setPrefix(String prefix) { + public void setPrefix(String prefix){ this.prefix = prefix; } /** * Handles a message with no additional parameters. */ - public CommandResponse handleMessage(String message) { + public CommandResponse handleMessage(String message){ return handleMessage(message, null); } /** * Handles a message with optional extra parameters. Runs the command if successful. - * * @return a response detailing whether or not the command was handled, and what went wrong, if applicable. */ - public CommandResponse handleMessage(String message, Object params) { - if (message == null || (!message.startsWith(prefix))) + public CommandResponse handleMessage(String message, Object params){ + if(message == null || (!message.startsWith(prefix))) return new CommandResponse(ResponseType.noCommand, null, null); int spaceIndex = message.indexOf(" "); String commandstr = spaceIndex != -1 ? message.substring(prefix.length(), spaceIndex) : message.substring(prefix.length()); Command command = commands.get(commandstr); - if (command != null) { + if(command != null){ CommandParamSplitter.SplitResponse splitResponse; - if (spaceIndex == -1) { + if(spaceIndex == -1){ splitResponse = CommandParamSplitter.split("", 0, 0, command.params); - } else { + }else{ splitResponse = CommandParamSplitter.split(message, spaceIndex + 1, message.length(), command.params); } - if (splitResponse.many) { + if(splitResponse.many){ return new CommandResponse(ResponseType.manyArguments, command, commandstr); - } else if (splitResponse.few) { + }else if(splitResponse.few){ return new CommandResponse(ResponseType.fewArguments, command, commandstr); } command.runner.accept(splitResponse.args, params); return new CommandResponse(ResponseType.valid, command, commandstr); - } else { + }else{ return new CommandResponse(ResponseType.unknownCommand, null, commandstr); } } - public void removeCommand(String text) { + public void removeCommand(String text){ Command c = commands.get(text); - if (c == null) return; + if(c == null) return; commands.remove(text); orderedCommands.remove(c); } @@ -81,7 +77,7 @@ public void removeCommand(String text) { /** * Register a command which handles a zero-sized list of arguments and one parameter. */ - public Command register(String text, String description, CommandRunner runner) { + public Command register(String text, String description, CommandRunner runner){ return register(text, "", description, runner); } @@ -95,7 +91,7 @@ public Command register(String text, String description, CommandRunner ru * There may only be one such argument, and it must be at the end. For example, the syntax * <arg1> [arg2...] will require a first argument, and then take any text after that and put it in the second argument, optionally. */ - public Command register(String text, String params, String description, CommandRunner runner) { + public Command register(String text, String params, String description, CommandRunner runner){ //remove previously registered commands orderedCommands.remove(c -> c.text.equals(text)); @@ -105,34 +101,34 @@ public Command register(String text, String params, String description, Comm return cmd; } - public Command register(String text, String description, Cons runner) { + public Command register(String text, String description, Cons runner){ return register(text, description, (args, p) -> runner.get(args)); } - public Command register(String text, String params, String description, Cons runner) { + public Command register(String text, String params, String description, Cons runner){ return register(text, params, description, (args, p) -> runner.get(args)); } - public Seq getCommandList() { + public Seq getCommandList(){ return orderedCommands; } - public enum ResponseType { + public enum ResponseType{ noCommand, unknownCommand, fewArguments, manyArguments, valid } - public interface CommandRunner { + public interface CommandRunner{ void accept(String[] args, T parameter); } - public static class Command { + public static class Command{ public final String text; public final String paramText; public final String description; public final CommandParams params; final CommandRunner runner; - public Command(String text, String paramText, String description, CommandRunner runner) { + public Command(String text, String paramText, String description, CommandRunner runner){ this.text = text; this.paramText = paramText; this.runner = runner; @@ -141,24 +137,24 @@ public Command(String text, String paramText, String description, CommandRunner } } - public static class CommandParam { + public static class CommandParam{ public final String name; public final boolean optional; public final boolean variadic; - public CommandParam(String name, boolean optional, boolean variadic) { + public CommandParam(String name, boolean optional, boolean variadic){ this.name = name; this.optional = optional; this.variadic = variadic; } } - public static class CommandResponse { + public static class CommandResponse{ public final ResponseType type; public final Command command; public final String runCommand; - public CommandResponse(ResponseType type, Command command, String runCommand) { + public CommandResponse(ResponseType type, Command command, String runCommand){ this.type = type; this.command = command; this.runCommand = runCommand; diff --git a/arc-core/src/arc/util/command/CommandParamParseException.java b/arc-core/src/arc/util/command/CommandParamParseException.java index 465ed493b..333b0d186 100644 --- a/arc-core/src/arc/util/command/CommandParamParseException.java +++ b/arc-core/src/arc/util/command/CommandParamParseException.java @@ -1,11 +1,11 @@ package arc.util.command; -public class CommandParamParseException extends RuntimeException { +public class CommandParamParseException extends RuntimeException{ public final int startIndex; public final int endIndex; public final String rawText; - public CommandParamParseException(String message, int startIndex, int endIndex, String rawText) { + public CommandParamParseException(String message, int startIndex, int endIndex, String rawText){ super(calculateMessage(message, rawText, startIndex, endIndex)); this.startIndex = startIndex; this.endIndex = endIndex; @@ -14,18 +14,18 @@ public CommandParamParseException(String message, int startIndex, int endIndex, } - private static String calculateMessage(String message, String rawText, int startIndex, int endIndex) { + private static String calculateMessage(String message, String rawText, int startIndex, int endIndex){ StringBuilder builder = new StringBuilder(message); appendRanges(builder, startIndex, endIndex); builder.append("\n").append(rawText).append("\n"); - for (int i = 0; i < startIndex; i++) { + for(int i = 0; i < startIndex; i++){ builder.append(" "); } - for (int i = startIndex; i < endIndex; i++) { + for(int i = startIndex; i < endIndex; i++){ builder.append("^"); } builder.append("\n"); - for (int i = 0; i < startIndex; i++) { + for(int i = 0; i < startIndex; i++){ builder.append(" "); } builder.append(message); @@ -33,7 +33,7 @@ private static String calculateMessage(String message, String rawText, int start return builder.toString(); } - private static void appendRanges(StringBuilder builder, int startIndex, int endIndex) { + private static void appendRanges(StringBuilder builder, int startIndex, int endIndex){ builder.append(" at "); builder.append('['); builder.append(startIndex); diff --git a/arc-core/src/arc/util/command/CommandParamParser.java b/arc-core/src/arc/util/command/CommandParamParser.java index 27d87e69c..765bf63e6 100644 --- a/arc-core/src/arc/util/command/CommandParamParser.java +++ b/arc-core/src/arc/util/command/CommandParamParser.java @@ -1,46 +1,46 @@ package arc.util.command; -import arc.struct.Seq; -import arc.util.CommandHandler; -import arc.util.pooling.Pool; +import arc.struct.*; +import arc.util.*; +import arc.util.pooling.*; -public class CommandParamParser { +public class CommandParamParser{ private static final byte SEARCH_PARAM = 0; private static final byte PARSING_REQUIRED = 1; private static final byte PARSING_OPTIONAL = 2; private static final Seq tmpRegions = new Seq<>(); - private static final Pool textRegionPool = new Pool() { + private static final Pool textRegionPool = new Pool(){ @Override - protected TextRegion newObject() { - return new TextRegion(-1, -1) { + protected TextRegion newObject(){ + return new TextRegion(-1, -1){ }; } }; - public static CommandParams parse(String text) throws CommandParamParseException { + public static CommandParams parse(String text) throws CommandParamParseException{ byte state = SEARCH_PARAM; int begin = -1; clear(); - for (int i = 0; i < text.length(); i++) { + for(int i = 0; i < text.length(); i++){ char c = text.charAt(i); - switch (state) { + switch(state){ case SEARCH_PARAM: - if (c != ' ' && c != '<' && c != '[') + if(c != ' ' && c != '<' && c != '[') throwException("Unexpected char '" + c + "'", i, text); - if (c == '<' || c == '[') { + if(c == '<' || c == '['){ state = c == '<' ? PARSING_REQUIRED : PARSING_OPTIONAL; begin = i; } break; case PARSING_REQUIRED: - if (c == '>') { + if(c == '>'){ state = completeParam(text, begin, i + 1); } break; case PARSING_OPTIONAL: - if (c == ']') { + if(c == ']'){ state = completeParam(text, begin, i + 1); } break; @@ -49,15 +49,15 @@ public static CommandParams parse(String text) throws CommandParamParseException } CommandHandler.CommandParam[] params = new CommandHandler.CommandParam[tmpRegions.size]; boolean wasVariadic = false; - for (int i = 0; i < tmpRegions.size; i++) { + for(int i = 0; i < tmpRegions.size; i++){ TextRegion region = tmpRegions.get(i); boolean isVariadic = false; int nameOffset = 0; - if (region.length() > 5) { - for (int j = 0; ; j++) { - if (text.charAt(region.end - i - 1) != '.') break; - if (j == 2) { - if (wasVariadic) { + if(region.length() > 5){ + for(int j = 0; ; j++){ + if(text.charAt(region.end - i - 1) != '.') break; + if(j == 2){ + if(wasVariadic){ throwException("Cannot be more than one variadic parameter!", region, text); } @@ -68,24 +68,24 @@ public static CommandParams parse(String text) throws CommandParamParseException } } params[i] = new CommandHandler.CommandParam( - text.substring(region.start + 1, region.end - 1 - nameOffset), - text.charAt(region.start) == '[', - isVariadic + text.substring(region.start + 1, region.end - 1 - nameOffset), + text.charAt(region.start) == '[', + isVariadic ); } clear(); return new CommandParams(params); } - private static void clear() { + private static void clear(){ textRegionPool.freeAll(tmpRegions); tmpRegions.clear(); } - private static byte completeParam(String text, int begin, int end) { - if (end - begin <= 2) { + private static byte completeParam(String text, int begin, int end){ + if(end - begin <= 2){ throwException("Malformed param '" + text.substring(begin, end) + "'", - begin, end, text + begin, end, text ); } @@ -93,44 +93,44 @@ private static byte completeParam(String text, int begin, int end) { return SEARCH_PARAM; } - private static TextRegion textRegion(int begin, int end) { + private static TextRegion textRegion(int begin, int end){ return textRegionPool.obtain().set(begin, end); } - static void throwException(String message, int startIndex, int endIndex, String rawText) { + static void throwException(String message, int startIndex, int endIndex, String rawText){ throw new CommandParamParseException(message, startIndex, endIndex, rawText); } - static void throwException(String message, int symbolIndex, String rawText) { + static void throwException(String message, int symbolIndex, String rawText){ throw new CommandParamParseException(message, symbolIndex, symbolIndex + 1, rawText); } - static void throwException(String message, TextRegion region, String rawText) { + static void throwException(String message, TextRegion region, String rawText){ throw new CommandParamParseException(message, region.start, region.end, rawText); } - private static abstract class TextRegion { + private static abstract class TextRegion{ public int start; public int end; - private TextRegion(int start, int end) { + private TextRegion(int start, int end){ this.start = start; this.end = end; } - public TextRegion set(int start, int end) { + public TextRegion set(int start, int end){ this.start = start; this.end = end; return this; } - public int length() { + public int length(){ return end - start; } - public String substring(String text) { + public String substring(String text){ return text.substring(start, end); } } diff --git a/arc-core/src/arc/util/command/CommandParamSplitter.java b/arc-core/src/arc/util/command/CommandParamSplitter.java index ad60790e6..755a49f34 100644 --- a/arc-core/src/arc/util/command/CommandParamSplitter.java +++ b/arc-core/src/arc/util/command/CommandParamSplitter.java @@ -1,28 +1,28 @@ package arc.util.command; -import arc.struct.IntSeq; +import arc.struct.*; -public class CommandParamSplitter { +public class CommandParamSplitter{ private static final SplitResponse response = new SplitResponse(); private static final String[] EMPTY_ARRAY = {}; private static IntSeq tmpSeq = new IntSeq(); - public static SplitResponse split(String text, CommandParams pattern) { + public static SplitResponse split(String text, CommandParams pattern){ return split(text, 0, text.length(), pattern); } - public static SplitResponse split(String text, int startIndex, int endIndex, CommandParams pattern) { - if(endIndex - startIndex == 0) { + public static SplitResponse split(String text, int startIndex, int endIndex, CommandParams pattern){ + if(endIndex - startIndex == 0){ return pattern.requiredAmount == 0 ? response.args(EMPTY_ARRAY) : response.few(); } int spaces = 0; tmpSeq.clear(); tmpSeq.add(startIndex - 1); - for(int i = startIndex; i < endIndex; i++) { - if(text.charAt(i) == ' ') { + for(int i = startIndex; i < endIndex; i++){ + if(text.charAt(i) == ' '){ tmpSeq.add(i); spaces++; - if(spaces % 5 == 0) { + if(spaces % 5 == 0){ if(spaces + 1 > pattern.params.length && pattern.variadicIndex == -1) return response.many(); } } @@ -36,15 +36,15 @@ public static SplitResponse split(String text, int startIndex, int endIndex, Com int optionalLeft = givenParams - pattern.requiredAmount; String[] resultArgs = new String[givenParams]; tmpSeq.add(endIndex); - for(int paramIndex = 0, spaceIndex = 0, argIndex = 0; paramIndex < pattern.params.length; paramIndex++) { + for(int paramIndex = 0, spaceIndex = 0, argIndex = 0; paramIndex < pattern.params.length; paramIndex++){ - if(pattern.params[paramIndex].optional) { - if(optionalLeft <= 0) { + if(pattern.params[paramIndex].optional){ + if(optionalLeft <= 0){ continue; - } else optionalLeft--; + }else optionalLeft--; } int begin = tmpSeq.get(spaceIndex) + 1; - if(pattern.variadicIndex == paramIndex && expandVariadic > 0) { + if(pattern.variadicIndex == paramIndex && expandVariadic > 0){ spaceIndex += expandVariadic; } int end = tmpSeq.get(spaceIndex + 1); @@ -55,29 +55,29 @@ public static SplitResponse split(String text, int startIndex, int endIndex, Com return response.args(resultArgs); } - public static class SplitResponse { + public static class SplitResponse{ public boolean many; public boolean few; public String[] args; - public SplitResponse many() { + public SplitResponse many(){ reset(); this.many = true; return this; } - private void reset() { + private void reset(){ few = many = false; args = null; } - public SplitResponse few() { + public SplitResponse few(){ reset(); this.few = true; return this; } - public SplitResponse args(String[] args) { + public SplitResponse args(String[] args){ reset(); this.args = args; return this; diff --git a/arc-core/src/arc/util/command/CommandParams.java b/arc-core/src/arc/util/command/CommandParams.java index 71d5f3e3a..47a51a5c5 100644 --- a/arc-core/src/arc/util/command/CommandParams.java +++ b/arc-core/src/arc/util/command/CommandParams.java @@ -1,18 +1,17 @@ package arc.util.command; -import arc.util.CommandHandler; -import arc.util.Structs; +import arc.util.*; -public class CommandParams { - public final CommandHandler.CommandParam[] params; - public final int variadicIndex; - public final int requiredAmount; +public class CommandParams{ + public final CommandHandler.CommandParam[] params; + public final int variadicIndex; + public final int requiredAmount; - public CommandParams(CommandHandler.CommandParam[] params) { - this.params = params; - variadicIndex= Structs.indexOf(params, it->it.variadic); - requiredAmount= Structs.count(params, it->!it.optional); + public CommandParams(CommandHandler.CommandParam[] params){ + this.params = params; + variadicIndex = Structs.indexOf(params, it -> it.variadic); + requiredAmount = Structs.count(params, it -> !it.optional); - } + } } From 619ba6a6eda0bae62b60feaaf58cdfd13f2743f9 Mon Sep 17 00:00:00 2001 From: Zelaux Date: Tue, 22 Aug 2023 17:42:52 +0300 Subject: [PATCH 7/7] Changed code formatting#2 --- .../arc/util/command/CommandParamParser.java | 18 +++++++++--------- .../arc/util/command/CommandParamSplitter.java | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/arc-core/src/arc/util/command/CommandParamParser.java b/arc-core/src/arc/util/command/CommandParamParser.java index 765bf63e6..e70b02166 100644 --- a/arc-core/src/arc/util/command/CommandParamParser.java +++ b/arc-core/src/arc/util/command/CommandParamParser.java @@ -5,9 +5,9 @@ import arc.util.pooling.*; public class CommandParamParser{ - private static final byte SEARCH_PARAM = 0; - private static final byte PARSING_REQUIRED = 1; - private static final byte PARSING_OPTIONAL = 2; + private static final byte searchParam = 0; + private static final byte parsingRequired = 1; + private static final byte parsingOptional = 2; private static final Seq tmpRegions = new Seq<>(); private static final Pool textRegionPool = new Pool(){ @Override @@ -18,28 +18,28 @@ protected TextRegion newObject(){ }; public static CommandParams parse(String text) throws CommandParamParseException{ - byte state = SEARCH_PARAM; + byte state = searchParam; int begin = -1; clear(); for(int i = 0; i < text.length(); i++){ char c = text.charAt(i); switch(state){ - case SEARCH_PARAM: + case searchParam: if(c != ' ' && c != '<' && c != '[') throwException("Unexpected char '" + c + "'", i, text); if(c == '<' || c == '['){ - state = c == '<' ? PARSING_REQUIRED : PARSING_OPTIONAL; + state = c == '<' ? parsingRequired : parsingOptional; begin = i; } break; - case PARSING_REQUIRED: + case parsingRequired: if(c == '>'){ state = completeParam(text, begin, i + 1); } break; - case PARSING_OPTIONAL: + case parsingOptional: if(c == ']'){ state = completeParam(text, begin, i + 1); } @@ -90,7 +90,7 @@ private static byte completeParam(String text, int begin, int end){ ); } tmpRegions.add(textRegion(begin, end)); - return SEARCH_PARAM; + return searchParam; } private static TextRegion textRegion(int begin, int end){ diff --git a/arc-core/src/arc/util/command/CommandParamSplitter.java b/arc-core/src/arc/util/command/CommandParamSplitter.java index 755a49f34..503106148 100644 --- a/arc-core/src/arc/util/command/CommandParamSplitter.java +++ b/arc-core/src/arc/util/command/CommandParamSplitter.java @@ -4,7 +4,7 @@ public class CommandParamSplitter{ private static final SplitResponse response = new SplitResponse(); - private static final String[] EMPTY_ARRAY = {}; + private static final String[] emptyStringArray = {}; private static IntSeq tmpSeq = new IntSeq(); public static SplitResponse split(String text, CommandParams pattern){ @@ -13,7 +13,7 @@ public static SplitResponse split(String text, CommandParams pattern){ public static SplitResponse split(String text, int startIndex, int endIndex, CommandParams pattern){ if(endIndex - startIndex == 0){ - return pattern.requiredAmount == 0 ? response.args(EMPTY_ARRAY) : response.few(); + return pattern.requiredAmount == 0 ? response.args(emptyStringArray) : response.few(); } int spaces = 0; tmpSeq.clear();