Add number to string conversion with the specified radix.
#KT-8286
This commit is contained in:
@@ -5,6 +5,34 @@
|
|||||||
|
|
||||||
package kotlin.text
|
package kotlin.text
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string representation of this [Byte] value in the specified [radix].
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
@kotlin.internal.InlineOnly
|
||||||
|
public inline fun Byte.toString(radix: Int): String = this.toInt().toString(radix)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string representation of this [Short] value in the specified [radix].
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
@kotlin.internal.InlineOnly
|
||||||
|
public inline fun Short.toString(radix: Int): String = this.toInt().toString(radix)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string representation of this [Int] value in the specified [radix].
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
@kotlin.internal.InlineOnly
|
||||||
|
// TODO: use checkRadix(radix) when PublishedApi is available
|
||||||
|
public inline fun Int.toString(radix: Int): String = java.lang.Integer.toString(this, radix)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string representation of this [Long] value in the specified [radix].
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.1")
|
||||||
|
@kotlin.internal.InlineOnly
|
||||||
|
public inline fun Long.toString(radix: Int): String = java.lang.Long.toString(this, radix)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
|
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
|
||||||
@@ -25,6 +53,7 @@ public inline fun String.toByte(): Byte = java.lang.Byte.parseByte(this)
|
|||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
|
// TODO: use checkRadix(radix) when PublishedApi is available
|
||||||
public inline fun String.toByte(radix: Int): Byte = java.lang.Byte.parseByte(this, radix)
|
public inline fun String.toByte(radix: Int): Byte = java.lang.Byte.parseByte(this, radix)
|
||||||
|
|
||||||
|
|
||||||
@@ -295,8 +324,11 @@ private inline fun <T> screenFloatValue(str: String, parse: (String) -> T): T? {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal fun digitOf(char: Char, radix: Int): Int = Character.digit(char.toInt(), radix)
|
internal fun digitOf(char: Char, radix: Int): Int = Character.digit(char.toInt(), radix)
|
||||||
internal fun checkRadix(radix: Int) {
|
|
||||||
|
// TODO: @PublishedApi
|
||||||
|
internal fun checkRadix(radix: Int): Int {
|
||||||
if(radix !in Character.MIN_RADIX..Character.MAX_RADIX) {
|
if(radix !in Character.MIN_RADIX..Character.MAX_RADIX) {
|
||||||
throw IllegalArgumentException("radix $radix was not in valid range ${Character.MIN_RADIX..Character.MAX_RADIX}")
|
throw IllegalArgumentException("radix $radix was not in valid range ${Character.MIN_RADIX..Character.MAX_RADIX}")
|
||||||
}
|
}
|
||||||
|
return radix
|
||||||
}
|
}
|
||||||
@@ -155,6 +155,40 @@ class ParsePrimitivesJVMTest {
|
|||||||
assertFailsOrNull("007 not a number")
|
assertFailsOrNull("007 not a number")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test fun byteToStringWithRadix() {
|
||||||
|
assertEquals("7a", 0x7a.toByte().toString(16))
|
||||||
|
assertEquals("-80", Byte.MIN_VALUE.toString(radix = 16))
|
||||||
|
assertEquals("3v", Byte.MAX_VALUE.toString(radix = 32))
|
||||||
|
assertEquals("-40", Byte.MIN_VALUE.toString(radix = 32))
|
||||||
|
// TODO: IllegalArgumentException on incorrect radix
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun shortToStringWithRadix() {
|
||||||
|
assertEquals("7FFF", 0x7FFF.toShort().toString(radix = 16).toUpperCase())
|
||||||
|
assertEquals("-8000", (-0x8000).toShort().toString(radix = 16))
|
||||||
|
assertEquals("-sfs", (-29180).toShort().toString(radix = 32))
|
||||||
|
|
||||||
|
// TODO: IllegalArgumentException on incorrect radix
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun intToStringWithRadix() {
|
||||||
|
assertEquals("-ff", (-255).toString(radix = 16))
|
||||||
|
assertEquals("1100110", 102.toString(radix = 2))
|
||||||
|
assertEquals("kona", 411787.toString(radix = 27))
|
||||||
|
// TODO: IllegalArgumentException on incorrect radix
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun longToStringWithRadix() {
|
||||||
|
assertEquals("7f11223344556677", 0x7F11223344556677.toString(radix = 16))
|
||||||
|
assertEquals("hazelnut", 1356099454469L.toString(radix = 36))
|
||||||
|
assertEquals("-8000000000000000", Long.MIN_VALUE.toString(radix = 16))
|
||||||
|
|
||||||
|
// TODO: IllegalArgumentException on incorrect radix
|
||||||
|
// assertFailsWith<IllegalArgumentException>("Expected to fail with radix 37") { 37.toString(radix = 37) }
|
||||||
|
// assertFailsWith<IllegalArgumentException>("Expected to fail with radix 1") { 1.toString(radix = 1) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -181,7 +215,7 @@ private class ConversionContext<T: Any>(val convertOrFail: (String) -> T,
|
|||||||
|
|
||||||
fun assertFailsOrNull(input: String) {
|
fun assertFailsOrNull(input: String) {
|
||||||
assertFailsWith<NumberFormatException>("Expected to fail on input \"$input\"") { convertOrFail(input) }
|
assertFailsWith<NumberFormatException>("Expected to fail on input \"$input\"") { convertOrFail(input) }
|
||||||
assertNull (convertOrNull(input), message = "On input \"$input\"")
|
assertNull(convertOrNull(input), message = "On input \"$input\"")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,7 +230,7 @@ private class ConversionWithRadixContext<T: Any>(val convertOrFail: String.(Int)
|
|||||||
assertFailsWith<NumberFormatException>("Expected to fail on input \"$input\" with radix $radix",
|
assertFailsWith<NumberFormatException>("Expected to fail on input \"$input\" with radix $radix",
|
||||||
{ input.convertOrFail(radix) })
|
{ input.convertOrFail(radix) })
|
||||||
|
|
||||||
assertNull (input.convertOrNull(radix), message = "On input \"$input\" with radix $radix")
|
assertNull(input.convertOrNull(radix), message = "On input \"$input\" with radix $radix")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user