Skip to content

Commit 50327d2

Browse files
authored
Rollup merge of #89825 - martinvonz:split-inclusive-empty, r=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 #89716.
2 parents 404c847 + f6e4c74 commit 50327d2

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

library/alloc/tests/slice.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ fn test_splitator_inclusive() {
863863
assert_eq!(xs.split_inclusive(|_| true).collect::<Vec<&[i32]>>(), splits);
864864

865865
let xs: &[i32] = &[];
866-
let splits: &[&[i32]] = &[&[]];
866+
let splits: &[&[i32]] = &[];
867867
assert_eq!(xs.split_inclusive(|x| *x == 5).collect::<Vec<&[i32]>>(), splits);
868868
}
869869

@@ -883,7 +883,7 @@ fn test_splitator_inclusive_reverse() {
883883
assert_eq!(xs.split_inclusive(|_| true).rev().collect::<Vec<_>>(), splits);
884884

885885
let xs: &[i32] = &[];
886-
let splits: &[&[i32]] = &[&[]];
886+
let splits: &[&[i32]] = &[];
887887
assert_eq!(xs.split_inclusive(|x| *x == 5).rev().collect::<Vec<_>>(), splits);
888888
}
889889

@@ -903,7 +903,7 @@ fn test_splitator_mut_inclusive() {
903903
assert_eq!(xs.split_inclusive_mut(|_| true).collect::<Vec<_>>(), splits);
904904

905905
let xs: &mut [i32] = &mut [];
906-
let splits: &[&[i32]] = &[&[]];
906+
let splits: &[&[i32]] = &[];
907907
assert_eq!(xs.split_inclusive_mut(|x| *x == 5).collect::<Vec<_>>(), splits);
908908
}
909909

@@ -923,7 +923,7 @@ fn test_splitator_mut_inclusive_reverse() {
923923
assert_eq!(xs.split_inclusive_mut(|_| true).rev().collect::<Vec<_>>(), splits);
924924

925925
let xs: &mut [i32] = &mut [];
926-
let splits: &[&[i32]] = &[&[]];
926+
let splits: &[&[i32]] = &[];
927927
assert_eq!(xs.split_inclusive_mut(|x| *x == 5).rev().collect::<Vec<_>>(), splits);
928928
}
929929

library/core/src/slice/iter.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ where
481481
impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusive<'a, T, P> {
482482
#[inline]
483483
pub(super) fn new(slice: &'a [T], pred: P) -> Self {
484-
Self { v: slice, pred, finished: false }
484+
let finished = slice.is_empty();
485+
Self { v: slice, pred, finished }
485486
}
486487
}
487488

@@ -729,7 +730,8 @@ where
729730
impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusiveMut<'a, T, P> {
730731
#[inline]
731732
pub(super) fn new(slice: &'a mut [T], pred: P) -> Self {
732-
Self { v: slice, pred, finished: false }
733+
let finished = slice.is_empty();
734+
Self { v: slice, pred, finished }
733735
}
734736
}
735737

0 commit comments

Comments
 (0)