Strict version of String.toBoolean() #KT-42071
This commit is contained in:
@@ -943,6 +943,12 @@ public inline fun kotlin.String.toBoolean(): kotlin.Boolean
|
|||||||
@kotlin.SinceKotlin(version = "1.4")
|
@kotlin.SinceKotlin(version = "1.4")
|
||||||
public fun kotlin.String?.toBoolean(): kotlin.Boolean
|
public fun kotlin.String?.toBoolean(): kotlin.Boolean
|
||||||
|
|
||||||
|
@kotlin.SinceKotlin(version = "1.5")
|
||||||
|
public fun kotlin.String.toBooleanStrict(): kotlin.Boolean
|
||||||
|
|
||||||
|
@kotlin.SinceKotlin(version = "1.5")
|
||||||
|
public fun kotlin.String.toBooleanStrictOrNull(): kotlin.Boolean?
|
||||||
|
|
||||||
public fun kotlin.String.toByte(): kotlin.Byte
|
public fun kotlin.String.toByte(): kotlin.Byte
|
||||||
|
|
||||||
public fun kotlin.String.toByte(radix: kotlin.Int): kotlin.Byte
|
public fun kotlin.String.toByte(radix: kotlin.Int): kotlin.Byte
|
||||||
|
|||||||
@@ -943,6 +943,12 @@ public inline fun kotlin.String.toBoolean(): kotlin.Boolean
|
|||||||
@kotlin.SinceKotlin(version = "1.4")
|
@kotlin.SinceKotlin(version = "1.4")
|
||||||
public fun kotlin.String?.toBoolean(): kotlin.Boolean
|
public fun kotlin.String?.toBoolean(): kotlin.Boolean
|
||||||
|
|
||||||
|
@kotlin.SinceKotlin(version = "1.5")
|
||||||
|
public fun kotlin.String.toBooleanStrict(): kotlin.Boolean
|
||||||
|
|
||||||
|
@kotlin.SinceKotlin(version = "1.5")
|
||||||
|
public fun kotlin.String.toBooleanStrictOrNull(): kotlin.Boolean?
|
||||||
|
|
||||||
public fun kotlin.String.toByte(): kotlin.Byte
|
public fun kotlin.String.toByte(): kotlin.Byte
|
||||||
|
|
||||||
public fun kotlin.String.toByte(radix: kotlin.Int): kotlin.Byte
|
public fun kotlin.String.toByte(radix: kotlin.Int): kotlin.Byte
|
||||||
|
|||||||
@@ -279,6 +279,8 @@ public expect fun String.toBoolean(): Boolean
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns `true` if this string is not `null` and its content 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.
|
||||||
|
*
|
||||||
|
* There are also strict versions of the function available on non-nullable String, [toBooleanStrict] and [toBooleanStrictOrNull].
|
||||||
*/
|
*/
|
||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
public expect fun String?.toBoolean(): Boolean
|
public expect fun String?.toBoolean(): Boolean
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ public actual 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.
|
* Returns `true` if this string is not `null` and its content is equal to the word "true", ignoring case, and `false` otherwise.
|
||||||
|
*
|
||||||
|
* There are also strict versions of the function available on non-nullable String, [toBooleanStrict] and [toBooleanStrictOrNull].
|
||||||
*/
|
*/
|
||||||
@OptIn(ExperimentalStdlibApi::class)
|
@OptIn(ExperimentalStdlibApi::class)
|
||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ public actual 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.
|
* Returns `true` if this string is not `null` and its content is equal to the word "true", ignoring case, and `false` otherwise.
|
||||||
|
*
|
||||||
|
* There are also strict versions of the function available on non-nullable String, [toBooleanStrict] and [toBooleanStrictOrNull].
|
||||||
*/
|
*/
|
||||||
@JvmName("toBooleanNullable")
|
@JvmName("toBooleanNullable")
|
||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
|
|||||||
@@ -467,4 +467,30 @@ class Strings {
|
|||||||
assertFalse("Kotlin".contentEquals(stringBuilder))
|
assertFalse("Kotlin".contentEquals(stringBuilder))
|
||||||
assertTrue("Kotlin".contentEquals(stringBuilder, ignoreCase = true))
|
assertTrue("Kotlin".contentEquals(stringBuilder, ignoreCase = true))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun toBooleanStrict() {
|
||||||
|
assertPrints("true".toBooleanStrict(), "true")
|
||||||
|
assertFails { "True".toBooleanStrict() }
|
||||||
|
assertFails { "TRUE".toBooleanStrict() }
|
||||||
|
|
||||||
|
assertPrints("false".toBooleanStrict(), "false")
|
||||||
|
assertFails { "False".toBooleanStrict() }
|
||||||
|
assertFails { "FALSE".toBooleanStrict() }
|
||||||
|
|
||||||
|
assertFails { "abc".toBooleanStrict() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun toBooleanStrictOrNull() {
|
||||||
|
assertPrints("true".toBooleanStrictOrNull(), "true")
|
||||||
|
assertPrints("True".toBooleanStrictOrNull(), "null")
|
||||||
|
assertPrints("TRUE".toBooleanStrictOrNull(), "null")
|
||||||
|
|
||||||
|
assertPrints("false".toBooleanStrictOrNull(), "false")
|
||||||
|
assertPrints("False".toBooleanStrictOrNull(), "null")
|
||||||
|
assertPrints("FALSE".toBooleanStrictOrNull(), "null")
|
||||||
|
|
||||||
|
assertPrints("abc".toBooleanStrictOrNull(), "null")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1431,3 +1431,35 @@ internal fun CharSequence?.contentEqualsImpl(other: CharSequence?): Boolean {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the content of this string is equal to the word "true", `false` if it is equal to "false",
|
||||||
|
* and throws an exception otherwise.
|
||||||
|
*
|
||||||
|
* There is also a lenient version of the function available on nullable String, [String?.toBoolean].
|
||||||
|
* Note that this function is case-sensitive.
|
||||||
|
*
|
||||||
|
* @sample samples.text.Strings.toBooleanStrict
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.5")
|
||||||
|
public fun String.toBooleanStrict(): Boolean = when (this) {
|
||||||
|
"true" -> true
|
||||||
|
"false" -> false
|
||||||
|
else -> throw IllegalArgumentException("The string doesn't represent a boolean value: $this")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the content of this string is equal to the word "true", `false` if it is equal to "false",
|
||||||
|
* and `null` otherwise.
|
||||||
|
*
|
||||||
|
* There is also a lenient version of the function available on nullable String, [String?.toBoolean].
|
||||||
|
* Note that this function is case-sensitive.
|
||||||
|
*
|
||||||
|
* @sample samples.text.Strings.toBooleanStrictOrNull
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.5")
|
||||||
|
public fun String.toBooleanStrictOrNull(): Boolean? = when (this) {
|
||||||
|
"true" -> true
|
||||||
|
"false" -> false
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
@@ -493,6 +493,8 @@ actual fun String.toBoolean(): Boolean = TODO("Wasm stdlib: Text")
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
|
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
|
||||||
|
*
|
||||||
|
* There are also strict versions of the function available on non-nullable String, [toBooleanStrict] and [toBooleanStrictOrNull].
|
||||||
*/
|
*/
|
||||||
actual fun String?.toBoolean(): Boolean = TODO("Wasm stdlib: Text")
|
actual fun String?.toBoolean(): Boolean = TODO("Wasm stdlib: Text")
|
||||||
|
|
||||||
|
|||||||
+2
@@ -5516,6 +5516,8 @@ public final class kotlin/text/StringsKt {
|
|||||||
public static final fun toBigDecimalOrNull (Ljava/lang/String;Ljava/math/MathContext;)Ljava/math/BigDecimal;
|
public static final fun toBigDecimalOrNull (Ljava/lang/String;Ljava/math/MathContext;)Ljava/math/BigDecimal;
|
||||||
public static final fun toBigIntegerOrNull (Ljava/lang/String;)Ljava/math/BigInteger;
|
public static final fun toBigIntegerOrNull (Ljava/lang/String;)Ljava/math/BigInteger;
|
||||||
public static final fun toBigIntegerOrNull (Ljava/lang/String;I)Ljava/math/BigInteger;
|
public static final fun toBigIntegerOrNull (Ljava/lang/String;I)Ljava/math/BigInteger;
|
||||||
|
public static final fun toBooleanStrict (Ljava/lang/String;)Z
|
||||||
|
public static final fun toBooleanStrictOrNull (Ljava/lang/String;)Ljava/lang/Boolean;
|
||||||
public static final fun toByteOrNull (Ljava/lang/String;)Ljava/lang/Byte;
|
public static final fun toByteOrNull (Ljava/lang/String;)Ljava/lang/Byte;
|
||||||
public static final fun toByteOrNull (Ljava/lang/String;I)Ljava/lang/Byte;
|
public static final fun toByteOrNull (Ljava/lang/String;I)Ljava/lang/Byte;
|
||||||
public static final fun toCharArray (Ljava/lang/String;II)[C
|
public static final fun toCharArray (Ljava/lang/String;II)[C
|
||||||
|
|||||||
Reference in New Issue
Block a user