Provide unsigned number to string conversion in arbitrary base
#KT-26161
This commit is contained in:
@@ -190,6 +190,54 @@ class StringNumberConversionTest {
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 37") { 37L.toString(radix = 37) }
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 1") { 1L.toString(radix = 1) }
|
||||
}
|
||||
|
||||
@Test fun ubyteToStringWithRadix() {
|
||||
assertEquals("7a", 0x7a.toUByte().toString(16))
|
||||
assertEquals("80", Byte.MIN_VALUE.toUByte().toString(radix = 16))
|
||||
assertEquals("ff", UByte.MAX_VALUE.toString(radix = 16))
|
||||
|
||||
assertEquals("40", Byte.MIN_VALUE.toUByte().toString(radix = 32))
|
||||
assertEquals("7v", UByte.MAX_VALUE.toString(radix = 32))
|
||||
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 37") { 37.toUByte().toString(radix = 37) }
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 1") { 1.toUByte().toString(radix = 1) }
|
||||
}
|
||||
|
||||
@Test fun ushortToStringWithRadix() {
|
||||
assertEquals("7FFF", 0x7FFF.toUShort().toString(radix = 16).toUpperCase())
|
||||
assertEquals("8000", 0x8000.toUShort().toString(radix = 16))
|
||||
assertEquals("ffff", UShort.MAX_VALUE.toString(radix = 16))
|
||||
|
||||
assertEquals("1ekf", UShort.MAX_VALUE.toString(radix = 36))
|
||||
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 37") { 37.toUShort().toString(radix = 37) }
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 1") { 1.toUShort().toString(radix = 1) }
|
||||
}
|
||||
|
||||
@Test fun uintToStringWithRadix() {
|
||||
assertEquals("ffffff01", (-255).toUInt().toString(radix = 16))
|
||||
assertEquals("ffffffff", UInt.MAX_VALUE.toString(radix = 16))
|
||||
|
||||
assertEquals("1100110", 102u.toString(radix = 2))
|
||||
assertEquals("kona", 411787u.toString(radix = 27))
|
||||
assertEquals("3vvvvvv", UInt.MAX_VALUE.toString(radix = 32))
|
||||
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 37") { 37u.toString(radix = 37) }
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 1") { 1u.toString(radix = 1) }
|
||||
|
||||
}
|
||||
|
||||
@Test fun ulongToStringWithRadix() {
|
||||
assertEquals("7f11223344556677", 0x7F11223344556677u.toString(radix = 16))
|
||||
assertEquals("89aabbccddeeff11", 0x89AABBCCDDEEFF11u.toString(radix = 16))
|
||||
assertEquals("8000000000000000", Long.MIN_VALUE.toULong().toString(radix = 16))
|
||||
assertEquals("ffffffffffffffff", ULong.MAX_VALUE.toString(radix = 16))
|
||||
|
||||
assertEquals("hazelnut", 1356099454469uL.toString(radix = 36))
|
||||
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 37") { 37uL.toString(radix = 37) }
|
||||
assertFailsWith<IllegalArgumentException>("Expected to fail with radix 1") { 1uL.toString(radix = 1) }
|
||||
}
|
||||
}
|
||||
|
||||
internal fun doubleTotalOrderEquals(a: Double?, b: Double?): Boolean = (a as Any?) == b
|
||||
|
||||
@@ -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