From 6c4344f714f2d32c29bddc3f23278600b5987c5a Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 24 Jun 2015 18:22:39 -0700 Subject: [PATCH] Fixed a bug in CharArrayUtils.lastIndexOf(char[], char[]) method. --- .../core/parser/tests/CharArrayUtilsTest.java | 31 ++++++++- .../cdt/core/parser/util/CharArrayUtils.java | 68 ++++++++++++++----- 2 files changed, 79 insertions(+), 20 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 be6c1c1e827..9ad5715b8ba 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2009 IBM Corporation and others. + * Copyright (c) 2004, 2015 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,14 +7,15 @@ * * Contributors: * IBM Corporation - initial implementation + * Sergey Prigogin (Google) *******************************************************************************/ 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; +import junit.framework.TestCase; + /** * @author Doug Schaefer */ @@ -50,4 +51,28 @@ public class CharArrayUtilsTest extends TestCase { assertFalse(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 4, "abcd".toCharArray(), true)); assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 2, "AB".toCharArray(), true)); } + + public void testTrim() { + assertEquals("", new String(CharArrayUtils.trim("".toCharArray()))); + assertEquals("", new String(CharArrayUtils.trim(" ".toCharArray()))); + assertEquals("a", new String(CharArrayUtils.trim(" a".toCharArray()))); + assertEquals("a", new String(CharArrayUtils.trim(" a ".toCharArray()))); + assertEquals("a b", new String(CharArrayUtils.trim(" a b ".toCharArray()))); + assertEquals("a b", new String(CharArrayUtils.trim("a b ".toCharArray()))); + } + + public void testLastIndexOf() { + assertEquals(-1, CharArrayUtils.lastIndexOf('a', "".toCharArray())); + assertEquals(3, CharArrayUtils.lastIndexOf('a', "array".toCharArray())); + assertEquals(-1, CharArrayUtils.lastIndexOf('a', "array".toCharArray(), 4)); + assertEquals(3, CharArrayUtils.lastIndexOf('a', "array".toCharArray(), 3)); + + assertEquals(8, CharArrayUtils.lastIndexOf("aaabbbaa".toCharArray(), + "aaabbbaaaaabbbaabbbaa".toCharArray())); + assertEquals(-1, CharArrayUtils.lastIndexOf("aaabbbaa".toCharArray(), + "aabbbaabbbaa".toCharArray())); + assertEquals(6, CharArrayUtils.lastIndexOf("".toCharArray(), "123456".toCharArray())); + assertEquals(4, CharArrayUtils.lastIndexOf("56".toCharArray(), "123456".toCharArray())); + assertEquals(-1, CharArrayUtils.lastIndexOf("123".toCharArray(), "".toCharArray())); + } } 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 f536acddce6..b5692e2f8cb 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2009 IBM Corporation and others. + * Copyright (c) 2004, 2015 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -33,7 +33,7 @@ public class CharArrayUtils { int end = start + length; for (int curr = start; curr < end; ++curr) { - h += (h << 3) + str[curr]; + h = 31 * h + str[curr]; } return h; @@ -300,33 +300,65 @@ public class CharArrayUtils { } public static final int lastIndexOf(char[] toBeFound, char[] array) { - int j = toBeFound.length - 1; - for (int i = array.length; --i >= 0;) { - if (toBeFound[j] == array[i]) { - if (--j == -1) - return i; - } else { - j = toBeFound.length - 1; + return lastIndexOf(toBeFound, array, 0); + } + + /** + * @since 5.11 + */ + public static int lastIndexOf(char toBeFound, char[] array) { + return lastIndexOf(toBeFound, array, 0); + } + + /** + * @since 5.11 + */ + public static int lastIndexOf(char toBeFound, char[] array, int fromIndex) { + for (int i = array.length; --i >= fromIndex;) { + if (array[i] == toBeFound) { + return i; } } return -1; } - + + /** + * @since 5.11 + */ + public static int lastIndexOf(char[] toBeFound, char[] array, int fromIndex) { + int i = array.length; + int j = toBeFound.length; + while (true) { + if (--j < 0) + return i; + if (--i < fromIndex) + return -1; + if (toBeFound[j] != array[i]) { + i += toBeFound.length - j - 1; + j = toBeFound.length; + } + } + } + static final public char[] trim(char[] chars) { if (chars == null) return null; - int start = 0, length = chars.length, end = length - 1; + int length = chars.length; + int start = 0; while (start < length && chars[start] == ' ') { start++; } - while (end > start && chars[end] == ' ') { - end--; + if (start == length) + return EMPTY_CHAR_ARRAY; + + int end = length; + while (--end > start && chars[end] == ' ') { } - if (start != 0 || end != length - 1) { - return subarray(chars, start, end + 1); - } - return chars; + end++; + if (start == 0 && end == length) + return chars; + return subarray(chars, start, end); } static final public char[] lastSegment(char[] array, char[] separator) { @@ -369,6 +401,8 @@ public class CharArrayUtils { */ public static char[] extractChars(StringBuilder buf) { final int len = buf.length(); + if (len == 0) + return EMPTY_CHAR_ARRAY; char[] result= new char[len]; buf.getChars(0, len, result, 0); return result;