Этот коммит содержится в:
Damian Gryski 2023-02-25 17:45:29 -08:00 коммит произвёл Ayke
родитель 43a4b256bd
коммит a7e3cf0826
2 изменённых файлов: 41 добавлений и 0 удалений

Просмотреть файл

@ -431,6 +431,33 @@ func (v Value) Slice(i, j int) Value {
}
func (v Value) Slice3(i, j, k int) Value {
switch v.Kind() {
case Slice:
hdr := *(*sliceHeader)(v.value)
i, j, k := uintptr(i), uintptr(j), uintptr(k)
if j < i || k < j || hdr.len < k {
slicePanic()
}
elemSize := v.typecode.underlying().elem().Size()
hdr.len = j - i
hdr.cap = k - i
hdr.data = unsafe.Add(hdr.data, i*elemSize)
return Value{
typecode: v.typecode,
value: unsafe.Pointer(&hdr),
flags: v.flags,
}
case Array:
// TODO(dgryski): can't do this yet because the resulting value needs type v.elem(), not array of v.elem().
// need to be able to look up this "new" type so pointer equality of types still works
}
panic("unimplemented: (reflect.Value).Slice3()")
}

Просмотреть файл

@ -147,6 +147,20 @@ func TestSlice(t *testing.T) {
}
}
s268 := s[2:6:8]
s268ref := refs.Slice3(2, 6, 8)
if len(s268) != s268ref.Len() || cap(s268) != s268ref.Cap() {
t.Errorf("len(s268)=%d s268ref.Len()=%d cap(s268)=%d s268ref.Cap()=%d\n", len(s268), s268ref.Len(), cap(s268), s268ref.Cap())
}
for i, got := range s268 {
want := int(s268ref.Index(i).Int())
if got != want {
t.Errorf("s268[%d]=%d, want %d", i, got, want)
}
}
refs = MakeSlice(TypeOf(s), 5, 10)
s = refs.Interface().([]int)