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
use std::mem;
use super::{str_as_ptr, str_as_mut_ptr, str_from_raw_parts, str_from_raw_parts_mut};
macro_rules! str_group_by_key {
(struct $name:ident, $elem:ty, $as_ptr:ident, $as_str:ident) => {
impl<'a, F> $name<'a, F> {
#[inline]
pub fn as_str(&self) -> &str {
self.inner
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline]
pub fn remainder_len(&self) -> usize {
self.inner.len()
}
}
impl<'a, F, K> std::iter::Iterator for $name<'a, F>
where F: FnMut(char) -> K,
K: PartialEq,
{
type Item = $elem;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.inner.is_empty() { return None }
let mut iter = self.inner.char_indices().peekable();
while let (Some((_, ac)), Some((bi, bc))) = (iter.next(), iter.peek().cloned())
{
if (self.func)(ac) != (self.func)(bc) {
let len = self.inner.len();
let ptr = $as_ptr(self.inner);
let left = unsafe { $as_str(ptr, bi) };
let right = unsafe { $as_str(ptr.add(bi), len - bi) };
self.inner = right;
return Some(left);
}
}
let output = mem::replace(&mut self.inner, Default::default());
return Some(output);
}
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<'a, F, K> std::iter::DoubleEndedIterator for $name<'a, F>
where F: FnMut(char) -> K,
K: PartialEq,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if self.inner.is_empty() { return None }
let mut iter = self.inner.char_indices().rev().peekable();
while let (Some((ai, ac)), Some((_, bc))) = (iter.next(), iter.peek().cloned())
{
if (self.func)(ac) != (self.func)(bc) {
let len = self.inner.len();
let ptr = $as_ptr(self.inner);
let left = unsafe { $as_str(ptr, ai) };
let right = unsafe { $as_str(ptr.add(ai), len - ai) };
self.inner = left;
return Some(right);
}
}
let output = mem::replace(&mut self.inner, Default::default());
return Some(output);
}
}
impl<'a, F, K> std::iter::FusedIterator for $name<'a, F>
where F: FnMut(char) -> K,
K: PartialEq,
{ }
}
}
pub struct LinearStrGroupByKey<'a, F> {
inner: &'a str,
func: F,
}
impl<'a, F> LinearStrGroupByKey<'a, F> {
pub fn new(string: &'a str, func: F) -> Self {
Self { inner: string, func }
}
}
str_group_by_key!{ struct LinearStrGroupByKey, &'a str, str_as_ptr, str_from_raw_parts }
pub struct LinearStrGroupByKeyMut<'a, F> {
inner: &'a mut str,
func: F,
}
impl<'a, F> LinearStrGroupByKeyMut<'a, F> {
pub fn new(string: &'a mut str, func: F) -> Self {
Self { inner: string, func }
}
#[inline]
pub fn as_str_mut(&mut self) -> &mut str {
&mut self.inner
}
}
str_group_by_key!{ struct LinearStrGroupByKeyMut, &'a mut str, str_as_mut_ptr, str_from_raw_parts_mut }