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

io: add new stringreader struct #20893

Merged
merged 5 commits into from
Feb 27, 2024
Merged

io: add new stringreader struct #20893

merged 5 commits into from
Feb 27, 2024

Conversation

Casper64
Copy link
Member

@Casper64 Casper64 commented Feb 23, 2024

This pr adds a new StringReader struct to the io module.

The StringRedeader is able to read data from a Reader interface and/or source string to a dynamically growing buffer using a string builder.

Unlike the BufferedReader, StringReader will keep the entire contents of the buffer in memory, allowing the incoming data to be reused and read in an efficient matter.

The StringReader also implements the io.ReaderWriter interface


// StringReader is able to read data from a Reader interface and/or source string to a dynamically
// growing buffer using a string builder. Unlike the BufferedReader, StringReader will
// keep the entire contents of the buffer in memory, allowing the incoming data to be reused
Copy link
Contributor

Choose a reason for hiding this comment

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

If this reader keep the entire buffers content in memory, i think its a good to place limit on them, or it would grow uncontrollable

Copy link
Contributor

Choose a reason for hiding this comment

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

Since it's a Reader, it's on the caller to control the size of the string. If it was a Writer, then yes, it would have to handle not allowing the string to run you out of memory.

Copy link
Member Author

Choose a reason for hiding this comment

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

if you want to control how many bytes are read in total, you can call ‘fill_buffer_until`. This function will only read a maximum amount of bytes

Copy link
Member Author

Choose a reason for hiding this comment

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

That said, it would be useful to provide methods to clear the buffer and offset. I'll add them

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a line in the doc comment to make it more clear:

The StringReader will not set a maximum capacity to the string builders buffer and could grow very large.

@Casper64 Casper64 marked this pull request as draft February 24, 2024 12:35
@Casper64 Casper64 marked this pull request as ready for review February 25, 2024 20:31
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

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

Excellent work.

@spytheman spytheman merged commit d8c4a84 into vlang:master Feb 27, 2024
48 checks passed
@Casper64 Casper64 deleted the stringreader branch February 27, 2024 22:14
raw-bin pushed a commit to raw-bin/v that referenced this pull request Mar 9, 2024
raw-bin pushed a commit to raw-bin/v that referenced this pull request Mar 9, 2024
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.

4 participants