Allow equals with ignoreCase parameter to take nullable Strings both as a receiver and a parameter.
#KT-9188
This commit is contained in:
@@ -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.isBlank(): Boolean = length() == 0 || matches("^[\\s\\xA0]+$")
|
||||||
|
|
||||||
public fun String.equals(anotherString: String, ignoreCase: Boolean = false): Boolean =
|
public fun String?.equals(anotherString: String?, ignoreCase: Boolean = false): Boolean =
|
||||||
if (!ignoreCase)
|
if (this == null)
|
||||||
|
anotherString == null
|
||||||
|
else if (!ignoreCase)
|
||||||
this == anotherString
|
this == anotherString
|
||||||
else
|
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 {
|
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`.
|
* @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)
|
return if (!ignoreCase)
|
||||||
(this as java.lang.String).equals(anotherString)
|
(this as java.lang.String).equals(anotherString)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -528,6 +528,10 @@ class StringTest {
|
|||||||
@test fun equalsIgnoreCase() {
|
@test fun equalsIgnoreCase() {
|
||||||
assertFalse("sample".equals("Sample", ignoreCase = false))
|
assertFalse("sample".equals("Sample", ignoreCase = false))
|
||||||
assertTrue("sample".equals("Sample", ignoreCase = true))
|
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user