Implements String equality which always compares all characters in the string, without stopping early if any characters do not match. Note: This implementation was adapted from MessageDigest#isEqual which we assume is as optimizer-defeating as possible. @param s1 The firs
(final String s1, final String s2, final boolean ignoreCase)
| 45 | * @return <code>true</code> if the strings are equal to each other, <code>false</code> otherwise. |
| 46 | */ |
| 47 | public static boolean equals(final String s1, final String s2, final boolean ignoreCase) { |
| 48 | if (s1 == s2) { |
| 49 | return true; |
| 50 | } |
| 51 | if (s1 == null || s2 == null) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | final int len1 = s1.length(); |
| 56 | final int len2 = s2.length(); |
| 57 | |
| 58 | if (len2 == 0) { |
| 59 | return len1 == 0; |
| 60 | } |
| 61 | |
| 62 | int result = 0; |
| 63 | result |= len1 - len2; |
| 64 | |
| 65 | // time-constant comparison |
| 66 | for (int i = 0; i < len1; i++) { |
| 67 | // If i >= len2, index2 is 0; otherwise, i. |
| 68 | final int index2 = ((i - len2) >>> 31) * i; |
| 69 | char c1 = s1.charAt(i); |
| 70 | char c2 = s2.charAt(index2); |
| 71 | if (ignoreCase) { |
| 72 | c1 = Character.toLowerCase(c1); |
| 73 | c2 = Character.toLowerCase(c2); |
| 74 | } |
| 75 | result |= c1 ^ c2; |
| 76 | } |
| 77 | return result == 0; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
no test coverage detected