[stdlib] Specify EXACTLY_ONCE contract for useLines
This commit is contained in:
committed by
Space Team
parent
ed8c71442b
commit
7175680917
@@ -16,6 +16,7 @@ import java.nio.file.Files
|
||||
import java.nio.file.OpenOption
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.StandardOpenOption
|
||||
import kotlin.contracts.*
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
/**
|
||||
@@ -277,6 +278,9 @@ public inline fun Path.readLines(charset: Charset = Charsets.UTF_8): List<String
|
||||
@Throws(IOException::class)
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Path.useLines(charset: Charset = Charsets.UTF_8, block: (Sequence<String>) -> T): T {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return Files.newBufferedReader(this, charset).use { block(it.lineSequence()) }
|
||||
}
|
||||
|
||||
|
||||
@@ -158,9 +158,13 @@ class PathReadWriteTest : AbstractPathTest() {
|
||||
|
||||
assertEquals(listOf("Hello", "World"), file.readLines())
|
||||
|
||||
file.useLines {
|
||||
assertEquals(listOf("Hello", "World"), it.toList())
|
||||
val lines: List<String>
|
||||
val linesResult = file.useLines {
|
||||
lines = it.toList()
|
||||
lines
|
||||
}
|
||||
assertEquals(listOf("Hello", "World"), lines)
|
||||
assertEquals(lines, linesResult)
|
||||
|
||||
val text = file.inputStream().reader().readText()
|
||||
assertTrue("Hello" in text)
|
||||
|
||||
@@ -15,6 +15,8 @@ import java.util.*
|
||||
import java.nio.charset.Charset
|
||||
import java.nio.charset.CharsetEncoder
|
||||
import java.nio.charset.CodingErrorAction
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
import kotlin.math.ceil
|
||||
|
||||
|
||||
@@ -279,5 +281,9 @@ public fun File.readLines(charset: Charset = Charsets.UTF_8): List<String> {
|
||||
* @param charset character set to use. By default uses UTF-8 charset.
|
||||
* @return the value returned by [block].
|
||||
*/
|
||||
public inline fun <T> File.useLines(charset: Charset = Charsets.UTF_8, block: (Sequence<String>) -> T): T =
|
||||
bufferedReader(charset).use { block(it.lineSequence()) }
|
||||
public inline fun <T> File.useLines(charset: Charset = Charsets.UTF_8, block: (Sequence<String>) -> T): T {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return bufferedReader(charset).use { block(it.lineSequence()) }
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import java.io.*
|
||||
import java.nio.charset.Charset
|
||||
import java.net.URL
|
||||
import java.util.NoSuchElementException
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
import kotlin.internal.*
|
||||
|
||||
|
||||
@@ -48,8 +50,12 @@ public fun Reader.readLines(): List<String> {
|
||||
* the processing is complete.
|
||||
* @return the value returned by [block].
|
||||
*/
|
||||
public inline fun <T> Reader.useLines(block: (Sequence<String>) -> T): T =
|
||||
buffered().use { block(it.lineSequence()) }
|
||||
public inline fun <T> Reader.useLines(block: (Sequence<String>) -> T): T {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return buffered().use { block(it.lineSequence()) }
|
||||
}
|
||||
|
||||
/** Creates a new reader for the string. */
|
||||
@kotlin.internal.InlineOnly
|
||||
|
||||
@@ -47,9 +47,13 @@ class ReadWriteTest {
|
||||
|
||||
assertEquals(listOf("Hello", "World"), sample().readLines())
|
||||
|
||||
sample().useLines {
|
||||
assertEquals(listOf("Hello", "World"), it.toList())
|
||||
val lines: List<String>
|
||||
val linesResult = sample().useLines {
|
||||
lines = it.toList()
|
||||
lines
|
||||
}
|
||||
assertEquals(listOf("Hello", "World"), lines)
|
||||
assertEquals(lines, linesResult)
|
||||
|
||||
|
||||
var reader = StringReader("")
|
||||
@@ -94,9 +98,13 @@ class ReadWriteTest {
|
||||
|
||||
assertEquals(arrayListOf("Hello", "World"), file.readLines())
|
||||
|
||||
file.useLines {
|
||||
assertEquals(arrayListOf("Hello", "World"), it.toList())
|
||||
val lines: List<String>
|
||||
val linesResult = file.useLines {
|
||||
lines = it.toList()
|
||||
lines
|
||||
}
|
||||
assertEquals(listOf("Hello", "World"), lines)
|
||||
assertEquals(lines, linesResult)
|
||||
|
||||
val text = file.inputStream().reader().readText()
|
||||
assertTrue(text.contains("Hello"))
|
||||
|
||||
Reference in New Issue
Block a user