From 22a7f048e77aad1b3302c6e172e8663513e62c11 Mon Sep 17 00:00:00 2001 From: Sergey Simonchik Date: Thu, 11 Dec 2014 00:44:15 +0300 Subject: [PATCH] StringUtil.stripQuotesAroundValue optimization: avoid two substring calls --- .../intellij/openapi/util/text/StringUtil.java | 15 +++++++++++++-- .../com/intellij/util/text/StringUtilTest.java | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java index 6712768019f8..eb3147012c76 100644 --- a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java +++ b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java @@ -1490,11 +1490,22 @@ public class StringUtil extends StringUtilRt { @NotNull @Contract(pure = true) public static String stripQuotesAroundValue(@NotNull String text) { - if (startsWithChar(text, '\"') || startsWithChar(text, '\'')) text = text.substring(1); - if (endsWithChar(text, '\"') || endsWithChar(text, '\'')) text = text.substring(0, text.length() - 1); + final int len = text.length(); + if (len > 0) { + final int from = isQuoteAt(text, 0) ? 1 : 0; + final int to = len > 1 && isQuoteAt(text, len - 1) ? len - 1 : len; + if (from > 0 || to < len) { + return text.substring(from, to); + }; + } return text; } + private static boolean isQuoteAt(@NotNull String text, int ind) { + char ch = text.charAt(ind); + return ch == '\'' || ch == '\"'; + } + @Contract(pure = true) public static boolean isQuotedString(@NotNull String text) { if (text.length() < 2) return false; diff --git a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java index 127e2459e85e..bec2a5ee6eba 100644 --- a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java +++ b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java @@ -173,6 +173,24 @@ public class StringUtilTest extends TestCase { assertEquals("\"foo\'", StringUtil.unquoteString("\"foo\'")); } + public void testStripQuotesAroundValue() { + assertEquals("", StringUtil.stripQuotesAroundValue("")); + assertEquals("", StringUtil.stripQuotesAroundValue("'")); + assertEquals("", StringUtil.stripQuotesAroundValue("\"")); + assertEquals("", StringUtil.stripQuotesAroundValue("''")); + assertEquals("", StringUtil.stripQuotesAroundValue("\"\"")); + assertEquals("", StringUtil.stripQuotesAroundValue("'\"")); + assertEquals("foo", StringUtil.stripQuotesAroundValue("'foo'")); + assertEquals("foo", StringUtil.stripQuotesAroundValue("'foo")); + assertEquals("foo", StringUtil.stripQuotesAroundValue("foo'")); + assertEquals("f'o'o", StringUtil.stripQuotesAroundValue("'f'o'o'")); + assertEquals("f\"o'o", StringUtil.stripQuotesAroundValue("\"f\"o'o'")); + assertEquals("f\"o'o", StringUtil.stripQuotesAroundValue("f\"o'o")); + assertEquals("\"'f\"o'o\"", StringUtil.stripQuotesAroundValue("\"\"'f\"o'o\"\"")); + assertEquals("''f\"o'o''", StringUtil.stripQuotesAroundValue("'''f\"o'o'''")); + assertEquals("foo' 'bar", StringUtil.stripQuotesAroundValue("foo' 'bar")); + } + public void testUnqoteWithQuotationChar() { assertEquals("", StringUtil.unquoteString("", '|')); assertEquals("|", StringUtil.unquoteString("|", '|'));