-
Notifications
You must be signed in to change notification settings - Fork 61
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
fzhinkin
wants to merge
3
commits into
develop
Choose a base branch
from
422-fix-buffer-index-of
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`. | ||
* | ||
* @param byteString the sequence of bytes to find within the source. | ||
* @param startIndex the index into the source to start searching from. | ||
* | ||
|
@@ -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)) { | ||
|
@@ -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]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, this behavior is also inconsistent 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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.