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
pub struct SplitPreserveWS<'a> {
string: Option<Token<'a>>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Token<'a> {
Whitespace(&'a str),
Other(&'a str),
}
impl<'a> SplitPreserveWS<'a> {
pub fn new(string: &'a str) -> Self {
if string.is_empty() {
Self { string: None }
} else if string.starts_with(char::is_whitespace) {
Self {
string: Some(Token::Whitespace(string)),
}
} else {
Self {
string: Some(Token::Other(string)),
}
}
}
pub fn map_words<S>(self, mut f: S) -> std::iter::Map<Self, impl FnMut(Token<'a>) -> String>
where
S: FnMut(&str) -> String,
{
self.map(move |t: Token<'a>| match t {
Token::Other(s) => f(s),
Token::Whitespace(s) => s.to_string(),
})
}
pub fn map_whitespace<S>(
self,
mut f: S,
) -> std::iter::Map<Self, impl FnMut(Token<'a>) -> String>
where
S: FnMut(&str) -> String,
{
self.map(move |t: Token<'a>| match t {
Token::Other(s) => s.to_string(),
Token::Whitespace(s) => f(s),
})
}
}
impl<'a> Iterator for SplitPreserveWS<'a> {
type Item = Token<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.string.take().map(|t| match t {
Token::Whitespace(s) => {
let (token, rest) = match s.find(|c: char| !c.is_whitespace()) {
Some(i) => {
let (a, b) = s.split_at(i);
(a, Some(Token::Other(b)))
}
None => (s, None),
};
self.string = rest;
Token::Whitespace(token)
}
Token::Other(s) => {
let (token, rest) = match s.find(char::is_whitespace) {
Some(i) => {
let (a, b) = s.split_at(i);
(a, Some(Token::Whitespace(b)))
}
None => (s, None),
};
self.string = rest;
Token::Other(token)
}
})
}
}