diff --git a/libraries/stdlib/src/kotlin/text/ParsePrimitivesJVM.kt b/libraries/stdlib/src/kotlin/text/ParsePrimitivesJVM.kt new file mode 100644 index 00000000000..6aee15ec5fb --- /dev/null +++ b/libraries/stdlib/src/kotlin/text/ParsePrimitivesJVM.kt @@ -0,0 +1,56 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("StringsKt") +@file:kotlin.jvm.JvmVersion +@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") + +package kotlin.text + + +/** + * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. + */ +@kotlin.internal.InlineOnly +public inline fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this) + +/** + * Parses the string as a signed [Byte] number and returns the result. + * @throws NumberFormatException if the string is not a valid representation of a number. + */ +@kotlin.internal.InlineOnly +public inline fun String.toByte(): Byte = java.lang.Byte.parseByte(this) + +/** + * Parses the string as a [Short] number and returns the result. + * @throws NumberFormatException if the string is not a valid representation of a number. + */ +@kotlin.internal.InlineOnly +public inline fun String.toShort(): Short = java.lang.Short.parseShort(this) + +/** + * Parses the string as an [Int] number and returns the result. + * @throws NumberFormatException if the string is not a valid representation of a number. + */ +@kotlin.internal.InlineOnly +public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this) + +/** + * Parses the string as a [Long] number and returns the result. + * @throws NumberFormatException if the string is not a valid representation of a number. + */ +@kotlin.internal.InlineOnly +public inline fun String.toLong(): Long = java.lang.Long.parseLong(this) + +/** + * Parses the string as a [Float] number and returns the result. + * @throws NumberFormatException if the string is not a valid representation of a number. + */ +@kotlin.internal.InlineOnly +public inline fun String.toFloat(): Float = java.lang.Float.parseFloat(this) + +/** + * Parses the string as a [Double] number and returns the result. + * @throws NumberFormatException if the string is not a valid representation of a number. + */ +@kotlin.internal.InlineOnly +public inline fun String.toDouble(): Double = java.lang.Double.parseDouble(this) + diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index e782a342764..b0c73036c4b 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -5,10 +5,9 @@ package kotlin.text -import java.io.StringReader -import java.util.regex.Pattern import java.nio.charset.Charset import java.util.* +import java.util.regex.Pattern /** @@ -366,62 +365,12 @@ public inline fun String.toLowerCase(locale: java.util.Locale): String = (this a @kotlin.internal.InlineOnly public inline fun String.toUpperCase(locale: java.util.Locale): String = (this as java.lang.String).toUpperCase(locale) -/** - * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. - */ -@kotlin.internal.InlineOnly -public inline fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this) - -/** - * Parses the string as a signed [Byte] number and returns the result. - * @throws NumberFormatException if the string is not a valid representation of a number. - */ -@kotlin.internal.InlineOnly -public inline fun String.toByte(): Byte = java.lang.Byte.parseByte(this) - -/** - * Parses the string as a [Short] number and returns the result. - * @throws NumberFormatException if the string is not a valid representation of a number. - */ -@kotlin.internal.InlineOnly -public inline fun String.toShort(): Short = java.lang.Short.parseShort(this) - -/** - * Parses the string as an [Int] number and returns the result. - * @throws NumberFormatException if the string is not a valid representation of a number. - */ -@kotlin.internal.InlineOnly -public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this) - -/** - * Parses the string as a [Long] number and returns the result. - * @throws NumberFormatException if the string is not a valid representation of a number. - */ -@kotlin.internal.InlineOnly -public inline fun String.toLong(): Long = java.lang.Long.parseLong(this) - -/** - * Parses the string as a [Float] number and returns the result. - * @throws NumberFormatException if the string is not a valid representation of a number. - */ -@kotlin.internal.InlineOnly -public inline fun String.toFloat(): Float = java.lang.Float.parseFloat(this) - -/** - * Parses the string as a [Double] number and returns the result. - * @throws NumberFormatException if the string is not a valid representation of a number. - */ -@kotlin.internal.InlineOnly -public inline fun String.toDouble(): Double = java.lang.Double.parseDouble(this) - /** * Encodes the contents of this string using the specified character set and returns the resulting byte array. */ @kotlin.internal.InlineOnly public inline fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray = (this as java.lang.String).getBytes(charset) - - /** * Converts the string into a regular expression [Pattern] optionally * with the specified [flags] from [Pattern] or'd together diff --git a/libraries/stdlib/test/text/ParsePrimitivesJVMTest.kt b/libraries/stdlib/test/text/ParsePrimitivesJVMTest.kt new file mode 100644 index 00000000000..f617c2694af --- /dev/null +++ b/libraries/stdlib/test/text/ParsePrimitivesJVMTest.kt @@ -0,0 +1,33 @@ +@file:kotlin.jvm.JvmVersion +package test.text + +import kotlin.test.assertEquals +import kotlin.test.assertFails +import org.junit.Test as test + +class ParsePrimitivesJVMTest { + + @test fun toBoolean() { + assertEquals(true, "true".toBoolean()) + assertEquals(true, "True".toBoolean()) + assertEquals(false, "false".toBoolean()) + assertEquals(false, "not so true".toBoolean()) + } + + @test fun toByte() { + assertEquals(77.toByte(), "77".toByte()) + assertFails { "255".toByte() } + } + + @test fun toShort() { + assertEquals(77.toShort(), "77".toShort()) + } + + @test fun toInt() { + assertEquals(77, "77".toInt()) + } + + @test fun toLong() { + assertEquals(77.toLong(), "77".toLong()) + } +} diff --git a/libraries/stdlib/test/text/StringJVMTest.kt b/libraries/stdlib/test/text/StringJVMTest.kt index 210d5402dba..898f363962f 100644 --- a/libraries/stdlib/test/text/StringJVMTest.kt +++ b/libraries/stdlib/test/text/StringJVMTest.kt @@ -2,7 +2,7 @@ package test.text import test.collections.assertArrayNotSameButEquals -import java.util.Locale +import java.util.* import kotlin.test.* import org.junit.Test