Matt Nelson
12/01/2025, 4:33 PMUTF-8 encoding/decoding to a library of mine. I have 2 replacement strategies for invalid sequences available from commonMain, and am wondering which I should use for the default.
class UTF8: EncoderDecoder {
class ReplacementStrategy private constructor {
companion object {
// Encodes/decodes UTF-8 the same way Kotlin/JVM does.
val U_003F // ...
// Encodes/decodes UTF-8 the same way Kotlin Js/WasmJs/Wasi/Native does.
val U_FFFD // ...
// Initalizes to U_003F on Jvm, and U_FFFD on all other platforms.
val KOTLIN // ...
// Throw on invalid sequences
val THROW // ...
}
}
}
The current default that I am using is UTF8.ReplacementStrategy.KOTLIN, but wondering if that is the best choice before publishing this feature release.
Kotlin's String.decodeToByteArray() and ByteArray.encodeToString() implementations are different on non-JVM platforms than JVM, producing different results; the above pseudo-code outlines that.
Context: okio and kotlinx-io UTF-8 implementations are the equivalent of UTF8.ReplacementStrategy.U_003F
Thoughts?cketti
12/01/2025, 5:07 PMjessewilson
12/21/2025, 11:01 PMjessewilson
12/21/2025, 11:01 PMjessewilson
12/21/2025, 11:03 PMMatt Nelson
12/22/2025, 2:52 PMUTF8.ReplacementStrategy.KOTLIN for the UTF8.Default static companion object instance, and also as the default value for UTF8.Builder . Added an additional UTF8.ThrowOnInvalid static object instance configured with the THROW strategy.