GoCollections/lang/Int64.go

105 lines
1.6 KiB
Go
Raw Normal View History

2022-11-25 18:19:33 +08:00
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
2022-03-23 10:15:18 +08:00
package lang
import "strconv"
type Int64 int64
func CastInt64(v any) (int64, bool) {
switch i := v.(type) {
2022-11-29 02:11:39 +08:00
case int:
return int64(i), true
case int8:
return int64(i), true
case int16:
return int64(i), true
case int32:
return int64(i), true
case int64:
return i, true
2022-11-29 02:11:39 +08:00
case uint:
return int64(i), true
case uint8:
return int64(i), true
case uint16:
return int64(i), true
case uint32:
return int64(i), true
case uint64:
return int64(i), true
case float32:
return int64(i), true
case float64:
return int64(i), true
case Number:
return i.ToInt64().V(), true
default:
return 0, false
}
}
2022-11-29 02:11:39 +08:00
func EqualsInt64(i1 Number, i2 any) bool {
i2, ok := CastInt64(i2)
2022-11-29 02:11:39 +08:00
return ok && i2 == i1.ToUInt64().V()
}
func (i Int64) V() int64 {
2022-03-23 10:15:18 +08:00
return int64(i)
}
func (i *Int64) P() *int64 {
return (*int64)(i)
}
2022-03-23 10:15:18 +08:00
func (i Int64) String() string {
return strconv.FormatInt(int64(i), 10)
}
func (i Int64) AsObject() Object {
return i
}
func (i Int64) Equals(e Object) bool {
return EqualsInt64(i, e)
2022-03-23 10:15:18 +08:00
}
func (i Int64) ToString() String {
return NewString(i.String())
}
func (i Int64) HashCode() int32 {
2022-11-29 02:11:39 +08:00
return HashInt64(int64(i))
2022-03-23 10:15:18 +08:00
}
func (i Int64) Compare(t Int64) int {
switch {
case i > t:
return 1
case i == t:
return 0
default:
return -1
}
}
2022-11-29 02:11:39 +08:00
func (i Int64) ToInt64() Int64 {
return Int64(i)
}
func (i Int64) ToUInt64() UInt64 {
return UInt64(i)
}
func (i Int64) ToFloat64() Float64 {
return Float64(i)
}
func HashInt64(i int64) int32 {
return int32(i) ^ int32(i>>32)
}