Deprecate old CharArray to String conversion api #KT-31343
This commit is contained in:
@@ -78,6 +78,7 @@ expect fun Char.isLowSurrogate(): Boolean
|
||||
* Converts the characters in the specified array to a string.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@Deprecated("Use CharArray.concatToString() instead", ReplaceWith("chars.concatToString()"))
|
||||
public expect fun String(chars: CharArray): String
|
||||
|
||||
/**
|
||||
@@ -87,6 +88,7 @@ public expect fun String(chars: CharArray): String
|
||||
* or `offset + length` is out of [chars] array bounds.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@Deprecated("Use CharArray.concatToString(startIndex, endIndex) instead", ReplaceWith("chars.concatToString(offset, offset + length)"))
|
||||
public expect fun String(chars: CharArray, offset: Int, length: Int): String
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ import kotlin.js.RegExp
|
||||
* Converts the characters in the specified array to a string.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@Deprecated("Use CharArray.concatToString() instead", ReplaceWith("chars.concatToString()"))
|
||||
public actual fun String(chars: CharArray): String {
|
||||
var result = ""
|
||||
for (char in chars) {
|
||||
@@ -26,6 +27,7 @@ public actual fun String(chars: CharArray): String {
|
||||
* or `offset + length` is out of [chars] array bounds.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@Deprecated("Use CharArray.concatToString(startIndex, endIndex) instead", ReplaceWith("chars.concatToString(offset, offset + length)"))
|
||||
public actual fun String(chars: CharArray, offset: Int, length: Int): String {
|
||||
if (offset < 0 || length < 0 || chars.size - offset < length)
|
||||
throw IndexOutOfBoundsException("size: ${chars.size}; offset: $offset; length: $length")
|
||||
|
||||
@@ -187,8 +187,6 @@ class StringBuilderTest {
|
||||
'm', 'y', ' ', 'a', 'p', 'p', 'e', 'n', 'd', ' ', 'c', 'h', 'a', 'r', ' ', 'a', 'r', 'r', 'a', 'y', ' ', 't', 'e', 's', 't'
|
||||
)
|
||||
|
||||
String(charArray, 0, 1)
|
||||
|
||||
sb.appendRange(charArray, 0, charArray.size /*25*/)
|
||||
sb.appendRange(charArray, 0, 9)
|
||||
sb.appendRange(charArray, 15, 25)
|
||||
@@ -473,11 +471,11 @@ class StringBuilderTest {
|
||||
val chars = CharArray(10) { '_' }
|
||||
|
||||
sb.toCharArray(chars, 8, 0, 2)
|
||||
assertEquals("________my", String(chars))
|
||||
assertEquals("________my", chars.concatToString())
|
||||
sb.toCharArray(chars, 3, 6, 11)
|
||||
assertEquals("___harArmy", String(chars))
|
||||
assertEquals("___harArmy", chars.concatToString())
|
||||
sb.toCharArray(chars, 0, 16, 19)
|
||||
assertEquals("estharArmy", String(chars))
|
||||
assertEquals("estharArmy", chars.concatToString())
|
||||
|
||||
sb.setLength(5)
|
||||
|
||||
|
||||
@@ -46,11 +46,13 @@ fun Char.isAsciiUpperCase() = this in 'A'..'Z'
|
||||
|
||||
class StringTest {
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun testStringFromChars(expected: String, chars: CharArray, offset: Int, length: Int) {
|
||||
assertEquals(expected, String(chars, offset, length))
|
||||
assertEquals(expected, chars.concatToString(startIndex = offset, endIndex = offset + length))
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun testStringFromChars(expected: String, chars: CharArray) {
|
||||
assertEquals(expected, String(chars))
|
||||
assertEquals(expected, chars.concatToString())
|
||||
@@ -61,6 +63,7 @@ class StringTest {
|
||||
testStringFromChars("Kotlin", chars, 0, chars.size)
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
@Test fun stringFromCharArraySlice() {
|
||||
val chars: CharArray = charArrayOf('K', 'o', 't', 'l', 'i', 'n', ' ', 'r', 'u', 'l', 'e', 's')
|
||||
testStringFromChars("rule", chars, 7, 4)
|
||||
@@ -74,6 +77,7 @@ class StringTest {
|
||||
assertTrue(longConcatString.all { it == 'k' })
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
@Test fun stringFromCharArray() {
|
||||
val chars: CharArray = charArrayOf('K', 'o', 't', 'l', 'i', 'n')
|
||||
testStringFromChars("Kotlin", chars)
|
||||
@@ -94,6 +98,7 @@ class StringTest {
|
||||
testStringFromChars("Ŭᎍ🀺", chars, 3, 4)
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
@Test fun stringFromCharArrayOutOfBounds() {
|
||||
fun test(chars: CharArray) {
|
||||
assertFailsWith<IndexOutOfBoundsException> { String(chars, -1, 1) }
|
||||
|
||||
Reference in New Issue
Block a user