diff --git a/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt b/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt index 2ca98e06b8b..49eb6978bbd 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/SpecialMethod.kt @@ -292,6 +292,12 @@ enum class SpecialMethod(val qualifiedClassName: String?, val methodName: String = MethodCallExpression.build(codeConverter.convertExpression(qualifier), "toByteArray", codeConverter.convertExpressions(arguments), emptyList(), false) }, + STRING_GET_CHARS(JAVA_LANG_STRING, "getChars", 4) { + override fun convertCall(qualifier: PsiExpression?, arguments: Array, typeArgumentsConverted: List, codeConverter: CodeConverter) + // reorder parameters: srcBegin(0), srcEnd(1), dst(2), dstOffset(3) -> destination(2), destinationOffset(3), startIndex(0), endIndex(1) + = MethodCallExpression.buildNotNull(codeConverter.convertExpression(qualifier), "toCharArray", codeConverter.convertExpressions(arguments.slice(listOf(2, 3, 0, 1)))) + }, + STRING_FORMAT_WITH_LOCALE(JAVA_LANG_STRING, "format", null) { override fun matches(method: PsiMethod, superMethodsSearcher: SuperMethodsSearcher): Boolean = super.matches(method, superMethodsSearcher) && diff --git a/j2k/testData/fileOrElement/methodCallExpression/stringMethods.java b/j2k/testData/fileOrElement/methodCallExpression/stringMethods.java index 4c00d9aebc9..633e33cb082 100644 --- a/j2k/testData/fileOrElement/methodCallExpression/stringMethods.java +++ b/j2k/testData/fileOrElement/methodCallExpression/stringMethods.java @@ -60,10 +60,7 @@ class A { s.toString(); s.toCharArray(); - - char[] chars = new char[10]; - s.getChars(1, 11, chars, 0); - } + } void specialMethods() throws Exception { String s = "test string"; @@ -86,6 +83,9 @@ class A { s.getBytes(); s.getBytes(Charset.forName("utf-8")); s.getBytes("utf-8"); + + char[] chars = new char[10]; + s.getChars(1, 11, chars, 0); } void staticMethods() { diff --git a/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt b/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt index 0e5ddbd05a1..264324d928c 100644 --- a/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt +++ b/j2k/testData/fileOrElement/methodCallExpression/stringMethods.kt @@ -61,9 +61,6 @@ internal class A { s.toString() s.toCharArray() - - val chars = CharArray(10) - s.getChars(1, 11, chars, 0) } @Throws(Exception::class) @@ -88,6 +85,9 @@ internal class A { s.toByteArray() s.toByteArray(Charset.forName("utf-8")) s.toByteArray("utf-8") + + val chars = CharArray(10) + s.toCharArray(chars, 0, 1, 11) } fun staticMethods() { diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index cc01217e8d5..7b716234add 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -93,6 +93,19 @@ public fun String.toLowerCase(): String = (this as java.lang.String).toLowerCase */ public fun String.toCharArray(): CharArray = (this as java.lang.String).toCharArray() +/** + * Copies characters from this string into the [destination] character array and returns that array. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the array to copy to. + * @param startIndex the start offset (inclusive) of the substring to copy. + * @param endIndex the end offset (exclusive) of the substring to copy. + */ +public fun String.toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = length): CharArray { + (this as java.lang.String).getChars(startIndex, endIndex, destination, destinationOffset) + return destination +} + /** * Uses this string as a format string and returns a string obtained by substituting the specified arguments, * using the default locale. @@ -282,6 +295,7 @@ public fun String.contentEquals(stringBuilder: StringBuffer): Boolean = (this as * @param dst the array to copy to. * @param dstBegin the position in the array to copy to. */ +@Deprecated("Use toCharArray() instead.", ReplaceWith("toCharArray(dst, dstBegin, srcBegin, srcEnd)")) public fun String.getChars(srcBegin: Int, srcEnd: Int, dst: CharArray, dstBegin: Int): Unit = (this as java.lang.String).getChars(srcBegin, srcEnd, dst, dstBegin) diff --git a/libraries/stdlib/test/text/StringJVMTest.kt b/libraries/stdlib/test/text/StringJVMTest.kt index 5e248301af0..53fdbfc74cc 100644 --- a/libraries/stdlib/test/text/StringJVMTest.kt +++ b/libraries/stdlib/test/text/StringJVMTest.kt @@ -1,5 +1,6 @@ package test.text +import test.collections.assertArrayNotSameButEquals import java.util.Locale import kotlin.test.* @@ -79,6 +80,16 @@ class StringJVMTest { assertEquals(String(s.toByteArray()), String(s.toByteArray(defaultCharset.name()))) } + @test fun toCharArray() { + val s = "hello" + val chars = s.toCharArray() + assertArrayNotSameButEquals(charArrayOf('h', 'e', 'l', 'l', 'o'), chars) + + val buffer = CharArray(4) + s.toCharArray(buffer, 2, 1, 3) + assertArrayNotSameButEquals(charArrayOf('\u0000', '\u0000', 'e', 'l'), buffer) + } + @test fun orderIgnoringCase() { val list = listOf("Beast", "Ast", "asterisk") assertEquals(listOf("Ast", "Beast", "asterisk"), list.sorted())