Skip to content

Commit aeb74e8

Browse files
committed
libct/cg/fscommon: use strings.Cut
Use strings.Cut in ParseKeyValue and GetValueByKey. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent c1e1a52 commit aeb74e8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

libcontainer/cgroups/fscommon/utils.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ func ParseUint(s string, base, bitSize int) (uint64, error) {
5656

5757
// ParseKeyValue parses a space-separated "name value" kind of cgroup
5858
// parameter and returns its key as a string, and its value as uint64
59-
// (ParseUint is used to convert the value). For example,
59+
// (using [ParseUint] to convert the value). For example,
6060
// "io_service_bytes 1234" will be returned as "io_service_bytes", 1234.
6161
func ParseKeyValue(t string) (string, uint64, error) {
62-
parts := strings.SplitN(t, " ", 3)
63-
if len(parts) != 2 {
62+
key, val, ok := strings.Cut(t, " ")
63+
if !ok {
6464
return "", 0, fmt.Errorf("line %q is not in key value format", t)
6565
}
6666

67-
value, err := ParseUint(parts[1], 10, 64)
67+
value, err := ParseUint(val, 10, 64)
6868
if err != nil {
6969
return "", 0, err
7070
}
7171

72-
return parts[0], value, nil
72+
return key, value, nil
7373
}
7474

7575
// GetValueByKey reads a key-value pairs from the specified cgroup file,
76-
// and returns a value of the specified key. ParseUint is used for value
76+
// and returns a value of the specified key. [ParseUint] is used for value
7777
// conversion.
7878
func GetValueByKey(path, file, key string) (uint64, error) {
7979
content, err := cgroups.ReadFile(path, file)
@@ -83,9 +83,9 @@ func GetValueByKey(path, file, key string) (uint64, error) {
8383

8484
lines := strings.Split(content, "\n")
8585
for _, line := range lines {
86-
arr := strings.Split(line, " ")
87-
if len(arr) == 2 && arr[0] == key {
88-
val, err := ParseUint(arr[1], 10, 64)
86+
k, v, ok := strings.Cut(line, " ")
87+
if ok && k == key {
88+
val, err := ParseUint(v, 10, 64)
8989
if err != nil {
9090
err = &ParseError{Path: path, File: file, Err: err}
9191
}

0 commit comments

Comments
 (0)