|
| 1 | +//! Split for every n occurences of a pattern iteratively. |
| 2 | +//! This crate **helps you** split a `string` for every `n` occurences of a `pattern`. |
| 3 | +//! It contains an exclusive `iterator`. |
| 4 | +//! |
| 5 | +//! # Examples |
| 6 | +//! |
| 7 | +//! ```rust |
| 8 | +//! use split_every::{SplitEveryImpl, SplitEvery}; |
| 9 | +//! // This prints: "Oh hi there" |
| 10 | +//! // "I don't really" |
| 11 | +//! // "know what to" |
| 12 | +//! // "say". |
| 13 | +//! let mut splitter: SplitEvery = |
| 14 | +//! "Oh hi there I don't really know what to say".split_every_n_of_str(" ", 3); |
| 15 | +//! println!("{}", splitter.next().unwrap()); |
| 16 | +//! println!("{}", splitter.next().unwrap()); |
| 17 | +//! println!("{}", splitter.next().unwrap()); |
| 18 | +//! println!("{}", splitter.next().unwrap()); |
| 19 | +//! ``` |
| 20 | +
|
| 21 | +/// A trait containing all `string` split-every functions. |
| 22 | +pub trait SplitEveryImpl: AsRef<str> { |
| 23 | + /// This splits a `string` every `n` times a `string` is found. |
| 24 | + /// This splits exclusively. |
| 25 | + /// The `string` must be `utf8-encoded`. |
| 26 | + #[must_use] |
| 27 | + fn split_every_n_of_str<'a>(&'a self, pat: &'a str, n: usize) -> SplitEvery<'a> { |
| 28 | + assert!(n > 0, "n must be greater than 0"); |
| 29 | + SplitEvery { |
| 30 | + inner: self.as_ref(), |
| 31 | + pat: Pattern::Str(pat), |
| 32 | + n, |
| 33 | + index: 0, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// This splits a `string` every `n` times a `char` is found. |
| 38 | + /// This splits exclusively. |
| 39 | + /// The `string` must be `utf8-encoded`. |
| 40 | + #[must_use] |
| 41 | + fn split_every_n_of_char(&self, pat: char, n: usize) -> SplitEvery<'_> { |
| 42 | + assert!(n > 0, "n must be greater than 0"); |
| 43 | + SplitEvery { |
| 44 | + inner: self.as_ref(), |
| 45 | + pat: Pattern::Ch(pat), |
| 46 | + n, |
| 47 | + index: 0, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<T: AsRef<str>> SplitEveryImpl for T {} |
| 53 | + |
| 54 | +/// A convinient substitution to `std::str::pattern::Pattern`. |
| 55 | +enum Pattern<'a> { |
| 56 | + Str(&'a str), |
| 57 | + Ch(char), |
| 58 | +} |
| 59 | + |
| 60 | +impl<'a> Pattern<'a> { |
| 61 | + /// A convinient `len` method. |
| 62 | + fn len(&self) -> usize { |
| 63 | + match self { |
| 64 | + Self::Str(inner) => inner.len(), |
| 65 | + Self::Ch(inner) => inner.len_utf8(), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/// An `Iterator` struct for splitting a `string` every `n` occurences of a `pattern`. |
| 71 | +pub struct SplitEvery<'a> { |
| 72 | + inner: &'a str, |
| 73 | + pat: Pattern<'a>, |
| 74 | + n: usize, |
| 75 | + index: usize, |
| 76 | +} |
| 77 | + |
| 78 | +impl<'a> Iterator for SplitEvery<'a> { |
| 79 | + type Item = &'a str; |
| 80 | + |
| 81 | + fn next(&mut self) -> Option<Self::Item> { |
| 82 | + if self.index == self.inner.len() { |
| 83 | + return None; |
| 84 | + } |
| 85 | + let haystack: &str = unsafe { self.inner.get_unchecked(self.index..) }; |
| 86 | + let mut len: usize = 0; |
| 87 | + for ind in 0..self.n { |
| 88 | + let haystack: &str = unsafe { haystack.get_unchecked(len..) }; |
| 89 | + if let Some(byte_ind) = match self.pat { |
| 90 | + Pattern::Str(inner) => haystack.find(inner), |
| 91 | + Pattern::Ch(inner) => haystack.find(inner), |
| 92 | + } { |
| 93 | + len = unsafe { len.unchecked_add(byte_ind).unchecked_add(self.pat.len()) }; |
| 94 | + continue; |
| 95 | + } |
| 96 | + if ind == 0 { |
| 97 | + self.index = self.inner.len(); |
| 98 | + return Some(haystack); |
| 99 | + } |
| 100 | + break; |
| 101 | + } |
| 102 | + self.index = unsafe { self.index.unchecked_add(len) }; |
| 103 | + Some(unsafe { haystack.get_unchecked(..len.unchecked_sub(self.pat.len())) }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#[test] |
| 108 | +fn test() { |
| 109 | + let mut splitter: SplitEvery = "oh oh oh oh oh".split_every_n_of_str(" ", 2); |
| 110 | + assert_eq!(splitter.next(), Some("oh oh")); |
| 111 | + assert_eq!(splitter.next(), Some("oh oh")); |
| 112 | + assert_eq!(splitter.next(), Some("oh")); |
| 113 | + assert_eq!(splitter.next(), None); |
| 114 | + assert_eq!(splitter.next(), None); |
| 115 | + |
| 116 | + let mut splitter: SplitEvery = "oboooobobobobob".split_every_n_of_char('o', 3); |
| 117 | + assert_eq!(splitter.next(), Some("obo")); |
| 118 | + assert_eq!(splitter.next(), Some("oob")); |
| 119 | + assert_eq!(splitter.next(), Some("bobob")); |
| 120 | + assert_eq!(splitter.next(), Some("b")); |
| 121 | + assert_eq!(splitter.next(), None); |
| 122 | + assert_eq!(splitter.next(), None); |
| 123 | + |
| 124 | + let mut splitter: SplitEvery = "hhhahahahaha".split_every_n_of_char('h', 1); |
| 125 | + assert_eq!(splitter.next(), Some("")); |
| 126 | + assert_eq!(splitter.next(), Some("")); |
| 127 | + assert_eq!(splitter.next(), Some("")); |
| 128 | + assert_eq!(splitter.next(), Some("a")); |
| 129 | + assert_eq!(splitter.next(), Some("a")); |
| 130 | + assert_eq!(splitter.next(), Some("a")); |
| 131 | + assert_eq!(splitter.next(), Some("a")); |
| 132 | + assert_eq!(splitter.next(), Some("a")); |
| 133 | + assert_eq!(splitter.next(), None); |
| 134 | + assert_eq!(splitter.next(), None); |
| 135 | + |
| 136 | + let mut splitter: SplitEvery = |
| 137 | + "Oh hi there I don't really know what to say".split_every_n_of_str(" ", 3); |
| 138 | + assert_eq!(splitter.next().unwrap(), "Oh hi there"); |
| 139 | + assert_eq!(splitter.next().unwrap(), "I don't really"); |
| 140 | + assert_eq!(splitter.next().unwrap(), "know what to"); |
| 141 | + assert_eq!(splitter.next().unwrap(), "say"); |
| 142 | +} |
0 commit comments