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
quick_error!
{
#[derive(Debug)]
pub enum ListParseError
{
IoError(err: io::Error)
{
cause(err)
from()
}
ContainsAnEmptyIndexOrRange
{
}
CouldNotParseIndex(description: &'static str, unparsable_index: String, cause: ParseIntError)
{
display("Could not parse {} index '{}' because '{}'", description, unparsable_index, cause)
cause(cause)
}
ContainsMisSortedIndices(first: u16, next_minimum_index_expected: u16)
{
display("Did not expect '{}' when minimum expectation is '{}'", first, next_minimum_index_expected)
}
RangeIsNotAnAscendingRangeWithMoreThanOneElement(first: u16, second: u16)
{
display("range is not an ascending range with more than one element; it is '{}-{}'!", first, second)
}
}
}
impl ListParseError
{
pub fn parse_linux_list_string<Mapper: Fn(u16) -> R, R: Ord>(linux_list_string: &str, mapper: Mapper) -> Result<BTreeSet<R>, ListParseError>
{
#[inline(always)]
fn parse_index(index_string: &str, description: &'static str) -> Result<u16, ListParseError>
{
match index_string.parse()
{
Ok(index) => Ok(index),
Err(error) => Err(ListParseError::CouldNotParseIndex(description, index_string.to_owned(), error)),
}
}
let mut result = BTreeSet::new();
use self::ListParseError::*;
let mut next_minimum_index_expected = 0;
for index_or_range in linux_list_string.split(',')
{
if index_or_range.is_empty()
{
return Err(ContainsAnEmptyIndexOrRange);
}
let mut range_iterator = index_or_range.splitn(2, '-');
let first =
{
let index = parse_index(range_iterator.next().unwrap(), "first")?;
if index < next_minimum_index_expected
{
return Err(ContainsMisSortedIndices(index, next_minimum_index_expected));
}
index
};
if let Some(second) = range_iterator.last()
{
let mut range_or_range_with_groups = second.splitn(2, ':');
let second =
{
let index = parse_index(range_or_range_with_groups.next().unwrap(), "second")?;
if first >= index
{
return Err(RangeIsNotAnAscendingRangeWithMoreThanOneElement(first, 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 = weird_but_rare_group_syntax.splitn(2, '/');
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)
}
}