https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Stefan Oltmann

12/10/2023, 3:14 PM
I'm looking to remove Ktor from my multiplatform project where it's now basically only used to encode bytes to ISO 8859-1 strings decode ISO 8859 byte arrays to String objects. The regular
myByteArray.decodeToString()
decodes to an UTF-8 string, which is what I need most of the time. In some places I need a decodeToString() that does the same with ISO 8859-1. How to achieve that?
myByteArray.map { it.toChar() }.joinToString("")
as ChatGPT suggests is wrong according to my unit tests.
s

Sebastian Schuberth

12/10/2023, 3:18 PM
What about
String(myByteArray)
?
s

Stefan Oltmann

12/10/2023, 3:21 PM
That's not multiplatform API.
String(myCharArray)
is the only available option.
j

Jan Holešovský

12/10/2023, 8:01 PM
From your description, it is a bit unclear what do you actually want to achieve? Given that 'String' is a CharSequence, and Char is a 16-bit unicode character, what do you mean by 'encode bytes to ISO 8859-1 strings'? Even the
decodeToString()
you've mentioned converts from UTF-8 representation to a String (ie. to 16-bit unicode) - so do you actually want to convert from ByteArray that represents ISO8859-1 to a 'normal' String (16-bit unicode), or something else? Maybe it might help to understand if you share your original code?
s

Stefan Oltmann

12/10/2023, 8:07 PM
Ktor currently has no WASM support and so I wondered if there is an easier solution to this so that I can drop Ktor entirely.
And yes, now that you mention it… my description is confusing and wrong. 😅
Also the method name is wrong. It must be „decodeToStringFromIsoBytes()“ or something like that. The result is always a UTF8 string, because it’s Kotlin 😅
j

Jan Holešovský

12/10/2023, 8:14 PM
No worries, thanks for the explanation! 🙂
I'm not aware of a function that does that out of the box, so what if you just have an array of chars that correspond to the 0-255, and use something like the ChatGPT's recommended myByteArray.map { mapping[it] }.joinToString("") where the 'mapping' is the array of characters?
[Not tested etc. - but hope you get the idea :-)]
s

Stefan Oltmann

12/10/2023, 8:18 PM
Yes, sounds reasonable. I hope this exists somewhere 😄
j

Jan Holešovský

12/10/2023, 8:18 PM
[and not sure if this is the most performant way to write that either - but seems you are not on a critical path there?]
s

Stefan Oltmann

12/10/2023, 8:19 PM
Performance is important. Ashampoo Photos is supposed to read thousands of images very fast. 👀
For Windows Kim on JVM is currently 6 times faster than calling ExifTool using System.exec()
j

Jan Holešovský

12/10/2023, 8:22 PM
OK - then I'd profile first if the conversion function even appears in the profile 😉. If it does, I'd check how the myByteArray.map { mapping[it] }.joinToString("") performs against a normal for loop, and whether it is better to have the mapping as an array, or use a when(...) { ... }.
👍 1
Wrt. "hope this exists somewhere", ChatGPT has generated it for me with this prompt: "Please forget the above, the following is a new problem: In Kotlin, please give me a CharArray that has 256 values, and each value is a iso8859-1 character represented as Kotlin Char." followed by "Please do generate me such a array, instead of writing me how to generate it." 😉 Let me DM you with the result I've got - I don't mean to check the correctness...
s

Stefan Oltmann

12/10/2023, 8:30 PM
That’s a good prompt 👍