Duration: do not use scientific format for large values

The largest duration value formatted in ns with maximal decimals
would fit in 40 chars.
This commit is contained in:
Ilya Gorbunov
2021-07-06 04:21:42 +03:00
committed by Space
parent 1be1e5279c
commit 3f6e2be687
7 changed files with 37 additions and 47 deletions
@@ -108,15 +108,10 @@ OBJ_GETTER(Kotlin_Long_toStringRadix, KLong value, KInt radix) {
}
OBJ_GETTER(Kotlin_DurationValue_formatToExactDecimals, KDouble value, KInt decimals) {
char cstring[32];
char cstring[40]; // log(2^62*1_000_000) + 2 (sign, decimal point) + 12 (max decimals)
konan::snprintf(cstring, sizeof(cstring), "%.*f", decimals, value);
RETURN_RESULT_OF(CreateStringFromCString, cstring)
}
OBJ_GETTER(Kotlin_DurationValue_formatScientificImpl, KDouble value) {
char cstring[16];
konan::snprintf(cstring, sizeof(cstring), "%.2e", value);
RETURN_RESULT_OF(CreateStringFromCString, cstring)
}
} // extern "C"
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -13,12 +13,3 @@ internal actual external fun formatToExactDecimals(value: Double, decimals: Int)
internal actual fun formatUpToDecimals(value: Double, decimals: Int): String {
return formatToExactDecimals(value, decimals).trimEnd('0')
}
@GCUnsafeCall("Kotlin_DurationValue_formatScientificImpl")
internal external fun formatScientificImpl(value: Double): String
internal actual fun formatScientific(value: Double): String {
val result = formatScientificImpl(value)
val expIndex = result.indexOf("e+0")
return if (expIndex < 0) result else result.removeRange(expIndex + 2, expIndex + 3)
}