-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
readUntilDelimiter*: read only if buffer not full #10557
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
Conversation
|
Not all readers support seekBy. I think a better solution would be to avoid reading at all if the output buffer is full. |
Yeah, it's so true that most streams don't support rewinding. #mybad I actually thought about that a few iterations before. Without reading the next byte, how's it going to be able to stop and return what can be a full buffer? |
|
For instance: test "Reader.readUntilDelimiter EndOfStream 2" {
var buf: [4]u8 = undefined;
const reader = std.io.fixedBufferStream("1234\n").reader();
try std.testing.expectEqualStrings("1234", try reader.readUntilDelimiter(&buf, '\n'));
try std.testing.expectError(error.EndOfStream, reader.readUntilDelimiter(&buf, '\n'));
} |
|
That test would no longer pass, the buffer would need to be at least 5 bytes long. An easy way to communicate that might be to say that the output buffer needs to contain (or be terminated by) the delimiter if one was found. |
89f5828 to
d789552
Compare
|
Thanks, @SpexGuy! PR updated. I would appreciate reading from you again, while I write tests for the remaining |
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.
Thanks for doing this work!
d789552 to
0e09fe1
Compare
|
You're welcome. I'm happy to contribute 🙂 And this is fun! It makes much sense after I thought about what you wrote. Thanks, @SpexGuy! |
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.
Just noticed one more thing :)
| /// large enough to hold the entire contents, `error.StreamTooLong` is returned. | ||
| /// Returns a slice of the stream data, with ptr equal to `buf.ptr`. The | ||
| /// delimiter byte is not included in the returned slice. | ||
| pub fn readUntilDelimiter(self: Self, buf: []u8, delimiter: u8) ![]u8 { |
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.
The documentation should be updated to say something like "The delimiter byte is written to the output buffer, but is not included in the returned slice."
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.
Oh yes, definitely! I'm only waiting until we have a resolute "new behavior" so I can update all those docs ;-)
57f001f to
bcae596
Compare
This is one way I see #9594 being solved. I hope I didn't get it too far off. I'm more than happy making any changes (and learn in the process)
I decided to start with
readUntilDelimiterandreadUntilDelimiterOrEofand submit this as draft to collect early feedbacks — which are more than welcome 🙂 I'll continue to the otherreadUntilDelimiter*functions.Happy to propose my first contribution to Zig! 🎉 Thanks to @SpexGuy for suggesting me this issue.