internal/lib/reflect: Value.bytesSlow

This commit is contained in:
visualfc
2024-11-13 21:29:01 +08:00
parent 38f1585ac6
commit f403916ef8

View File

@@ -340,28 +340,25 @@ func (v Value) Bytes() []byte {
} }
func (v Value) bytesSlow() []byte { func (v Value) bytesSlow() []byte {
/* switch v.kind() {
switch v.kind() { case Slice:
case Slice: if v.typ().Elem().Kind() != abi.Uint8 {
if v.typ().Elem().Kind() != abi.Uint8 { panic("reflect.Value.Bytes of non-byte slice")
panic("reflect.Value.Bytes of non-byte slice")
}
// Slice is always bigger than a word; assume flagIndir.
return *(*[]byte)(v.ptr)
case Array:
if v.typ().Elem().Kind() != abi.Uint8 {
panic("reflect.Value.Bytes of non-byte array")
}
if !v.CanAddr() {
panic("reflect.Value.Bytes of unaddressable byte array")
}
p := (*byte)(v.ptr)
n := int((*arrayType)(unsafe.Pointer(v.typ())).Len)
return unsafe.Slice(p, n)
} }
panic(&ValueError{"reflect.Value.Bytes", v.kind()}) // Slice is always bigger than a word; assume flagIndir.
*/ return *(*[]byte)(v.ptr)
panic("todo: reflect.Value.byteSlow") case Array:
if v.typ().Elem().Kind() != abi.Uint8 {
panic("reflect.Value.Bytes of non-byte array")
}
if !v.CanAddr() {
panic("reflect.Value.Bytes of unaddressable byte array")
}
p := (*byte)(v.ptr)
n := int((*arrayType)(unsafe.Pointer(v.typ())).Len)
return unsafe.Slice(p, n)
}
panic(&ValueError{"reflect.Value.Bytes", v.kind()})
} }
// runes returns v's underlying value. // runes returns v's underlying value.