Skip to content

Commit f0431a2

Browse files
committed
cpuset: rewrite the parse function from scratch
no intended changes in behaviour. Signed-off-by: Francesco Romani <[email protected]>
1 parent 76ce617 commit f0431a2

File tree

3 files changed

+83
-69
lines changed

3 files changed

+83
-69
lines changed

Diff for: cpuset.go

-69
This file was deleted.

Diff for: cpuset_empty.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2020 Red Hat, Inc.
15+
*/
16+
17+
package cpuset
18+
19+
// Empty returns a empty cpuset, a slice of ints
20+
func Empty() []int {
21+
return []int{}
22+
}

Diff for: cpuset_parse.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2020 Red Hat, Inc.
15+
*/
16+
17+
package cpuset
18+
19+
import (
20+
"sort"
21+
"strconv"
22+
"strings"
23+
)
24+
25+
// Parse takes a string representing a cpuset definition, and returns as sorted slice of ints
26+
func Parse(s string) ([]int, error) {
27+
cpus := Empty()
28+
if len(s) == 0 {
29+
return cpus, nil
30+
}
31+
32+
for _, item := range strings.Split(s, ",") {
33+
item = strings.TrimSpace(item)
34+
if !strings.Contains(item, "-") {
35+
// single cpu: "2"
36+
cpuid, err := strconv.Atoi(item)
37+
if err != nil {
38+
return cpus, err
39+
}
40+
cpus = append(cpus, cpuid)
41+
continue
42+
}
43+
44+
// range of cpus: "0-3"
45+
cpuRange := strings.SplitN(item, "-", 2)
46+
cpuBegin, err := strconv.Atoi(cpuRange[0])
47+
if err != nil {
48+
return cpus, err
49+
}
50+
cpuEnd, err := strconv.Atoi(cpuRange[1])
51+
if err != nil {
52+
return cpus, err
53+
}
54+
for cpuid := cpuBegin; cpuid <= cpuEnd; cpuid++ {
55+
cpus = append(cpus, cpuid)
56+
}
57+
}
58+
59+
sort.Ints(cpus)
60+
return cpus, nil
61+
}

0 commit comments

Comments
 (0)