-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
166 lines (157 loc) · 3.63 KB
/
util.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2021 BlueStorm
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package datatable
import (
"reflect"
"strconv"
"time"
"unsafe"
)
// BytToStr nocopy 转换string
func BytToStr(src []byte) (dst string) {
s := (*reflect.SliceHeader)(unsafe.Pointer(&src))
d := (*reflect.StringHeader)(unsafe.Pointer(&dst))
d.Len = s.Len
d.Data = s.Data
return
}
func ToString(value interface{}) string {
switch v := value.(type) {
case float32, float64:
return strconv.FormatFloat(ToFloat64(v), 'f', -1, 64)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return strconv.FormatInt(ToInt64(v), 10)
case []byte:
return BytToStr(v)
case string:
return v
case bool:
if v {
return "true"
}
return "false"
case nil:
return ""
case time.Time:
return v.Format("2006-01-02 15:04:05")
default:
return ""
}
}
func ToFloat32(value interface{}) float32 {
return float32(ToFloat64(value))
}
func ToFloat64(value interface{}) float64 {
switch v := value.(type) {
case int, int8, int16, int32, uint, uint8, uint16, uint32:
return float64(ToInt(v))
case int64, uint64:
return float64(ToInt64(v))
case float64:
return v
case float32:
return float64(v)
case bool:
if v {
return 1
}
return 0
case []byte:
x, _ := strconv.ParseFloat(BytToStr(v), 64)
return x
case string:
x, _ := strconv.ParseFloat(v, 64)
return x
default:
return 0
}
}
func ToInt(value interface{}) int {
switch v := value.(type) {
case float64:
return int(v)
case float32:
return int(v)
case string:
f, _ := strconv.Atoi(v)
return f
case int:
return v
case uint:
return int(v)
case uint8:
return int(v)
case uint16:
return int(v)
case uint32:
return int(v)
case uint64:
return int(v)
case int8:
return int(v)
case int16:
return int(v)
case int32:
return int(v)
case int64:
return int(v)
case bool:
if v {
return 1
}
return 0
case []byte:
x, _ := strconv.Atoi(BytToStr(v))
return x
default:
return 0
}
}
func ToInt64(value interface{}) int64 {
switch v := value.(type) {
case float64:
return int64(v)
case float32:
return int64(v)
case string, int, uint, uint8, uint16, uint32, int8, int16, int32, bool:
return int64(ToInt(v))
case uint64:
return int64(v)
case int64:
return v
case []byte:
x, _ := strconv.ParseInt(BytToStr(v), 10, 64)
return x
default:
return 0
}
}
func FormatFloat(value interface{}) (float64, bool) {
switch v := value.(type) {
case float32, float64:
return ToFloat64(v), true
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return float64(ToInt(v)), true
default:
return 0, false
}
}