implement lastIndexOf
This commit is contained in:
parent
dee8f6fb84
commit
217da1e6aa
|
@ -266,22 +266,33 @@ public class CharSequenceUtils {
|
||||||
start = len1 - len2;
|
start = len1 - len2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (check(cs, searchChar, len2, start)) {
|
char char0 = searchChar.charAt(0);
|
||||||
return start;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = start - 1; i >= 0; i--) {
|
int i = start;
|
||||||
if (check(cs, searchChar, len2, i)) {
|
while (true) {
|
||||||
|
while (cs.charAt(i) != char0) {
|
||||||
|
i--;
|
||||||
|
if (i < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (checkLaterThan1(cs, searchChar, len2, i)) {
|
||||||
return i;
|
return i;
|
||||||
|
} else {
|
||||||
|
i--;
|
||||||
|
if (i < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean check(final CharSequence cs, final CharSequence searchChar, int len2, int start1) {
|
private static boolean checkLaterThan1(final CharSequence cs, final CharSequence searchChar, int len2, int start1) {
|
||||||
for (int i = 0; i < len2; i++) {
|
for (int i = 1, j = len2 - 1; i <= j; i++, j--) {
|
||||||
if (cs.charAt(start1 + i) != searchChar.charAt(i)) {
|
if (cs.charAt(start1 + i) != searchChar.charAt(i)
|
||||||
|
||
|
||||||
|
cs.charAt(start1 + j) != searchChar.charAt(j)
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,23 +16,14 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
import static java.nio.CharBuffer.wrap;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import org.apache.commons.lang3.text.StrBuilder;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests CharSequenceUtils
|
* Tests CharSequenceUtils
|
||||||
|
@ -258,7 +249,7 @@ public class CharSequenceUtilsTest {
|
||||||
|
|
||||||
private void testNewLastIndexOfSingle(CharSequence a, CharSequence b) {
|
private void testNewLastIndexOfSingle(CharSequence a, CharSequence b) {
|
||||||
int maxa = Math.max(a.length(), b.length());
|
int maxa = Math.max(a.length(), b.length());
|
||||||
for (int i = -maxa-10; i <= maxa+10; i++) {
|
for (int i = -maxa - 10; i <= maxa + 10; i++) {
|
||||||
testNewLastIndexOfSingle(a, b, i);
|
testNewLastIndexOfSingle(a, b, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -269,14 +260,8 @@ public class CharSequenceUtilsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testNewLastIndexOfSingleSingle(CharSequence a, CharSequence b, int start) {
|
private void testNewLastIndexOfSingleSingle(CharSequence a, CharSequence b, int start) {
|
||||||
int expected = a.toString().lastIndexOf(b.toString(), start);
|
|
||||||
// assertEquals(
|
|
||||||
// expected,
|
|
||||||
// lastIndexOf(new WrapperString(a), b, start),
|
|
||||||
// "testNewLastIndexOf fails! original : " + a + " seg : " + b + " start : " + start
|
|
||||||
// );
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
expected,
|
a.toString().lastIndexOf(b.toString(), start),
|
||||||
CharSequenceUtils.lastIndexOf(new WrapperString(a.toString()), b.toString(), start),
|
CharSequenceUtils.lastIndexOf(new WrapperString(a.toString()), b.toString(), start),
|
||||||
"testNewLastIndexOf fails! original : " + a + " seg : " + b + " start : " + start
|
"testNewLastIndexOf fails! original : " + a + " seg : " + b + " start : " + start
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue