diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java index d3bc49ae05a7926143e15abd389fab85c46ab575..e2e5cb0997efb9be716cbc18400e5666892ef30c 100644 --- a/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java +++ b/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java @@ -801,4 +801,15 @@ public class CommonUtils { cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)); } + + public static String escapeBourneShellString(@NotNull String s) { + return "'" + s.replace("'", "'\\''") + "'"; + } + + public static String unescapeBourneShellString(@NotNull String s) { + if (!s.startsWith("'") || !s.endsWith("'") || s.length() < 2) { //not an escaped bourne shell string + return s; + } + return s.substring(1, s.length() - 1).replace("'\\''", "'"); + } } diff --git a/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java b/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java index 35a006377052c8d325efb1e82c6edb93f8d65665..00931a5f589083a86f06b96a1efa50784cae13c1 100644 --- a/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java +++ b/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java @@ -522,4 +522,29 @@ public class CommonUtilsTest { public void testgetSingleLineString() { Assert.assertEquals("a¶bc d ", CommonUtils.getSingleLineString("a\nb\rc\td\0")); } + + @Test + public void testEscapeStringForBourneShell() { + Assert.assertEquals("''", CommonUtils.escapeBourneShellString("")); + Assert.assertEquals("'string'", CommonUtils.escapeBourneShellString("string")); + Assert.assertEquals("'string with '\\''one single quote symbol'", CommonUtils.escapeBourneShellString("string with 'one single quote symbol")); + Assert.assertEquals("'string with '\\''two '\\''single quote symbols'", CommonUtils.escapeBourneShellString("string with 'two 'single quote symbols")); + Assert.assertEquals("'string with '\\''three '\\''single '\\''quote symbols'", CommonUtils.escapeBourneShellString("string with 'three 'single 'quote symbols")); + Assert.assertEquals("''\\'''", CommonUtils.escapeBourneShellString("'")); + Assert.assertEquals("'unit'\\'''\\''test'", CommonUtils.escapeBourneShellString("unit''test")); + Assert.assertEquals("'unit'\\'''\\'''\\''test'", CommonUtils.escapeBourneShellString("unit'''test")); + } + + @Test + public void testUnescapeStringForBourneShell() { + Assert.assertEquals("", CommonUtils.unescapeBourneShellString("''")); + Assert.assertEquals("string", CommonUtils.unescapeBourneShellString("'string'")); + Assert.assertEquals("string with 'one single quote symbol", CommonUtils.unescapeBourneShellString("'string with '\\''one single quote symbol'")); + Assert.assertEquals("string with 'two 'single quote symbols", CommonUtils.unescapeBourneShellString("'string with '\\''two '\\''single quote symbols'")); + Assert.assertEquals("string with 'three 'single 'quote symbols", CommonUtils.unescapeBourneShellString("'string with '\\''three '\\''single '\\''quote symbols'")); + Assert.assertEquals("'", CommonUtils.unescapeBourneShellString("''\\'''")); + Assert.assertEquals("unit''test", CommonUtils.unescapeBourneShellString("'unit'\\'''\\''test'")); + Assert.assertEquals("unit'''test", CommonUtils.unescapeBourneShellString("'unit'\\'''\\'''\\''test'")); + Assert.assertEquals("'''unit'''test'''", CommonUtils.unescapeBourneShellString("''\\'''\\'''\\''unit'\\'''\\'''\\''test'\\'''\\'''\\'''")); + } }