MCPcopy Create free account
hub / github.com/google/gapid / replace

Function replace

core/data/slice/slice.go:129–150  ·  view source on GitHub ↗
(ptr, old reflect.Value, first, count int, with interface{})

Source from the content-addressed store, hash-verified

127}
128
129func replace(ptr, old reflect.Value, first, count int, with interface{}) {
130 d := toSlice(with, old.Type())
131 newLen, oldLen := old.Len()-count+d.Len(), old.Len()
132 new := old
133 // ensure the slice is big enough to hold all the new data.
134 if old.Cap() < newLen {
135 // l isn't big enough to hold the new elements.
136 // Create a new buffer, with room to grow.
137 new = reflect.MakeSlice(old.Type(), newLen, newLen*2)
138 reflect.Copy(new, old.Slice(0, first))
139 } else {
140 new.SetLen(newLen) // Extend the slice length.
141 }
142 // move the part after the insertion to the right by len(with).
143 reflect.Copy(
144 new.Slice(first+d.Len(), new.Len()),
145 old.Slice(first+count, oldLen),
146 )
147 // copy in with.
148 reflect.Copy(new.Slice(first, new.Len()), d)
149 ptr.Elem().Set(new)
150}
151
152// getSlicePtr checks s is a pointer to a slice, returning both the pointer
153// and slice as a reflect.Value.

Callers 2

ReplaceFunction · 0.70
AppendFunction · 0.70

Calls 7

toSliceFunction · 0.85
MakeSliceMethod · 0.80
TypeMethod · 0.65
LenMethod · 0.65
CopyMethod · 0.65
SliceMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected