github.com/tursom/GoCollections@v0.3.10/lang/UInt8.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 UInt8 uint8
    12  
    13  func CastUInt8(v any) (uint8, bool) {
    14  	switch i := v.(type) {
    15  	case int:
    16  		return uint8(i), true
    17  	case int8:
    18  		return uint8(i), true
    19  	case int16:
    20  		return uint8(i), true
    21  	case int32:
    22  		return uint8(i), true
    23  	case int64:
    24  		return uint8(i), true
    25  	case uint:
    26  		return uint8(i), true
    27  	case uint8:
    28  		return i, true
    29  	case uint16:
    30  		return uint8(i), true
    31  	case uint32:
    32  		return uint8(i), true
    33  	case uint64:
    34  		return uint8(i), true
    35  	case float32:
    36  		return uint8(i), true
    37  	case float64:
    38  		return uint8(i), true
    39  	case Number:
    40  		return uint8(i.ToUInt64().V()), true
    41  	default:
    42  		return 0, false
    43  	}
    44  }
    45  
    46  func EqualsUInt8(i1 Number, i2 any) bool {
    47  	i2, ok := CastUInt8(i2)
    48  	return ok && i2 == i1.ToUInt64().V()
    49  }
    50  
    51  func (i UInt8) V() uint8 {
    52  	return uint8(i)
    53  }
    54  
    55  func (i *UInt8) P() *uint8 {
    56  	return (*uint8)(i)
    57  }
    58  
    59  func (i UInt8) String() string {
    60  	return strconv.FormatUint(uint64(i), 10)
    61  }
    62  
    63  func (i UInt8) AsObject() Object {
    64  	return i
    65  }
    66  
    67  func (i UInt8) Equals(e Object) bool {
    68  	return EqualsUInt8(i, e)
    69  }
    70  
    71  func (i UInt8) ToString() String {
    72  	return NewString(i.String())
    73  }
    74  
    75  func (i UInt8) HashCode() int32 {
    76  	return HashUInt8(uint8(i))
    77  }
    78  
    79  func (i UInt8) Compare(t UInt8) 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 UInt8) ToInt64() Int64 {
    91  	return Int64(i)
    92  }
    93  
    94  func (i UInt8) ToUInt64() UInt64 {
    95  	return UInt64(i)
    96  }
    97  
    98  func (i UInt8) ToFloat64() Float64 {
    99  	return Float64(i)
   100  }
   101  
   102  func HashUInt8(i uint8) int32 {
   103  	return HashUInt32(uint32(i))
   104  }