From d7bada0c29e02f1dc9e72cede9f33ebb41fdfe17 Mon Sep 17 00:00:00 2001 From: Florian Steitz Date: Tue, 1 May 2018 11:07:37 +0200 Subject: [PATCH] [KT-10456] Added JS implementation for Int.toString(radix) and similar overloads --- libraries/stdlib/common/src/kotlin/TextH.kt | 31 +++++++++++++++++ .../stdlib/js/src/kotlin/numberConversions.kt | 33 +++++++++++++++++++ .../kotlin/text/StringNumberConversionsJVM.kt | 8 ++--- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/libraries/stdlib/common/src/kotlin/TextH.kt b/libraries/stdlib/common/src/kotlin/TextH.kt index 54a44fe36d0..d6f9a4d1d66 100644 --- a/libraries/stdlib/common/src/kotlin/TextH.kt +++ b/libraries/stdlib/common/src/kotlin/TextH.kt @@ -234,6 +234,37 @@ expect fun String.toDoubleOrNull(): Double? */ expect fun String.toFloatOrNull(): Float? +/** + * Returns a string representation of this [Byte] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +expect fun Byte.toString(radix: Int): String + +/** + * Returns a string representation of this [Short] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +expect fun Short.toString(radix: Int): String + +/** + * Returns a string representation of this [Int] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +expect fun Int.toString(radix: Int): String + +/** + * Returns a string representation of this [Long] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +expect fun Long.toString(radix: Int): String @PublishedApi internal expect fun checkRadix(radix: Int): Int diff --git a/libraries/stdlib/js/src/kotlin/numberConversions.kt b/libraries/stdlib/js/src/kotlin/numberConversions.kt index 1f9d771b59a..69b66185762 100644 --- a/libraries/stdlib/js/src/kotlin/numberConversions.kt +++ b/libraries/stdlib/js/src/kotlin/numberConversions.kt @@ -95,6 +95,39 @@ public actual fun String.toDoubleOrNull(): Double? = (+(this.asDynamic())).unsaf @kotlin.internal.InlineOnly public actual inline fun String.toFloatOrNull(): Float? = toDoubleOrNull().unsafeCast() +/** + * Returns a string representation of this [Byte] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public actual inline fun Byte.toString(radix: Int): String = this.toInt().toString(radix) + +/** + * Returns a string representation of this [Short] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public actual inline fun Short.toString(radix: Int): String = this.toInt().toString(radix) + +/** + * Returns a string representation of this [Long] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +public actual fun Long.toString(radix: Int): String = asDynamic().toString(checkRadix(radix)) + +/** + * Returns a string representation of this [Int] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +public actual fun Int.toString(radix: Int): String = asDynamic().toString(checkRadix(radix)) private fun String.isNaN(): Boolean = when (this.toLowerCase()) { "nan", "+nan", "-nan" -> true diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt index b039a67b128..a3721302e17 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringNumberConversionsJVM.kt @@ -19,7 +19,7 @@ import kotlin.* */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly -public inline fun Byte.toString(radix: Int): String = this.toInt().toString(checkRadix(radix)) +public actual inline fun Byte.toString(radix: Int): String = this.toInt().toString(checkRadix(radix)) /** * Returns a string representation of this [Short] value in the specified [radix]. @@ -28,7 +28,7 @@ public inline fun Byte.toString(radix: Int): String = this.toInt().toString(chec */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly -public inline fun Short.toString(radix: Int): String = this.toInt().toString(checkRadix(radix)) +public actual inline fun Short.toString(radix: Int): String = this.toInt().toString(checkRadix(radix)) /** * Returns a string representation of this [Int] value in the specified [radix]. @@ -37,7 +37,7 @@ public inline fun Short.toString(radix: Int): String = this.toInt().toString(che */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly -public inline fun Int.toString(radix: Int): String = java.lang.Integer.toString(this, checkRadix(radix)) +public actual inline fun Int.toString(radix: Int): String = java.lang.Integer.toString(this, checkRadix(radix)) /** * Returns a string representation of this [Long] value in the specified [radix]. @@ -46,7 +46,7 @@ public inline fun Int.toString(radix: Int): String = java.lang.Integer.toString( */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly -public inline fun Long.toString(radix: Int): String = java.lang.Long.toString(this, checkRadix(radix)) +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.