EastAsianText

Decodes the multi-byte East Asian encodings that subtitle files still arrive in.

Shift_JIS, EUC-JP, GBK, Big5 and EUC-KR, each decoded the way the WHATWG Encoding Standard says a browser decodes it, from tables generated out of the standard's own index files. The tables are Kotlin, so a file reads the same on every target. Most targets have no decoder of their own for these, and the platforms that have one disagree with each other on some codes.

GBK is read with the standard's gb18030 decoder, which also reads the four-byte sequences of GB18030. Big5 includes the Hong Kong supplementary characters. A byte sequence that the encoding cannot read becomes U+FFFD. Each table is expanded on first use, into about 100 KB at most.

Functions

Link copied to clipboard
fun decode(bytes: ByteArray, encoding: String): String?

Decodes bytes as encoding, or answers null when encoding is not one of the five.