split_every/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Split for every n occurrences of a pattern iteratively.
//! This crate **helps you** split a `string` for every `n` occurrences of a `pattern`.  
//! It contains an exclusive `iterator`.
//!
//! # Examples
//!
//! ```rust
//! use split_every::prelude::*;
//!
//! // This prints: [(0, 0), (0, 1)]
//! //              [(0, 0)]
//! //              [(0, 1), (0, 0)]
//! //              [(0, 1)]
//! let mut splitter: SplitEvery<&[(u8, u8)], &[(u8, u8)]> = [
//!     (0, 0), (0, 1), (0, 0),
//!     (0, 0), (0, 0), (0, 1),
//!     (0, 0), (0, 0), (0, 1),
//! ].split_every_n_times(&[(0, 0)], 2);
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//!
//! // This prints: "Oh hi there"
//! //              "I don't really"
//! //              "know what to"
//! //              "say".
//! let mut splitter: SplitEvery<&str, &str> =
//!     "Oh hi there I don't really know what to say".split_every_n_times(" ", 3);
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//!
//! // This prints: ["This", "is", "you", "This"]
//! //              ["me", "This", "is", "someone", "This"]
//! //              ["them"]
//! let mut iter = [
//!     ["This", "is", "you"],
//!     ["This", "is", "me"],
//!     ["This", "is", "someone"],
//!     ["This", "is", "them"],
//! ].iter().flatten().map(|val| *val);
//! let mut splitter: SplitEvery<Box<dyn FnMut() -> Option<&'static str>>, &str> =
//!     SplitEvery::n_times_from_fn(Box::new(move || iter.next()), "is", 2);
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//! println!("{:?}", splitter.next().unwrap());
//! ```

/// Import all necessary traits and structs.
pub mod prelude {
    pub use crate::{SplitEvery, SplitEveryImpl};
}

pub trait SplitEveryImpl: Sized {
    fn split_every_n_times(self, pat: Self, n: usize) -> SplitEvery<Self, Self> {
        SplitEvery {
            input: self,
            pat,
            n,
            ind: 0,
        }
    }
}

impl SplitEveryImpl for &str {}
impl SplitEveryImpl for String {}
impl<'a> SplitEveryImpl for std::string::Drain<'a> {}
impl<T: Clone + PartialEq> SplitEveryImpl for Vec<T> {}
impl<T: Clone + PartialEq> SplitEveryImpl for &[T] {}

pub struct SplitEvery<Input, Pattern> {
    input: Input,
    pat: Pattern,
    n: usize,
    ind: usize,
}

impl<Input: FnMut() -> Option<Pattern>, Pattern: PartialEq> SplitEvery<Input, Pattern> {
    pub fn n_times_from_fn(input: Input, pat: Pattern, n: usize) -> SplitEvery<Input, Pattern> {
        SplitEvery {
            input,
            pat,
            n,
            ind: 0,
        }
    }
}

impl<Input: FnMut() -> Option<Pattern>, Pattern: PartialEq> Iterator
    for SplitEvery<Input, Pattern>
{
    type Item = Vec<Pattern>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.n == 0 {
            let out: Vec<Pattern> = std::iter::repeat_with(|| (self.input)())
                .take_while(Option::is_some)
                .flatten()
                .collect();
            if out.is_empty() {
                return None;
            }
            return Some(out);
        }
        let mut out: Vec<Pattern> = Vec::with_capacity(5);
        'main: for ind in 0..self.n {
            while let Some(val) = (self.input)() {
                if val == self.pat {
                    if ind == unsafe { self.n.unchecked_sub(1) } {
                        break 'main;
                    }
                    out.push(val);
                    continue 'main;
                }
                out.push(val);
            }
        }
        if out.is_empty() {
            return None;
        }
        Some(out)
    }
}

impl<Pattern: AsRef<str>> Iterator for SplitEvery<&str, Pattern> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        split_every_next_str_helper(self)
    }
}

impl<Pattern: AsRef<str>> Iterator for SplitEvery<String, Pattern> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        split_every_next_str_helper(self)
    }
}

impl<'a, Pattern: AsRef<str>> Iterator for SplitEvery<std::string::Drain<'a>, Pattern> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        split_every_next_str_helper(self)
    }
}

fn split_every_next_str_helper<Input: AsRef<str>, Pattern: AsRef<str>>(
    split_every: &mut SplitEvery<Input, Pattern>,
) -> Option<String> {
    let input: &str = split_every.input.as_ref();
    if split_every.ind == input.len() {
        return None;
    }
    let pat: &str = split_every.pat.as_ref();
    let iter_haystack: &str = unsafe { input.get_unchecked(split_every.ind..) };
    let mut len: usize = 0;
    for ind in 0..split_every.n {
        let haystack: &str = unsafe { iter_haystack.get_unchecked(len..) };
        if let Some(byte_ind) = haystack.find(pat) {
            len = unsafe { len.unchecked_add(byte_ind).unchecked_add(pat.len()) };
            continue;
        }
        if ind == 0 {
            split_every.ind = input.len();
            return Some(haystack.to_string());
        }
        break;
    }
    split_every.ind = unsafe { split_every.ind.unchecked_add(len) };
    Some(unsafe { iter_haystack.get_unchecked(..len.unchecked_sub(pat.len())) }.to_string())
}

