]> Cypherpunks repositories - gostls13.git/commitdiff
strings,bytes: use result of copy in subsequent slicing
authorKeith Randall <khr@golang.org>
Mon, 28 Oct 2024 21:15:13 +0000 (14:15 -0700)
committerKeith Randall <khr@golang.org>
Tue, 29 Oct 2024 16:47:05 +0000 (16:47 +0000)
This can get rid of a bounds check.
Followup to CL 622240.

Change-Id: I9d0a2c0408b8d274c46136d32d7a5fb09b4aad1c
Reviewed-on: https://go-review.googlesource.com/c/go/+/622955
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/bytes/buffer.go
src/runtime/string.go

index 4176d670ec83fe00d8d445e49b213e956556796a..f90d9eca0fe49611766d016f817237504da670b9 100644 (file)
@@ -247,8 +247,8 @@ func growSlice(b []byte, n int) []byte {
                c = 2 * cap(b)
        }
        b2 := append([]byte(nil), make([]byte, c)...)
-       copy(b2, b)
-       return b2[:len(b)]
+       i := copy(b2, b)
+       return b2[:i]
 }
 
 // WriteTo writes data to w until the buffer is drained or an error occurs.
index 640ee02a3cbb5c996f2f0ab2b0d6d2444c2c427f..e43f4cca51ccd4243f614529e73832e3bb5eb3a4 100644 (file)
@@ -51,8 +51,8 @@ func concatstrings(buf *tmpBuf, a []string) string {
        }
        s, b := rawstringtmp(buf, l)
        for _, x := range a {
-               copy(b, x)
-               b = b[len(x):]
+               n := copy(b, x)
+               b = b[n:]
        }
        return s
 }