From 34c6abe0bdf45e4d3ce0081453b1f79e29efeda3 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Thu, 18 Nov 2021 17:44:01 +0100 Subject: [PATCH] [WASM] Add Long.toString(radix) implementation --- libraries/stdlib/wasm/src/kotlin/Math.kt | 17 ++++++++---- libraries/stdlib/wasm/src/kotlin/Text.kt | 35 +++++++++++++++++++++--- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/libraries/stdlib/wasm/src/kotlin/Math.kt b/libraries/stdlib/wasm/src/kotlin/Math.kt index 7a6e665663f..6113e123528 100644 --- a/libraries/stdlib/wasm/src/kotlin/Math.kt +++ b/libraries/stdlib/wasm/src/kotlin/Math.kt @@ -939,9 +939,11 @@ public actual val Int.absoluteValue: Int get() = TODO("Wasm stdlib: Math") * - `1` if the value is positive */ @SinceKotlin("1.2") -public actual val Int.sign: Int get() = TODO("Wasm stdlib: Math") - - +public actual val Int.sign: Int get() = when { + this < 0 -> -1 + this > 0 -> 1 + else -> 0 +} /** * Returns the absolute value of the given value [n]. @@ -952,7 +954,7 @@ public actual val Int.sign: Int get() = TODO("Wasm stdlib: Math") * @see absoluteValue extension property for [Long] */ @SinceKotlin("1.2") -public actual fun abs(n: Long): Long = TODO("Wasm stdlib: Math") +public actual fun abs(n: Long): Long = if (n >= 0) n else -n /** * Returns the smaller of two values. @@ -984,7 +986,10 @@ public actual val Long.absoluteValue: Long get() = TODO("Wasm stdlib: Math") * - `1` if the value is positive */ @SinceKotlin("1.2") -public actual val Long.sign: Int get() = TODO("Wasm stdlib: Math") - +public actual val Long.sign: Int get() = when { + this < 0 -> -1 + this > 0 -> 1 + else -> 0 +} // endregion diff --git a/libraries/stdlib/wasm/src/kotlin/Text.kt b/libraries/stdlib/wasm/src/kotlin/Text.kt index 48eb7a0703e..4e6fc6a8a39 100644 --- a/libraries/stdlib/wasm/src/kotlin/Text.kt +++ b/libraries/stdlib/wasm/src/kotlin/Text.kt @@ -5,6 +5,8 @@ package kotlin.text +import kotlin.math.abs + actual class Regex { actual constructor(pattern: String) { TODO("Wasm stdlib: Text") } actual constructor(pattern: String, option: RegexOption) { TODO("Wasm stdlib: Text") } @@ -900,7 +902,7 @@ public actual fun String.toDoubleOrNull(): Double? { * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. */ @SinceKotlin("1.2") -public actual fun Byte.toString(radix: Int): String = this.toInt().toString(radix) +public actual fun Byte.toString(radix: Int): String = this.toLong().toString(radix) /** * Returns a string representation of this [Short] value in the specified [radix]. @@ -908,7 +910,7 @@ public actual fun Byte.toString(radix: Int): String = this.toInt().toString(radi * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. */ @SinceKotlin("1.2") -public actual fun Short.toString(radix: Int): String = this.toInt().toString(radix) +public actual fun Short.toString(radix: Int): String = this.toLong().toString(radix) /** * Returns a string representation of this [Int] value in the specified [radix]. @@ -916,7 +918,7 @@ public actual fun Short.toString(radix: Int): String = this.toInt().toString(rad * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. */ @SinceKotlin("1.2") -actual fun Int.toString(radix: Int): String = TODO("Wasm stdlib: Text") +actual fun Int.toString(radix: Int): String = toLong().toString(radix) /** * Returns a string representation of this [Long] value in the specified [radix]. @@ -924,7 +926,32 @@ actual fun Int.toString(radix: Int): String = TODO("Wasm stdlib: Text") * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. */ @SinceKotlin("1.2") -actual fun Long.toString(radix: Int): String = TODO("Wasm stdlib: Text") +actual fun Long.toString(radix: Int): String { + checkRadix(radix) + + fun Long.getChar() = toInt().let { if (it < 10) '0' + it else 'a' + (it - 10) } + + if (radix == 10) return toString() + if (this in 0 until radix) return getChar().toString() + + val isNegative = this < 0 + val buffer = CharArray(Long.SIZE_BITS + 1) + + var currentBufferIndex= buffer.lastIndex + var current: Long = this + while(current != 0L) { + buffer[currentBufferIndex] = abs(current % radix).getChar() + current /= radix + currentBufferIndex-- + } + + if (isNegative) { + buffer[currentBufferIndex] = '-' + currentBufferIndex-- + } + + return buffer.concatToString(currentBufferIndex + 1) +} @PublishedApi internal actual fun checkRadix(radix: Int): Int {