Changed byte array copying to a more efficient mechanism.
This commit is contained in:
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 {
|
override fun toString(): String {
|
||||||
val array = reinterpret<CArray<CInt8Var>>()
|
val array = reinterpret<CArray<CInt8Var>>()
|
||||||
|
|
||||||
val bytes = ByteArray(this.length())
|
val len = this.length()
|
||||||
bytes.forEachIndexed { i, byte ->
|
val bytes = ByteArray(len)
|
||||||
bytes[i] = array[i].value
|
|
||||||
}
|
nativeMemUtils.getByteArray(array[0], bytes, len)
|
||||||
return String(bytes) // TODO: encoding
|
return String(bytes) // TODO: encoding
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,9 +188,7 @@ fun CString.Companion.fromString(str: String?, placement: NativePlacement): CStr
|
|||||||
val len = bytes.size
|
val len = bytes.size
|
||||||
val nativeBytes = nativeHeap.allocArray<CInt8Var>(len + 1)
|
val nativeBytes = nativeHeap.allocArray<CInt8Var>(len + 1)
|
||||||
|
|
||||||
bytes.forEachIndexed { i, byte ->
|
nativeMemUtils.putByteArray(bytes, nativeBytes[0], len)
|
||||||
nativeBytes[i].value = byte
|
|
||||||
}
|
|
||||||
nativeBytes[len].value = 0
|
nativeBytes[len].value = 0
|
||||||
|
|
||||||
return CString.fromArray(nativeBytes)
|
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 {
|
inline fun <R> memScoped(block: MemScope.()->R): R {
|
||||||
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") // TODO: it is a hack
|
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") // TODO: it is a hack
|
||||||
return MemScope.use(block)
|
return MemScope.use(block)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user