https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
e

Edoardo Luppi

11/06/2023, 9:52 AM
I need to read and write
.properties
files in ISO-8859-1. Does Native offer something related to this charset or am I on my own?
e

ephemient

11/06/2023, 4:31 PM
you'll need to use platform-specific facilities or write your own
e

Edoardo Luppi

11/06/2023, 4:32 PM
Thanks! Yeah I was experimenting, see the other posts.
@ephemient btw, does K/N encode string in UTF-16 or UTF-8 by default?
There are contradicting information around. Docs suggest UTF-8, this post suggests UTF-16.
e

ephemient

11/06/2023, 4:54 PM
I can see how it's confusing, but it's not really
conceptually
String
is a read-only wrapper around
CharArray
, for Java compatibility. a
Char
is a UTF-16 unit.
Kotlin/Native always uses
UTF-8
when converting between
kotlin.String
and anything outside Kotlin, such as
.toKString()
or
.encodeToByteArray()
e

Edoardo Luppi

11/06/2023, 4:58 PM
So basically the backing array is always UTF-16 units, which are then converted transparently to UTF-8 for any kind of C-interop.
e

ephemient

11/06/2023, 5:00 PM
hypothetically that doesn't need to be the case; for example, on JVM String has a specialization for Latin-1 only strings. but it should not have any behavior noticeably different than that, yes
e

Edoardo Luppi

11/06/2023, 5:02 PM
I guess for the ones working with Windows APIs the fact a string is represented as UTF-16 is an advantage. And btw, now I can see what you're saying:
Copy code
public val String.cstr: CValues<ByteVar>
    get() = if (isEmpty()) EmptyCString else CString(encodeToUtf8(this))

public val String.wcstr: CValues<UShortVar>
    get() = U16CString(this.toCharArray())
Only the
cstr
performs a conversion. And on the runtime the string is identified as UTF-16
Copy code
const KChar* utf16 = CharArrayAddressOfElementAt(thiz, start);