Simplify code paths where a statement is unnecessarily nested within an else clause. Also flip some if/else statements when tests can be expressed more clearly.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@619137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
db456f2f89
commit
004d7b3e8f
|
@ -1204,9 +1204,8 @@ public static boolean containsAny(String str, char[] searchChars) {
|
||||||
public static boolean containsAny(String str, String searchChars) {
|
public static boolean containsAny(String str, String searchChars) {
|
||||||
if (searchChars == null) {
|
if (searchChars == null) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
return containsAny(str, searchChars.toCharArray());
|
|
||||||
}
|
}
|
||||||
|
return containsAny(str, searchChars.toCharArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexOfAnyBut chars
|
// IndexOfAnyBut chars
|
||||||
|
@ -1665,9 +1664,8 @@ public static String left(String str, int len) {
|
||||||
}
|
}
|
||||||
if (str.length() <= len) {
|
if (str.length() <= len) {
|
||||||
return str;
|
return str;
|
||||||
} else {
|
|
||||||
return str.substring(0, len);
|
|
||||||
}
|
}
|
||||||
|
return str.substring(0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1699,9 +1697,8 @@ public static String right(String str, int len) {
|
||||||
}
|
}
|
||||||
if (str.length() <= len) {
|
if (str.length() <= len) {
|
||||||
return str;
|
return str;
|
||||||
} else {
|
|
||||||
return str.substring(str.length() - len);
|
|
||||||
}
|
}
|
||||||
|
return str.substring(str.length() - len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1740,9 +1737,8 @@ public static String mid(String str, int pos, int len) {
|
||||||
}
|
}
|
||||||
if (str.length() <= (pos + len)) {
|
if (str.length() <= (pos + len)) {
|
||||||
return str.substring(pos);
|
return str.substring(pos);
|
||||||
} else {
|
|
||||||
return str.substring(pos, pos + len);
|
|
||||||
}
|
}
|
||||||
|
return str.substring(pos, pos + len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubStringAfter/SubStringBefore
|
// SubStringAfter/SubStringBefore
|
||||||
|
@ -2018,11 +2014,10 @@ public static String[] substringsBetween(String str, String open, String close)
|
||||||
list.add(str.substring(start, end));
|
list.add(str.substring(start, end));
|
||||||
pos = end + closeLen;
|
pos = end + closeLen;
|
||||||
}
|
}
|
||||||
if (list.size() > 0) {
|
if (list.isEmpty()) {
|
||||||
return (String[]) list.toArray(new String [list.size()]);
|
|
||||||
} else {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return (String[]) list.toArray(new String [list.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nested extraction
|
// Nested extraction
|
||||||
|
@ -2507,9 +2502,8 @@ private static String[] splitWorker(String str, char separatorChar, boolean pres
|
||||||
}
|
}
|
||||||
start = ++i;
|
start = ++i;
|
||||||
continue;
|
continue;
|
||||||
} else {
|
|
||||||
lastMatch = false;
|
|
||||||
}
|
}
|
||||||
|
lastMatch = false;
|
||||||
match = true;
|
match = true;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -2642,9 +2636,8 @@ private static String[] splitWorker(String str, String separatorChars, int max,
|
||||||
}
|
}
|
||||||
start = ++i;
|
start = ++i;
|
||||||
continue;
|
continue;
|
||||||
} else {
|
|
||||||
lastMatch = false;
|
|
||||||
}
|
}
|
||||||
|
lastMatch = false;
|
||||||
match = true;
|
match = true;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -2664,9 +2657,8 @@ private static String[] splitWorker(String str, String separatorChars, int max,
|
||||||
}
|
}
|
||||||
start = ++i;
|
start = ++i;
|
||||||
continue;
|
continue;
|
||||||
} else {
|
|
||||||
lastMatch = false;
|
|
||||||
}
|
}
|
||||||
|
lastMatch = false;
|
||||||
match = true;
|
match = true;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -2685,9 +2677,8 @@ private static String[] splitWorker(String str, String separatorChars, int max,
|
||||||
}
|
}
|
||||||
start = ++i;
|
start = ++i;
|
||||||
continue;
|
continue;
|
||||||
} else {
|
|
||||||
lastMatch = false;
|
|
||||||
}
|
}
|
||||||
|
lastMatch = false;
|
||||||
match = true;
|
match = true;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -3876,9 +3867,8 @@ public static String replaceChars(String str, String searchChars, String replace
|
||||||
}
|
}
|
||||||
if (modified) {
|
if (modified) {
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
} else {
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overlay
|
// Overlay
|
||||||
|
@ -4013,9 +4003,8 @@ public static String chomp(String str) {
|
||||||
char ch = str.charAt(0);
|
char ch = str.charAt(0);
|
||||||
if (ch == CharUtils.CR || ch == CharUtils.LF) {
|
if (ch == CharUtils.CR || ch == CharUtils.LF) {
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
} else {
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastIdx = str.length() - 1;
|
int lastIdx = str.length() - 1;
|
||||||
|
@ -4098,9 +4087,8 @@ public static String chompLast(String str, String sep) {
|
||||||
String sub = str.substring(str.length() - sep.length());
|
String sub = str.substring(str.length() - sep.length());
|
||||||
if (sep.equals(sub)) {
|
if (sep.equals(sub)) {
|
||||||
return str.substring(0, str.length() - sep.length());
|
return str.substring(0, str.length() - sep.length());
|
||||||
} else {
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4139,11 +4127,10 @@ public static String getChomp(String str, String sep) {
|
||||||
*/
|
*/
|
||||||
public static String prechomp(String str, String sep) {
|
public static String prechomp(String str, String sep) {
|
||||||
int idx = str.indexOf(sep);
|
int idx = str.indexOf(sep);
|
||||||
if (idx != -1) {
|
if (idx == -1) {
|
||||||
return str.substring(idx + sep.length());
|
|
||||||
} else {
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
return str.substring(idx + sep.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4160,11 +4147,10 @@ public static String prechomp(String str, String sep) {
|
||||||
*/
|
*/
|
||||||
public static String getPrechomp(String str, String sep) {
|
public static String getPrechomp(String str, String sep) {
|
||||||
int idx = str.indexOf(sep);
|
int idx = str.indexOf(sep);
|
||||||
if (idx != -1) {
|
if (idx == -1) {
|
||||||
return str.substring(0, idx + sep.length());
|
|
||||||
} else {
|
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
|
return str.substring(0, idx + sep.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chopping
|
// Chopping
|
||||||
|
@ -5629,9 +5615,8 @@ public static int indexOfDifference(String[] strs) {
|
||||||
// shortest string and didn't find a match, but the string lengths
|
// shortest string and didn't find a match, but the string lengths
|
||||||
// vary, so return the length of the shortest string.
|
// vary, so return the length of the shortest string.
|
||||||
return shortestStrLen;
|
return shortestStrLen;
|
||||||
} else {
|
|
||||||
return firstDiff;
|
|
||||||
}
|
}
|
||||||
|
return firstDiff;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -5676,9 +5661,8 @@ public static String getCommonPrefix(String[] strs) {
|
||||||
// all strings were identical
|
// all strings were identical
|
||||||
if (strs[0] == null) {
|
if (strs[0] == null) {
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
} else {
|
|
||||||
return strs[0];
|
|
||||||
}
|
}
|
||||||
|
return strs[0];
|
||||||
} else if (smallestIndexOfDiff == 0) {
|
} else if (smallestIndexOfDiff == 0) {
|
||||||
// there were no common initial characters
|
// there were no common initial characters
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
|
|
Loading…
Reference in New Issue