Browse code

modernize: Use strings.Builder instead of string concatenation

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2025/12/16 02:28:50
Showing 8 changed files
... ...
@@ -79,12 +79,13 @@ func dispatchLabel(ctx context.Context, d dispatchRequest, c *instructions.Label
79 79
 	if d.state.runConfig.Labels == nil {
80 80
 		d.state.runConfig.Labels = make(map[string]string)
81 81
 	}
82
-	commitStr := "LABEL"
82
+	var commitStr strings.Builder
83
+	commitStr.WriteString("LABEL")
83 84
 	for _, v := range c.Labels {
84 85
 		d.state.runConfig.Labels[v.Key] = v.Value
85
-		commitStr += " " + v.String()
86
+		commitStr.WriteString(" " + v.String())
86 87
 	}
87
-	return d.builder.commit(ctx, d.state, commitStr)
88
+	return d.builder.commit(ctx, d.state, commitStr.String())
88 89
 }
89 90
 
90 91
 // ADD foo /path
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"path/filepath"
11 11
 	"reflect"
12 12
 	"runtime"
13
+	"strings"
13 14
 
14 15
 	"github.com/containerd/containerd/v2/pkg/tracing"
15 16
 	"github.com/containerd/log"
... ...
@@ -78,7 +79,7 @@ func (l *tarexporter) Load(ctx context.Context, inTar io.ReadCloser, outStream i
78 78
 	}
79 79
 
80 80
 	var parentLinks []parentLink
81
-	var imageIDsStr string
81
+	var imageIDsStr strings.Builder
82 82
 	var imageRefCount int
83 83
 
