From 7ad14d08db00decd5747b8d271ddfc0b205101b0 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Mon, 21 Sep 2009 15:06:52 +0000 Subject: [PATCH] Fixes char-array comparison, bug 289852. --- .../cdt/core/parser/tests/CharArrayUtilsTest.java | 11 +++++++++++ .../eclipse/cdt/core/parser/util/CharArrayUtils.java | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java index eca36f1e4e7..282e53846e8 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java @@ -13,6 +13,7 @@ package org.eclipse.cdt.core.parser.tests; import junit.framework.TestCase; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; +import org.eclipse.cdt.core.parser.util.CharArrayUtils; /** * @author Doug Schaefer @@ -39,4 +40,14 @@ public class CharArrayUtilsTest extends TestCase { Object ivalue4 = map.get("ikey4".toCharArray()); assertEquals(ivalue4, new Integer(4)); } + + public void testEquals_Bug289852() { + assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 3, "abc".toCharArray(), false)); + assertFalse(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 4, "abcd".toCharArray(), false)); + assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 2, "ab".toCharArray(), false)); + + assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 3, "ABC".toCharArray(), true)); + assertFalse(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 4, "abcd".toCharArray(), true)); + assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 2, "AB".toCharArray(), true)); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java index 9f10c714582..13ded97fdd4 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java @@ -89,7 +89,7 @@ public class CharArrayUtils { } public static final boolean equals(char[] str1, int start1, int length1, char[] str2) { - if (length1 != str2.length || str1.length < length1 ) + if (length1 != str2.length || str1.length < length1+start1 ) return false; if( str1 == str2 && start1 == 0 ) return true; @@ -104,7 +104,7 @@ public class CharArrayUtils { if( !ignoreCase ) return equals( str1, start1, length1, str2 ); - if (length1 != str2.length || str1.length < length1 ) + if (length1 != str2.length || str1.length < start1 + length1 ) return false; for (int i = 0; i < length1; ++i)