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

Conversation

fzhinkin
Copy link
Collaborator

@fzhinkin fzhinkin commented Dec 4, 2024

Closes #422

@fzhinkin fzhinkin requested a review from shanshin December 4, 2024 19:41
@@ -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.

core/jvm/test/BufferTest.kt Outdated Show resolved Hide resolved
@@ -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
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?

* @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants