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

View File

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

View File

@ -1,5 +1,7 @@
package me.libraryaddict.disguise.utilities;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
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",
"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) {
@ -116,5 +133,7 @@ public class DisguiseUtilitiesTest {
.toArray(String[]::new);
Assert.assertArrayEquals(expect, splitted);
splitAndBack(toSplit);
}
}