Add simplified overloads of String<->ByteArray/CharArray conversions

These overloads cover the most common cases of conversion of the entire
String or Byte/CharArray, avoiding extra index check and branching.

#KT-24810, KT-29265
This commit is contained in:
Ilya Gorbunov
2019-05-07 05:35:20 +03:00
parent f9c12db3b5
commit 2c26dc3af6
4 changed files with 112 additions and 2 deletions
@@ -116,6 +116,13 @@ public expect fun String(chars: CharArray): String
@SinceKotlin("1.2")
public expect fun String(chars: CharArray, offset: Int, length: Int): String
/**
* Concatenates characters in this [CharArray] into a String.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public expect fun CharArray.concatToString(): String
/**
* Concatenates characters in this [CharArray] or its subrange into a String.
*
@@ -129,6 +136,13 @@ public expect fun String(chars: CharArray, offset: Int, length: Int): String
@ExperimentalStdlibApi
public expect fun CharArray.concatToString(startIndex: Int = 0, endIndex: Int = this.size): String
/**
* Returns a [CharArray] containing characters of this string.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public expect fun String.toCharArray(): CharArray
/**
* Returns a [CharArray] containing characters of this string or its substring.
*
@@ -142,6 +156,15 @@ public expect fun CharArray.concatToString(startIndex: Int = 0, endIndex: Int =
@ExperimentalStdlibApi
public expect fun String.toCharArray(startIndex: Int = 0, endIndex: Int = this.length): CharArray
/**
* Decodes a string from the bytes in UTF-8 encoding in this array.
*
* Malformed byte sequences are replaced by the replacement char `\uFFFD`.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public expect fun ByteArray.decodeToString(): String
/**
* Decodes a string from the bytes in UTF-8 encoding in this array or its subrange.
*
@@ -161,6 +184,15 @@ public expect fun ByteArray.decodeToString(
throwOnInvalidSequence: Boolean = false
): String
/**
* Encodes this string to an array of bytes in UTF-8 encoding.
*
* Any malformed char sequence is replaced by the replacement byte sequence.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public expect fun String.encodeToByteArray(): ByteArray
/**
* Encodes this string or its substring to an array of bytes in UTF-8 encoding.
*
@@ -36,6 +36,19 @@ public actual fun String(chars: CharArray, offset: Int, length: Int): String {
return result
}
/**
* Concatenates characters in this [CharArray] into a String.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public actual fun CharArray.concatToString(): String {
var result = ""
for (char in this) {
result += char
}
return result
}
/**
* Concatenates characters in this [CharArray] or its subrange into a String.
*
@@ -57,6 +70,15 @@ public actual fun CharArray.concatToString(startIndex: Int = 0, endIndex: Int =
return result
}
/**
* Returns a [CharArray] containing characters of this string.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public actual fun String.toCharArray(): CharArray {
return CharArray(length) { get(it) }
}
/**
* Returns a [CharArray] containing characters of this string or its substring.
*
@@ -74,6 +96,17 @@ public actual fun String.toCharArray(startIndex: Int = 0, endIndex: Int = this.l
return CharArray(endIndex - startIndex) { get(startIndex + it) }
}
/**
* Decodes a string from the bytes in UTF-8 encoding in this array.
*
* Malformed byte sequences are replaced by the replacement char `\uFFFD`.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public actual fun ByteArray.decodeToString(): String {
return decodeUtf8(this, 0, size, false)
}
/**
* Decodes a string from the bytes in UTF-8 encoding in this array or its subrange.
*
@@ -97,6 +130,17 @@ public actual fun ByteArray.decodeToString(
return decodeUtf8(this, startIndex, endIndex, throwOnInvalidSequence)
}
/**
* Encodes this string to an array of bytes in UTF-8 encoding.
*
* Any malformed char sequence is replaced by the replacement byte sequence.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public actual fun String.encodeToByteArray(): ByteArray {
return encodeUtf8(this, 0, length, false)
}
/**
* Encodes this string or its substring to an array of bytes in UTF-8 encoding.
*
@@ -111,6 +111,15 @@ public actual inline fun String.toUpperCase(): String = (this as java.lang.Strin
@kotlin.internal.InlineOnly
public actual inline fun String.toLowerCase(): String = (this as java.lang.String).toLowerCase()
/**
* Concatenates characters in this [CharArray] into a String.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public actual fun CharArray.concatToString(): String {
return String(this)
}
/**
* Concatenates characters in this [CharArray] or its subrange into a String.
*
@@ -145,6 +154,17 @@ public actual fun String.toCharArray(startIndex: Int = 0, endIndex: Int = this.l
return toCharArray(CharArray(endIndex - startIndex), 0, startIndex, endIndex)
}
/**
* Decodes a string from the bytes in UTF-8 encoding in this array.
*
* Malformed byte sequences are replaced by the replacement char `\uFFFD`.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public actual fun ByteArray.decodeToString(): String {
return String(this)
}
/**
* Decodes a string from the bytes in UTF-8 encoding in this array or its subrange.
*
@@ -177,6 +197,17 @@ public actual fun ByteArray.decodeToString(
return decoder.decode(ByteBuffer.wrap(this, startIndex, endIndex - startIndex)).toString()
}
/**
* Encodes this string to an array of bytes in UTF-8 encoding.
*
* Any malformed char sequence is replaced by the replacement byte sequence.
*/
@SinceKotlin("1.3")
@ExperimentalStdlibApi
public actual fun String.encodeToByteArray(): ByteArray {
return this.toByteArray(Charsets.UTF_8)
}
/**
* Encodes this string or its substring to an array of bytes in UTF-8 encoding.
*
@@ -215,10 +246,10 @@ public actual fun String.encodeToByteArray(
}
/**
* Returns a new character array containing the characters from this string.
* Returns a [CharArray] containing characters of this string.
*/
@kotlin.internal.InlineOnly
public inline fun String.toCharArray(): CharArray = (this as java.lang.String).toCharArray()
public actual inline fun String.toCharArray(): CharArray = (this as java.lang.String).toCharArray()
/**
* Copies characters from this string into the [destination] character array and returns that array.
@@ -4906,6 +4906,7 @@ public final class kotlin/text/StringsKt {
public static synthetic fun commonSuffixWith$default (Ljava/lang/CharSequence;Ljava/lang/CharSequence;ZILjava/lang/Object;)Ljava/lang/String;
public static final fun compareTo (Ljava/lang/String;Ljava/lang/String;Z)I
public static synthetic fun compareTo$default (Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)I
public static final fun concatToString ([C)Ljava/lang/String;
public static final fun concatToString ([CII)Ljava/lang/String;
public static synthetic fun concatToString$default ([CIIILjava/lang/Object;)Ljava/lang/String;
public static final fun contains (Ljava/lang/CharSequence;CZ)Z
@@ -4914,6 +4915,7 @@ public final class kotlin/text/StringsKt {
public static synthetic fun contains$default (Ljava/lang/CharSequence;Ljava/lang/CharSequence;ZILjava/lang/Object;)Z
public static final fun count (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)I
public static final fun decapitalize (Ljava/lang/String;)Ljava/lang/String;
public static final fun decodeToString ([B)Ljava/lang/String;
public static final fun decodeToString ([BIIZ)Ljava/lang/String;
public static synthetic fun decodeToString$default ([BIIZILjava/lang/Object;)Ljava/lang/String;
public static final fun drop (Ljava/lang/CharSequence;I)Ljava/lang/CharSequence;
@@ -4924,6 +4926,7 @@ public final class kotlin/text/StringsKt {
public static final fun dropLastWhile (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Ljava/lang/String;
public static final fun dropWhile (Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Ljava/lang/CharSequence;
public static final fun dropWhile (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Ljava/lang/String;
public static final fun encodeToByteArray (Ljava/lang/String;)[B
public static final fun encodeToByteArray (Ljava/lang/String;IIZ)[B
public static synthetic fun encodeToByteArray$default (Ljava/lang/String;IIZILjava/lang/Object;)[B
public static final fun endsWith (Ljava/lang/CharSequence;CZ)Z