From 72d5733ebcae6acd916a65b4672585b2c351b848 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 6 Oct 2014 20:11:20 +0400 Subject: [PATCH 1/3] accept @Nullable parameters --- .../src/com/intellij/openapi/util/text/StringUtil.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 e8132521cf6e..ba8554c08e28 100644 --- a/platform/util/src/com/intellij/openapi/util/text/StringUtil.java +++ b/platform/util/src/com/intellij/openapi/util/text/StringUtil.java @@ -2683,7 +2683,15 @@ public class StringUtil extends StringUtilRt { } @Contract(pure = true) - public static boolean equalsIgnoreWhitespaces(@NotNull CharSequence s1, @NotNull CharSequence s2) { + public static boolean equalsIgnoreWhitespaces(@Nullable CharSequence s1, @Nullable CharSequence s2) { + if (s1 == null ^ s2 == null) { + return false; + } + + if (s1 == null) { + return true; + } + int len1 = s1.length(); int len2 = s2.length(); From 24b9ce219a9f4bba62ab9fd861e5993590db0905 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 6 Oct 2014 20:11:47 +0400 Subject: [PATCH 2/3] add test on equalsIgnoreWhitespace() --- .../intellij/util/text/StringUtilTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java index 93ebb038cae1..84c2acbba767 100644 --- a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java +++ b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java @@ -233,4 +233,29 @@ public class StringUtilTest extends TestCase { public void testReplace() { assertEquals(StringUtil.replace("$PROJECT_FILE$/filename", "$PROJECT_FILE$", "/tmp"), "/tmp/filename"); } + + public void testEqualsIgnoreWhitespaces() { + assertTrue(StringUtil.equalsIgnoreWhitespaces(null, null)); + assertFalse(StringUtil.equalsIgnoreWhitespaces("", null)); + + assertTrue(StringUtil.equalsIgnoreWhitespaces("", "")); + assertTrue(StringUtil.equalsIgnoreWhitespaces("\n\t ", "")); + assertTrue(StringUtil.equalsIgnoreWhitespaces("", "\t\n \n\t")); + assertTrue(StringUtil.equalsIgnoreWhitespaces("\t", "\n")); + + assertTrue(StringUtil.equalsIgnoreWhitespaces("x", " x")); + assertTrue(StringUtil.equalsIgnoreWhitespaces("x", "x ")); + assertTrue(StringUtil.equalsIgnoreWhitespaces("x\n", "x")); + + assertTrue(StringUtil.equalsIgnoreWhitespaces("abcd", "a\nb\nc\nd\n")); + assertTrue(StringUtil.equalsIgnoreWhitespaces("x y x", "x y x")); + assertTrue(StringUtil.equalsIgnoreWhitespaces("xyx", "x y x")); + + assertFalse(StringUtil.equalsIgnoreWhitespaces("x", "\t\n ")); + assertFalse(StringUtil.equalsIgnoreWhitespaces("", " x ")); + assertFalse(StringUtil.equalsIgnoreWhitespaces("", "x ")); + assertFalse(StringUtil.equalsIgnoreWhitespaces("", " x")); + assertFalse(StringUtil.equalsIgnoreWhitespaces("xyx", "xxx")); + assertFalse(StringUtil.equalsIgnoreWhitespaces("xyx", "xYx")); + } } From 6b85507a2efa0d534bf5905e1ae4b0d84e1fe22f Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 6 Oct 2014 20:16:57 +0400 Subject: [PATCH 3/3] add test on testStringHashCodeIgnoreWhitespaces --- .../intellij/util/text/StringUtilTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java index 84c2acbba767..8b11f1f33db6 100644 --- a/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java +++ b/platform/util/testSrc/com/intellij/util/text/StringUtilTest.java @@ -15,6 +15,7 @@ */ package com.intellij.util.text; +import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.text.StringUtil; import junit.framework.TestCase; @@ -258,4 +259,26 @@ public class StringUtilTest extends TestCase { assertFalse(StringUtil.equalsIgnoreWhitespaces("xyx", "xxx")); assertFalse(StringUtil.equalsIgnoreWhitespaces("xyx", "xYx")); } + + public void testStringHashCodeIgnoreWhitespaces() { + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces(""))); + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("\n\t "), StringUtil.stringHashCodeIgnoreWhitespaces(""))); + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces("\t\n \n\t"))); + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("\t"), StringUtil.stringHashCodeIgnoreWhitespaces("\n"))); + + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x"), StringUtil.stringHashCodeIgnoreWhitespaces(" x"))); + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x"), StringUtil.stringHashCodeIgnoreWhitespaces("x "))); + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x\n"), StringUtil.stringHashCodeIgnoreWhitespaces("x"))); + + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("abcd"), StringUtil.stringHashCodeIgnoreWhitespaces("a\nb\nc\nd\n"))); + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x y x"), StringUtil.stringHashCodeIgnoreWhitespaces("x y x"))); + assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("xyx"), StringUtil.stringHashCodeIgnoreWhitespaces("x y x"))); + + assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x"), StringUtil.stringHashCodeIgnoreWhitespaces("\t\n "))); + assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces(" x "))); + assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces("x "))); + assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces(" x"))); + assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("xyx"), StringUtil.stringHashCodeIgnoreWhitespaces("xxx"))); + assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("xyx"), StringUtil.stringHashCodeIgnoreWhitespaces("xYx"))); + } }