refactor(containermanager): use slices.Contains for PATH segment checks

The (:|^)…(:|$) regexes only tested whether a path was an exact
colon-delimited segment — what slices.Contains over strings.Split does
directly, without recompiling a regex on every loop iteration.

Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
This commit is contained in:
Luca Di Maio committed 2026-07-24 14:25:45 +02:00
1 parent e513cf8d8a
commit 60103b1da1
1 file changed
+5 -6
+5 -6
View File
@@ -6,6 +6,7 @@ import (
"os"
"regexp"
"runtime"
"slices"
"strings"
"time"
@@ -192,10 +193,10 @@ func BuildContainerPath(cleanPath bool, hostPath string, containerPath string) s
}
// Add standard paths not in host PATH
hostSegments := strings.Split(hostPath, ":")
var additionalPaths []string
for _, sp := range standardPaths {
pattern := regexp.MustCompile(`(:|^)` + regexp.QuoteMeta(sp) + `(:|$)`)
if !pattern.MatchString(hostPath) {
if !slices.Contains(hostSegments, sp) {
additionalPaths = append(additionalPaths, sp)
}
}
@@ -231,8 +232,7 @@ func reorderFHSPath(path string) string {
// If /usr/bin or /usr/sbin were absent, their local counterparts were
// skipped above; re-add any that went missing (prepended, like the shell).
for _, lp := range []string{"/usr/local/bin", "/usr/local/sbin"} {
pattern := regexp.MustCompile(`(:|^)` + regexp.QuoteMeta(lp) + `(:|$)`)
if !pattern.MatchString(result) {
if !slices.Contains(strings.Split(result, ":"), lp) {
result = lp + ":" + result
}
}
@@ -244,10 +244,9 @@ func BuildXDGPaths(envVar string, standardPaths []string) string {
containerPaths := os.Getenv(envVar)
for _, sp := range standardPaths {
pattern := regexp.MustCompile(`(:|^)` + regexp.QuoteMeta(sp) + `(:|$)`)
if containerPaths == "" {
containerPaths = sp
} else if !pattern.MatchString(containerPaths) {
} else if !slices.Contains(strings.Split(containerPaths, ":"), sp) {
containerPaths = containerPaths + ":" + sp
}
}