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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#[derive(Debug)]
pub enum ListParseError
{
IoError(io::Error),
ContainsAnEmptyIndexOrRange,
CouldNotParseIndexAsNotAString
{
description: &'static str,
unparsable_index: Box<[u8]>,
cause: Utf8Error,
},
CouldNotParseIndex
{
description: &'static str,
unparsable_index: String,
cause: ParseIntError,
},
ContainsMisSortedIndices
{
first: u16,
next_minimum_index_expected: u16
},
RangeIsNotAnAscendingRangeWithMoreThanOneElement
{
first: u16,
second: u16
},
}
impl Display for ListParseError
{
#[inline(always)]
fn fmt(&self, f: &mut Formatter) -> fmt::Result
{
<ListParseError as Debug>::fmt(self, f)
}
}
impl error::Error for ListParseError
{
#[inline(always)]
fn source(&self) -> Option<&(error::Error + 'static)>
{
use self::ListParseError::*;
match self
{
&IoError(ref error) => Some(error),
&ContainsAnEmptyIndexOrRange => None,
&CouldNotParseIndexAsNotAString { ref cause, .. } => Some(cause),
&CouldNotParseIndex { ref cause, .. } => Some(cause),
&ContainsMisSortedIndices { .. } => None,
&RangeIsNotAnAscendingRangeWithMoreThanOneElement { .. } => None,
}
}
}
impl From<io::Error> for ListParseError
{
#[inline(always)]
fn from(error: io::Error) -> Self
{
ListParseError::IoError(error)
}
}
impl ListParseError
{
pub fn parse_linux_list_string<Mapper: Fn(u16) -> R, R: Ord>(linux_list_string: &[u8], mapper: Mapper) -> Result<BTreeSet<R>, ListParseError>
{
#[inline(always)]
fn parse_index(index_string: &[u8], description: &'static str) -> Result<u16, ListParseError>
{
use self::ListParseError::*;
let index_string = match from_utf8(index_string)
{
Ok(index_string) => index_string,
Err(cause) => return Err(CouldNotParseIndexAsNotAString { description, unparsable_index: index_string.to_vec().into_boxed_slice(), cause }),
};
match index_string.parse()
{
Ok(index) => Ok(index),
Err(cause) => Err(CouldNotParseIndex { description, unparsable_index: index_string.to_owned(), cause }),
}
}
let mut result = BTreeSet::new();
use self::ListParseError::*;
let mut next_minimum_index_expected = 0;
for index_or_range in split(linux_list_string, b',')
{
if index_or_range.is_empty()
{
return Err(ContainsAnEmptyIndexOrRange);
}
let mut range_iterator = splitn(index_or_range, 2, b'-');
let first =
{
let index = parse_index(range_iterator.next().unwrap(), "first")?;
if index < next_minimum_index_expected
{
return Err(ContainsMisSortedIndices { first: index, next_minimum_index_expected });
}
index
};
if let Some(second) = range_iterator.last()
{
let mut range_or_range_with_groups = splitn(second, 2, b':');
let second =
{
let index = parse_index(range_or_range_with_groups.next().unwrap(), "second")?;
if first >= index
{
return Err(RangeIsNotAnAscendingRangeWithMoreThanOneElement { first, second: index });
}
index
};
match range_or_range_with_groups.last()
{
None =>
{
for index in first .. (second + 1)
{
result.insert(mapper(index));
}
next_minimum_index_expected = second;
}
Some(weird_but_rare_group_syntax) =>
{
let mut weird_but_rare_group_syntax = splitn(weird_but_rare_group_syntax, 2, b'/');
let used_size = parse_index(weird_but_rare_group_syntax.next().unwrap(), "used_size")?;
let group_size = parse_index(weird_but_rare_group_syntax.last().expect("a group does not have group_size"), "group_size")?;
assert_ne!(used_size, 0, "used_size is zero");
assert_ne!(group_size, 0, "group_size is zero");
let mut base_cpu_index = first;
while base_cpu_index < second
{
for cpu_index_increment in 0 .. used_size
{
let cpu_index = base_cpu_index + cpu_index_increment;
result.insert(mapper(cpu_index));
}
base_cpu_index += group_size;
}
}
}
}
else
{
let sole = first;
result.insert(mapper(sole));
next_minimum_index_expected = sole;
}
}
Ok(result)
}
}