mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
StringUtil.stripQuotesAroundValue optimization: avoid two substring calls
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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("|", '|'));
|
||||
|
||||
Reference in New Issue
Block a user