KT-23356 Cross-platform function to convert CharArray slice to String
Signed-off-by: Valeriy Zhirnov <neonailol@gmail.com>
This commit is contained in:
@@ -90,6 +90,20 @@ expect fun Char.isLowSurrogate(): Boolean
|
|||||||
|
|
||||||
// From string.kt
|
// From string.kt
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the characters in the specified array to a string.
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.2")
|
||||||
|
public expect fun String(chars: CharArray): String
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the characters from a portion of the specified array to a string.
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.2")
|
||||||
|
public expect fun String(chars: CharArray, offset: Int, length: Int): String
|
||||||
|
|
||||||
|
|
||||||
internal expect fun String.nativeIndexOf(str: String, fromIndex: Int): Int
|
internal expect fun String.nativeIndexOf(str: String, fromIndex: Int): Int
|
||||||
internal expect fun String.nativeLastIndexOf(str: String, fromIndex: Int): Int
|
internal expect fun String.nativeLastIndexOf(str: String, fromIndex: Int): Int
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,24 @@ package kotlin.text
|
|||||||
|
|
||||||
import kotlin.js.RegExp
|
import kotlin.js.RegExp
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the characters in the specified array to a string.
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.2")
|
||||||
|
@kotlin.internal.InlineOnly
|
||||||
|
public actual inline fun String(chars: CharArray): String {
|
||||||
|
return js("String.fromCharCode").apply(null, chars)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the characters from a portion of the specified array to a string.
|
||||||
|
*/
|
||||||
|
@SinceKotlin("1.2")
|
||||||
|
public actual fun String(chars: CharArray, offset: Int, length: Int): String {
|
||||||
|
return String(chars.copyOfRange(offset, offset + length))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public actual inline fun String.toUpperCase(): String = asDynamic().toUpperCase()
|
public actual inline fun String.toUpperCase(): String = asDynamic().toUpperCase()
|
||||||
|
|
||||||
|
|||||||
@@ -260,14 +260,14 @@ public inline fun String(bytes: ByteArray): String =
|
|||||||
* Converts the characters in the specified array to a string.
|
* Converts the characters in the specified array to a string.
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun String(chars: CharArray): String =
|
public actual inline fun String(chars: CharArray): String =
|
||||||
java.lang.String(chars) as String
|
java.lang.String(chars) as String
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the characters from a portion of the specified array to a string.
|
* Converts the characters from a portion of the specified array to a string.
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun String(chars: CharArray, offset: Int, length: Int): String =
|
public actual inline fun String(chars: CharArray, offset: Int, length: Int): String =
|
||||||
java.lang.String(chars, offset, length) as String
|
java.lang.String(chars, offset, length) as String
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -43,6 +43,28 @@ fun Char.isAsciiUpperCase() = this in 'A'..'Z'
|
|||||||
|
|
||||||
class StringTest {
|
class StringTest {
|
||||||
|
|
||||||
|
@Test fun stringFromCharArrayFullSlice() {
|
||||||
|
val chars: CharArray = charArrayOf('K', 'o', 't', 'l', 'i', 'n')
|
||||||
|
assertEquals("Kotlin", String(chars, 0, chars.size))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun stringFromCharArraySlice() {
|
||||||
|
val chars: CharArray = charArrayOf('K', 'o', 't', 'l', 'i', 'n', ' ', 'r', 'u', 'l', 'e', 's')
|
||||||
|
assertEquals("rule", String(chars, 7, 4))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun stringFromCharArray() {
|
||||||
|
val chars: CharArray = charArrayOf('K', 'o', 't', 'l', 'i', 'n')
|
||||||
|
assertEquals("Kotlin", String(chars))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun stringFromCharArrayUnicodeSurrogatePairs() {
|
||||||
|
val chars: CharArray = charArrayOf('Ц', '月', '語', '\u016C', '\u138D', '\uD83C', '\uDC3A')
|
||||||
|
assertEquals("Ц月語Ŭᎍ🀺", String(chars))
|
||||||
|
assertEquals("月", String(chars, 1, 1))
|
||||||
|
assertEquals("Ŭᎍ🀺", String(chars, 3, 4))
|
||||||
|
}
|
||||||
|
|
||||||
@Test fun isEmptyAndBlank() = withOneCharSequenceArg { arg1 ->
|
@Test fun isEmptyAndBlank() = withOneCharSequenceArg { arg1 ->
|
||||||
class Case(val value: String?, val isNull: Boolean = false, val isEmpty: Boolean = false, val isBlank: Boolean = false)
|
class Case(val value: String?, val isNull: Boolean = false, val isEmpty: Boolean = false, val isBlank: Boolean = false)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user