diff options
| author | Martin von Zweigbergk <martinvonz@google.com> | 2021-10-11 16:40:16 -0700 |
|---|---|---|
| committer | Martin von Zweigbergk <martinvonz@google.com> | 2021-10-12 08:34:03 -0700 |
| commit | f6e4c742f41c5505c9a371ce8ef2c7aed55c35e4 (patch) | |
| tree | 026d5aa2214d02c3dee3ac08cbd520de4a1d75fe | |
| parent | 1067e2ca5e9cfe5c79f956e49ffc684c5142d49b (diff) | |
| download | rust-f6e4c742f41c5505c9a371ce8ef2c7aed55c35e4.tar.gz rust-f6e4c742f41c5505c9a371ce8ef2c7aed55c35e4.zip | |
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 #89716.
| -rw-r--r-- | library/alloc/tests/slice.rs | 8 | ||||
| -rw-r--r-- | library/core/src/slice/iter.rs | 6 |
2 files changed, 8 insertions, 6 deletions
diff --git a/library/alloc/tests/slice.rs b/library/alloc/tests/slice.rs index 13b8c059e37..18ea6a21413 100644 --- a/library/alloc/tests/slice.rs +++ b/library/alloc/tests/slice.rs @@ -863,7 +863,7 @@ fn test_splitator_inclusive() { assert_eq!(xs.split_inclusive(|_| true).collect::<Vec<&[i32]>>(), splits); let xs: &[i32] = &[]; - let splits: &[&[i32]] = &[&[]]; + let splits: &[&[i32]] = &[]; assert_eq!(xs.split_inclusive(|x| *x == 5).collect::<Vec<&[i32]>>(), splits); } @@ -883,7 +883,7 @@ fn test_splitator_inclusive_reverse() { assert_eq!(xs.split_inclusive(|_| true).rev().collect::<Vec<_>>(), splits); let xs: &[i32] = &[]; - let splits: &[&[i32]] = &[&[]]; + let splits: &[&[i32]] = &[]; assert_eq!(xs.split_inclusive(|x| *x == 5).rev().collect::<Vec<_>>(), splits); } @@ -903,7 +903,7 @@ fn test_splitator_mut_inclusive() { assert_eq!(xs.split_inclusive_mut(|_| true).collect::<Vec<_>>(), splits); let xs: &mut [i32] = &mut []; - let splits: &[&[i32]] = &[&[]]; + let splits: &[&[i32]] = &[]; assert_eq!(xs.split_inclusive_mut(|x| *x == 5).collect::<Vec<_>>(), splits); } @@ -923,7 +923,7 @@ fn test_splitator_mut_inclusive_reverse() { assert_eq!(xs.split_inclusive_mut(|_| true).rev().collect::<Vec<_>>(), splits); let xs: &mut [i32] = &mut []; - let splits: &[&[i32]] = &[&[]]; + let splits: &[&[i32]] = &[]; assert_eq!(xs.split_inclusive_mut(|x| *x == 5).rev().collect::<Vec<_>>(), splits); } diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 70e59cda63f..d88c85613a9 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -479,7 +479,8 @@ where impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusive<'a, T, P> { #[inline] pub(super) fn new(slice: &'a [T], pred: P) -> Self { - Self { v: slice, pred, finished: false } + let finished = slice.is_empty(); + Self { v: slice, pred, finished } } } @@ -727,7 +728,8 @@ where impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusiveMut<'a, T, P> { #[inline] pub(super) fn new(slice: &'a mut [T], pred: P) -> Self { - Self { v: slice, pred, finished: false } + let finished = slice.is_empty(); + Self { v: slice, pred, finished } } } |
