Expand description
Splitter
A string and slice splitter library
String Example
use splitter::StrSplitter;
let sp = StrSplitter::new("bytes example", " ");
assert_eq!(
sp.collect::<Vec<_>>(),
vec!["bytes", " ", "example"],
);Slice Example
use splitter::Splitter;
let sp = Splitter::new(&[1, 2, 3, 3, 4], [[2], [4]]);
let re: Vec<&[usize]> = vec![&[1], &[2], &[3, 3], &[4]];
assert_eq!(sp.collect::<Vec<_>>(), re);Custom Info
use std::ops::Range;
use splitter::{StrInfo, StrSplitter};
#[derive(Default)]
struct CustomCtx {
cursor: usize,
}
#[derive(Debug, PartialEq)]
struct Custom<'a> {
content: &'a str,
span: Range<usize>,
}
impl<'a> StrInfo<'a> for Custom<'a> {
type Context = CustomCtx;
fn generate(ctx: &mut Self::Context, ts: &'a str) -> Self {
let start = ctx.cursor;
ctx.cursor += ts.len();
Custom { content: ts, span: start..ctx.cursor }
}
}
let sp = StrSplitter::new("bytes example", " ").with_info::<Custom>();
assert_eq!(
sp.collect::<Vec<_>>(),
vec![
Custom { content: "bytes", span: 0..5 },
Custom { content: " ", span: 5..6 },
Custom { content: "example", span: 6..13 },
],
);Derive Info
When using the derive macro, and lifetimes are provided, the first lifetime has to be the lifetime of the slice or of the string.
Same with generic type parameters, the first generic type parameter has to be the the type of the slice elements.
Features
std- enables the standard library (currently only used withimpls- feature)impls- automatically implementsInfoandStrInfofor usefull types fromcoreorstdinfos- adds pre-defined usefullInfoandStrInfotypesderive- enables theInfoandStrInfoderive macrofull- enables all features
Automatic Implementations
impls - feature
core::pin::Pincore::marker::PhantomDatacore::marker::PhantomPinnedcore::mem::ManuallyDropcore::cell::Cellcore::cell::RefCellcore::cell::UnsafeCellcore::ops::Rangecore::ops::RangeInclusive
impls and std - feature
- [
std::boxed::Box] - [
std::rc::Rc] - [
std::sync::Arc] - [
std::sync::Mutex] - [
std::sync::RwLock] - [
std::vec::Vec] (only for slices) - [
std::path::PathBuf] (only for strings) - [
std::string::String] (only for strings)
Re-exports
Modules
Structs
The base splitter over any type of slice