github.com/gogf/gf/v2@v2.7.4/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/gogf/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"math"
    11  	"reflect"
    12  	"strconv"
    13  
    14  	"github.com/gogf/gf/v2/encoding/gbinary"
    15  	"github.com/gogf/gf/v2/util/gconv/internal/localinterface"
    16  )
    17  
    18  // Uint converts `any` to uint.
    19  func Uint(any interface{}) uint {
    20  	if any == nil {
    21  		return 0
    22  	}
    23  	if v, ok := any.(uint); ok {
    24  		return v
    25  	}
    26  	return uint(Uint64(any))
    27  }
    28  
    29  // Uint8 converts `any` to uint8.
    30  func Uint8(any interface{}) uint8 {
    31  	if any == nil {
    32  		return 0
    33  	}
    34  	if v, ok := any.(uint8); ok {
    35  		return v
    36  	}
    37  	return uint8(Uint64(any))
    38  }
    39  
    40  // Uint16 converts `any` to uint16.
    41  func Uint16(any interface{}) uint16 {
    42  	if any == nil {
    43  		return 0
    44  	}
    45  	if v, ok := any.(uint16); ok {
    46  		return v
    47  	}
    48  	return uint16(Uint64(any))
    49  }
    50  
    51  // Uint32 converts `any` to uint32.
    52  func Uint32(any interface{}) uint32 {
    53  	if any == nil {
    54  		return 0
    55  	}
    56  	if v, ok := any.(uint32); ok {
    57  		return v
    58  	}
    59  	return uint32(Uint64(any))
    60  }
    61  
    62  // Uint64 converts `any` to uint64.
    63  func Uint64(any interface{}) uint64 {
    64  	if any == nil {
    65  		return 0
    66  	}
    67  	rv := reflect.ValueOf(any)
    68  	switch rv.Kind() {
    69  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    70  		return uint64(rv.Int())
    71  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    72  		return uint64(rv.Uint())
    73  	case reflect.Uintptr:
    74  		return uint64(rv.Uint())
    75  	case reflect.Float32, reflect.Float64:
    76  		return uint64(rv.Float())
    77  	case reflect.Bool:
    78  		if rv.Bool() {
    79  			return 1
    80  		}
    81  		return 0
    82  	case reflect.Ptr:
    83  		if rv.IsNil() {
    84  			return 0
    85  		}
    86  		if f, ok := any.(localinterface.IUint64); ok {
    87  			return f.Uint64()
    88  		}
    89  		return Uint64(rv.Elem().Interface())
    90  	case reflect.Slice:
    91  		// TODOļ¼šThese types should be panic
    92  		if rv.Type().Elem().Kind() == reflect.Uint8 {
    93  			return gbinary.DecodeToUint64(rv.Bytes())
    94  		}
    95  	case reflect.String:
    96  		var (
    97  			s = rv.String()
    98  		)
    99  		// Hexadecimal
   100  		if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
   101  			if v, e := strconv.ParseUint(s[2:], 16, 64); e == nil {
   102  				return v
   103  			}
   104  		}
   105  		// Decimal
   106  		if v, e := strconv.ParseUint(s, 10, 64); e == nil {
   107  			return v
   108  		}
   109  		// Float64
   110  		if valueFloat64 := Float64(any); math.IsNaN(valueFloat64) {
   111  			return 0
   112  		} else {
   113  			return uint64(valueFloat64)
   114  		}
   115  	default:
   116  		if f, ok := any.(localinterface.IUint64); ok {
   117  			return f.Uint64()
   118  		}
   119  	}
   120  	return 0
   121  }