Provide unsigned number to string conversion in arbitrary base
#KT-26161
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. 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 [Byte] value in the specified [radix].
|
||||
*
|
||||
* @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
//@kotlin.internal.InlineOnly
|
||||
public /*inline*/ fun UByte.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.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
//@kotlin.internal.InlineOnly
|
||||
public /*inline*/ fun UShort.toString(radix: Int): String = this.toInt().toString(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.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
//@kotlin.internal.InlineOnly
|
||||
public /*inline*/ fun UInt.toString(radix: Int): String = this.toLong().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.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun ULong.toString(radix: Int): String = ulongToString(this.toLong(), checkRadix(radix))
|
||||
|
||||
|
||||
@@ -60,8 +60,7 @@ internal fun ulongRemainder(v1: ULong, v2: ULong): ULong {
|
||||
internal fun ulongToString(v: Long): String = ulongToString(v, 10)
|
||||
|
||||
internal fun ulongToString(v: Long, base: Int): String {
|
||||
require(base == 10) // TODO: toString(base) support in common
|
||||
if (v >= 0) return v.toString(/* base */)
|
||||
if (v >= 0) return v.toString(base)
|
||||
|
||||
var quotient = ((v ushr 1) / base) shl 1
|
||||
var rem = v - quotient * base
|
||||
@@ -69,6 +68,6 @@ internal fun ulongToString(v: Long, base: Int): String {
|
||||
rem -= base
|
||||
quotient += 1
|
||||
}
|
||||
return quotient.toString(/* base */) + rem.toString(/* base */)
|
||||
return quotient.toString(base) + rem.toString(base)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user