[Wasm] Improve string constructor in stdlib

This commit is contained in:
Igor Laevsky
2021-10-22 21:16:04 +03:00
parent 8dc81b6c57
commit 46b56c7642
6 changed files with 32 additions and 11 deletions
+20 -2
View File
@@ -252,7 +252,15 @@ public actual fun Char.isWhitespace(): Boolean = TODO("Wasm stdlib: Text")
* Converts the characters in the specified array to a string.
*/
@SinceKotlin("1.2")
public actual fun String(chars: CharArray): String = TODO("Wasm stdlib: Text")
@Deprecated("Use CharArray.concatToString() instead", ReplaceWith("chars.concatToString()"))
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5")
public actual fun String(chars: CharArray): String {
var result = ""
for (char in chars) {
result += char
}
return result
}
/**
* Converts the characters from a portion of the specified array to a string.
@@ -261,7 +269,17 @@ public actual fun String(chars: CharArray): String = TODO("Wasm stdlib: Text")
* or `offset + length` is out of [chars] array bounds.
*/
@SinceKotlin("1.2")
public actual fun String(chars: CharArray, offset: Int, length: Int): String = TODO("Wasm stdlib: Text")
@Deprecated("Use CharArray.concatToString(startIndex, endIndex) instead", ReplaceWith("chars.concatToString(offset, offset + length)"))
@DeprecatedSinceKotlin(warningSince = "1.4", errorSince = "1.5")
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")
var result = ""
for (index in offset until offset + length) {
result += chars[index]
}
return result
}
/**
* Concatenates characters in this [CharArray] into a String.
@@ -15,7 +15,7 @@ internal fun insertString(array: CharArray, distIndex: Int, value: String, sourc
}
internal fun unsafeStringFromCharArray(array: CharArray, start: Int, size: Int): String =
kotlin.String(array.copyOfRange(start, start + size))
String.unsafeFromCharArray(array.copyOfRange(start, start + size))
internal fun insertInt(array: CharArray, start: Int, value: Int): Int {
val valueString = value.toString()