From fc22cfa896e03f5e878ac577537de0d2473b621e Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Thu, 27 Feb 2020 23:15:29 +0300 Subject: [PATCH] String.format does not support no locale #KT-22932 --- .../stdlib/jvm/src/kotlin/text/StringsJVM.kt | 22 +++++++++++++++++++ libraries/stdlib/jvm/test/testUtilsJVM.kt | 6 ++++- .../stdlib/jvm/test/text/StringJVMTest.kt | 6 +++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt index 238c48ebab8..7bb6b735f3a 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt @@ -15,6 +15,7 @@ import java.nio.charset.Charset import java.nio.charset.CodingErrorAction import java.util.Locale import java.util.regex.Pattern +import kotlin.internal.LowPriorityInOverloadResolution /** @@ -288,17 +289,38 @@ public inline fun String.Companion.format(format: String, vararg args: Any?): St * Uses this string as a format string and returns a string obtained by substituting the specified arguments, * using the specified locale. */ +@LowPriorityInOverloadResolution @kotlin.internal.InlineOnly public inline fun String.format(locale: Locale, vararg args: Any?): String = java.lang.String.format(locale, this, *args) +/** + * Uses this string as a format string and returns a string obtained by substituting the specified arguments, + * using the specified locale. If [locale] is `null` then no localization is applied. + */ +@SinceKotlin("1.4") +@JvmName("formatNullable") +@kotlin.internal.InlineOnly +public inline fun String.format(locale: Locale?, vararg args: Any?): String = java.lang.String.format(locale, this, *args) + /** * Uses the provided [format] as a format string and returns a string obtained by substituting the specified arguments, * using the specified locale. */ +@LowPriorityInOverloadResolution @kotlin.internal.InlineOnly public inline fun String.Companion.format(locale: Locale, format: String, vararg args: Any?): String = java.lang.String.format(locale, format, *args) +/** + * Uses the provided [format] as a format string and returns a string obtained by substituting the specified arguments, + * using the specified locale. If [locale] is `null` then no localization is applied. + */ +@SinceKotlin("1.4") +@JvmName("formatNullable") +@kotlin.internal.InlineOnly +public inline fun String.Companion.format(locale: Locale?, format: String, vararg args: Any?): String = + java.lang.String.format(locale, format, *args) + /** * Splits this char sequence around matches of the given regular expression. diff --git a/libraries/stdlib/jvm/test/testUtilsJVM.kt b/libraries/stdlib/jvm/test/testUtilsJVM.kt index 3d65aa70485..6fe3b763d57 100644 --- a/libraries/stdlib/jvm/test/testUtilsJVM.kt +++ b/libraries/stdlib/jvm/test/testUtilsJVM.kt @@ -5,6 +5,7 @@ package test +import java.util.* import kotlin.test.assertEquals public actual fun assertTypeEquals(expected: Any?, actual: Any?) { @@ -26,4 +27,7 @@ internal actual inline fun testOnNonJvm6And7(f: () -> Unit) { } } public actual fun testOnJvm(action: () -> Unit) = action() -public actual fun testOnJs(action: () -> Unit) {} \ No newline at end of file +public actual fun testOnJs(action: () -> Unit) {} + +@Suppress("HasPlatformType", "UNCHECKED_CAST") +public fun platformNull() = Collections.singletonList(null as T).first() \ No newline at end of file diff --git a/libraries/stdlib/jvm/test/text/StringJVMTest.kt b/libraries/stdlib/jvm/test/text/StringJVMTest.kt index b748d461f9f..73d81f58445 100644 --- a/libraries/stdlib/jvm/test/text/StringJVMTest.kt +++ b/libraries/stdlib/jvm/test/text/StringJVMTest.kt @@ -6,6 +6,7 @@ package test.text import test.collections.assertArrayNotSameButEquals +import test.platformNull import java.util.* import kotlin.test.* @@ -41,9 +42,14 @@ class StringJVMTest { assertEquals("1,234,567.890", "%,.3f".format(Locale.ENGLISH, 1234567.890)) assertEquals("1.234.567,890", "%,.3f".format(Locale.GERMAN, 1234567.890)) assertEquals("1 234 567,890", "%,.3f".format(Locale("fr"), 1234567.890)) + assertEquals("1,234,567.890", "%,.3f".format(null, 1234567.890)) + assertEquals("1,234,567.890", "%,.3f".format(platformNull(), 1234567.890)) + assertEquals("1,234,567.890", String.format(Locale.ENGLISH, "%,.3f", 1234567.890)) assertEquals("1.234.567,890", String.format(Locale.GERMAN, "%,.3f", 1234567.890)) assertEquals("1 234 567,890", String.format(Locale("fr"), "%,.3f", 1234567.890)) + assertEquals("1,234,567.890", String.format(null, "%,.3f", 1234567.890)) + assertEquals("1,234,567.890", String.format(platformNull(), "%,.3f", 1234567.890)) } @Test fun toByteArrayEncodings() {