Allow equals with ignoreCase parameter to take nullable Strings both as a receiver and a parameter.

#KT-9188
This commit is contained in:
Ilya Gorbunov
2015-10-09 17:45:03 +03:00
parent c0faf82f77
commit 6d7cc0671e
3 changed files with 12 additions and 4 deletions
+5 -3
View File
@@ -47,11 +47,13 @@ public inline fun CharSequence.isEmpty(): Boolean = this.length() == 0
public fun String.isBlank(): Boolean = length() == 0 || matches("^[\\s\\xA0]+$")
public fun String.equals(anotherString: String, ignoreCase: Boolean = false): Boolean =
if (!ignoreCase)
public fun String?.equals(anotherString: String?, ignoreCase: Boolean = false): Boolean =
if (this == null)
anotherString == null
else if (!ignoreCase)
this == anotherString
else
this.toLowerCase() == anotherString.toLowerCase()
anotherString != null && this.toLowerCase() == anotherString.toLowerCase()
public fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean {
@@ -34,7 +34,9 @@ internal fun String.nativeLastIndexOf(str: String, fromIndex: Int): Int = (this
*
* @param ignoreCase `true` to ignore character case when comparing strings. By default `false`.
*/
public fun String.equals(anotherString: String, ignoreCase: Boolean = false): Boolean {
public fun String?.equals(anotherString: String?, ignoreCase: Boolean = false): Boolean {
if (this === null)
return anotherString === null
return if (!ignoreCase)
(this as java.lang.String).equals(anotherString)
else
+4
View File
@@ -528,6 +528,10 @@ class StringTest {
@test fun equalsIgnoreCase() {
assertFalse("sample".equals("Sample", ignoreCase = false))
assertTrue("sample".equals("Sample", ignoreCase = true))
assertFalse("sample".equals(null, ignoreCase = false))
assertFalse("sample".equals(null, ignoreCase = true))
assertTrue(null.equals(null, ignoreCase = true))
assertTrue(null.equals(null, ignoreCase = false))
}