[Wasm] Improve string constructor in stdlib
This commit is contained in:
@@ -93,7 +93,7 @@ public class Char private constructor(public val value: Char) : Comparable<Char>
|
||||
this.toInt().toDouble()
|
||||
|
||||
override fun toString(): String =
|
||||
String(charArrayOf(this))
|
||||
String.unsafeFromCharArray(charArrayOf(this))
|
||||
|
||||
override fun hashCode(): Int =
|
||||
this.toInt().hashCode()
|
||||
|
||||
@@ -12,8 +12,11 @@ import kotlin.math.min
|
||||
* The `String` class represents character strings. All string literals in Kotlin programs, such as `"abc"`, are
|
||||
* implemented as instances of this class.
|
||||
*/
|
||||
public class String internal constructor(internal val chars: CharArray) : Comparable<String>, CharSequence {
|
||||
public companion object;
|
||||
public class String private constructor(internal val chars: CharArray) : Comparable<String>, CharSequence {
|
||||
public companion object {
|
||||
// Note: doesn't copy the array, use with care.
|
||||
internal fun unsafeFromCharArray(chars: CharArray) = String(chars)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string obtained by concatenating this string with the string representation of the given [other] object.
|
||||
@@ -68,4 +71,4 @@ public class String internal constructor(internal val chars: CharArray) : Compar
|
||||
}
|
||||
}
|
||||
|
||||
internal fun stringLiteral(startAddr: Int, length: Int) = String(unsafeRawMemoryToCharArray(startAddr, length))
|
||||
internal fun stringLiteral(startAddr: Int, length: Int) = String.unsafeFromCharArray(unsafeRawMemoryToCharArray(startAddr, length))
|
||||
|
||||
@@ -147,7 +147,7 @@ internal fun convertJsStringToKotlinString(x: ExternalInterfaceType): String {
|
||||
val length = stringLength(x)
|
||||
val addr = unsafeGetScratchRawMemory(INT_SIZE_BYTES + length * CHAR_SIZE_BYTES)
|
||||
jsWriteStringIntoMemory(x, addr)
|
||||
return kotlin.String(unsafeRawMemoryToCharArray(addr, length))
|
||||
return String.unsafeFromCharArray(unsafeRawMemoryToCharArray(addr, length))
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ internal fun itoa32(inputValue: Int, radix: Int): String {
|
||||
if (sign == 1)
|
||||
buf[0] = CharCodes.MINUS.code.toChar()
|
||||
|
||||
return kotlin.String(buf)
|
||||
return String.unsafeFromCharArray(buf)
|
||||
}
|
||||
|
||||
private fun utoaDecSimple(buffer: CharArray, numInput: Int, offsetInput: Int) {
|
||||
@@ -144,7 +144,7 @@ internal fun itoa64(inputValue: Long, radix: Int): String {
|
||||
if (sign == 1)
|
||||
buf[0] = CharCodes.MINUS.code.toChar()
|
||||
|
||||
return kotlin.String(buf)
|
||||
return String.unsafeFromCharArray(buf)
|
||||
}
|
||||
|
||||
// Count number of decimals for u64 values
|
||||
@@ -178,7 +178,7 @@ internal fun dtoa(value: Double): String {
|
||||
val size = dtoaCore(buf, value)
|
||||
val ret = CharArray(size)
|
||||
buf.copyInto(ret, 0, 0, size)
|
||||
return kotlin.String(ret)
|
||||
return String.unsafeFromCharArray(ret)
|
||||
}
|
||||
|
||||
private fun dtoaCore(buffer: CharArray, valueInp: Double): Int {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user