rust/src/liballoc/tests
bors 87e494c4cd Auto merge of #67330 - golddranks:split_inclusive, r=kodraus
Implement split_inclusive for slice and str

# Overview
* Implement `split_inclusive` for `slice` and `str` and `split_inclusive_mut` for `slice`
* `split_inclusive` is a substring/subslice splitting iterator that includes the matched part in the iterated substrings as a terminator.
* EDIT: The behaviour has now changed, as per @KodrAus 's input, to the same semantics with the `split_terminator` function. I updated the examples below.
* Two examples below:
```Rust
    let data = "\nMäry häd ä little lämb\nLittle lämb\n";
    let split: Vec<&str> = data.split_inclusive('\n').collect();
    assert_eq!(split, ["\n", "Märy häd ä little lämb\n", "Little lämb\n"]);
```

```Rust
    let uppercase_separated = "SheePSharKTurtlECaT";
    let mut first_char = true;
    let split: Vec<&str> = uppercase_separated.split_inclusive(|c: char| {
        let split = !first_char && c.is_uppercase();
        first_char = split;
        split
    }).collect();
    assert_eq!(split, ["SheeP", "SharK", "TurtlE", "CaT"]);
```

# Justification for the API
* I was surprised to find that stdlib currently only has splitting iterators that leave out the matched part. In my experience, wanting to leave a substring terminator as a part of the substring is a pretty common usecase.
* This API is strictly more expressive than the standard `split` API: it's easy to get the behaviour of `split` by mapping a subslicing operation that drops the terminator. On the other hand it's impossible to derive this behaviour from `split` without using hacky and brittle `unsafe` code. The normal way to achieve this functionality would be implementing the iterator yourself.
* Especially when dealing with mutable slices, the only way currently is to use `split_at_mut`. This API provides an ergonomic alternative that plays to the strengths of the iterating capabilities of Rust. (Using `split_at_mut` iteratively used to be a real pain before NLL, fortunately the situation is a bit better now.)

# Discussion items
* <s>Does it make sense to mimic `split_terminator` in that the final empty slice would be left off in case of the string/slice ending with a terminator? It might do, as this use case is naturally geared towards considering the matching part as a terminator instead of a separator.</s>
  * EDIT: The behaviour was changed to mimic `split_terminator`.
* Does it make sense to have `split_inclusive_mut` for `&mut str`?
2020-02-22 03:54:50 +00:00
..
btree Lighten tests, in particular for Miri, yet test and explain more 2020-02-16 22:35:44 +01:00
arc.rs Use drop instead of the toilet closure |_| () 2020-01-02 08:56:12 +00:00
binary_heap.rs Format the world 2019-12-22 17:42:47 -05:00
boxed.rs Format liballoc with rustfmt 2019-11-29 20:25:07 -08:00
cow_str.rs Small Cow improvements 2019-12-11 21:01:33 +01:00
fmt.rs Remove licenses 2018-12-25 21:08:33 -07:00
heap.rs Rename Alloc to AllocRef 2020-01-27 21:39:51 +01:00
lib.rs Implement split_inclusive for slice and str, an splitting iterator that includes the matched part in the iterated substrings as a terminator. 2020-02-09 23:48:52 +09:00
linked_list.rs Format the world 2019-12-22 17:42:47 -05:00
rc.rs Use drop instead of the toilet closure |_| () 2020-01-02 08:56:12 +00:00
slice.rs Don't return empty slice on last iteration with matched terminator. Test reverse iteration. 2020-02-09 23:49:44 +09:00
str.rs Don't return empty slice on last iteration with matched terminator. Test reverse iteration. 2020-02-09 23:49:44 +09:00
string.rs Derive Clone + PartialEq + Eq for std::string::FromUtf8Error 2020-02-02 02:29:28 +08:00
vec.rs ef em ti ... :P 2020-01-04 23:57:34 +05:30
vec_deque.rs VecDeque: drop remaining items on destructor panic 2019-12-11 19:38:45 +01:00