Crates.io | str_splitter |
lib.rs | str_splitter |
version | 0.1.1 |
source | src |
created_at | 2023-01-21 00:15:51.828125 |
updated_at | 2023-01-21 00:21:03.375076 |
description | Experiment using a combinator API for flexible string splitting |
homepage | https://github.com/pitaj/str_splitter |
repository | https://github.com/pitaj/str_splitter |
max_upload_size | |
id | 763907 |
size | 71,771 |
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:
Both have almost identical APIs. Usage looks like this:
use str_splitter::combinators::SplitExt;
// OR
// use str_splitter::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(' ').with_limit(3).collect();
assert_eq!(v, ["Mary", "had", "a little lambda"]);
// `rsplitn`
let v: Vec<&str> = "lion::tiger::leopard".splitter("::").to_reversed().with_limit(2).collect();
assert_eq!(v, ["leopard", "lion::tiger"]);
// `rsplit_terminator`
let v: Vec<&str> = "A.B.".splitter('.').to_terminated().to_reversed().collect();
assert_eq!(v, ["B", "A"]);
// `rsplit_inclusive_once` if it existed
assert_eq!("cfg=foo=bar".splitter('=').to_inclusive().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.