github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_uint.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"math"
    11  	"strconv"
    12  
    13  	"github.com/wangyougui/gf/v2/encoding/gbinary"
    14  )
    15  
    16  // Uint converts `any` to uint.
    17  func Uint(any interface{}) uint {
    18  	if any == nil {
    19  		return 0
    20  	}
    21  	if v, ok := any.(uint); ok {
    22  		return v
    23  	}
    24  	return uint(Uint64(any))
    25  }
    26  
    27  // Uint8 converts `any` to uint8.
    28  func Uint8(any interface{}) uint8 {
    29  	if any == nil {
    30  		return 0
    31  	}
    32  	if v, ok := any.(uint8); ok {
    33  		return v
    34  	}
    35  	return uint8(Uint64(any))
    36  }
    37  
    38  // Uint16 converts `any` to uint16.
    39  func Uint16(any interface{}) uint16 {
    40  	if any == nil {
    41  		return 0
    42  	}
    43  	if v, ok := any.(uint16); ok {
    44  		return v
    45  	}
    46  	return uint16(Uint64(any))
    47  }
    48  
    49  // Uint32 converts `any` to uint32.
    50  func Uint32(any interface{}) uint32 {
    51  	if any == nil {
    52  		return 0
    53  	}
    54  	if v, ok := any.(uint32); ok {
    55  		return v
    56  	}
    57  	return uint32(Uint64(any))
    58  }
    59  
    60  // Uint64 converts `any` to uint64.
    61  func Uint64(any interface{}) uint64 {
    62  	if any == nil {
    63  		return 0
    64  	}
    65  	switch value := any.(type) {
    66  	case int:
    67  		return uint64(value)
    68  	case int8:
    69  		return uint64(value)
    70  	case int16:
    71  		return uint64(value)
    72  	case int32:
    73  		return uint64(value)
    74  	case int64:
    75  		return uint64(value)
    76  	case uint:
    77  		return uint64(value)
    78  	case uint8:
    79  		return uint64(value)
    80  	case uint16:
    81  		return uint64(value)
    82  	case uint32:
    83  		return uint64(value)
    84  	case uint64:
    85  		return value
    86  	case float32:
    87  		return uint64(value)
    88  	case float64:
    89  		return uint64(value)
    90  	case bool:
    91  		if value {
    92  			return 1
    93  		}
    94  		return 0
    95  	case []byte:
    96  		return gbinary.DecodeToUint64(value)
    97  	default:
    98  		if f, ok := value.(iUint64); ok {
    99  			return f.Uint64()
   100  		}
   101  		s := String(value)
   102  		// Hexadecimal
   103  		if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
   104  			if v, e := strconv.ParseUint(s[2:], 16, 64); e == nil {
   105  				return v
   106  			}
   107  		}
   108  		// Decimal
   109  		if v, e := strconv.ParseUint(s, 10, 64); e == nil {
   110  			return v
   111  		}
   112  		// Float64
   113  		if valueFloat64 := Float64(value); math.IsNaN(valueFloat64) {
   114  			return 0
   115  		} else {
   116  			return uint64(valueFloat64)
   117  		}
   118  	}
   119  }