mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Fixed a bug in CharArrayUtils.lastIndexOf(char[], char[]) method.
This commit is contained in:
parent
75a3d71546
commit
6c4344f714
2 changed files with 79 additions and 20 deletions
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue