This crate aims to prevent the combinatorial explosion of str
splitting APIs.
The explosions comes from the combinations of the following:
rsplit
, rsplit_once
, rsplit_terminator
, rsplitn
)split_inclusive
)split_terminator
, rsplit_terminator
)splitn
, rsplitn
)split_once
, rsplit_once
)As you can see, various combinations are currently missing (not exhaustive):
Plus, it may be useful to have right- or left-inclusive versions.
This could quickly balloon the API surface of str
which is not ideal.
Instead, this crate implements a kind of builder API. It does this two different ways: - using const generic parameters - using struct combinators
Both have almost identical APIs. Usage looks like this:
```rust use strsplitter::combinators::SplitExt; // OR // use strsplitter::const_generics::SplitExt;
// split
let v: Vec<&str> = "lionXXtigerXleopard".splitter('X').collect();
assert_eq!(v, ["lion", "", "tiger", "leopard"]);
// splitn
let v: Vec<&str> = "Mary had a little lambda".splitter(' ').withlimit(3).collect();
asserteq!(v, ["Mary", "had", "a little lambda"]);
// rsplitn
let v: Vec<&str> = "lion::tiger::leopard".splitter("::").toreversed().withlimit(2).collect();
assert_eq!(v, ["leopard", "lion::tiger"]);
// rsplit_terminator
let v: Vec<&str> = "A.B.".splitter('.').toterminated().toreversed().collect();
assert_eq!(v, ["B", "A"]);
// rsplit_inclusive_once
if it existed
asserteq!("cfg=foo=bar".splitter('=').toinclusive().to_reversed().once(), Some(("cfg=foo", "=bar")));
```
The idea is that the struct returned by the existing split
method would add the various
modifier methods that Splitter
has in this library.
This crate relies on the unstable Pattern
trait, so requires nightly Rust.