Minor code improvements, add more test cases #KT-11208

Compare new readLine with the reference implementation in BufferedReader.
This commit is contained in:
Ilya Gorbunov
2018-03-28 18:57:53 +03:00
parent 12e427b4b5
commit d2a235b752
2 changed files with 33 additions and 19 deletions
+2 -3
View File
@@ -179,7 +179,6 @@ internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String
val length = position()
val first = get(0)
val second = get(1)
flip()
when (length) {
2 -> {
if (!(first == '\r' && second == '\n')) stringBuilder.append(first)
@@ -199,7 +198,7 @@ private fun CharsetDecoder.tryDecode(byteBuffer: ByteBuffer, charBuffer: CharBuf
if (isError) throwException()
}
return (charBuffer.position() > positionBefore).also { isDecoded ->
byteBuffer.apply { if (isDecoded) clear() else flipBack() }
if (isDecoded) byteBuffer.clear() else byteBuffer.flipBack()
}
}
@@ -207,7 +206,7 @@ private fun CharBuffer.containsLineSeparator(): Boolean {
return get(1) == '\n' || get(0) == '\n'
}
private fun Buffer.flipBack(): Buffer = apply {
private fun Buffer.flipBack() {
position(limit())
limit(capacity())
}