github.com/tursom/GoCollections@v0.3.10/lang/UInt.go (about) 1 /* 2 * Copyright (c) 2022 tursom. All rights reserved. 3 * Use of this source code is governed by a GPL-3 4 * license that can be found in the LICENSE file. 5 */ 6 7 package lang 8 9 import "strconv" 10 11 type UInt uint 12 13 func CastUInt(v any) (uint, bool) { 14 switch i := v.(type) { 15 case int: 16 return uint(i), true 17 case int8: 18 return uint(i), true 19 case int16: 20 return uint(i), true 21 case int32: 22 return uint(i), true 23 case int64: 24 return uint(i), true 25 case uint: 26 return i, true 27 case uint8: 28 return uint(i), true 29 case uint16: 30 return uint(i), true 31 case uint32: 32 return uint(i), true 33 case uint64: 34 return uint(i), true 35 case float32: 36 return uint(i), true 37 case float64: 38 return uint(i), true 39 case Number: 40 return uint(i.ToUInt64().V()), true 41 default: 42 return 0, false 43 } 44 } 45 46 func EqualsUInt(i1 Number, i2 any) bool { 47 i2, ok := CastUInt(i2) 48 return ok && i2 == i1.ToUInt64().V() 49 } 50 51 func (i UInt) V() uint { 52 return uint(i) 53 } 54 55 func (i *UInt) P() *uint { 56 return (*uint)(i) 57 } 58 59 func (i UInt) String() string { 60 return strconv.FormatUint(uint64(i), 10) 61 } 62 63 func (i UInt) AsObject() Object { 64 return i 65 } 66 67 func (i UInt) Equals(e Object) bool { 68 return EqualsUInt(i, e) 69 } 70 71 func (i UInt) ToString() String { 72 return NewString(i.String()) 73 } 74 75 func (i UInt) HashCode() int32 { 76 return HashUInt(uint(i)) 77 } 78 79 func (i UInt) Compare(t UInt) int { 80 switch { 81 case i > t: 82 return 1 83 case i == t: 84 return 0 85 default: 86 return -1 87 } 88 } 89 90 func (i UInt) ToInt64() Int64 { 91 return Int64(i) 92 } 93 94 func (i UInt) ToUInt64() UInt64 { 95 return UInt64(i) 96 } 97 98 func (i UInt) ToFloat64() Float64 { 99 return Float64(i) 100 } 101 102 func HashUInt(i uint) int32 { 103 return HashInt(int(i)) 104 }