Use String#lastIndexOf(int) where possible

This commit is contained in:
Sam Brannen
2022-03-15 17:03:20 +01:00
parent a683e9e81b
commit 9fbf5dc945
3 changed files with 12 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,6 +65,8 @@ public abstract class StringUtils {
private static final String FOLDER_SEPARATOR = "/";
private static final char FOLDER_SEPARATOR_CHAR = '/';
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
private static final String TOP_PATH = "..";
@@ -581,7 +583,7 @@ public abstract class StringUtils {
return null;
}
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
return (separatorIndex != -1 ? path.substring(separatorIndex + 1) : path);
}
@@ -602,7 +604,7 @@ public abstract class StringUtils {
return null;
}
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
if (folderIndex > extIndex) {
return null;
}
@@ -622,7 +624,7 @@ public abstract class StringUtils {
return path;
}
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
if (folderIndex > extIndex) {
return path;
}
@@ -639,11 +641,11 @@ public abstract class StringUtils {
* @return the full file path that results from applying the relative path
*/
public static String applyRelativePath(String path, String relativePath) {
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR_CHAR);
if (separatorIndex != -1) {
String newPath = path.substring(0, separatorIndex);
if (!relativePath.startsWith(FOLDER_SEPARATOR)) {
newPath += FOLDER_SEPARATOR;
newPath += FOLDER_SEPARATOR_CHAR;
}
return newPath + relativePath;
}