From 1ca59d413e1488b62c1fff59261a0817cc4dbded Mon Sep 17 00:00:00 2001 From: Sergey Simonchik Date: Sun, 11 Jan 2015 14:44:39 +0300 Subject: [PATCH] StringUtil.stripQuotesAroundValue deprecated; StringUtil.unquoteString implementation simplified --- .../openapi/util/text/StringUtil.java | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 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 f95039d7b40a..efb174a77b73 100644 --- a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java +++ b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java @@ -613,24 +613,32 @@ public class StringUtil extends StringUtilRt { return buffer.toString(); } + private static boolean isQuoteAt(@NotNull String s, int ind) { + char ch = s.charAt(ind); + return ch == '\'' || ch == '\"'; + } + + @Contract(pure = true) + public static boolean isQuotedString(@NotNull String s) { + return s.length() > 1 && isQuoteAt(s, 0) && s.charAt(0) == s.charAt(s.length() - 1); + } + @NotNull @Contract(pure = true) public static String unquoteString(@NotNull String s) { - char c; - if (s.length() <= 1 || (c = s.charAt(0)) != '"' && c != '\'' || s.charAt(s.length() - 1) != c) { - return s; + if (isQuotedString(s)) { + return s.substring(1, s.length() - 1); } - return s.substring(1, s.length() - 1); + return s; } @NotNull @Contract(pure = true) public static String unquoteString(@NotNull String s, char quotationChar) { - char c; - if (s.length() <= 1 || (c = s.charAt(0)) != quotationChar || s.charAt(s.length() - 1) != c) { - return s; + if (s.length() > 1 && quotationChar == s.charAt(0) && quotationChar == s.charAt(s.length() - 1)) { + return s.substring(1, s.length() - 1); } - return s.substring(1, s.length() - 1); + return s; } /** @@ -1487,8 +1495,15 @@ public class StringUtil extends StringUtilRt { return builder.toString(); } + /** + * Strips quotes around the value. + * Quotes are removed even if leading and trailing quotes are different or if there is only one quote (leading or trailing). + * @deprecated use {@link com.intellij.openapi.util.text.StringUtil#unquoteString(String)} instead + * To be removed in IDEA 17 + */ @NotNull @Contract(pure = true) + @Deprecated public static String stripQuotesAroundValue(@NotNull String text) { final int len = text.length(); if (len > 0) { @@ -1501,18 +1516,6 @@ public class StringUtil extends StringUtilRt { 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; - return startsWithChar(text, '\"') && endsWithChar(text, '\"') - || startsWithChar(text, '\'') && endsWithChar(text, '\''); - } - /** * Formats the specified file size as a string. * -- 2.32.0