Skip to content

Commit f8ec941

Browse files
committed
Fixed bug in StringReplacer
1 parent c32c351 commit f8ec941

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

app/src/processing/app/helpers/StringReplacer.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ public static String[] quotedSplit(String src, String quoteChars,
5757
for (String i : src.split(" ")) {
5858
if (escapingChar == null) {
5959
// If the first char is not an escape char..
60-
String first = i.substring(0, 1);
61-
if (!quoteChars.contains(first)) {
60+
String first = null;
61+
if (i.length() > 0)
62+
first = i.substring(0, 1);
63+
if (first == null || !quoteChars.contains(first)) {
6264
if (i.trim().length() != 0 || acceptEmptyArguments)
6365
res.add(i);
6466
continue;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package processing.app.helpers;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
5+
import org.junit.Test;
6+
7+
public class StringReplacerTest {
8+
9+
@Test
10+
public void quotingCheck() throws Exception {
11+
String in = "a\"bc ab'c 'abc abc' ";
12+
in += "\"abc abc\" '\"abc abc\"' ";
13+
in += "\"'abc abc'\"";
14+
String[] res = StringReplacer.quotedSplit(in, "\"'", false);
15+
assertArrayEquals(res, new String[] { "a\"bc", "ab'c", "abc abc",
16+
"abc abc", "\"abc abc\"", "'abc abc'" });
17+
}
18+
19+
@Test
20+
public void quotingCheckWithEmptyStringsAccepted() throws Exception {
21+
String in = "a\"bc ab'c 'abc abc' ";
22+
in += "\"abc abc\" '\"abc abc\"' ";
23+
in += "\"'abc abc'\"";
24+
String[] res = StringReplacer.quotedSplit(in, "\"'", true);
25+
assertArrayEquals(res, new String[] { "a\"bc", "ab'c", "", "", "abc abc",
26+
"abc abc", "\"abc abc\"", "'abc abc'" });
27+
}
28+
29+
}

0 commit comments

Comments
 (0)