Scoop API

scoop:scoop.idna:0.1.0

src/native.scoop

Selected production source · src/native.scoop

1package scoop.idna
3@CLayout internal struct IdnaResult(val data: Ptr<Byte>?, val size: Long, val code: Int, val label: Long)
5internal fun convertIdna(text: String, mode: Int, operation: String): String {
6 val bytes = text.toByteArray()
7 @Unsafe {
8 val result = bytes.withDataPointer { data, count -> @Unsafe { return@withDataPointer idnaDomain(data, count, mode) } }
9 try { return unpackIdna(result, operation) } finally { idnaUnicodeFree(result.data, result.size) }
10 }
13internal fun convertPunycode(text: String, decode: Boolean, operation: String): String {
14 val bytes = text.toByteArray()
15 @Unsafe {
16 val result = bytes.withDataPointer { data, count -> @Unsafe { return@withDataPointer idnaPunycode(data, count, decode) } }
17 try { return unpackIdna(result, operation) } finally { idnaFree(result.data, result.size) }
18 }
21private fun unpackIdna(result: IdnaResult, operation: String): String {
22 @Unsafe {
23 if (result.code != 0) {
24 val detail = String.fromCString(idnaMessage(result.code))
25 throw IdnaException(operation, failureKind(result.code), None, detail,
26 if (result.label < 0L) None else Some(result.label))
27 }
28 val output = Array<Byte>(result.size) { index -> @Unsafe { return@Array result.data!!.load(index) } }
29 return String.fromUtf8(output)
30 }
33@Extern(name = "scoop_idna_domain") private fun idnaDomain(data: Ptr<Byte>, count: Long, mode: Int): IdnaResult
34@Extern(name = "scoop_idna_punycode") private fun idnaPunycode(data: Ptr<Byte>, count: Long, decode: Boolean): IdnaResult
35@Extern(name = "scoop_idna_message") private fun idnaMessage(code: Int): Ptr<Byte>
36@Extern(name = "scoop_unicode_free") internal fun idnaUnicodeFree(data: Ptr<Byte>?, count: Long)
37@Extern(name = "scoop_text_free") private fun idnaFree(data: Ptr<Byte>?, count: Long)
38@Extern(name = "scoop_idna_version") internal fun idnaVersion(): Ptr<Byte>
39@Extern(name = "scoop_text_unicode_version") internal fun idnaUnicodeVersion(): Ptr<Byte>