This is ridiculous, but I simply cannot figure out...
# javascript
s
This is ridiculous, but I simply cannot figure out how to get a String from a specified codepoint in Kotlin/JS. I have many codepoints, and I need to make them into a String. And I can't find a way to do it in Kotlin/JS...in fact, all (two) constructors for
String
are deprecated in Kotlin/JS!
e
UTF-16 units or full (32-bit) Unicode codepoints? the former can be handled with `.concatToString()`; the latter has no built-in support
although I think you could sneakily
Copy code
js("String.fromCodePoint").apply(null, codepoints).unsafeCast<String>()
s
Yeah, doing 32-bit codepoints. I was hoping for a platform-agnostic solution, but I suppose that will work. Thanks.
e
building your own
Copy code
buildString {
    for (codepoint in codepoints) {
        if (codepoint and 0xFFFF.inv() == 0) {
            append(codepoint.toChar())
        } else {
            append((0xD800 or (codepoint and 0xFFC00 shr 10)).toChar())
            append((0xDC00 or (codepoint and 0x3FF)).toChar())
        }
    }
}
would be cross-platform
s
i assume that's converting it to UTF-8, and then appending the surrogate pairs?
e
directly converting UTF-32 to UTF-16… but I forgot to shift
the else clause should have
(codepoint - 0x10000)
s
thank you so much!