modernize: Use strings.Cut instead of strings.Index where possible

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-12-15 17:56:10 +01:00
parent a25907b485
commit ff33808a79
2 changed files with 8 additions and 8 deletions

View File

@@ -9,14 +9,14 @@ import (
func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
cache := make(map[string]int, len(defaults))
for i, e := range defaults {
index := strings.Index(e, "=")
cache[e[:index]] = i
before, _, _ := strings.Cut(e, "=")
cache[before] = i
}
for _, value := range overrides {
// Values w/o = means they want this env to be removed/unset.
index := strings.Index(value, "=")
if index < 0 {
before, _, ok := strings.Cut(value, "=")
if !ok {
// no "=" in value
if i, exists := cache[value]; exists {
defaults[i] = "" // Used to indicate it should be removed
@@ -24,7 +24,7 @@ func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
continue
}
if i, exists := cache[value[:index]]; exists {
if i, exists := cache[before]; exists {
defaults[i] = value
} else {
defaults = append(defaults, value)