chore: find and replace

This commit is contained in:
hyb1996
2018-02-26 19:52:47 +08:00
parent a3fd8f2423
commit 84ab4d481c
4 changed files with 155 additions and 35 deletions

View File

@@ -55,4 +55,95 @@ public class TextUtils {
}
return -1;
}
public static int indexOf(CharSequence source,
CharSequence target,
int fromIndex) {
if (source instanceof String && target instanceof String) {
return ((String) source).indexOf((String) target);
}
if (fromIndex >= source.length()) {
return (target.length() == 0 ? source.length() : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (target.length() == 0) {
return fromIndex;
}
char first = target.charAt(0);
int max = (source.length() - target.length());
for (int i = fromIndex; i <= max; i++) {
/* Look for first character. */
if (source.charAt(i) != first) {
while (++i <= max && source.charAt(i) != first) ;
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + target.length() - 1;
for (int k = 1; j < end && source.charAt(j)
== target.charAt(k); j++, k++)
;
if (j == end) {
/* Found whole string. */
return i;
}
}
}
return -1;
}
public static int lastIndexOf(CharSequence source,
CharSequence target,
int fromIndex) {
if (source instanceof String && target instanceof String) {
return ((String) source).lastIndexOf((String) target);
}
/*
* Check arguments; return immediately where possible. For
* consistency, don't check for null str.
*/
int rightIndex = source.length() - target.length();
if (fromIndex < 0) {
return -1;
}
if (fromIndex > rightIndex) {
fromIndex = rightIndex;
}
/* Empty string always matches. */
if (target.length() == 0) {
return fromIndex;
}
int strLastIndex = target.length() - 1;
char strLastChar = target.charAt(strLastIndex);
int min = target.length() - 1;
int i = min + fromIndex;
startSearchForLastChar:
while (true) {
while (i >= min && source.charAt(i) != strLastChar) {
i--;
}
if (i < min) {
return -1;
}
int j = i - 1;
int start = j - (target.length() - 1);
int k = strLastIndex - 1;
while (j > start) {
if (source.charAt(j--) != target.charAt(k--)) {
i--;
continue startSearchForLastChar;
}
}
return start + 1;
}
}
}