Changed byte array copying to a more efficient mechanism.

This commit is contained in:
Alexander Gorshenev
2016-12-28 18:08:32 +03:00
committed by alexander-gorshenev
parent 174cdc321c
commit 3f65eeaeab
@@ -169,10 +169,10 @@ class CString private constructor(override val rawPtr: NativePtr) : CPointed {
override fun toString(): String {
val array = reinterpret<CArray<CInt8Var>>()
val bytes = ByteArray(this.length())
bytes.forEachIndexed { i, byte ->
bytes[i] = array[i].value
}
val len = this.length()
val bytes = ByteArray(len)
nativeMemUtils.getByteArray(array[0], bytes, len)
return String(bytes) // TODO: encoding
}
@@ -188,9 +188,7 @@ fun CString.Companion.fromString(str: String?, placement: NativePlacement): CStr
val len = bytes.size
val nativeBytes = nativeHeap.allocArray<CInt8Var>(len + 1)
bytes.forEachIndexed { i, byte ->
nativeBytes[i].value = byte
}
nativeMemUtils.putByteArray(bytes, nativeBytes[0], len)
nativeBytes[len].value = 0
return CString.fromArray(nativeBytes)
@@ -222,4 +220,4 @@ class MemScope private constructor(private val arena: Arena) : NativePlacement b
inline fun <R> memScoped(block: MemScope.()->R): R {
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") // TODO: it is a hack
return MemScope.use(block)
}
}