Skip to content

Commit

Permalink
Fix delimiters in various functions to not be interpreted as regex
Browse files Browse the repository at this point in the history
These functions had their delimiters interpreted as regex rather than literal strings:
- json.toList() - 2nd parameter
- json.fromStrProp() - 2nd parameter
- execLink() - 4th parameter
- execFunction() - 5th parameter
- selectTokens() - 3rd parameter
- deselectTokens() - 2nd parameter
- broadcast() - 3rd parameter
- exposeFOW() - 3rd parameter
- input() - "DELIMITER=" for lists and radios

This was done by introducing a new `StringUtil.split(` utility function, and changing the implementation of these
functions to use it instead of `String.split()` directly. There are other places that could benefit from this new
function, but have been left alone for now as they work as they are.
  • Loading branch information
kwvanderlinde committed Nov 6, 2023
1 parent 3622aea commit 8364590
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.rptools.maptool.client.ui.commandpanel.CommandPanel;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.TextMessage;
import net.rptools.maptool.util.StringUtil;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;
Expand Down Expand Up @@ -93,7 +94,9 @@ private Object broadcast(VariableResolver resolver, List<Object> param) throws P
jarray = JsonParser.parseString(temp).getAsJsonArray();
else {
jarray = new JsonArray();
for (String t : temp.split(delim)) jarray.add(t.trim());
for (String t : StringUtil.split(temp, delim)) {
jarray.add(t.trim());
}
}
if (jarray.size() == 0) {
return ""; // dont send to empty lists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.rptools.maptool.client.functions.json.JsonArrayFunctions;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.util.FunctionUtil;
import net.rptools.maptool.util.StringUtil;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;
Expand Down Expand Up @@ -88,7 +89,7 @@ public Object childEvaluate(
jsonTargets = jsonArrayFunctions.parseJsonArray(strTargets);
} else {
jsonTargets = new JsonArray();
for (String t : strTargets.split(delim)) {
for (String t : StringUtil.split(strTargets, delim)) {
jsonTargets.add(t.trim());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.rptools.maptool.model.Token;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.util.FunctionUtil;
import net.rptools.maptool.util.StringUtil;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;
Expand Down Expand Up @@ -172,7 +173,7 @@ private Set<GUID> getTokenSetFromList(final Zone zone, final String paramStr, fi
}
} else {
// String List
String[] strList = paramStr.split(delim);
String[] strList = StringUtil.split(paramStr, delim);
for (String s : strList) {
Token t = zone.resolveToken(s.trim());
if (t != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.Token;
import net.rptools.maptool.util.ImageManager;
import net.rptools.maptool.util.StringUtil;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;
Expand Down Expand Up @@ -394,7 +395,7 @@ public List<String> parseStringList(String valueString, String delim) {
}
}
} else {
String[] values = valueString.split(delim);
String[] values = StringUtil.split(valueString, delim);
for (String s : values) {
ret.add(s.trim());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ public Object childEvaluate(
jsonTargets = JSONMacroFunctions.getInstance().asJsonElement(strTargets).getAsJsonArray();
else {
jsonTargets = new JsonArray();
for (String t : strTargets.split(delim)) jsonTargets.add(t.trim());
for (String t : StringUtil.split(strTargets, delim)) {
jsonTargets.add(t.trim());
}
}
if (jsonTargets.size() == 0) {
return ""; // dont send to empty lists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.rptools.maptool.model.Token;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.util.FunctionUtil;
import net.rptools.maptool.util.StringUtil;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;
Expand Down Expand Up @@ -107,7 +108,7 @@ private void deselectTokens(List<Object> parameters) throws ParserException {
}
} else {
// String List
String[] strList = paramStr.split(delim);
String[] strList = StringUtil.split(paramStr, delim);
for (String s : strList) {
Token t = zone.resolveToken(s.trim());
if (t != null) {
Expand Down Expand Up @@ -185,7 +186,7 @@ private void selectTokens(List<Object> parameters) throws ParserException {
}
} else {
// String List
String[] strList = paramStr.split(delim);
String[] strList = StringUtil.split(paramStr, delim);
for (String s : strList) {
Token t = zone.resolveToken(s.trim());
if (t != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.util.StringUtil;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;

Expand Down Expand Up @@ -104,7 +105,7 @@ public int compare(JsonObject jo1, JsonObject jo2) {
* @return the string list as a json array.
*/
public JsonArray fromStringList(String strList, String delim) {
String[] list = strList.split(delim);
String[] list = StringUtil.split(strList, delim);

// An Empty list should generate an empty JSON array.
if (list.length == 1 && list[0].length() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.math.BigDecimal;
import java.util.List;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.util.StringUtil;
import net.rptools.parser.ParserException;
import net.rptools.parser.VariableResolver;

Expand Down Expand Up @@ -48,7 +49,7 @@ public JsonObjectFunctions(JsonMTSTypeConversion converter) {
* @return a {@link JsonObject} convert4d from the string properties.
*/
public JsonObject fromStrProp(String prop, String delim) {
String[] propsArray = prop.split(delim);
String[] propsArray = StringUtil.split(prop, delim);
JsonObject jsonObject = new JsonObject();

for (String s : propsArray) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/rptools/maptool/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;

Expand Down Expand Up @@ -319,4 +320,16 @@ public static String htmlize(String input, String type) {
default -> input;
};
}

/**
* Splits a string using a literal delimiter.
*
* <p>Unlike {@link String#split(String)}, {@code delim} is not a regular expression.
*
* @return
*/
public static String[] split(String string, String delim) {
var pattern = Pattern.quote(delim);
return string.split(pattern);
}
}

0 comments on commit 8364590

Please sign in to comment.