-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
71 lines (59 loc) · 1.82 KB
/
main.rs
File metadata and controls
71 lines (59 loc) · 1.82 KB
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
struct TokenSet {
data: String
}
impl TokenSet {
pub fn new(data: String) -> TokenSet {
TokenSet{ data: data }
}
pub fn iter<'a>(&'a self) -> TokenIterator<'a> {
TokenIterator::new(&self.data)
}
}
struct TokenIterator<'a> {
data: &'a str,
token_start: usize,
token_end: usize,
}
impl<'a> TokenIterator<'a> {
pub fn new(data: &'a str) -> TokenIterator<'a> {
TokenIterator{
data: data,
token_start: 0,
token_end: 0
}
}
}
impl<'a> Iterator for TokenIterator<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
// Move beyond the previous token, if any.
self.token_start = self.token_end;
// Try to find a non-whitespace character that
// will denote the beginning of another token.
let next_char_index = self.data[self.token_start..]
.char_indices()
.find(|&(_, c)| !c.is_whitespace())
.map(|(i, _)| i + self.token_start);
if let Some(start_index) = next_char_index {
// We've found the start of another token.
self.token_start = start_index;
// Find the end of the token, whether that
// is whitespace or the end of the string.
self.token_end = self.data[self.token_start..]
.char_indices()
.find(|&(_, c)| c.is_whitespace())
.map(|(i, _)| i + self.token_start)
.unwrap_or(self.data.len());
Some(&self.data[self.token_start..self.token_end])
} else {
None
}
}
}
fn main() {
let iterator = TokenSet::new("iterator data".to_string());
let tokens: Vec<&str> = iterator.iter().collect();
for token in tokens.iter() {
println!("{}", token);
}
}