Minor code improvements, add more test cases #KT-11208
Compare new readLine with the reference implementation in BufferedReader.
This commit is contained in:
@@ -179,7 +179,6 @@ internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String
|
|||||||
val length = position()
|
val length = position()
|
||||||
val first = get(0)
|
val first = get(0)
|
||||||
val second = get(1)
|
val second = get(1)
|
||||||
flip()
|
|
||||||
when (length) {
|
when (length) {
|
||||||
2 -> {
|
2 -> {
|
||||||
if (!(first == '\r' && second == '\n')) stringBuilder.append(first)
|
if (!(first == '\r' && second == '\n')) stringBuilder.append(first)
|
||||||
@@ -199,7 +198,7 @@ private fun CharsetDecoder.tryDecode(byteBuffer: ByteBuffer, charBuffer: CharBuf
|
|||||||
if (isError) throwException()
|
if (isError) throwException()
|
||||||
}
|
}
|
||||||
return (charBuffer.position() > positionBefore).also { isDecoded ->
|
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'
|
return get(1) == '\n' || get(0) == '\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Buffer.flipBack(): Buffer = apply {
|
private fun Buffer.flipBack() {
|
||||||
position(limit())
|
position(limit())
|
||||||
limit(capacity())
|
limit(capacity())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2017 JetBrains s.r.o.
|
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
*
|
* that can be found in the license/LICENSE.txt file.
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io
|
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||||
|
@file:kotlin.jvm.JvmVersion
|
||||||
|
package test.io
|
||||||
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
@@ -30,8 +21,19 @@ class ConsoleTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun shouldReadOneLetter() {
|
fun shouldReadSingleLine() {
|
||||||
testReadLine("a", listOf("a"))
|
for (length in 1..3) {
|
||||||
|
val line = buildString { repeat(length) { append('a' + it) } }
|
||||||
|
testReadLine(line, listOf(line))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun trailingEmptyLineIsIgnored() {
|
||||||
|
testReadLine(linuxLineSeparator, listOf(""))
|
||||||
|
testReadLine(windowsLineSeparator, listOf(""))
|
||||||
|
testReadLine("a$linuxLineSeparator", listOf("a"))
|
||||||
|
testReadLine("a$windowsLineSeparator", listOf("a"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -47,6 +49,9 @@ class ConsoleTest {
|
|||||||
@Test
|
@Test
|
||||||
fun shouldReadConsecutiveEmptyLines() {
|
fun shouldReadConsecutiveEmptyLines() {
|
||||||
testReadLine("$linuxLineSeparator$linuxLineSeparator", listOf("", ""))
|
testReadLine("$linuxLineSeparator$linuxLineSeparator", listOf("", ""))
|
||||||
|
testReadLine("$linuxLineSeparator$windowsLineSeparator", listOf("", ""))
|
||||||
|
testReadLine("$windowsLineSeparator$linuxLineSeparator", listOf("", ""))
|
||||||
|
testReadLine("$windowsLineSeparator$windowsLineSeparator", listOf("", ""))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -62,14 +67,24 @@ class ConsoleTest {
|
|||||||
private fun testReadLine(text: String, expected: List<String>, charset: Charset = Charsets.UTF_8) {
|
private fun testReadLine(text: String, expected: List<String>, charset: Charset = Charsets.UTF_8) {
|
||||||
val actual = readLines(text, charset)
|
val actual = readLines(text, charset)
|
||||||
assertEquals(expected, actual)
|
assertEquals(expected, actual)
|
||||||
|
val referenceExpected = readLinesReference(text, charset)
|
||||||
|
assertEquals(referenceExpected, actual, "Comparing to reference readLine")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun readLines(text: String, charset: Charset): List<String> {
|
private fun readLines(text: String, charset: Charset): List<String> {
|
||||||
text.byteInputStream(charset).use { stream ->
|
text.byteInputStream(charset).use { stream ->
|
||||||
val decoder = charset.newDecoder()
|
val decoder = charset.newDecoder()
|
||||||
|
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||||
return generateSequence { readLine(stream, decoder) }.toList().also {
|
return generateSequence { readLine(stream, decoder) }.toList().also {
|
||||||
assertTrue("All bytes should be read") { stream.read() == -1 }
|
assertTrue("All bytes should be read") { stream.read() == -1 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun readLinesReference(text: String, charset: Charset): List<String> {
|
||||||
|
text.byteInputStream(charset).bufferedReader(charset).use { reader ->
|
||||||
|
return generateSequence { reader.readLine() }.toList()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user