mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 04:07:47 +08:00
121 lines
2.2 KiB
Go
121 lines
2.2 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gconv
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gbinary"
|
|
)
|
|
|
|
// Uint converts `any` to uint.
|
|
func Uint(any interface{}) uint {
|
|
if any == nil {
|
|
return 0
|
|
}
|
|
if v, ok := any.(uint); ok {
|
|
return v
|
|
}
|
|
return uint(Uint64(any))
|
|
}
|
|
|
|
// Uint8 converts `any` to uint8.
|
|
func Uint8(any interface{}) uint8 {
|
|
if any == nil {
|
|
return 0
|
|
}
|
|
if v, ok := any.(uint8); ok {
|
|
return v
|
|
}
|
|
return uint8(Uint64(any))
|
|
}
|
|
|
|
// Uint16 converts `any` to uint16.
|
|
func Uint16(any interface{}) uint16 {
|
|
if any == nil {
|
|
return 0
|
|
}
|
|
if v, ok := any.(uint16); ok {
|
|
return v
|
|
}
|
|
return uint16(Uint64(any))
|
|
}
|
|
|
|
// Uint32 converts `any` to uint32.
|
|
func Uint32(any interface{}) uint32 {
|
|
if any == nil {
|
|
return 0
|
|
}
|
|
if v, ok := any.(uint32); ok {
|
|
return v
|
|
}
|
|
return uint32(Uint64(any))
|
|
}
|
|
|
|
// Uint64 converts `any` to uint64.
|
|
func Uint64(any interface{}) uint64 {
|
|
if any == nil {
|
|
return 0
|
|
}
|
|
switch value := any.(type) {
|
|
case int:
|
|
return uint64(value)
|
|
case int8:
|
|
return uint64(value)
|
|
case int16:
|
|
return uint64(value)
|
|
case int32:
|
|
return uint64(value)
|
|
case int64:
|
|
return uint64(value)
|
|
case uint:
|
|
return uint64(value)
|
|
case uint8:
|
|
return uint64(value)
|
|
case uint16:
|
|
return uint64(value)
|
|
case uint32:
|
|
return uint64(value)
|
|
case uint64:
|
|
return value
|
|
case float32:
|
|
return uint64(value)
|
|
case float64:
|
|
return uint64(value)
|
|
case bool:
|
|
if value {
|
|
return 1
|
|
}
|
|
return 0
|
|
case []byte:
|
|
return gbinary.DecodeToUint64(value)
|
|
default:
|
|
if f, ok := value.(iUint64); ok {
|
|
return f.Uint64()
|
|
}
|
|
s := String(value)
|
|
// Hexadecimal
|
|
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
|
|
if v, e := strconv.ParseUint(s[2:], 16, 64); e == nil {
|
|
return v
|
|
}
|
|
}
|
|
// Octal
|
|
if len(s) > 1 && s[0] == '0' {
|
|
if v, e := strconv.ParseUint(s[1:], 8, 64); e == nil {
|
|
return v
|
|
}
|
|
}
|
|
// Decimal
|
|
if v, e := strconv.ParseUint(s, 10, 64); e == nil {
|
|
return v
|
|
}
|
|
// Float64
|
|
return uint64(Float64(value))
|
|
}
|
|
}
|