From 217da1e6aa2b2c9f626ed2efa42e3e3996175571 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Mon, 1 Jun 2020 02:26:14 +0800 Subject: [PATCH] implement lastIndexOf --- .../commons/lang3/CharSequenceUtils.java | 31 +++++++++++++------ .../commons/lang3/CharSequenceUtilsTest.java | 23 +++----------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java b/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java index 4dab0637e..01de17c2b 100644 --- a/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java +++ b/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java @@ -266,22 +266,33 @@ public class CharSequenceUtils { start = len1 - len2; } - if (check(cs, searchChar, len2, start)) { - return start; - } + char char0 = searchChar.charAt(0); - for (int i = start - 1; i >= 0; i--) { - if (check(cs, searchChar, len2, i)) { + int i = start; + while (true) { + while (cs.charAt(i) != char0) { + i--; + if (i < 0) { + return -1; + } + } + if (checkLaterThan1(cs, searchChar, len2, 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) { - for (int i = 0; i < len2; i++) { - if (cs.charAt(start1 + i) != searchChar.charAt(i)) { + private static boolean checkLaterThan1(final CharSequence cs, final CharSequence searchChar, int len2, int start1) { + for (int i = 1, j = len2 - 1; i <= j; i++, j--) { + if (cs.charAt(start1 + i) != searchChar.charAt(i) + || + cs.charAt(start1 + j) != searchChar.charAt(j) + ) { return false; } } diff --git a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java index c230c4507..796e8f24e 100644 --- a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java @@ -16,23 +16,14 @@ */ package org.apache.commons.lang3; -import static java.nio.CharBuffer.wrap; -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 org.junit.jupiter.api.Test; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; -import java.nio.CharBuffer; import java.util.Random; import java.util.stream.IntStream; -import org.apache.commons.lang3.text.StrBuilder; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; /** * Tests CharSequenceUtils @@ -258,7 +249,7 @@ public class CharSequenceUtilsTest { private void testNewLastIndexOfSingle(CharSequence a, CharSequence b) { 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); } } @@ -269,14 +260,8 @@ public class CharSequenceUtilsTest { } 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( - expected, + a.toString().lastIndexOf(b.toString(), start), CharSequenceUtils.lastIndexOf(new WrapperString(a.toString()), b.toString(), start), "testNewLastIndexOf fails! original : " + a + " seg : " + b + " start : " + start );