modernize: Prefer strings.SplitSeq instead of Split

Avoids extra allocations. Added in Go 1.24.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-12-15 17:53:59 +01:00
parent c9b0a21bb1
commit a25907b485
29 changed files with 50 additions and 50 deletions

View File

@@ -545,7 +545,7 @@ func validateEndpointSettings(nw *libnetwork.Network, nwName string, epConfig *n
}
if sysctls, ok := epConfig.DriverOpts[netlabel.EndpointSysctls]; ok {
for _, sysctl := range strings.Split(sysctls, ",") {
for sysctl := range strings.SplitSeq(sysctls, ",") {
scname := strings.SplitN(sysctl, ".", 5)
// Allow "ifname" as well as "IFNAME", because the CLI converts to lower case.
if len(scname) != 5 ||

View File

@@ -404,7 +404,7 @@ func extractDistributionSources(labels map[string]string) []distributionSource {
// if yes, read it as source
for k, v := range labels {
if reg := strings.TrimPrefix(k, containerdlabels.LabelDistributionSource); reg != k {
for _, repo := range strings.Split(v, ",") {
for repo := range strings.SplitSeq(v, ",") {
ref, err := reference.ParseNamed(reg + "/" + repo)
if err != nil {
continue

View File

@@ -56,7 +56,7 @@ func Scan(text string) (*events.Message, error) {
}
attrs := make(map[string]string)
for _, a := range strings.Split(md["attributes"], ", ") {
for a := range strings.SplitSeq(md["attributes"], ", ") {
k, v, _ := strings.Cut(a, "=")
attrs[k] = v
}

View File

@@ -264,7 +264,7 @@ func (d *Driver) getLowerDirs(id string) ([]string, error) {
var lowersArray []string
lowers, err := os.ReadFile(path.Join(d.dir(id), lowerFile))
if err == nil {
for _, s := range strings.Split(string(lowers), ":") {
for s := range strings.SplitSeq(string(lowers), ":") {
lp, err := os.Readlink(path.Join(d.home, s))
if err != nil {
return nil, err

View File

@@ -469,7 +469,7 @@ func (d *Driver) getLowerDirs(id string) ([]string, error) {
var lowersArray []string
lowers, err := os.ReadFile(path.Join(d.dir(id), lowerFile))
if err == nil {
for _, s := range strings.Split(string(lowers), ":") {
for s := range strings.SplitSeq(string(lowers), ":") {
lp, err := os.Readlink(path.Join(d.home, s))
if err != nil {
return nil, err

View File

@@ -361,8 +361,8 @@ func parseInitVersion(v string) (version string, commit string, _ error) {
// commit: 69663f0bd4b60df09991c08812a60108003fa340
// spec: 1.0.0
func parseRuntimeVersion(v string) (runtime, version, commit string, _ error) {
lines := strings.Split(strings.TrimSpace(v), "\n")
for _, line := range lines {
lines := strings.SplitSeq(strings.TrimSpace(v), "\n")
for line := range lines {
if strings.Contains(line, "version") {
s := strings.Split(line, "version")
runtime = strings.TrimSpace(s[0])

View File

@@ -60,7 +60,7 @@ func (e *imageExporter) Resolve(ctx context.Context, id int, attrs map[string]st
for k, v := range attrs {
switch exptypes.ImageExporterOptKey(k) {
case exptypes.OptKeyName:
for _, v := range strings.Split(v, ",") {
for v := range strings.SplitSeq(v, ",") {
ref, err := reference.ParseNormalizedNamed(v)
if err != nil {
return nil, err

View File

@@ -100,7 +100,7 @@ func (i *imageExporterInstanceWrapper) processNamedCallback(ctx context.Context,
return
}
for _, name := range strings.Split(imageName, ",") {
for name := range strings.SplitSeq(imageName, ",") {
ref, err := reference.ParseNormalizedNamed(name)
if err != nil {
// Shouldn't happen, but log if it does and continue.

View File

@@ -86,8 +86,8 @@ func appendDistributionSourceLabel(originLabel, repo string) string {
}
func hasDistributionSource(label, repo string) bool {
sources := strings.Split(label, ",")
for _, s := range sources {
sources := strings.SplitSeq(label, ",")
for s := range sources {
if s == repo {
return true
}

View File

@@ -31,7 +31,7 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string
extra := make(map[string]string)
if labels, ok := info.Config["labels"]; ok && labels != "" {
for _, l := range strings.Split(labels, ",") {
for l := range strings.SplitSeq(labels, ",") {
if v, ok := info.ContainerLabels[l]; ok {
if keyMod != nil {
l = keyMod(l)
@@ -69,7 +69,7 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string
}
if env, ok := info.Config["env"]; ok && env != "" {
for _, l := range strings.Split(env, ",") {
for l := range strings.SplitSeq(env, ",") {
if v, ok := envMapping[l]; ok {
if keyMod != nil {
l = keyMod(l)

View File

@@ -408,7 +408,7 @@ const (
// hasMountInfoOption checks if any of the passed any of the given option values
// are set in the passed in option string.
func hasMountInfoOption(opts string, vals ...string) bool {
for _, opt := range strings.Split(opts, " ") {
for opt := range strings.SplitSeq(opts, " ") {
for _, val := range vals {
if strings.HasPrefix(opt, val) {
return true

View File

@@ -419,7 +419,7 @@ func (v *localVolume) LiveRestoreVolume(ctx context.Context, _ string) error {
// getAddress finds out address/hostname from options
func getAddress(opts string) string {
for _, opt := range strings.Split(opts, ",") {
for opt := range strings.SplitSeq(opts, ",") {
if strings.HasPrefix(opt, "addr=") {
return strings.TrimPrefix(opt, "addr=")
}
@@ -429,7 +429,7 @@ func getAddress(opts string) string {
// getPassword finds out a password from options
func getPassword(opts string) string {
for _, opt := range strings.Split(opts, ",") {
for opt := range strings.SplitSeq(opts, ",") {
if strings.HasPrefix(opt, "password=") {
return strings.TrimPrefix(opt, "password=")
}

View File

@@ -174,7 +174,7 @@ var linuxPropagationModes = map[mount.Propagation]bool{
const linuxDefaultPropagationMode = mount.PropagationRPrivate
func linuxGetPropagation(mode string) mount.Propagation {
for _, o := range strings.Split(mode, ",") {
for o := range strings.SplitSeq(mode, ",") {
prop := mount.Propagation(o)
if linuxPropagationModes[prop] {
return prop
@@ -184,7 +184,7 @@ func linuxGetPropagation(mode string) mount.Propagation {
}
func linuxHasPropagation(mode string) bool {
for _, o := range strings.Split(mode, ",") {
for o := range strings.SplitSeq(mode, ",") {
if linuxPropagationModes[mount.Propagation(o)] {
return true
}
@@ -203,7 +203,7 @@ func linuxValidMountMode(mode string) bool {
copyModeCount := 0
consistencyModeCount := 0
for _, o := range strings.Split(mode, ",") {
for o := range strings.SplitSeq(mode, ",") {
switch {
case rwModes[o]:
rwModeCount++
@@ -256,7 +256,7 @@ func (p *linuxParser) ReadWrite(mode string) bool {
return false
}
for _, o := range strings.Split(mode, ",") {
for o := range strings.SplitSeq(mode, ",") {
if o == "ro" {
return false
}

View File

@@ -14,7 +14,7 @@ func copyModeExists(mode string) bool {
// GetCopyMode gets the copy mode from the mode string for mounts
func getCopyMode(mode string, def bool) (bool, bool) {
for _, o := range strings.Split(mode, ",") {
for o := range strings.SplitSeq(mode, ",") {
if isEnabled, exists := copyModes[o]; exists {
return isEnabled, true
}