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

libcore: handle trailing newlines more like other languages. #5398

Merged
merged 2 commits into from
Mar 22, 2013

Conversation

huonw
Copy link
Member

@huonw huonw commented Mar 15, 2013

The each_line function in ReaderUtil acts very differently to equivalent functions in Python, Ruby, Clojure etc. E.g. given a file t with contents trailing\nnew line\n and n containing no trailing\nnew line:

Rust:

t: ~[~"trailing", ~"new line", ~""]
n: ~[~"no trailing", ~"new line"]

Python:

>>> open('t').readlines()
['trailing\n', 'new line\n']
>>> open('n').readlines()
['no trailing\n', 'new line']

Ruby:

irb(main):001:0> File.readlines('t')
=> ["trailing\n", "new line\n"]
irb(main):002:0> File.readlines('n')
=> ["no trailing\n", "new line"]

Clojure

user=> (read-lines "t")
("trailing" "new line")
user=> (read-lines "n")
("no trailing" "new line")

The extra string that rust includes at the end is inconsistent, and means that it is impossible to distinguish between the "real" empty line a file that ends ...\n\n, and the "fake" one after the last \n.

The code attached makes Rust's each_line act like Clojure (and PHP, i.e. not including the \n), as well as adjusting str::lines to fix the trailing empty line problem.

Also, add a convenience read_lines method to read all the lines in a file into a vector.

@huonw huonw mentioned this pull request Mar 15, 2013
@huonw
Copy link
Member Author

huonw commented Mar 19, 2013

@graydon, I've updated this: added split_char_no_trailing (and split_no_trailing) which act like str::lines. This was easier, and seemed like it would be less surprising in terms of behaviour, than changing split_char and adding split_char_keep_trailing. (And, I noticed some old(?) code in compiletest, which I've converted to use the more appropriate str function.)

Also, I did some experimentation and it seems that it wouldn't be too difficult to convert many of the functions that return ~str into &str in the str library: is it worth me spending my time doing that? (Just checking in case there is a broader plan in terms of rewrites/redesigns.)

huonw added 2 commits March 23, 2013 00:15
Specifically, `lines` and `each_line` will not emit a trailing empty string
when given "...\n". Also, add `read_lines`, which just collects all of
`each_line` into a vector, and `split_*_no_trailing` which will is the
generalised version of `lines`.
bors added a commit that referenced this pull request Mar 22, 2013
The `each_line` function in `ReaderUtil` acts very differently to equivalent functions in Python, Ruby, Clojure etc. E.g. given a file `t` with contents `trailing\nnew line\n` and `n` containing `no trailing\nnew line`:

Rust:
```Rust
t: ~[~"trailing", ~"new line", ~""]
n: ~[~"no trailing", ~"new line"]
```

Python:
```Python
>>> open('t').readlines()
['trailing\n', 'new line\n']
>>> open('n').readlines()
['no trailing\n', 'new line']
```

Ruby:
```Ruby
irb(main):001:0> File.readlines('t')
=> ["trailing\n", "new line\n"]
irb(main):002:0> File.readlines('n')
=> ["no trailing\n", "new line"]
```

Clojure
```Clojure
user=> (read-lines "t")
("trailing" "new line")
user=> (read-lines "n")
("no trailing" "new line")
```

The extra string that rust includes at the end is inconsistent, and means that it is impossible to distinguish between the "real" empty line a file that ends `...\n\n`, and the "fake" one after the last `\n`.

The code attached makes Rust's `each_line` act like Clojure (and PHP, i.e. not including the `\n`), as well as adjusting `str::lines` to fix the trailing empty line problem.

Also, add a convenience `read_lines` method to read all the lines in a file into a vector.
@bors bors closed this Mar 22, 2013
@bors bors merged commit f832339 into rust-lang:incoming Mar 22, 2013
@huonw huonw deleted the core-readlines branch March 24, 2013 10:43
bors added a commit to rust-lang-ci/rust that referenced this pull request May 2, 2020
Stop updating the lint counter with every new lint

r? @Manishearth

This PR does two things:

1. Clean up the clippy_dev module a bit (first 3 commits; cc rust-lang#5394 )
2. Make the counter in the README count in steps of 50 lints. Also use a `lazy_static` `Vec` for the lint list, so no counter is required there anymore.

changelog: none
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.

3 participants