84 84
 	for _, m := range manifest {
... ...
@@ -142,7 +143,7 @@ func (l *tarexporter) Load(ctx context.Context, inTar io.ReadCloser, outStream i
142 142
 		if err != nil {
143 143
 			return err
144 144
 		}
145
-		imageIDsStr += fmt.Sprintf("Loaded image ID: %s\n", imgID)
145
+		imageIDsStr.WriteString(fmt.Sprintf("Loaded image ID: %s\n", imgID))
146 146
 
147 147
 		imageRefCount = 0
148 148
 		for _, repoTag := range m.RepoTags {
... ...
@@ -172,7 +173,7 @@ func (l *tarexporter) Load(ctx context.Context, inTar io.ReadCloser, outStream i
172 172
 	}
173 173
 
174 174
 	if imageRefCount == 0 {
175
-		outStream.Write([]byte(imageIDsStr))
175
+		outStream.Write([]byte(imageIDsStr.String()))
176 176
 	}
177 177
 
178 178
 	return nil
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"net"
8 8
 	"net/http"
9 9
 	"strconv"
10
+	"strings"
10 11
 	"sync"
11 12
 	"time"
12 13
 
... ...
@@ -154,13 +155,13 @@ func (s *Server) help(w http.ResponseWriter, r *http.Request) {
154 154
 		"url":       r.URL.String(),
155 155
 	}).Info("help done")
156 156
 
157
-	var result string
157
+	var result strings.Builder
158 158
 	s.mu.Lock()
159 159
 	for path := range s.handlers {
160
-		result += fmt.Sprintf("%s\n", path)
160
+		result.WriteString(fmt.Sprintf("%s\n", path))
161 161
 	}
162 162
 	s.mu.Unlock()
163
-	_, _ = HTTPReply(w, CommandSucceed(&StringCmd{Info: result}), jsonOutput)
163
+	_, _ = HTTPReply(w, CommandSucceed(&StringCmd{Info: result.String()}), jsonOutput)
164 164
 }
165 165
 
166 166
 func ready(w http.ResponseWriter, r *http.Request) {
... ...
@@ -3,6 +3,7 @@ package diagnostic
3 3
 import (
4 4
 	"fmt"
5 5
 	"net/netip"
6
+	"strings"
6 7
 )
7 8
 
8 9
 // StringInterface interface that has to be implemented by messages
... ...
@@ -82,11 +83,12 @@ type TableObj struct {
82 82
 }
83 83
 
84 84
 func (t *TableObj) String() string {
85
-	output := fmt.Sprintf("total entries: %d\n", t.Length)
85
+	var output strings.Builder
86
+	output.WriteString(fmt.Sprintf("total entries: %d\n", t.Length))
86 87
 	for _, e := range t.Elements {
87
-		output += e.String()
88
+		output.WriteString(e.String())
88 89
 	}
89
-	return output
90
+	return output.String()
90 91
 }
91 92
 
92 93
 // PeerEntryObj entry in the networkdb peer table
... ...
@@ -163,16 +163,16 @@ func testIptabler(t *testing.T, tn string, config firewaller.Config, netConfig f
163 163
 		// - iptables-nft and iptables-legacy pick a different order when dumping all tables
164 164
 		// - if the raw table isn't used it's not included in the all-tables dump but, once it's been used, it's always
165 165
 		//   included ... so, "cleaned" results would differ only in the empty raw table.
166
-		var dump string
166
+		var dump strings.Builder
167 167
 		for _, table := range []string{"raw", "filter", "nat"} {
168 168
 			res := icmd.RunCommand(cmd+"-save", "-t", table)
169 169
 			assert.Assert(t, res.Error)
170 170
 			if !en {
171 171
 				name = tn + "/no"
172 172
 			}
173
-			dump += res.Combined()
173
+			dump.WriteString(res.Combined())
174 174
 		}
175
-		assert.Check(t, golden.String(stripComments(dump), name+"__"+cmd+".golden"))
175
+		assert.Check(t, golden.String(stripComments(dump.String()), name+"__"+cmd+".golden"))
176 176
 	}
177 177
 
178 178
 	makePB := func(hip string, cip netip.Addr) types.PortBinding {
... ...
@@ -199,19 +199,19 @@ func TestRCModify(t *testing.T) {
199 199
 	for _, tc := range testcases {
200 200
 		t.Run(tc.name, func(t *testing.T) {
201 201
 			tc := tc
202
-			var input string
202
+			var input strings.Builder
203 203
 			if len(tc.inputNS) != 0 {
204 204
 				for _, ns := range tc.inputNS {
205
-					input += "nameserver " + ns + "\n"
205
+					input.WriteString("nameserver " + ns + "\n")
206 206
 				}
207 207
 			}
208 208
 			if len(tc.inputSearch) != 0 {
209
-				input += "search " + strings.Join(tc.inputSearch, " ") + "\n"
209
+				input.WriteString("search " + strings.Join(tc.inputSearch, " ") + "\n")
210 210
 			}
211 211
 			if len(tc.inputOptions) != 0 {
212
-				input += "options " + strings.Join(tc.inputOptions, " ") + "\n"
212
+				input.WriteString("options " + strings.Join(tc.inputOptions, " ") + "\n")
213 213
 			}
214
-			rc, err := Parse(bytes.NewBufferString(input), "")
214
+			rc, err := Parse(bytes.NewBufferString(input.String()), "")
215 215
 			assert.NilError(t, err)
216 216
 			assert.Check(t, is.DeepEqual(a2s(rc.NameServers()), tc.inputNS, cmpopts.EquateEmpty()))
217 217
 			assert.Check(t, is.DeepEqual(rc.Search(), tc.inputSearch))
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"context"
5 5
 	"fmt"
6 6
 	"runtime"
7
+	"strings"
7 8
 	"sync"
8 9
 
9 10
 	"github.com/moby/moby/v2/pkg/parsers/kernel"
... ...
@@ -80,19 +81,19 @@ const charsToEscape = `();\`
80 80
 
81 81
 // escapeStr returns s with every rune in charsToEscape escaped by a backslash
82 82
 func escapeStr(s string) string {
83
-	var ret string
83
+	var ret strings.Builder
84 84
 	for _, currRune := range s {
85 85
 		appended := false
86 86
 		for _, escapableRune := range charsToEscape {
87 87
 			if currRune == escapableRune {
88
-				ret += `\` + string(currRune)
88
+				ret.WriteString(`\` + string(currRune))
89 89
 				appended = true
90 90
 				break
91 91
 			}
92 92
 		}
93 93
 		if !appended {
94
-			ret += string(currRune)
94
+			ret.WriteRune(currRune)
95 95
 		}
96 96
 	}
97
-	return ret
97
+	return ret.String()
98 98
 }
... ...
@@ -16,19 +16,20 @@ func printArgs(args []fnArg) string {
16 16
 }
17 17
 
18 18
 func buildImports(specs []importSpec) string {
19
-	imports := `
19
+	var imports strings.Builder
20
+	imports.WriteString(`
20 21
 import(
21 22
 	"errors"
22 23
 	"time"
23 24
 
24 25
 	"github.com/moby/moby/v2/pkg/plugins"
25
-`
26
+`)
26 27
 	for _, i := range specs {
27
-		imports += "\t" + i.String() + "\n"
28
+		imports.WriteString("\t" + i.String() + "\n")
28 29
 	}
29
-	imports += `)
30
-`
31
-	return imports
30
+	imports.WriteString(`)
31
+`)
32
+	return imports.String()
32 33
 }
33 34
 
34 35
 func marshalType(t string) string {