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

Refine Source|Buffer.indexOf(ByteBuffer) contracts #424

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 20 additions & 7 deletions core/common/src/ByteStrings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public fun Source.readByteString(byteCount: Int): ByteString {
* expands the source's buffer as necessary until [byteString] is found. This reads an unbounded number of
* bytes into the buffer. Returns `-1` if the stream is exhausted before the requested bytes are found.
*
* For empty byte strings this function always return `0`.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #423. I'm not sure how to handle empty byte strings yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this is really better than fetching the data, provided that we always ignore what has been fetched.

Maybe it should be explicitly written that in the case of an empty byte string, the data will never be read from the source?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should be explicitly written that in the case of an empty byte string, the data will never be read from the source?

Then, it would be inconsistent with the Buffer's "overload". These two functions already behave differently, but I'd like to keep them as close to each other as possible.

*
* @param byteString the sequence of bytes to find within the source.
* @param startIndex the index into the source to start searching from.
*
Expand All @@ -96,11 +98,9 @@ public fun Source.readByteString(byteCount: Int): ByteString {
*/
@OptIn(InternalIoApi::class, UnsafeByteStringApi::class)
public fun Source.indexOf(byteString: ByteString, startIndex: Long = 0): Long {
require(startIndex >= 0) { "startIndex: $startIndex" }
require(startIndex >= 0) { "startIndex should be non-negative, but was: $startIndex" }

if (byteString.isEmpty()) {
return 0
}
if (byteString.isEmpty()) return 0

var offset = startIndex
while (request(offset + byteString.size)) {
Expand All @@ -117,11 +117,24 @@ public fun Source.indexOf(byteString: ByteString, startIndex: Long = 0): Long {
return -1
}

/**
* Returns the index of the first match for [byteString] in the buffer at or after [startIndex].
* Returns `-1` if the stream is exhausted before the requested bytes are found.
*
* For empty byte strings this function always return `0`.
*
* @param byteString the sequence of bytes to find within the buffer.
* @param startIndex the index into the buffer to start searching from.
*
* @throws IllegalArgumentException if [startIndex] is negative or if it is greater of equal to buffer's [Buffer.size].
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this behavior is also inconsistent with CharSequence.indexOf, which I'm considering maybe not as a paragon, but as something to be aligned with.

*
* @sample kotlinx.io.samples.ByteStringSamples.indexOfByteString
*/
@OptIn(UnsafeByteStringApi::class)
public fun Buffer.indexOf(byteString: ByteString, startIndex: Long = 0): Long {
require(startIndex <= size) {
"startIndex ($startIndex) should not exceed size ($size)"
}
require(startIndex <= size) { "startIndex ($startIndex) should not exceed size ($size)" }
require(startIndex >= 0) { "startIndex should be non-negative, but was: $startIndex" }

if (byteString.isEmpty()) return 0
if (startIndex > size - byteString.size) return -1L

Expand Down
16 changes: 16 additions & 0 deletions core/common/test/CommonBufferTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import kotlinx.io.bytestring.ByteString
import kotlinx.io.bytestring.encodeToByteString
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

Expand Down Expand Up @@ -618,4 +619,19 @@ class CommonBufferTest {
assertEquals(null, dst.head?.prev)
assertEquals(null, dst.tail?.next)
}

@Test
fun indexOfByteString() {
val buffer = Buffer()
buffer.writeString("hello")

assertFailsWith<IllegalArgumentException> { buffer.indexOf(ByteString(1, 2, 3), -1) }
assertFailsWith<IllegalArgumentException> { buffer.indexOf(ByteString(1, 2, 3), 10) }

assertEquals(2, buffer.indexOf("ll".encodeToByteString()))
assertEquals(2, buffer.indexOf("ll".encodeToByteString(), 2))
assertEquals(-1, buffer.indexOf("ll".encodeToByteString(), 3))
assertEquals(-1, buffer.indexOf("hello world".encodeToByteString()))
assertEquals(0, buffer.indexOf(ByteString()))
}
}