Skip to content

Commit 3b0e0c3

Browse files
author
Marcel Overdijk
committed
Fixed #40 to support quoted strings come in as one arg regardless of spaces
1 parent 4319657 commit 3b0e0c3

1 file changed

Lines changed: 43 additions & 2 deletions

File tree

rivescript-core/src/main/java/com/rivescript/RiveScript.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,11 +2001,11 @@ private String processTags(String username, String message, String reply, List<S
20012001
}
20022002

20032003
String text = matcher.group(1).trim();
2004-
String[] parts = text.split(" ");
2004+
String[] parts = text.split(" ", 2);
20052005
String obj = parts[0];
20062006
String[] args;
20072007
if (parts.length > 1) {
2008-
args = Arrays.copyOfRange(parts, 1, parts.length);
2008+
args = parseCallArgsString(parts[1]);
20092009
} else {
20102010
args = new String[0];
20112011
}
@@ -2031,6 +2031,47 @@ private String processTags(String username, String message, String reply, List<S
20312031
return reply;
20322032
}
20332033

2034+
/**
2035+
* Converts an args {@link String} into a array of arguments.
2036+
*
2037+
* @param args the args string to convert
2038+
* @return the array of arguments
2039+
*/
2040+
private String[] parseCallArgsString(String args) {
2041+
List<String> result = new ArrayList<>();
2042+
String buffer = "";
2043+
boolean insideAString = false;
2044+
2045+
if (args != null) {
2046+
for (char c : args.toCharArray()) {
2047+
if (Character.isWhitespace(c) && !insideAString) {
2048+
if (buffer.length() > 0) {
2049+
result.add(buffer);
2050+
}
2051+
buffer = "";
2052+
continue;
2053+
}
2054+
if (c == '"') {
2055+
if (insideAString) {
2056+
if (buffer.length() > 0) {
2057+
result.add(buffer);
2058+
}
2059+
buffer = "";
2060+
}
2061+
insideAString = !insideAString;
2062+
continue;
2063+
}
2064+
buffer += c;
2065+
}
2066+
2067+
if (buffer.length() > 0) {
2068+
result.add(buffer);
2069+
}
2070+
}
2071+
2072+
return result.toArray(new String[0]);
2073+
}
2074+
20342075
/**
20352076
* Applies a substitution to an input message.
20362077
*

0 commit comments

Comments
 (0)