readLine: extend buffer to decode surrogate pairs from utf-8 correctly

CharBuffer had capacity to hold 2 chars, which was not enough to append
a surrogate pair when the buffer was not empty.

The buffer was extended and the decoding algorithm rewritten to deal with
a buffer of any length.

#KT-28572 Fixed
This commit is contained in:
Ilya Gorbunov
2018-11-30 06:40:31 +03:00
parent 297054037c
commit 7b08f6f8f1
2 changed files with 32 additions and 16 deletions
+10
View File
@@ -64,6 +64,16 @@ class ConsoleTest {
testReadLine("first${linuxLineSeparator}second", listOf("first", "second"), charset = Charsets.UTF_32)
}
@Test
fun readSurrogatePairs() {
val c = "\uD83D\uDC4D" // thumb-up emoji
testReadLine("$c$linuxLineSeparator", listOf(c))
testReadLine("e $c$linuxLineSeparator", listOf("e $c"))
testReadLine("$c$windowsLineSeparator", listOf(c))
testReadLine("e $c$c", listOf("e $c$c"))
testReadLine("e $c$linuxLineSeparator$c", listOf("e $c", c))
}
private fun testReadLine(text: String, expected: List<String>, charset: Charset = Charsets.UTF_8) {
val actual = readLines(text, charset)
assertEquals(expected, actual)