impl<T: Clone + PartialEq> Iterator for SplitEvery<Vec<T>, Vec<T>> {
    type Item = Vec<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let (ind, out): (usize, Option<Vec<T>>) =
            split_every_next_arr_helper(self, &self.input, &self.pat);
        self.ind = ind;
        out
    }
}

impl<T: Clone + PartialEq> Iterator for SplitEvery<&[T], &[T]> {
    type Item = Vec<T>;

    fn next(&mut self) -> Option<Self::Item> {
        let (ind, out): (usize, Option<Vec<T>>) =
            split_every_next_arr_helper(self, self.input, self.pat);
        self.ind = ind;
        out
    }
}

fn split_every_next_arr_helper<T, U: Clone + PartialEq>(
    split_every: &SplitEvery<T, T>,
    input: &[U],
    pat: &[U],
) -> (usize, Option<Vec<U>>) {
    if split_every.ind == input.len() {
        return (split_every.ind, None);
    }
    let iter_haystack: &[U] = unsafe { input.get_unchecked(split_every.ind..) };
    let mut len: usize = 0;
    for ind in 0..split_every.n {
        if len == iter_haystack.len() {
            break;
        }
        if let Some((ind, _)) = unsafe { iter_haystack.get_unchecked(len..) }
            .windows(pat.len())
            .enumerate()
            .find(|(_, val)| val == &pat)
        {
            len = unsafe { len.unchecked_add(ind).unchecked_add(pat.len()) };
            continue;
        }
        if ind == 0 {
            return (input.len(), Some(iter_haystack.to_vec()));
        }
        break;
    }
    (
        unsafe { split_every.ind.unchecked_add(len) },
        Some(unsafe { iter_haystack.get_unchecked(..len.unchecked_sub(pat.len())) }.to_vec()),
    )
}

#[test]
fn test() {
    let mut splitter: SplitEvery<&str, &str> = "oh oh oh oh oh".split_every_n_times(" ", 2);
    assert_eq!(splitter.next().unwrap(), "oh oh");
    assert_eq!(splitter.next().unwrap(), "oh oh");
    assert_eq!(splitter.next().unwrap(), "oh");
    assert_eq!(splitter.next(), None);

    let mut splitter: SplitEvery<&str, &str> = "a a a a".split_every_n_times("b", 2);
    assert_eq!(splitter.next().unwrap(), "a a a a");
    assert_eq!(splitter.next(), None);

    let mut iter = [
        ["This", "is", "you"],
        ["This", "is", "me"],
        ["This", "is", "someone"],
        ["This", "is", "them"],
    ]
    .iter()
    .flatten()
    .copied();
    let mut splitter: SplitEvery<Box<dyn FnMut() -> Option<&'static str>>, &str> =
        SplitEvery::<Box<dyn FnMut() -> Option<&'static str>>, &str>::n_times_from_fn(
            Box::new(move || iter.next()),
            "is",
            2,
        );
    assert_eq!(splitter.next().unwrap(), vec!["This", "is", "you", "This"]);
    assert_eq!(
        splitter.next().unwrap(),
        vec!["me", "This", "is", "someone", "This"]
    );
    assert_eq!(splitter.next().unwrap(), vec!["them"]);
    assert_eq!(splitter.next(), None);

    #[allow(clippy::type_complexity)]
    let mut splitter: SplitEvery<&[(u8, u8)], &[(u8, u8)]> = [
        (0, 0),
        (0, 1),
        (0, 0), // Split
        (0, 0),
        (0, 0), // Split
        (0, 1),
        (0, 0),
        (0, 0), // Split
        (0, 1),
    ]
    .split_every_n_times(&[(0, 0)], 2);
    assert_eq!(splitter.next().unwrap(), vec![(0, 0), (0, 1)]);
    assert_eq!(splitter.next().unwrap(), vec![(0, 0)]);
    assert_eq!(splitter.next().unwrap(), vec![(0, 1), (0, 0)]);
    assert_eq!(splitter.next().unwrap(), vec![(0, 1)]);
    assert_eq!(splitter.next(), None);

    let mut splitter: SplitEvery<&str, &str> =
        "Oh hi there I don't really know what to say".split_every_n_times(" ", 3);
    assert_eq!(splitter.next().unwrap(), "Oh hi there");
    assert_eq!(splitter.next().unwrap(), "I don't really");
    assert_eq!(splitter.next().unwrap(), "know what to");
    assert_eq!(splitter.next().unwrap(), "say");
    assert_eq!(splitter.next(), None);
}