Skip to content

Commit 03f714e

Browse files
committed
libct: use strings.CutPrefix where possible
Using strings.CutPrefix (available since Go 1.20) instead of strings.HasPrefix and/or strings.TrimPrefix makes the code a tad more straightforward. No functional change. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 62de3a4 commit 03f714e

File tree

5 files changed

+7
-11
lines changed

5 files changed

+7
-11
lines changed

devices/systemd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ func findDeviceGroup(ruleType devices.Type, ruleMajor int64) (string, error) {
224224
continue
225225
}
226226

227-
group := strings.TrimPrefix(line, ruleMajorStr)
228-
if len(group) < len(line) { // got it
227+
if group, ok := strings.CutPrefix(line, ruleMajorStr); ok {
229228
return prefix + group, nil
230229
}
231230
}

file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func openFile(dir, file string, flags int) (*os.File, error) {
151151
if prepareOpenat2() != nil {
152152
return openFallback(path, flags, mode)
153153
}
154-
relPath := strings.TrimPrefix(path, cgroupfsPrefix)
155-
if len(relPath) == len(path) { // non-standard path, old system?
154+
relPath, ok := strings.CutPrefix(path, cgroupfsPrefix)
155+
if !ok { // Non-standard path, old system?
156156
return openFallback(path, flags, mode)
157157
}
158158

fs2/freezer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ func waitFrozen(dirPath string) (cgroups.FreezerState, error) {
104104
if i == maxIter {
105105
return cgroups.Undefined, fmt.Errorf("timeout of %s reached waiting for the cgroup to freeze", waitTime*maxIter)
106106
}
107-
line := scanner.Text()
108-
val := strings.TrimPrefix(line, "frozen ")
109-
if val != line { // got prefix
107+
if val, ok := strings.CutPrefix(scanner.Text(), "frozen "); ok {
110108
if val[0] == '1' {
111109
return cgroups.Frozen, nil
112110
}

systemd/user.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ func DetectUID() (int, error) {
6161
scanner := bufio.NewScanner(bytes.NewReader(b))
6262
for scanner.Scan() {
6363
s := strings.TrimSpace(scanner.Text())
64-
if strings.HasPrefix(s, "OwnerUID=") {
65-
uidStr := strings.TrimPrefix(s, "OwnerUID=")
64+
if uidStr, ok := strings.CutPrefix(s, "OwnerUID="); ok {
6665
i, err := strconv.Atoi(uidStr)
6766
if err != nil {
6867
return -1, fmt.Errorf("could not detect the OwnerUID: %w", err)

utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ func getHugePageSizeFromFilenames(fileNames []string) ([]string, error) {
332332

333333
for _, file := range fileNames {
334334
// example: hugepages-1048576kB
335-
val := strings.TrimPrefix(file, "hugepages-")
336-
if len(val) == len(file) {
335+
val, ok := strings.CutPrefix(file, "hugepages-")
336+
if !ok {
337337
// Unexpected file name: no prefix found, ignore it.
338338
continue
339339
}

0 commit comments

Comments
 (0)