https://kotlinlang.org logo
m

Michal Klimczak

07/27/2021, 3:13 PM
I have some formatting defined in common code between ios and android target and just noticed that they treat special chars differently which messes my impl. I can't even properly paste it into slack, so I'll use screenshots to make it clearer, but take
twenti̩
as example. This
with a little dash at the bottom is treated by kotlin as two separate chars, but swift treats it as one. So when I defined formatting which spans across the whole word in shared code with
text.length
, it calculates it as 7 chars. When I use it in Swift, which thinks of it as 6 chars (via
*let* characters = Array(text)
), I get
Fatal error: Index out of range
. Any ideas how to work with these?
it has something to do with how both systems treat utf16 but I have no idea yet how to approach this
r

russhwolf

07/27/2021, 4:28 PM
Try
text.codePointAt(index)
and
text.codePointCount()
, but note they're JVM-only
👀 1
m

Michal Klimczak

07/27/2021, 4:37 PM
iunfortunately it returns the same thing:
Copy code
println("twenti̩: ${text.codePointCount(0, text.length)} | ${text.length}")

//twenti̩: 7 | 7
For anyone wondering - this helped https://kotlinlang.slack.com/archives/C0922A726/p1627401848232700. The problem is that kotlin represents strings with utf16 and swift with so called 21-bit unicode scalar value. Unfortunately I didn't find any better solution than to just create an interface
Copy code
interface TextLengthProvider {
    fun length(text: String) : Int
}
and implement it on both platforms (i.e. delegate completely to the swift impl). Could not have used expect/actual, because we're dealing with pure swift here.
3 Views