Introduce String.toCharArray() instead of String.getChars()

Make special method conversion for java.lang.String.getChars() in J2K
This commit is contained in:
Ilya Gorbunov
2015-11-18 18:40:35 +03:00
parent bf3a77b736
commit 775755dfac
5 changed files with 38 additions and 7 deletions
@@ -292,6 +292,12 @@ enum class SpecialMethod(val qualifiedClassName: String?, val methodName: String
= MethodCallExpression.build(codeConverter.convertExpression(qualifier), "toByteArray", codeConverter.convertExpressions(arguments), emptyList(), false) = 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<PsiExpression>, typeArgumentsConverted: List<Type>, 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) { STRING_FORMAT_WITH_LOCALE(JAVA_LANG_STRING, "format", null) {
override fun matches(method: PsiMethod, superMethodsSearcher: SuperMethodsSearcher): Boolean override fun matches(method: PsiMethod, superMethodsSearcher: SuperMethodsSearcher): Boolean
= super.matches(method, superMethodsSearcher) && = super.matches(method, superMethodsSearcher) &&
@@ -60,10 +60,7 @@ class A {
s.toString(); s.toString();
s.toCharArray(); s.toCharArray();
}
char[] chars = new char[10];
s.getChars(1, 11, chars, 0);
}
void specialMethods() throws Exception { void specialMethods() throws Exception {
String s = "test string"; String s = "test string";
@@ -86,6 +83,9 @@ class A {
s.getBytes(); s.getBytes();
s.getBytes(Charset.forName("utf-8")); s.getBytes(Charset.forName("utf-8"));
s.getBytes("utf-8"); s.getBytes("utf-8");
char[] chars = new char[10];
s.getChars(1, 11, chars, 0);
} }
void staticMethods() { void staticMethods() {
@@ -61,9 +61,6 @@ internal class A {
s.toString() s.toString()
s.toCharArray() s.toCharArray()
val chars = CharArray(10)
s.getChars(1, 11, chars, 0)
} }
@Throws(Exception::class) @Throws(Exception::class)
@@ -88,6 +85,9 @@ internal class A {
s.toByteArray() s.toByteArray()
s.toByteArray(Charset.forName("utf-8")) s.toByteArray(Charset.forName("utf-8"))
s.toByteArray("utf-8") s.toByteArray("utf-8")
val chars = CharArray(10)
s.toCharArray(chars, 0, 1, 11)
} }
fun staticMethods() { fun staticMethods() {
@@ -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() 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, * Uses this string as a format string and returns a string obtained by substituting the specified arguments,
* using the default locale. * 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 dst the array to copy to.
* @param dstBegin the position in 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) public fun String.getChars(srcBegin: Int, srcEnd: Int, dst: CharArray, dstBegin: Int): Unit = (this as java.lang.String).getChars(srcBegin, srcEnd, dst, dstBegin)
@@ -1,5 +1,6 @@
package test.text package test.text
import test.collections.assertArrayNotSameButEquals
import java.util.Locale import java.util.Locale
import kotlin.test.* import kotlin.test.*
@@ -79,6 +80,16 @@ class StringJVMTest {
assertEquals(String(s.toByteArray()), String(s.toByteArray(defaultCharset.name()))) 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() { @test fun orderIgnoringCase() {
val list = listOf("Beast", "Ast", "asterisk") val list = listOf("Beast", "Ast", "asterisk")
assertEquals(listOf("Ast", "Beast", "asterisk"), list.sorted()) assertEquals(listOf("Ast", "Beast", "asterisk"), list.sorted())