Fix string quoter not handling it properly

This commit is contained in:
libraryaddict 2020-03-25 15:53:49 +13:00
parent f254a99d89
commit e3d4659545
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4
2 changed files with 20 additions and 2 deletions

@ -1633,8 +1633,7 @@ public class DisguiseUtilities {
return string; return string;
} }
return "\"" + string.replaceAll("\\B\"", "\\\"").replaceAll("\\\\(?=\\\\*\"\\B)", "\\\\") return "\"" + string.replaceAll("\\\\(?=\\\\*\"( |$))", "\\\\\\\\").replaceAll("((?<= )\")|(\"(?= ))", "\\\\\"") + "\"";
.replaceAll("(?=\"\\B)", "\\") + "\"";
} }
public static String[] split(String string) { public static String[] split(String string) {

@ -1,5 +1,7 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -108,6 +110,21 @@ public class DisguiseUtilitiesTest {
"Foobar", "is", "not", "Foo Bar", "but", "is", "a", "single", "word", "foobar", "or", "as", "some", "Foobar", "is", "not", "Foo Bar", "but", "is", "a", "single", "word", "foobar", "or", "as", "some",
"quote", "it,", "'foobar'", "and", "again,", "not", "'foo", "bar'", "-", "It", "is", "'foobar'!"); "quote", "it,", "'foobar'", "and", "again,", "not", "'foo", "bar'", "-", "It", "is", "'foobar'!");
splitAndBack("Hi \" bye");
splitAndBack("Hi\\\" I'm Sam");
splitAndBack("\"Hi\\\" I'm Sam");
splitAndBack("\"Hi\\\\\" I'm Sam");
splitAndBack("\"Hi\\\\\\\" I'm Sam");
splitAndBack("\"Hi\\\\\\\" \"I'm Sam");
}
private void splitAndBack(String string) {
String quoted = DisguiseUtilities.quote(string);
String[] split = DisguiseUtilities.split(quoted);
Assert.assertEquals(1, split.length);
Assert.assertEquals(string, split[0]);
} }
private void splitEquals(String toSplit, String... expected) { private void splitEquals(String toSplit, String... expected) {
@ -116,5 +133,7 @@ public class DisguiseUtilitiesTest {
.toArray(String[]::new); .toArray(String[]::new);
Assert.assertArrayEquals(expect, splitted); Assert.assertArrayEquals(expect, splitted);
splitAndBack(toSplit);
} }
} }