[JS IR] stdlib: Fix bugs in Long.toString(radix)

This commit is contained in:
Svyatoslav Kuzmich
2019-05-05 18:54:33 +03:00
parent bc29a5b15c
commit e3bcabeae3
6 changed files with 36 additions and 13 deletions
+1 -1
View File
@@ -259,5 +259,5 @@ public class Long internal constructor(
override fun hashCode(): Int = hashCode(this)
override fun toString(): String = toString(10)
override fun toString(): String = this.toStringImpl(radix = 10)
}
+5 -4
View File
@@ -24,7 +24,7 @@ internal fun Long.getLowBitsUnsigned() = if (low >= 0) low.toDouble() else TWO_P
internal fun hashCode(l: Long) = l.low xor l.high
internal fun Long.toString(radix: Int): String {
internal fun Long.toStringImpl(radix: Int): String {
if (radix < 2 || 36 < radix) {
throw Exception("radix out of range: $radix")
}
@@ -39,10 +39,11 @@ internal fun Long.toString(radix: Int): String {
// the bottom-most digit in this base and then recurse to do the rest.
val radixLong = fromInt(radix)
val div = div(radixLong)
val rem = div.multiply(radixLong).subtract(this).toInt();
return js("div.toString(radix) + rem.toString(radix)")
val rem = div.multiply(radixLong).subtract(this).toInt()
// Using rem.asDynamic() to break dependency on "kotlin.text" package
return div.toStringImpl(radix) + rem.asDynamic().toString(radix).unsafeCast<String>()
} else {
return "-${negate().toString()}"
return "-${negate().toStringImpl(radix)}"
}
}
@@ -0,0 +1,15 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.text
/**
* 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 =
this.toStringImpl(checkRadix(radix))
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.text
/**
* 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))
@@ -113,14 +113,6 @@ public actual inline fun Byte.toString(radix: Int): String = this.toInt().toStri
@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].
*