-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Feature Request: BufferedReader function for reading without returning delimeter #11404
Comments
+1. |
I agree. I think that this mod should be applied at least to Current behavior forces to trim EOL chars when for example casting stdin input to a numbered type. We probably need a |
My initial thoughts in designing the That being said, this is a convenience method, so correctness/completeness may not be paramount. I think I based this off Go's interface, but I would also be curious about what other languages do as well. |
@alexcrichton We could just return an Option with steveklabnik/rust_for_rubyists#48 is related; the current read_line must also deal with Windows line endings:
|
Since @alexcrichton seemed interested in what other languages do; in the Python world this would typically be handled by reading the entire file into a string and calling splitlines. Which takes advantage of an underlying Python TextIO feature of "universal newlines" in which the File IO layer hides the different types of newlines from the user; all instances of '\n', '\r' and '\r\n' are returned as '\n'. This was introduced in PEP3116. This would be similar to using Rust's AnyLineIterator. Of course reading the whole file in as a string isn't always a great idea. Generally I'd guess (as with @WebeWizard here) you'd be dealing with CSV's. In Python's case this is handled by a separate library. I did see one rust CSV library which looked to be struggling slightly with newlines itself. I wonder if Rust needs higher level File IO libraries (such as csv) or if extending the BufferedReader as suggested here is a good idea for the meantime? It's also worth considering if introducing something like "universal newlines" could be easier now than later. |
@alexcrichton The other point is efficiency. If you need an owned pointer you have to take a slice without the delimiter and then covert that to owned. In Go you can just slice it and you are done. |
Why do you have to convert to owned? |
@sfackler So I can send it to a channel for example |
Ruby also keeps the newline characters intact: "abc\ndef".lines # => ["abc\n", "def"]
STDIN.readline # => "the text you enter\n" But in Ruby you can easily chop them off using "abc\n".chomp # => "abc"
"abc\r\n".chomp # => "abc"
"abc".chomp # => "abc"
"abc\n\n".chomp # => "abc\n" -- Only one newline is chomped off! I think that we should introduce a convenience function like Ruby's |
But I would not do this directly in the reader. |
The |
We could make a variant of |
@sfackler: Chopping off the newline character is so common that there should be a utility function for this purpose. I expect |
@mneumann right, that's what the |
@sfackler: Yes, but they also trim whitespaces, unless you want to write |
Ah, gotcha |
But I would neither add any new method to |
We're now using the RFC process to deal with standard library changes, and indeed, the IO RFC is currently active. If anyone still cares about this, that's the right place to get involved. |
fix: Work around Code bug with empty diagnostics Closes rust-lang#11404
fix "derivable_impls: attributes are ignored" *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: [`derivable_impls`]: allow the lint when the trait-impl methods has any attribute.
I keep running into situations where I need to read (BufferedReader) from a list of comma separated values or newline separated values and then storing the result. Should I have to trim off the delimiter every time? I feel like this is a common enough use case to be included in libstd.
The text was updated successfully, but these errors were encountered: