Skip to content

Commit c994067

Browse files
ianlancetaylorgopherbot
authored andcommitted
encoding/gob: update decgen to generate current dec_helpers
I edited dec_helpers.go without realizing that it is a generated file. Fix the generator to generate the current version (which generates a small comment change). Change-Id: I70e3bc78eb0728d23c08972611218f288dc1d29c Reviewed-on: https://go-review.googlesource.com/c/go/+/479117 Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Andrew Ekstedt <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent bd7b193 commit c994067

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/encoding/gob/dec_helpers.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/encoding/gob/decgen.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ func main() {
180180
fmt.Fprintf(&b, arrayHelper, t.lower, t.upper)
181181
fmt.Fprintf(&b, sliceHelper, t.lower, t.upper, t.decoder)
182182
}
183+
fmt.Fprintf(&b, trailer)
183184
source, err := format.Source(b.Bytes())
184185
if err != nil {
185186
log.Fatal("source format error:", err)
@@ -236,8 +237,29 @@ func dec%[2]sSlice(state *decoderState, v reflect.Value, length int, ovfl error)
236237
if state.b.Len() == 0 {
237238
errorf("decoding %[1]s array or slice: length exceeds input size (%%d elements)", length)
238239
}
240+
if i >= len(slice) {
241+
// This is a slice that we only partially allocated.
242+
growSlice(v, &slice, length)
243+
}
239244
%[3]s
240245
}
241246
return true
242247
}
243248
`
249+
250+
const trailer = `
251+
// growSlice is called for a slice that we only partially allocated,
252+
// to grow it up to length.
253+
func growSlice[E any](v reflect.Value, ps *[]E, length int) {
254+
var zero E
255+
s := *ps
256+
s = append(s, zero)
257+
cp := cap(s)
258+
if cp > length {
259+
cp = length
260+
}
261+
s = s[:cp]
262+
v.Set(reflect.ValueOf(s))
263+
*ps = s
264+
}
265+
`

0 commit comments

Comments
 (0)