Support '+' in front of parsed unsigned string values

#KT-26161
This commit is contained in:
Ilya Gorbunov
2018-08-17 06:08:18 +03:00
parent 2530a8e98c
commit d99f97ad96
2 changed files with 25 additions and 13 deletions
@@ -159,7 +159,7 @@ class StringNumberConversionTest {
@Test fun toUByte() {
compareConversion({it.toUByte()}, {it.toUByteOrNull()}) {
assertProduces("255", UByte.MAX_VALUE)
// assertProduces("+77", 77.toUByte())
assertProduces("+77", 77.toUByte())
assertProduces("128", 128u.toUByte())
assertFailsOrNull("-1")
assertFailsOrNull("256")
@@ -169,7 +169,7 @@ class StringNumberConversionTest {
compareConversionWithRadix(String::toUByte, String::toUByteOrNull) {
assertProduces(16, "7a", 0x7a.toUByte())
// assertProduces(16, "+7F", 127.toByte())
assertProduces(16, "+8F", 0x8Fu.toUByte())
assertProduces(16, "80", 128u.toUByte())
assertProduces(16, "Ff", 255u.toUByte())
assertFailsOrNull(2, "100000000")
@@ -180,7 +180,7 @@ class StringNumberConversionTest {
@Test fun toUShort() {
compareConversion({it.toUShort()}, {it.toUShortOrNull()}) {
// assertProduces("+77", 77.toUShort())
assertProduces("+77", 77.toUShort())
assertProduces("65535", UShort.MAX_VALUE)
assertFailsOrNull("+65536")
assertFailsOrNull("-32768")
@@ -189,7 +189,7 @@ class StringNumberConversionTest {
}
compareConversionWithRadix(String::toUShort, String::toUShortOrNull) {
assertProduces(16, "7FFF", 0x7FFF.toUShort())
assertProduces(16, "+7FFF", 0x7FFF.toUShort())
assertProduces(16, "FfFf", UShort.MAX_VALUE)
assertFailsOrNull(16, "-8000")
assertFailsOrNull(5, "10000000")
@@ -200,7 +200,7 @@ class StringNumberConversionTest {
@Test fun toUInt() {
compareConversion({it.toUInt()}, {it.toUIntOrNull()}) {
assertProduces("77", 77u)
assertProduces("+77", 77u)
assertProduces("4294967295", UInt.MAX_VALUE)
assertFailsOrNull("-1")
@@ -215,7 +215,7 @@ class StringNumberConversionTest {
compareConversionWithRadix(String::toUInt, String::toUIntOrNull) {
assertProduces(10, "0", 0u)
assertProduces(10, "473", 473u)
// assertProduces(10, "+42", 42u)
assertProduces(10, "+42", 42u)
assertProduces(10, "2147483647", 2147483647u)
assertProduces(16, "FF", 255)
@@ -239,7 +239,7 @@ class StringNumberConversionTest {
@Test fun toULong() {
compareConversion({it.toULong()}, {it.toULongOrNull()}) {
assertProduces("77", 77uL)
assertProduces("18446744073709551615", ULong.MAX_VALUE)
assertProduces("+18446744073709551615", ULong.MAX_VALUE)
assertFailsOrNull("-1")
assertFailsOrNull("18446744073709551616")
@@ -254,10 +254,10 @@ class StringNumberConversionTest {
compareConversionWithRadix(String::toULong, String::toULongOrNull) {
assertProduces(10, "0", 0uL)
assertProduces(10, "473", 473uL)
// assertProduces(10, "+42", 42uL)
assertProduces(10, "+42", 42uL)
assertProduces(16, "7F11223344556677", 0x7F11223344556677uL)
// assertProduces(16, "+7faabbccddeeff00", 0x7faabbccddeeff00uL)
assertProduces(16, "+7faabbccddeeff00", 0x7faabbccddeeff00uL)
assertProduces(16, "8000000000000000", Long.MIN_VALUE.toULong())
assertProduces(16, "FFFFffffFFFFffff", ULong.MAX_VALUE)
assertProduces(2, "1100110", 102uL)
@@ -186,14 +186,20 @@ public fun String.toUIntOrNull(radix: Int): UInt? {
if (length == 0) return null
val limit: UInt = UInt.MAX_VALUE
val start: Int
val firstChar = this[0]
if (firstChar < '0') return null
if (firstChar < '0') {
if (length == 1 || firstChar != '+') return null
start = 1
} else {
start = 0
}
val uradix = radix.toUInt()
val limitBeforeMul = limit / uradix
var result = 0u
for (i in 0 until length) {
for (i in start until length) {
val digit = digitOf(this[i], radix)
if (digit < 0) return null
@@ -232,15 +238,21 @@ public fun String.toULongOrNull(radix: Int): ULong? {
if (length == 0) return null
val limit: ULong = ULong.MAX_VALUE
val start: Int
val firstChar = this[0]
if (firstChar < '0') return null
if (firstChar < '0') {
if (length == 1 || firstChar != '+') return null
start = 1
} else {
start = 0
}
val uradix = radix.toUInt()
val limitBeforeMul = limit / uradix
var result = 0uL
for (i in 0 until length) {
for (i in start until length) {
val digit = digitOf(this[i], radix)
if (digit < 0) return null