Add toKString() from short array
This commit is contained in:
@@ -395,6 +395,7 @@ val String.wcstr: CValues<ShortVar>
|
|||||||
*
|
*
|
||||||
* @return the [kotlin.String] decoded from given zero-terminated UTF-8-encoded C string.
|
* @return the [kotlin.String] decoded from given zero-terminated UTF-8-encoded C string.
|
||||||
*/
|
*/
|
||||||
|
// TODO: optimize
|
||||||
fun CPointer<ByteVar>.toKString(): String {
|
fun CPointer<ByteVar>.toKString(): String {
|
||||||
val nativeBytes = this
|
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")
|
@SymbolName("Kotlin_interop_malloc")
|
||||||
private external fun malloc(size: Long, align: Int): NativePtr
|
private external fun malloc(size: Long, align: Int): NativePtr
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user