reflect: add OverflowFloat(), OverflowInt(), OverflowUint()
Этот коммит содержится в:
родитель
79930a209c
коммит
0ff243e5e2
1 изменённых файлов: 36 добавлений и 3 удалений
|
@ -735,8 +735,24 @@ func (v Value) NumMethod() int {
|
||||||
return v.typecode.NumMethod()
|
return v.typecode.NumMethod()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OverflowFloat reports whether the float64 x cannot be represented by v's type.
|
||||||
|
// It panics if v's Kind is not Float32 or Float64.
|
||||||
func (v Value) OverflowFloat(x float64) bool {
|
func (v Value) OverflowFloat(x float64) bool {
|
||||||
panic("unimplemented: (reflect.Value).OverflowFloat()")
|
k := v.Kind()
|
||||||
|
switch k {
|
||||||
|
case Float32:
|
||||||
|
return overflowFloat32(x)
|
||||||
|
case Float64:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
panic(&ValueError{"reflect.Value.OverflowFloat", v.Kind()})
|
||||||
|
}
|
||||||
|
|
||||||
|
func overflowFloat32(x float64) bool {
|
||||||
|
if x < 0 {
|
||||||
|
x = -x
|
||||||
|
}
|
||||||
|
return math.MaxFloat32 < x && x <= math.MaxFloat64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Value) MapKeys() []Value {
|
func (v Value) MapKeys() []Value {
|
||||||
|
@ -977,12 +993,29 @@ func (v Value) checkAddressable() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OverflowInt reports whether the int64 x cannot be represented by v's type.
|
||||||
|
// It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
|
||||||
func (v Value) OverflowInt(x int64) bool {
|
func (v Value) OverflowInt(x int64) bool {
|
||||||
panic("unimplemented: reflect.OverflowInt()")
|
switch v.Kind() {
|
||||||
|
case Int, Int8, Int16, Int32, Int64:
|
||||||
|
bitSize := v.typecode.Size() * 8
|
||||||
|
trunc := (x << (64 - bitSize)) >> (64 - bitSize)
|
||||||
|
return x != trunc
|
||||||
|
}
|
||||||
|
panic(&ValueError{"reflect.Value.OverflowInt", v.Kind()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OverflowUint reports whether the uint64 x cannot be represented by v's type.
|
||||||
|
// It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
|
||||||
func (v Value) OverflowUint(x uint64) bool {
|
func (v Value) OverflowUint(x uint64) bool {
|
||||||
panic("unimplemented: reflect.OverflowUint()")
|
k := v.Kind()
|
||||||
|
switch k {
|
||||||
|
case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64:
|
||||||
|
bitSize := v.typecode.Size() * 8
|
||||||
|
trunc := (x << (64 - bitSize)) >> (64 - bitSize)
|
||||||
|
return x != trunc
|
||||||
|
}
|
||||||
|
panic(&ValueError{"reflect.Value.OverflowUint", v.Kind()})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Value) Convert(t Type) Value {
|
func (v Value) Convert(t Type) Value {
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче