From e3d46595452c04154d83eca8cfce9a4f4bf06ce8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 25 Mar 2020 15:53:49 +1300 Subject: [PATCH] Fix string quoter not handling it properly --- .../disguise/utilities/DisguiseUtilities.java | 3 +-- .../utilities/DisguiseUtilitiesTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 2535b497..4698585d 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -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) { diff --git a/src/test/java/me/libraryaddict/disguise/utilities/DisguiseUtilitiesTest.java b/src/test/java/me/libraryaddict/disguise/utilities/DisguiseUtilitiesTest.java index d1b4f24b..1942bb7a 100644 --- a/src/test/java/me/libraryaddict/disguise/utilities/DisguiseUtilitiesTest.java +++ b/src/test/java/me/libraryaddict/disguise/utilities/DisguiseUtilitiesTest.java @@ -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); } }