/
gconv_uint.go
119 lines (111 loc) · 2.15 KB
/
gconv_uint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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 (
"math"
"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
}
}
// Decimal
if v, e := strconv.ParseUint(s, 10, 64); e == nil {
return v
}
// Float64
if valueFloat64 := Float64(value); math.IsNaN(valueFloat64) {
return 0
} else {
return uint64(valueFloat64)
}
}
}