Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename string read/writeUtf8 methods #156

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions benchmarks/src/commonMain/kotlin/BufferOps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ open class Utf8StringBenchmark : BufferRWBenchmarkBase() {
@Benchmark
fun benchmark(): String {
val s = buffer.size
buffer.writeUtf8(string)
return buffer.readUtf8(buffer.size - s)
buffer.writeString(string)
return buffer.readString(buffer.size - s)
}
}

Expand Down Expand Up @@ -257,16 +257,16 @@ open class Utf8LineBenchmarkBase : BufferRWBenchmarkBase() {
open class Utf8LineBenchmark : Utf8LineBenchmarkBase() {
@Benchmark
fun benchmark(): String? {
buffer.writeUtf8(string)
return buffer.readUtf8Line()
buffer.writeString(string)
return buffer.readLine()
}
}

open class Utf8LineStrictBenchmark : Utf8LineBenchmarkBase() {
@Benchmark
fun benchmark(): String {
buffer.writeUtf8(string)
return buffer.readUtf8LineStrict()
buffer.writeString(string)
return buffer.readLineStrict()
}
}

Expand Down
16 changes: 8 additions & 8 deletions core/api/kotlinx-io-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ public final class kotlinx/io/SourceExtKt {
}

public final class kotlinx/io/Utf8Kt {
public static final fun readUtf8 (Lkotlinx/io/Buffer;)Ljava/lang/String;
public static final fun readUtf8 (Lkotlinx/io/Source;)Ljava/lang/String;
public static final fun readUtf8 (Lkotlinx/io/Source;J)Ljava/lang/String;
public static final fun readUtf8Line (Lkotlinx/io/Source;)Ljava/lang/String;
public static final fun readUtf8LineStrict (Lkotlinx/io/Source;J)Ljava/lang/String;
public static synthetic fun readUtf8LineStrict$default (Lkotlinx/io/Source;JILjava/lang/Object;)Ljava/lang/String;
public static final fun writeUtf8 (Lkotlinx/io/Sink;Ljava/lang/String;II)V
public static synthetic fun writeUtf8$default (Lkotlinx/io/Sink;Ljava/lang/String;IIILjava/lang/Object;)V
public static final fun readLine (Lkotlinx/io/Source;)Ljava/lang/String;
public static final fun readLineStrict (Lkotlinx/io/Source;J)Ljava/lang/String;
public static synthetic fun readLineStrict$default (Lkotlinx/io/Source;JILjava/lang/Object;)Ljava/lang/String;
public static final fun readString (Lkotlinx/io/Buffer;)Ljava/lang/String;
public static final fun readString (Lkotlinx/io/Source;)Ljava/lang/String;
public static final fun readString (Lkotlinx/io/Source;J)Ljava/lang/String;
public static final fun writeString (Lkotlinx/io/Sink;Ljava/lang/String;II)V
public static synthetic fun writeString$default (Lkotlinx/io/Sink;Ljava/lang/String;IIILjava/lang/Object;)V
}

public final class kotlinx/io/files/Path {
Expand Down
2 changes: 1 addition & 1 deletion core/common/src/SinkExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public fun Sink.writeDecimalLong(long: Long) {
if (v < 0L) {
v = -v
if (v < 0L) { // Only true for Long.MIN_VALUE.
writeUtf8("-9223372036854775808")
writeString("-9223372036854775808")
return
}
negative = true
Expand Down
4 changes: 2 additions & 2 deletions core/common/src/SourceExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public fun Source.readDecimalLong(): Long {
writeByte(b)

if (!negative) readByte() // Skip negative sign.
throw NumberFormatException("Number too large: ${readUtf8()}")
throw NumberFormatException("Number too large: ${readString()}")
}
}
value = value * 10L + digit
Expand Down Expand Up @@ -143,7 +143,7 @@ public fun Source.readHexadecimalUnsignedLong(): Long {
with(Buffer()) {
writeHexadecimalUnsignedLong(result)
writeByte(b)
throw NumberFormatException("Number too large: " + readUtf8())
throw NumberFormatException("Number too large: " + readString())
}
}
result = result.shl(4) + bDigit
Expand Down
22 changes: 11 additions & 11 deletions core/common/src/Utf8.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ package kotlinx.io
import kotlinx.io.internal.*

/**
* Returns the number of bytes used to encode the slice of `string` as UTF-8 when using [Sink.writeUtf8].
* Returns the number of bytes used to encode the slice of `string` as UTF-8 when using [Sink.writeString].
*
* @param startIndex the index (inclusive) of the first character to encode, `0` by default.
* @param endIndex the index (exclusive) of the character past the last character to encode, `string.length` by default.
Expand Down Expand Up @@ -140,7 +140,7 @@ internal fun Sink.writeUtf8CodePoint(codePoint: Int): Unit =
* @throws IllegalStateException when the sink is closed.
*/
@OptIn(DelicateIoApi::class)
public fun Sink.writeUtf8(string: String, startIndex: Int = 0, endIndex: Int = string.length): Unit =
public fun Sink.writeString(string: String, startIndex: Int = 0, endIndex: Int = string.length): Unit =
writeToInternalBuffer { it.commonWriteUtf8(string, startIndex, endIndex) }

/**
Expand All @@ -151,7 +151,7 @@ public fun Sink.writeUtf8(string: String, startIndex: Int = 0, endIndex: Int = s
* @throws IllegalStateException when the source is closed.
*/
@OptIn(InternalIoApi::class)
public fun Source.readUtf8(): String {
public fun Source.readString(): String {
var req: Long = Segment.SIZE.toLong()
while (request(req)) {
req *= 2
Expand All @@ -164,7 +164,7 @@ public fun Source.readUtf8(): String {
*
* Returns the empty string if this buffer is empty.
*/
public fun Buffer.readUtf8(): String {
public fun Buffer.readString(): String {
return commonReadUtf8(size)
}

Expand All @@ -178,7 +178,7 @@ public fun Buffer.readUtf8(): String {
* @throws IllegalStateException when the source is closed.
*/
@OptIn(InternalIoApi::class)
public fun Source.readUtf8(byteCount: Long): String {
public fun Source.readString(byteCount: Long): String {
require(byteCount)
return buffer.commonReadUtf8(byteCount)
}
Expand Down Expand Up @@ -220,15 +220,15 @@ internal fun Buffer.readUtf8CodePoint(): Int {
}

/**
* Removes and returns characters up to but not including the next line break. A line break is
* Removes and returns UTF-8 encoded characters up to but not including the next line break. A line break is
* either `"\n"` or `"\r\n"`; these characters are not included in the result.
*
* On the end of the stream this method returns null. If the source doesn't end with a line break, then
* an implicit line break is assumed. Null is returned once the source is exhausted.
*
* @throws IllegalStateException when the source is closed.
*/
public fun Source.readUtf8Line(): String? {
public fun Source.readLine(): String? {
if (!request(1)) return null

val peekSource = peek()
Expand All @@ -247,13 +247,13 @@ public fun Source.readUtf8Line(): String? {
}
offset++
}
val line = readUtf8(offset)
val line = readString(offset)
skip(newlineSize)
return line
}

/**
* Removes and returns characters up to but not including the next line break, throwing
* Removes and returns UTF-8 encoded characters up to but not including the next line break, throwing
* [EOFException] if a line break was not encountered. A line break is either `"\n"` or `"\r\n"`;
* these characters are not included in the result.
*
Expand All @@ -270,7 +270,7 @@ public fun Source.readUtf8Line(): String? {
* @throws IllegalStateException when the source is closed.
* @throws IllegalArgumentException when [limit] is negative.
*/
public fun Source.readUtf8LineStrict(limit: Long = Long.MAX_VALUE): String {
public fun Source.readLineStrict(limit: Long = Long.MAX_VALUE): String {
require(limit >= 0) { "limit ($limit) < 0" }
require(1)

Expand Down Expand Up @@ -300,7 +300,7 @@ public fun Source.readUtf8LineStrict(limit: Long = Long.MAX_VALUE): String {
}
}
if (newlineSize == 0L) throw EOFException()
val line = readUtf8(offset)
val line = readString(offset)
skip(newlineSize)
return line
}
Expand Down
58 changes: 29 additions & 29 deletions core/common/test/AbstractSinkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ abstract class AbstractSinkTest internal constructor(

@Test
fun writeNothing() {
sink.writeUtf8("")
sink.writeString("")
sink.flush()
assertEquals(0, data.size)
}
Expand All @@ -89,12 +89,12 @@ abstract class AbstractSinkTest internal constructor(

@Test
fun writeLastByteInSegment() {
sink.writeUtf8("a".repeat(Segment.SIZE - 1))
sink.writeString("a".repeat(Segment.SIZE - 1))
sink.writeByte(0x20)
sink.writeByte(0x21)
sink.flush()
assertEquals(listOf(Segment.SIZE, 1), segmentSizes(data))
assertEquals("a".repeat(Segment.SIZE - 1), data.readUtf8(Segment.SIZE - 1L))
assertEquals("a".repeat(Segment.SIZE - 1), data.readString(Segment.SIZE - 1L))
assertEquals("Buffer(size=2 hex=2021)", data.toString())
}

Expand Down Expand Up @@ -138,23 +138,23 @@ abstract class AbstractSinkTest internal constructor(

@Test
fun writeLastIntegerInSegment() {
sink.writeUtf8("a".repeat(Segment.SIZE - 4))
sink.writeString("a".repeat(Segment.SIZE - 4))
sink.writeInt(-0x543210ff)
sink.writeInt(-0x789abcdf)
sink.flush()
assertEquals(listOf(Segment.SIZE, 4), segmentSizes(data))
assertEquals("a".repeat(Segment.SIZE - 4), data.readUtf8(Segment.SIZE - 4L))
assertEquals("a".repeat(Segment.SIZE - 4), data.readString(Segment.SIZE - 4L))
assertEquals("Buffer(size=8 hex=abcdef0187654321)", data.toString())
}

@Test
fun writeIntegerDoesNotQuiteFitInSegment() {
sink.writeUtf8("a".repeat(Segment.SIZE - 3))
sink.writeString("a".repeat(Segment.SIZE - 3))
sink.writeInt(-0x543210ff)
sink.writeInt(-0x789abcdf)
sink.flush()
assertEquals(listOf(Segment.SIZE - 3, 8), segmentSizes(data))
assertEquals("a".repeat(Segment.SIZE - 3), data.readUtf8(Segment.SIZE - 3L))
assertEquals("a".repeat(Segment.SIZE - 3), data.readString(Segment.SIZE - 3L))
assertEquals("Buffer(size=8 hex=abcdef0187654321)", data.toString())
}

Expand Down Expand Up @@ -192,12 +192,12 @@ abstract class AbstractSinkTest internal constructor(
@Test
fun writeAll() {
val source = Buffer()
source.writeUtf8("abcdef")
source.writeString("abcdef")

assertEquals(6, sink.transferFrom(source))
assertEquals(0, source.size)
sink.flush()
assertEquals("abcdef", data.readUtf8())
assertEquals("abcdef", data.readString())
}

@Test
Expand All @@ -210,53 +210,53 @@ abstract class AbstractSinkTest internal constructor(
@Test
fun writeSource() {
val source = Buffer()
source.writeUtf8("abcdef")
source.writeString("abcdef")

// Force resolution of the Source method overload.
sink.write(source as RawSource, 4)
sink.flush()
assertEquals("abcd", data.readUtf8())
assertEquals("ef", source.readUtf8())
assertEquals("abcd", data.readString())
assertEquals("ef", source.readString())
}

@Test
fun writeSourceReadsFully() {
val source = object : RawSource by Buffer() {
override fun readAtMostTo(sink: Buffer, byteCount: Long): Long {
sink.writeUtf8("abcd")
sink.writeString("abcd")
return 4
}
}

sink.write(source, 8)
sink.flush()
assertEquals("abcdabcd", data.readUtf8())
assertEquals("abcdabcd", data.readString())
}

@Test
fun writeSourcePropagatesEof() {
val source: RawSource = Buffer().also { it.writeUtf8("abcd") }
val source: RawSource = Buffer().also { it.writeString("abcd") }

assertFailsWith<EOFException> {
sink.write(source, 8)
}

// Ensure that whatever was available was correctly written.
sink.flush()
assertEquals("abcd", data.readUtf8())
assertEquals("abcd", data.readString())
}

@Test
fun writeBufferThrowsIAE() {
val source = Buffer()
source.writeUtf8("abcd")
source.writeString("abcd")

assertFailsWith<IllegalArgumentException> {
sink.write(source, 8)
}

sink.flush()
assertEquals("", data.readUtf8())
assertEquals("", data.readString())
}

@Test
Expand Down Expand Up @@ -348,11 +348,11 @@ abstract class AbstractSinkTest internal constructor(
private fun assertLongDecimalString(string: String, value: Long) {
with(sink) {
writeDecimalLong(value)
writeUtf8("zzz")
writeString("zzz")
flush()
}
val expected = "${string}zzz"
val actual = data.readUtf8()
val actual = data.readString()
assertEquals(expected, actual, "$value expected $expected but was $actual")
}

Expand All @@ -371,33 +371,33 @@ abstract class AbstractSinkTest internal constructor(
private fun assertLongHexString(value: Long) {
with(sink) {
writeHexadecimalUnsignedLong(value)
writeUtf8("zzz")
writeString("zzz")
flush()
}
val expected = "${value.toHexString()}zzz"
val actual = data.readUtf8()
val actual = data.readString()
assertEquals(expected, actual, "$value expected $expected but was $actual")
}

@Test
fun writeUtf8FromIndex() {
sink.writeUtf8("12345", 3)
sink.writeString("12345", 3)
sink.emit()
assertEquals("45", data.readUtf8())
assertEquals("45", data.readString())
}

@Test
fun writeUtf8FromRange() {
sink.writeUtf8("0123456789", 4, 7)
sink.writeString("0123456789", 4, 7)
sink.emit()
assertEquals("456", data.readUtf8())
assertEquals("456", data.readString())
}

@Test
fun writeUtf8WithInvalidIndexes() {
assertFailsWith<IndexOutOfBoundsException> { sink.writeUtf8("hello", startIndex = -1) }
assertFailsWith<IndexOutOfBoundsException> { sink.writeUtf8("hello", startIndex = 0, endIndex = 6) }
assertFailsWith<IllegalArgumentException> { sink.writeUtf8("hello", startIndex = 6) }
assertFailsWith<IndexOutOfBoundsException> { sink.writeString("hello", startIndex = -1) }
assertFailsWith<IndexOutOfBoundsException> { sink.writeString("hello", startIndex = 0, endIndex = 6) }
assertFailsWith<IllegalArgumentException> { sink.writeString("hello", startIndex = 6) }
}

@Test
Expand Down
Loading