| 127 | } |
| 128 | |
| 129 | func 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. |