Add toKString() from short array

This commit is contained in:
igotti-google
2018-06-30 19:45:11 +03:00
parent 986f5220df
commit dfac13a7a7
2 changed files with 18 additions and 0 deletions
@@ -395,6 +395,7 @@ val String.wcstr: CValues<ShortVar>
*
* @return the [kotlin.String] decoded from given zero-terminated UTF-8-encoded C string.
*/
// TODO: optimize
fun CPointer<ByteVar>.toKString(): String {
val nativeBytes = this
@@ -112,6 +112,23 @@ internal object nativeMemUtils {
}
}
fun CPointer<ShortVar>.toKString(): String {
val nativeBytes = this
var length = 0
while (nativeBytes[length] != 0.toShort()) {
++length
}
val bytes = CharArray(length)
var index = 0
while (index < length) {
bytes[index] = nativeBytes[index].toChar()
++index
}
return String(bytes)
}
@SymbolName("Kotlin_interop_malloc")
private external fun malloc(size: Long, align: Int): NativePtr