-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
293 lines (249 loc) · 6.13 KB
/
converter.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package fcnv
import (
"encoding/hex"
"encoding/json"
"errors"
"reflect"
"strconv"
"strings"
"time"
"unsafe"
"golang.org/x/exp/constraints"
"golang.org/x/text/width"
)
func Atoi(s string) (ret int, err error) {
return strconv.Atoi(s)
}
func Atoi8(s string) (ret int8, err error) {
return atoi8(s)
}
func Atoi16(s string) (ret int16, err error) {
return atoi16(s)
}
func Atoi32(s string) (ret int32, err error) {
return atoi32(s)
}
func Atoi64(s string) (ret int64, err error) {
return atoi64(s)
}
func Atoui(s string) (ret uint, err error) {
return atoui(s)
}
func Atoui8(s string) (ret uint8, err error) {
return atoui8(s)
}
func Atoui16(s string) (ret uint16, err error) {
return atoui16(s)
}
func Atoui32(s string) (ret uint32, err error) {
return atoui32(s)
}
func Atoui64(s string) (ret uint64, err error) {
return atoui64(s)
}
func Atof32(s string) (ret float32, err error) {
f, e := strconv.ParseFloat(s, 32)
return float32(f), e
}
func Atof64(s string) (ret float64, err error) {
return strconv.ParseFloat(s, 64)
}
func Atoc64(s string) (ret complex64, err error) {
cm, e := strconv.ParseComplex(s, 64)
return complex64(cm), e
}
func Atoc128(s string) (ret complex128, err error) {
return strconv.ParseComplex(s, 128)
}
func Atob(s string) (b []byte) {
h := *(*reflect.SliceHeader)(unsafe.Pointer(&s))
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: h.Data,
Len: h.Len,
Cap: h.Len,
}))
}
// Itoa converts an int* to a string.
func Itoa[intx constraints.Signed](i intx) (ret string) {
return strconv.FormatInt(int64(i), 10)
}
// Deprecated: I8toa is discontinued in the future.
// Itoa should be used.
func I8toa(i int8) (ret string) { return Itoa(i) }
// Deprecated: I16toa is discontinued in the future.
// Itoa should be used.
func I16toa(i int16) (ret string) { return Itoa(i) }
// Deprecated: I32toa is discontinued in the future.
// Itoa should be used.
func I32toa(i int32) (ret string) { return Itoa(i) }
// Deprecated: I64toa is discontinued in the future.
// Itoa should be used.
func I64toa(i int64) (ret string) { return Itoa(i) }
// Uitoa converts an uint* to a string.
func Uitoa[uintx constraints.Unsigned](ui uintx) (ret string) {
return strconv.FormatUint(uint64(ui), 10)
}
// Deprecated: Ui8toa is discontinued in the future.
// Uitoa should be used.
func Ui8toa(ui uint8) (ret string) { return Uitoa(ui) }
// Deprecated: Ui16toa is discontinued in the future.
// Uitoa should be used.
func Ui16toa(ui uint16) (ret string) { return Uitoa(ui) }
// Deprecated: Ui32toa is discontinued in the future.
// Uitoa should be used.
func Ui32toa(ui uint32) (ret string) { return Uitoa(ui) }
// Deprecated: Ui64toa is discontinued in the future.
// Uitoa should be used.
func Ui64toa(ui uint64) (ret string) { return Uitoa(ui) }
// Ctoa converts a Complex (complex64, complex128) to a string.
// The representation precision is complex64 compliant.
func Ctoa[cmx constraints.Complex](f cmx) (ret string) {
return strconv.FormatComplex(complex128(f), 'f', -1, 128)
}
// Ftoa converts a Float (float32, float64) to a string.
// The representation precision is float64 compliant.
func Ftoa[floatx constraints.Float](f floatx) (ret string) {
return strconv.FormatFloat(float64(f), 'f', -1, 64)
}
func Btoa(b []byte) (ret string) {
h := *(*reflect.StringHeader)(unsafe.Pointer(&b))
return *(*string)(unsafe.Pointer(&reflect.StringHeader{
Data: h.Data,
Len: h.Len,
}))
}
func Byte2Int(b []byte) (ret int, err error) {
return strconv.Atoi(hex.EncodeToString(b))
}
func Int2Byte[intx constraints.Signed](i intx) (ret []byte) {
return []byte(string(rune(i)))
}
func Byte2Bool(by []byte) (bo bool) {
i, _ := Byte2Int(by)
if i == 0 {
bo = false
} else {
bo = true
}
return
}
func Bool2Byte(bo bool) (by []byte) {
if bo {
by = []byte{0x01}
} else {
by = []byte{0x00}
}
return
}
func Bool2Int(b bool) (i int) {
if b {
i = 1
} else {
i = 0
}
return
}
func Int2Bool[INT constraints.Integer](i INT) (b bool) {
if i == 0 {
b = false
} else {
b = true
}
return
}
// Deprecated: Bool2Uint is discontinued in the future.
// Bool2Int should be used.
func Bool2Uint(b bool) (i uint) { return uint(Bool2Int(b)) }
// Deprecated: Uint2Bool is discontinued in the future.
// Int2Bool should be used.
func Uint2Bool(i uint) (b bool) { return Int2Bool(i) }
func Bool2Str(b bool) (s string) {
if b {
s = "true"
} else {
s = "false"
}
return
}
func Str2Bool(s string) (ret bool, err error) {
switch s {
case "1", "t", "T", "true", "TRUE", "True":
return true, nil
case "0", "f", "F", "false", "FALSE", "False":
return false, nil
}
return false, errors.New("invalid syntax")
}
func Struct2Json(structure any) (jsonStr string, err error) {
b, err := json.Marshal(structure)
jsonStr = Btoa(b)
return
}
func Datetime2Date(t time.Time) (ret time.Time) {
return t.Truncate(time.Hour).Add(-time.Duration(t.Hour()) * time.Hour)
}
func Hankaku2Zenkaku(str string) (ret string) {
harf2full := strings.NewReplacer(
"ヴ", "ヴ",
"ガ", "ガ",
"ギ", "ギ",
"グ", "グ",
"ゲ", "ゲ",
"ゴ", "ゴ",
"ザ", "ザ",
"ジ", "ジ",
"ズ", "ズ",
"ゼ", "ゼ",
"ゾ", "ゾ",
"ダ", "ダ",
"ヂ", "ヂ",
"ヅ", "ヅ",
"デ", "デ",
"ド", "ド",
"バ", "バ",
"ビ", "ビ",
"ブ", "ブ",
"ベ", "ベ",
"ボ", "ボ",
"パ", "パ",
"ピ", "ピ",
"プ", "プ",
"ペ", "ペ",
"ポ", "ポ",
)
return width.Widen.String(harf2full.Replace(str))
}
func Zenkaku2Hankaku(str string) (ret string) {
full2harf := strings.NewReplacer(
"ヴ", "ヴ",
"ガ", "ガ",
"ギ", "ギ",
"グ", "グ",
"ゲ", "ゲ",
"ゴ", "ゴ",
"ザ", "ザ",
"ジ", "ジ",
"ズ", "ズ",
"ゼ", "ゼ",
"ゾ", "ゾ",
"ダ", "ダ",
"ヂ", "ヂ",
"ヅ", "ヅ",
"デ", "デ",
"ド", "ド",
"バ", "バ",
"ビ", "ビ",
"ブ", "ブ",
"ベ", "ベ",
"ボ", "ボ",
"パ", "パ",
"ピ", "ピ",
"プ", "プ",
"ペ", "ペ",
"ポ", "ポ",
"ヰ", "イ",
"ヱ", "エ",
"ヮ", "ワ",
)
return width.Narrow.String(full2harf.Replace(str))
}