Platform library for iconv.h on macos, ios and linux.
This commit is contained in:
committed by
alexander-gorshenev
parent
f975c1d251
commit
18b7bb31fc
@@ -0,0 +1,39 @@
|
||||
import kotlinx.cinterop.*
|
||||
import platform.iconv.*
|
||||
import platform.posix.size_tVar
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
val sourceByteArray = "Hello!".toUtf8()
|
||||
|
||||
val golden = listOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21)
|
||||
|
||||
memScoped {
|
||||
|
||||
val sourceLength = alloc<size_tVar>()
|
||||
val destLength = alloc<size_tVar>()
|
||||
|
||||
val sourceBytes = allocArrayOf(sourceByteArray)
|
||||
val destBytes = allocArray<ByteVar>(golden.size)
|
||||
|
||||
val sourcePtr = alloc<CArrayPointerVar<ByteVar>>()
|
||||
sourcePtr.value = sourceBytes
|
||||
|
||||
val destPtr = alloc<CArrayPointerVar<ByteVar>>()
|
||||
destPtr.value = destBytes
|
||||
|
||||
sourceLength.value = sourceByteArray.size.signExtend();
|
||||
destLength.value = golden.size.signExtend();
|
||||
|
||||
val conversion = iconv_open("UTF-8", "LATIN1")
|
||||
|
||||
iconv(conversion, sourcePtr.ptr, sourceLength.ptr, destPtr.ptr, destLength.ptr)
|
||||
|
||||
golden.forEachIndexed { index, it ->
|
||||
println("$it ${destBytes[index]}")
|
||||
it == destBytes[index].toInt()
|
||||
}
|
||||
|
||||
iconv_close(conversion)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user