diff --git a/libraries/stdlib/common/src/kotlin/TextH.kt b/libraries/stdlib/common/src/kotlin/TextH.kt index 40cb8d596fb..1fec8957928 100644 --- a/libraries/stdlib/common/src/kotlin/TextH.kt +++ b/libraries/stdlib/common/src/kotlin/TextH.kt @@ -5,6 +5,8 @@ package kotlin.text +import kotlin.internal.LowPriorityInOverloadResolution + expect class Regex { constructor(pattern: String) constructor(pattern: String, option: RegexOption) @@ -282,9 +284,17 @@ public expect val String.Companion.CASE_INSENSITIVE_ORDER: Comparator /** - * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. + * Returns `true` if the content of this string is equal to the word "true", ignoring case, and `false` otherwise. */ -expect fun String.toBoolean(): Boolean +@LowPriorityInOverloadResolution +@kotlin.internal.InlineOnly +public inline fun String.toBoolean(): Boolean = this.toBoolean() + +/** + * Returns `true` if this string is not `null` and its content is equal to the word "true", ignoring case, and `false` otherwise. + */ +@SinceKotlin("1.4") +public expect fun String?.toBoolean(): Boolean /** * Parses the string as a signed [Byte] number and returns the result. diff --git a/libraries/stdlib/jdk7/test/UseAutoCloseableResourceTest.kt b/libraries/stdlib/jdk7/test/UseAutoCloseableResourceTest.kt index 3f58d8b3ef1..86741b098e7 100644 --- a/libraries/stdlib/jdk7/test/UseAutoCloseableResourceTest.kt +++ b/libraries/stdlib/jdk7/test/UseAutoCloseableResourceTest.kt @@ -5,14 +5,12 @@ package kotlin.jdk7.test +import test.platformNull import java.io.* -import java.util.* import kotlin.test.* class UseAutoCloseableResourceTest { - @Suppress("HasPlatformType", "UNCHECKED_CAST") fun platformNull() = Collections.singletonList(null as T).first() - class Resource(val faultyClose: Boolean = false) : AutoCloseable { var isClosed = false diff --git a/libraries/stdlib/jdk7/test/UseCloseableResourceTest.kt b/libraries/stdlib/jdk7/test/UseCloseableResourceTest.kt index 2a2e3a92ea2..cbd9ccb3170 100644 --- a/libraries/stdlib/jdk7/test/UseCloseableResourceTest.kt +++ b/libraries/stdlib/jdk7/test/UseCloseableResourceTest.kt @@ -5,14 +5,12 @@ package kotlin.jdk7.test +import test.platformNull import java.io.* -import java.util.* import kotlin.test.* class UseCloseableResourceTest { - @Suppress("HasPlatformType", "UNCHECKED_CAST") fun platformNull() = Collections.singletonList(null as T).first() - class Resource(val faultyClose: Boolean = false) : Closeable { var isClosed = false diff --git a/libraries/stdlib/js/src/kotlin/text/numberConversions.kt b/libraries/stdlib/js/src/kotlin/text/numberConversions.kt index c701aa85c29..ead48a0f658 100644 --- a/libraries/stdlib/js/src/kotlin/text/numberConversions.kt +++ b/libraries/stdlib/js/src/kotlin/text/numberConversions.kt @@ -7,9 +7,10 @@ package kotlin.text /** - * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. + * Returns `true` if this string is not `null` and its content is equal to the word "true", ignoring case, and `false` otherwise. */ -public actual fun String.toBoolean(): Boolean = toLowerCase() == "true" +@SinceKotlin("1.4") +public actual fun String?.toBoolean(): Boolean = this != null && this.toLowerCase() == "true" /** * Parses the string as a signed [Byte] number and returns the result. diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt index 878a7434ce9..91e9b37f97d 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt @@ -46,10 +46,12 @@ public actual inline fun Int.toString(radix: Int): String = java.lang.Integer.to public actual inline fun Long.toString(radix: Int): String = java.lang.Long.toString(this, checkRadix(radix)) /** - * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. + * Returns `true` if this string is not `null` and its content is equal to the word "true", ignoring case, and `false` otherwise. */ +@JvmName("toBooleanNullable") +@SinceKotlin("1.4") @kotlin.internal.InlineOnly -public actual inline fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this) +public actual inline fun String?.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this) /** * Parses the string as a signed [Byte] number and returns the result. diff --git a/libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt b/libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt index bc433134531..489b13434b2 100644 --- a/libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt +++ b/libraries/stdlib/jvm/test/io/UseCloseableResourceTest.kt @@ -5,8 +5,8 @@ package test.io +import test.platformNull import java.io.* -import java.util.* import kotlin.test.* class UseCloseableResourceTest { @@ -23,9 +23,6 @@ class UseCloseableResourceTest { assertEquals("World", secondLine) } - - @Suppress("HasPlatformType", "UNCHECKED_CAST") fun platformNull() = Collections.singletonList(null as T).first() - class Resource(val faultyClose: Boolean = false) : Closeable { var isClosed = false diff --git a/libraries/stdlib/jvm/test/text/StringJVMTest.kt b/libraries/stdlib/jvm/test/text/StringJVMTest.kt index 73d81f58445..84b6ea48d35 100644 --- a/libraries/stdlib/jvm/test/text/StringJVMTest.kt +++ b/libraries/stdlib/jvm/test/text/StringJVMTest.kt @@ -104,4 +104,19 @@ class StringJVMTest { assertEquals("dzdzdz", "DZdzdz".decapitalize(Locale.US)) assertEquals("dzdzdz", "Dzdzdz".decapitalize(Locale.US)) } + + @Test + fun stringToBoolean() { + assertFalse(platformNull().toBoolean()) + } + + @Test + fun stringEquals() { + assertFalse(platformNull().equals("sample", ignoreCase = false)) + assertFalse(platformNull().equals("sample", ignoreCase = true)) + assertFalse("sample".equals(platformNull(), ignoreCase = false)) + assertFalse("sample".equals(platformNull(), ignoreCase = true)) + assertTrue(platformNull().equals(platformNull(), ignoreCase = true)) + assertTrue(platformNull().equals(platformNull(), ignoreCase = false)) + } } diff --git a/libraries/stdlib/test/text/StringNumberConversionTest.kt b/libraries/stdlib/test/text/StringNumberConversionTest.kt index 82c758cee71..adc6d60ff9b 100644 --- a/libraries/stdlib/test/text/StringNumberConversionTest.kt +++ b/libraries/stdlib/test/text/StringNumberConversionTest.kt @@ -15,6 +15,7 @@ class StringNumberConversionTest { assertEquals(true, "True".toBoolean()) assertEquals(false, "false".toBoolean()) assertEquals(false, "not so true".toBoolean()) + assertEquals(false, (null as String?).toBoolean()) } @Test fun toByte() {