summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java
diff options
context:
space:
mode:
Diffstat (limited to 'dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java')
-rw-r--r--dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java95
1 files changed, 1 insertions, 94 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java
index 4ac9bd3..b112367 100644
--- a/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java
+++ b/dicore3/command/src/main/java/io/dico/dicore/command/parameter/IArgumentPreProcessor.java
@@ -27,100 +27,7 @@ public interface IArgumentPreProcessor {
* @return The IArgumentPreProcessor
*/
static IArgumentPreProcessor mergeOnTokens(String tokens, char escapeChar) {
- if (tokens.isEmpty() || (tokens.length() & 1) != 0) {
- throw new IllegalArgumentException();
- }
-
- return (argStart, args) -> {
- if (!(0 <= argStart && argStart <= args.length)) {
- throw new IndexOutOfBoundsException();
- }
-
- args = args.clone();
- int removeCount = 0;
- int closingTokenIdx = 0;
- int sectionStart = -1;
-
- for (int i = argStart; i < args.length; i++) {
- String arg = args[i];
- if (arg == null || arg.isEmpty()) {
- continue;
- }
-
- if (closingTokenIdx != 0) {
- int idx = tokens.indexOf(arg.charAt(arg.length() - 1));
- if (idx == closingTokenIdx) {
-
- // count escape chars
- int index = arg.length() - 1;
- int count = 0;
- while (index > 0 && arg.charAt(--index) == escapeChar) {
- count++;
- }
-
- // remove the final char plus half the count, rounding upwards.
- args[i] = arg.substring(0, args.length - 1 - (count + 1) / 2);
-
- if ((count & 1) == 0) {
- // not escaped
- StringBuilder concat = new StringBuilder(args[sectionStart].substring(1));
- for (int j = sectionStart + 1; j <= i; j++) {
- concat.append(' ').append(args[j]);
- args[j] = null;
- removeCount++;
- }
-
- args[sectionStart] = concat.toString();
-
- sectionStart = -1;
- closingTokenIdx = 0;
-
- } else {
- // it's escaped
- // add final char because it was escaped
- args[i] += tokens.charAt(closingTokenIdx);
-
- }
- }
-
- if (i == args.length - 1) {
- // if the closing token isn't found, reset state and start from the index subsequent to the one where the opener was found
- // it should also undo removal of any escapes... it doesn't do that
- i = sectionStart + 1;
- closingTokenIdx = 0;
- sectionStart = -1;
- }
-
- continue;
- }
-
- int idx = tokens.indexOf(arg.charAt(0));
- if (idx == -1 || (idx & 1) != 0) {
- continue;
- }
-
- closingTokenIdx = idx | 1;
- sectionStart = i;
-
- // make sure to check from the current index for a closer
- i--;
- }
-
- if (removeCount == 0) {
- return args;
- }
-
- String[] result = new String[args.length - removeCount];
- int i = 0;
- for (String arg : args) {
- if (arg != null) {
- result[i++] = arg;
- }
- }
-
- return result;
- };
-
+ return new ArgumentMergingPreProcessor(tokens, escapeChar);
}
}