stdlib: Fix number parsing

This commit is contained in:
Ilya Matveev
2017-04-21 10:10:43 +07:00
committed by ilmat192
parent 8df15dca5a
commit 0553b119b2
8 changed files with 107 additions and 63 deletions
@@ -50,7 +50,12 @@ private class ConversionWithRadixContext<T: Any>(val convertOrFail: (String, Int
}
}
private fun doubleTotalOrderEquals(a: Any?, b: Any?) = a == b
private fun doubleTotalOrderEquals(a: Double?, b: Double?): Boolean {
if (a != null && b != null && a.isNaN() && b.isNaN()) {
return true
}
return a == b
}
fun box() {
compareConversion(String::toDouble, String::toDoubleOrNull, ::doubleTotalOrderEquals) {
@@ -57,6 +57,12 @@ fun box() {
assertProduces("0x77p1", (0x77 shl 1).toDouble())
assertProduces("0x.77P8", 0x77.toDouble())
assertFailsOrNull("0x77e1")
// TODO: Java Double.valueOf specification requires mandatory binary exponent character (p) in the string parsed if the string is a hex one.
// See: http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-
// E.g.
// "0x77p0".toDouble() // OK for both Kotlin/JVM and Kotlin/Native.
// "0x77".toDouble() // throws NumberFormatException in Kotlin/JVM and OK in Kotlin/Native.
// Do we need to handle such case? Or it is OK to consume such strings?
//assertFailsOrNull("0x77e1")
}
}