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

split_inclusive() on empty slice yields a single empty slice (instead of no slices at all) #89716

Closed
martinvonz opened this issue Oct 9, 2021 · 0 comments · Fixed by #89825
Closed
Labels
C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@martinvonz
Copy link
Contributor

martinvonz commented Oct 9, 2021

I tried this code:

    let empty_str = "";
    let expected : Vec<&str> = vec![];
    assert_eq!(empty_str.split_inclusive(|ch| ch == '\n').collect::<Vec<_>>(), expected);
    let empty_slice: &[u8] = &[];
    let expected : Vec<&[u8]> = vec![];
    assert_eq!(empty_slice.split_inclusive(|b| *b == b'\n').collect::<Vec<_>>(), expected);

I expected to see this happen: Both assertions pass, i.e. split_inclusive() yields no results on empty input.

Instead, this happened: The second assertion fails because split_inclusive() on an empty slice yields a single empty element instead of no elements.

Meta

rustc --version --verbose:

cargo 1.57.0-nightly (0121d66aa 2021-09-22)

(Skipping backtrace from template as that doesn't seem relevant.)

@martinvonz martinvonz added the C-bug Category: This is a bug. label Oct 9, 2021
martinvonz added a commit to martinvonz/jj that referenced this issue Oct 13, 2021
@m-ou-se m-ou-se added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Nov 1, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2021
…=m-ou-se

Make split_inclusive() on an empty slice yield an empty output

`[].split_inclusive()` currently yields a single, empty slice. That's
different from `"".split_inslusive()`, which yields no output at
all. I think that makes the slice version harder to use.

The case where I ran into this bug was when writing code for
generating a diff between two slices of bytes. I wanted to prefix
removed lines with "-" and a added lines with "+". Due to
`split_inclusive()`'s current behavior, that means that my code prints
just a "-" or "+" for empty files. I suspect most existing callers
have similar "bugs" (which would be fixed by this patch).

Closes rust-lang#89716.
@bors bors closed this as completed in f6e4c74 Dec 15, 2021
Jules-Bertholet pushed a commit to Jules-Bertholet/rust that referenced this issue Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants