Skip to content

Commit 8d9a697

Browse files
committed
✨ v3.1.0
1 parent 60793c3 commit 8d9a697

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "split-every"
3-
version = "3.0.0"
3+
version = "3.1.0"
44
edition = "2021"
55
authors = ["JumperBot_"]
66
description = "Split for every n occurrences of a pattern iteratively!"

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ println!("{:?}", splitter.next().unwrap());
3939
println!("{:?}", splitter.next().unwrap());
4040
println!("{:?}", splitter.next().unwrap());
4141

42+
// This prints: ["This", "is", "you", "This"]
43+
// ["me", "This", "is", "someone", "This"]
44+
// ["them"]
45+
let mut splitter: SplitEvery<Box<dyn FnMut() -> Option<&'static str>>, &str> = [
46+
["This", "is", "you"],
47+
["This", "is", "me"],
48+
["This", "is", "someone"],
49+
["This", "is", "them"],
50+
]
51+
.iter()
52+
.flatten()
53+
.copied()
54+
.split_every_n_times("is", 2);
55+
println!("{:?}", splitter.next().unwrap());
56+
println!("{:?}", splitter.next().unwrap());
57+
println!("{:?}", splitter.next().unwrap());
58+
4259
// This prints: ["This", "is", "you", "This"]
4360
// ["me", "This", "is", "someone", "This"]
4461
// ["them"]

src/lib.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Split for every n occurrences of a pattern iteratively.
2-
//! This crate **helps you** split a `string` for every `n` occurrences of a `pattern`.
2+
//! This crate **helps you** split data for every `n` occurrences of a `pattern`.
33
//! It contains an exclusive `iterator`.
44
//!
55
//! # Examples
@@ -35,6 +35,24 @@
3535
//! // This prints: ["This", "is", "you", "This"]
3636
//! // ["me", "This", "is", "someone", "This"]
3737
//! // ["them"]
38+
//! let mut splitter: SplitEvery<Box<dyn FnMut() -> Option<&'static str>>, &str> = [
39+
//! ["This", "is", "you"],
40+
//! ["This", "is", "me"],
41+
//! ["This", "is", "someone"],
42+
//! ["This", "is", "them"],
43+
//! ]
44+
//! .iter()
45+
//! .flatten()
46+
//! .copied()
47+
//! .split_every_n_times("is", 2);
48+
//! println!("{:?}", splitter.next().unwrap());
49+
//! println!("{:?}", splitter.next().unwrap());
50+
//! println!("{:?}", splitter.next().unwrap());
51+
//!
52+
//! // This prints: ["This", "is", "you", "This"]
53+
//! // ["me", "This", "is", "someone", "This"]
54+
//! // ["them"]
55+
//!
3856
//! let mut iter = [
3957
//! ["This", "is", "you"],
4058
//! ["This", "is", "me"],
@@ -50,7 +68,7 @@
5068
5169
/// Import all necessary traits and structs.
5270
pub mod prelude {
53-
pub use crate::{SplitEvery, SplitEveryImpl};
71+
pub use crate::{SplitEvery, SplitEveryImpl, SplitEveryIterImpl};
5472
}
5573

5674
pub trait SplitEveryImpl: Sized {
@@ -66,10 +84,22 @@ pub trait SplitEveryImpl: Sized {
6684

6785
impl SplitEveryImpl for &str {}
6886
impl SplitEveryImpl for String {}
69-
impl<'a> SplitEveryImpl for std::string::Drain<'a> {}
87+
impl SplitEveryImpl for std::string::Drain<'_> {}
7088
impl<T: Clone + PartialEq> SplitEveryImpl for Vec<T> {}
7189
impl<T: Clone + PartialEq> SplitEveryImpl for &[T] {}
7290

91+
pub trait SplitEveryIterImpl<'a, T: Clone + PartialEq>: Iterator<Item = T> + Sized + 'a {
92+
fn split_every_n_times(
93+
mut self,
94+
pat: T,
95+
n: usize,
96+
) -> SplitEvery<Box<dyn FnMut() -> Option<T> + 'a>, T> {
97+
SplitEvery::n_times_from_fn(Box::new(move || self.next()), pat, n)
98+
}
99+
}
100+
101+
impl<'a, T: Clone + PartialEq, U: Iterator<Item = T> + Sized + 'a> SplitEveryIterImpl<'a, T> for U {}
102+
73103
pub struct SplitEvery<Input, Pattern> {
74104
input: Input,
75105
pat: Pattern,
@@ -140,7 +170,7 @@ impl<Pattern: AsRef<str>> Iterator for SplitEvery<String, Pattern> {
140170
}
141171
}
142172

143-
impl<'a, Pattern: AsRef<str>> Iterator for SplitEvery<std::string::Drain<'a>, Pattern> {
173+
impl<Pattern: AsRef<str>> Iterator for SplitEvery<std::string::Drain<'_>, Pattern> {
144174
type Item = String;
145175

146176
fn next(&mut self) -> Option<Self::Item> {
@@ -241,6 +271,24 @@ fn test() {
241271
assert_eq!(splitter.next().unwrap(), "a a a a");
242272
assert_eq!(splitter.next(), None);
243273

274+
let mut splitter: SplitEvery<Box<dyn FnMut() -> Option<&'static str>>, &str> = [
275+
["This", "is", "you"],
276+
["This", "is", "me"],
277+
["This", "is", "someone"],
278+
["This", "is", "them"],
279+
]
280+
.iter()
281+
.flatten()
282+
.copied()
283+
.split_every_n_times("is", 2);
284+
assert_eq!(splitter.next().unwrap(), vec!["This", "is", "you", "This"]);
285+
assert_eq!(
286+
splitter.next().unwrap(),
287+
vec!["me", "This", "is", "someone", "This"]
288+
);
289+
assert_eq!(splitter.next().unwrap(), vec!["them"]);
290+
assert_eq!(splitter.next(), None);
291+
244292
let mut iter = [
245293
["This", "is", "you"],
246294
["This", "is", "me"],

0 commit comments

Comments
 (0)