diff --git a/js/js.libraries/src/core/stringsCode.kt b/js/js.libraries/src/core/stringsCode.kt index bf5ef406ea8..852ba5f0486 100644 --- a/js/js.libraries/src/core/stringsCode.kt +++ b/js/js.libraries/src/core/stringsCode.kt @@ -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 { diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index e4288150cc7..575445ff693 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -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 diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index ada215d34e9..d6762d42447 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -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)) }