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
@@ -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)
@@ -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())