Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit 7da8c78

Browse files
committed
Parse stringified arguments as single strings
1 parent 8108cd6 commit 7da8c78

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

src/main/java/de/kaleidox/javacord/util/commands/CommandHandler.java

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ private void handleCommand(final Message message, final TextChannel channel, fin
334334
if (usedPrefix == -1 || pref == null) return;
335335

336336
CommandRepresentation cmd;
337-
String[] split = content.split("[\\s&&[^\\n]]++");
337+
String[] split = splitContent(content);
338338
String[] args;
339339
if (pref[usedPrefix].matches("^(.*\\s.*)+$")) {
340340
cmd = commands.get(split[1]);
@@ -410,6 +410,90 @@ else if (!message.isPrivateMessage() && !cmd.enableServerChat)
410410
else doInvoke(cmd, commandParams, channel, message);
411411
}
412412

413+
private String[] splitContent(String content) {
414+
List<String> yields = new ArrayList<>();
415+
yields.add("");
416+
417+
boolean inString = false;
418+
int i = 0, y = 0, s = -1;
419+
char c = 0, p;
420+
421+
while (i < content.length()) {
422+
p = c;
423+
c = content.charAt(i);
424+
425+
switch (c) {
426+
case '"':
427+
// if not in string & starter is not escaped
428+
if (!inString && p != '\\') {
429+
// if string starts after a space
430+
if (p == ' ') {
431+
// start string
432+
yields.add("");
433+
s = y++;
434+
inString = true;
435+
} else {
436+
// escape "
437+
yields.set(y, yields.get(y) + c);
438+
}
439+
// if in string & ender is not escaped
440+
} else if (inString && p != '\\') {
441+
// if next char is space
442+
if (content.charAt(i + 1) == ' ') {
443+
// end string
444+
yields.add("");
445+
s = y++;
446+
inString = false;
447+
} else {
448+
// escape "
449+
yields.set(y, yields.get(y) + c);
450+
}
451+
// if " was escaped
452+
} else {
453+
// append to string
454+
yields.set(y, yields.get(y) + c);
455+
}
456+
break;
457+
case ' ':
458+
if (inString) {
459+
// append to string
460+
yields.set(y, yields.get(y) + c);
461+
} else {
462+
// start new item
463+
yields.add("");
464+
s = y++;
465+
}
466+
break;
467+
case '\\':
468+
// never include escaping character
469+
break;
470+
default:
471+
// append to string
472+
yields.set(y, yields.get(y) + c);
473+
break;
474+
}
475+
476+
i++;
477+
}
478+
479+
if (inString) { // if still in string -> join split string together
480+
int prevSize = yields.size();
481+
new ArrayList<>(yields)
482+
.stream()
483+
.skip(s)
484+
.flatMap(str -> Arrays.stream(str.split(" ")))
485+
.forEachOrdered(yields::add);
486+
for (int r = prevSize; r > s; r--)
487+
//noinspection SuspiciousListRemoveInLoop
488+
yields.remove(r);
489+
yields.set(s + 1, '"' + yields.get(s + 1));
490+
}
491+
492+
yields.removeIf(String::isEmpty);
493+
494+
return yields.toArray(new String[0]);
495+
}
496+
413497
private void doInvoke(CommandRepresentation commandRep, Params commandParams, TextChannel channel, Message message) {
414498
Object reply;
415499
try {

0 commit comments

Comments
 (0)