Skip to content

provide a peeking API to std.io.BufferedReader #4501

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

Open
andrewrk opened this issue Feb 18, 2020 · 4 comments · May be fixed by #22125
Open

provide a peeking API to std.io.BufferedReader #4501

andrewrk opened this issue Feb 18, 2020 · 4 comments · May be fixed by #22125
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Feb 18, 2020

The point of BufferedInStream is to call read() on the underlying stream in bursts, rather than separate calls. E.g. to make 1 syscall to read() reading 999 bytes, rather than 999 syscalls to read() reading 1 byte each.

The point of PeekStream is to be able to "put back" bytes, so that the next call to read() will get them again. In order to accomplish this, PeekStream needs a buffer. That makes PeekStream also a BufferedInStream.

All we need to unify these two APIs is for BufferedInStream to have a parameter called reserved_buffer_space or something like this, and then it's guaranteed to be able to "put back" this many elements.

Related:

@andrewrk andrewrk added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. standard library This issue involves writing Zig code for the standard library. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Feb 18, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Feb 18, 2020
@andrewrk andrewrk added the accepted This proposal is planned. label Feb 19, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 9, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 20, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@IridescentRose IridescentRose mentioned this issue Nov 4, 2022
26 tasks
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jun 29, 2023
@andrewrk andrewrk changed the title merge std.io.PeekStream and std.io.BufferedInStream provide a peeking API to std.io.BufferedReader Mar 14, 2024
@andrewrk andrewrk modified the milestones: 0.13.0, 0.15.0 Mar 14, 2024
@pierrec
Copy link
Contributor

pierrec commented Mar 14, 2024

Hello,

Since I make use of io.PeekStream and it just got removed, I would like to contribute to this issue.
The idea is to simply implement the old methods on BufferedReader.

pub fn putBackByte(self: *Self, byte: u8) !void { ... }

pub fn putBack(self: *Self, byte: []u8) !void { ... }

Would this be acceptable?

@nektro
Copy link
Contributor

nektro commented Mar 14, 2024

that seems backwards. there should be a peek() function that returns br.buf[br.start] and read() should eat the byte

@travisstaloch
Copy link
Contributor

that seems backwards. there should be a peek()

I agree that it would be nice to have a peek(). How about peek([]u8) !void and peekByte() !u8?

But previously io.PeekStream() had putBackByte(u8) and putBack([]const u8). So maybe we could have both peek() and putBack()?

@septemhill
Copy link

septemhill commented Oct 1, 2024

I have a question, how do we handle the data after peek ? use read to consume or add one more method discard to ignore the data (byte or slice) ?

Edit: I didn't notice that the putBack maybe the solution of my question, sorry

@notcancername notcancername linked a pull request Dec 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
5 participants