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
use std::fmt;
pub struct Strings {
strings: Vec<Vec<u8>>,
current_string_index: usize,
}
impl Strings {
pub fn new(string_area: Vec<u8>) -> Strings {
Strings {
strings: {
if string_area == &[] {
vec![]
} else {
string_area
.split(|num| *num == 0)
.into_iter()
.map(|string_slice| string_slice.to_vec())
.collect()
}
},
current_string_index: 0,
}
}
fn reset(&mut self) {
self.current_string_index = 0;
}
pub fn get_string(&self, index: u8) -> Option<String> {
let index_usize = index as usize;
if index_usize == 0 || index_usize > self.strings.len() {
return None;
}
Some(
self.strings[index_usize - 1]
.iter()
.map(|x| *x as char)
.collect(),
)
}
pub fn iter(&self) -> std::slice::Iter<'_, Vec<u8>> {
self.strings.iter()
}
}
impl Iterator for Strings {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
if self.current_string_index == self.strings.len() {
self.reset();
return None;
}
let result: String = self.strings[self.current_string_index]
.iter()
.map(|x| *x as char)
.collect();
self.current_string_index = self.current_string_index + 1;
Some(result)
}
}
impl IntoIterator for &Strings {
type Item = String;
type IntoIter = Strings;
fn into_iter(self) -> Self::IntoIter {
Strings {
strings: self.strings.clone(),
current_string_index: 0,
}
}
}
impl fmt::Debug for Strings {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_list().entries(self.into_iter()).finish()
}
}