KT-11208 Remove readLine's lineSeparator parameter

This commit is contained in:
meztihn
2018-02-04 13:30:13 +05:00
committed by Ilya Gorbunov
parent f6c1886394
commit 68cb66ff0e
2 changed files with 24 additions and 33 deletions
+17 -15
View File
@@ -137,10 +137,7 @@ public inline fun println() {
} }
private const val BUFFER_SIZE: Int = 32 private const val BUFFER_SIZE: Int = 32
private const val LINE_SEPARATOR_MAX_LENGTH: Int = 2
// Since System.in can change its value on the course of program running, we should always delegate to current value.
private val stdin: InputStream
get() = System.`in`
private var cachedDecoder: CharsetDecoder? = null private var cachedDecoder: CharsetDecoder? = null
@@ -157,14 +154,13 @@ private fun decoderFor(charset: Charset): CharsetDecoder {
* *
* @return the line read or `null` if the input stream is redirected to a file and the end of file has been reached. * @return the line read or `null` if the input stream is redirected to a file and the end of file has been reached.
*/ */
fun readLine(lineSeparator: String = System.lineSeparator(), charset: Charset = Charset.defaultCharset()): String? = fun readLine(charset: Charset = Charset.defaultCharset()): String? = readLine(System.`in`, decoderFor(charset))
readLine(stdin, lineSeparator, decoderFor(charset))
internal fun readLine(inputStream: InputStream, lineSeparator: String, decoder: CharsetDecoder): String? { internal fun readLine(inputStream: InputStream, decoder: CharsetDecoder): String? {
require(decoder.maxCharsPerByte() <= 1) { "Encodings with multiple chars per byte are not supported" } require(decoder.maxCharsPerByte() <= 1) { "Encodings with multiple chars per byte are not supported" }
val byteBuffer = ByteBuffer.allocate(BUFFER_SIZE) val byteBuffer = ByteBuffer.allocate(BUFFER_SIZE)
val charBuffer = CharBuffer.allocate(lineSeparator.length) val charBuffer = CharBuffer.allocate(LINE_SEPARATOR_MAX_LENGTH)
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
var read = inputStream.read() var read = inputStream.read()
@@ -172,7 +168,7 @@ internal fun readLine(inputStream: InputStream, lineSeparator: String, decoder:
while (read != -1) { while (read != -1) {
byteBuffer.put(read.toByte()) byteBuffer.put(read.toByte())
if (decoder.tryDecode(byteBuffer, charBuffer, false)) { if (decoder.tryDecode(byteBuffer, charBuffer, false)) {
if (charBuffer.contentEquals(lineSeparator)) { if (charBuffer.containsLineSeparator()) {
break break
} }
if (!charBuffer.hasRemaining()) { if (!charBuffer.hasRemaining()) {
@@ -188,9 +184,16 @@ internal fun readLine(inputStream: InputStream, lineSeparator: String, decoder:
} }
with(charBuffer) { with(charBuffer) {
if (!contentEquals(lineSeparator)) { val length = position()
flip() val first = get(0)
while (hasRemaining()) stringBuilder.append(get()) val second = get(1)
flip()
when (length) {
2 -> {
if (!(first == '\r' && second == '\n')) stringBuilder.append(first)
if (second != '\n') stringBuilder.append(second)
}
1 -> if (first != '\n') stringBuilder.append(first)
} }
} }
@@ -208,9 +211,8 @@ private fun CharsetDecoder.tryDecode(byteBuffer: ByteBuffer, charBuffer: CharBuf
} }
} }
private fun CharBuffer.contentEquals(string: String): Boolean { private fun CharBuffer.containsLineSeparator(): Boolean {
flip() return get(1) == '\n' || get(0) == '\n'
return string.contentEquals(this).also { flipBack() }
} }
private fun Buffer.flipBack(): Buffer = apply { private fun Buffer.flipBack(): Buffer = apply {
+7 -18
View File
@@ -21,7 +21,7 @@ import java.nio.charset.Charset
import kotlin.test.* import kotlin.test.*
class ConsoleTest { class ConsoleTest {
private val defaultLineSeparator: String = "\\n" private val defaultLineSeparator: String = "\n"
@Test @Test
fun shouldReadEmptyLine() { fun shouldReadEmptyLine() {
@@ -45,14 +45,8 @@ class ConsoleTest {
@Test @Test
fun shouldReadWindowsLineSeparator() { fun shouldReadWindowsLineSeparator() {
val lineSeparator = "\\r\\n" val lineSeparator = "\r\n"
testReadLine("first${lineSeparator}second", "first", "second", lineSeparator = lineSeparator) testReadLine("first${lineSeparator}second", "first", "second")
}
@Test
fun shouldReadOldMacLineSeparator() {
val lineSeparator = "\\r"
testReadLine("first${lineSeparator}second", "first", "second", lineSeparator = lineSeparator)
} }
@Test @Test
@@ -60,20 +54,15 @@ class ConsoleTest {
testReadLine("first${defaultLineSeparator}second", "first", "second", charset = Charsets.UTF_32) testReadLine("first${defaultLineSeparator}second", "first", "second", charset = Charsets.UTF_32)
} }
private fun testReadLine( private fun testReadLine(text: String, vararg expected: String, charset: Charset = Charsets.UTF_8) {
text: String, val actual = readLines(text, charset)
vararg expected: String,
lineSeparator: String = defaultLineSeparator,
charset: Charset = Charsets.UTF_8
) {
val actual = readLines(text, lineSeparator, charset)
assertEquals(expected.asList(), actual) assertEquals(expected.asList(), actual)
} }
private fun readLines(text: String, lineSeparator: 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()
return generateSequence { readLine(stream, lineSeparator, 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 }
} }
} }