-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstring_helper.go
executable file
·1199 lines (1077 loc) · 26.1 KB
/
string_helper.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Description : 字符串,数据类型转换,字符编码等等相关的操作方法
* Author : ManGe
* Mail : [email protected]
**/
package gathertool
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/base64"
"encoding/binary"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"math"
"net"
"reflect"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
"unsafe"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// Json2Map json -> map
func Json2Map(str string) (map[string]any, error) {
var tempMap map[string]any
err := json.Unmarshal([]byte(str), &tempMap)
return tempMap, err
}
// Any2Map any -> map[string]any
func Any2Map(data any) map[string]any {
if v, ok := data.(map[string]any); ok {
return v
}
if reflect.ValueOf(data).Kind() == reflect.String {
dataMap, err := Json2Map(data.(string))
if err == nil {
return dataMap
}
}
return nil
}
// Any2String any -> string
func Any2String(data any) string {
return StringValue(data)
}
// StringValue 任何类型返回值字符串形式
func StringValue(i any) string {
if i == nil {
return ""
}
if reflect.ValueOf(i).Kind() == reflect.String {
return i.(string)
}
var buf bytes.Buffer
stringValue(reflect.ValueOf(i), 0, &buf)
return buf.String()
}
func stringValue(v reflect.Value, indent int, buf *bytes.Buffer) {
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
buf.WriteString("{\n")
for i := 0; i < v.Type().NumField(); i++ {
ft := v.Type().Field(i)
fv := v.Field(i)
if ft.Name[0:1] == strings.ToLower(ft.Name[0:1]) {
continue
}
if (fv.Kind() == reflect.Ptr || fv.Kind() == reflect.Slice) && fv.IsNil() {
continue
}
buf.WriteString(strings.Repeat(" ", indent+2))
buf.WriteString(ft.Name + ": ")
if tag := ft.Tag.Get("sensitive"); tag == "true" {
buf.WriteString("<sensitive>")
} else {
stringValue(fv, indent+2, buf)
}
buf.WriteString(",\n")
}
buf.WriteString("\n" + strings.Repeat(" ", indent) + "}")
case reflect.Slice:
nl, id, id2 := "", "", ""
if v.Len() > 3 {
nl, id, id2 = "\n", strings.Repeat(" ", indent), strings.Repeat(" ", indent+2)
}
buf.WriteString("[" + nl)
for i := 0; i < v.Len(); i++ {
buf.WriteString(id2)
stringValue(v.Index(i), indent+2, buf)
if i < v.Len()-1 {
buf.WriteString("," + nl)
}
}
buf.WriteString(nl + id + "]")
case reflect.Map:
buf.WriteString("{\n")
for i, k := range v.MapKeys() {
buf.WriteString(strings.Repeat(" ", indent+2))
buf.WriteString(k.String() + ": ")
stringValue(v.MapIndex(k), indent+2, buf)
if i < v.Len()-1 {
buf.WriteString(",\n")
}
}
buf.WriteString("\n" + strings.Repeat(" ", indent) + "}")
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
buf.WriteString(strconv.FormatInt(v.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
buf.WriteString(strconv.FormatUint(v.Uint(), 10))
case reflect.Float32, reflect.Float64:
result := fmt.Sprintf("%f", v.Float())
// 去除result末尾的0
for strings.HasSuffix(result, "0") {
result = strings.TrimSuffix(result, "0")
}
if strings.HasSuffix(result, ".") {
result = strings.TrimSuffix(result, ".")
}
buf.WriteString(result)
default:
format := "%v"
switch v.Interface().(type) {
case string:
format = "%q"
}
_, _ = fmt.Fprintf(buf, format, v.Interface())
}
}
// Any2Int any -> int
func Any2Int(data any) int {
var t2 int
switch data.(type) {
case uint:
t2 = int(data.(uint))
break
case int8:
t2 = int(data.(int8))
break
case uint8:
t2 = int(data.(uint8))
break
case int16:
t2 = int(data.(int16))
break
case uint16:
t2 = int(data.(uint16))
break
case int32:
t2 = int(data.(int32))
break
case uint32:
t2 = int(data.(uint32))
break
case int64:
t2 = int(data.(int64))
break
case uint64:
t2 = int(data.(uint64))
break
case float32:
t2 = int(data.(float32))
break
case float64:
t2 = int(data.(float64))
break
case string:
t2, _ = strconv.Atoi(data.(string))
break
default:
t2 = data.(int)
break
}
return t2
}
// Any2Int64 any -> int64
func Any2Int64(data any) int64 {
return int64(Any2Int(data))
}
// Any2Arr any -> []any
func Any2Arr(data any) []any {
if v, ok := data.([]any); ok {
return v
}
return nil
}
// Any2Float64 any -> float64
func Any2Float64(data any) float64 {
if v, ok := data.(float64); ok {
return v
}
if v, ok := data.(float32); ok {
return float64(v)
}
return 0
}
// Any2Strings any -> []string
func Any2Strings(data any) []string {
listValue, ok := data.([]any)
if !ok {
return nil
}
keyStringValues := make([]string, len(listValue))
for i, arg := range listValue {
keyStringValues[i] = arg.(string)
}
return keyStringValues
}
// Any2Json any -> json string
func Any2Json(data any) (string, error) {
jsonStr, err := json.Marshal(data)
return string(jsonStr), err
}
// Int2Hex int -> hex
func Int2Hex(i int64) string {
return fmt.Sprintf("%x", i)
}
// Int642Hex int64 -> hex
func Int642Hex(i int64) string {
return fmt.Sprintf("%x", i)
}
// Hex2Int hex -> int
func Hex2Int(s string) int {
n, err := strconv.ParseUint(s, 16, 8)
if err != nil {
panic("Parse Error")
}
n2 := uint8(n)
return int(*(*int8)(unsafe.Pointer(&n2)))
}
// Hex2Int64 hex -> int
func Hex2Int64(s string) int64 {
n, err := strconv.ParseUint(s, 16, 8)
if err != nil {
panic("Parse Error")
}
n2 := uint8(n)
return int64(*(*int8)(unsafe.Pointer(&n2)))
}
// Str2Int64 string -> int64
func Str2Int64(str string) int64 {
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0
}
return i
}
// Str2Int string -> int
func Str2Int(str string) int {
i, err := strconv.Atoi(str)
if err != nil {
return 0
}
return i
}
// Str2Int32 string -> int32
func Str2Int32(str string) int32 {
i, err := strconv.Atoi(str)
if err != nil {
return 0
}
return int32(i)
}
// Str2Float64 string -> float64
func Str2Float64(str string) float64 {
i, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return i
}
// Str2Float32 string -> float32
func Str2Float32(str string) float32 {
i, err := strconv.ParseFloat(str, 32)
if err != nil {
return 0
}
return float32(i)
}
// Uint82Str []uint8 -> string
func Uint82Str(bs []uint8) string {
ba := make([]byte, 0)
for _, b := range bs {
ba = append(ba, b)
}
return string(ba)
}
// Str2Byte string -> []byte
func Str2Byte(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
// Byte2Str []byte -> string
func Byte2Str(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// Bool2Byte bool -> []byte
func Bool2Byte(b bool) []byte {
if b == true {
return []byte{1}
}
return []byte{0}
}
// Byte2Bool []byte -> bool
func Byte2Bool(b []byte) bool {
if len(b) == 0 || bytes.Compare(b, make([]byte, len(b))) == 0 {
return false
}
return true
}
// Int2Byte int -> []byte
func Int2Byte(i int) []byte {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(i))
return b
}
// Byte2Int []byte -> int
func Byte2Int(b []byte) int {
return int(binary.LittleEndian.Uint32(b))
}
// Int642Byte int64 -> []byte
func Int642Byte(i int64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(i))
return b
}
// Byte2Int64 []byte -> int64
func Byte2Int64(b []byte) int64 {
return int64(binary.LittleEndian.Uint64(b))
}
// Float322Byte float32 -> []byte
func Float322Byte(f float32) []byte {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, Float322Uint32(f))
return b
}
// Float322Uint32 float32 -> uint32
func Float322Uint32(f float32) uint32 {
return math.Float32bits(f)
}
// Byte2Float32 []byte -> float32
func Byte2Float32(b []byte) float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(b))
}
// Float642Byte float64 -> []byte
func Float642Byte(f float64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, Float642Uint64(f))
return b
}
// Float642Uint64 float64 -> uint64
func Float642Uint64(f float64) uint64 {
return math.Float64bits(f)
}
// Byte2Float64 []byte -> float64
func Byte2Float64(b []byte) float64 {
return math.Float64frombits(binary.LittleEndian.Uint64(b))
}
// Struct2Map Struct -> map
// hasValue=true表示字段值不管是否存在都转换成map
// hasValue=false表示字段为空或者不为0则转换成map
func Struct2Map(obj any, hasValue bool) (map[string]any, error) {
mp := make(map[string]any)
value := reflect.ValueOf(obj).Elem()
typeOf := reflect.TypeOf(obj).Elem()
for i := 0; i < value.NumField(); i++ {
switch value.Field(i).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if hasValue {
if value.Field(i).Int() != 0 {
mp[typeOf.Field(i).Name] = value.Field(i).Int()
}
} else {
mp[typeOf.Field(i).Name] = value.Field(i).Int()
}
case reflect.String:
if hasValue {
if len(value.Field(i).String()) != 0 {
mp[typeOf.Field(i).Name] = value.Field(i).String()
}
} else {
mp[typeOf.Field(i).Name] = value.Field(i).String()
}
case reflect.Float32, reflect.Float64:
if hasValue {
if len(value.Field(i).String()) != 0 {
mp[typeOf.Field(i).Name] = value.Field(i).Float()
}
} else {
mp[typeOf.Field(i).Name] = value.Field(i).Float()
}
case reflect.Bool:
if hasValue {
if len(value.Field(i).String()) != 0 {
mp[typeOf.Field(i).Name] = value.Field(i).Bool()
}
} else {
mp[typeOf.Field(i).Name] = value.Field(i).Bool()
}
default:
return mp, fmt.Errorf("数据类型不匹配")
}
}
return mp, nil
}
// Byte2Bit []byte -> []uint8 (bit)
func Byte2Bit(b []byte) []uint8 {
bits := make([]uint8, 0)
for _, v := range b {
bits = bits2Uint(bits, uint(v), 8)
}
return bits
}
// bits2Uint bits2Uint
func bits2Uint(bits []uint8, ui uint, l int) []uint8 {
a := make([]uint8, l)
for i := l - 1; i >= 0; i-- {
a[i] = uint8(ui & 1)
ui >>= 1
}
if bits != nil {
return append(bits, a...)
}
return a
}
// Bit2Byte []uint8 -> []byte
func Bit2Byte(b []uint8) []byte {
if len(b)%8 != 0 {
for i := 0; i < len(b)%8; i++ {
b = append(b, 0)
}
}
by := make([]byte, 0)
for i := 0; i < len(b); i += 8 {
by = append(b, byte(bitsToUint(b[i:i+8])))
}
return by
}
// bitsToUint bitsToUint
func bitsToUint(bits []uint8) uint {
v := uint(0)
for _, i := range bits {
v = v<<1 | uint(i)
}
return v
}
// CleaningStr 清理字符串前后空白 和回车 换行符号
func CleaningStr(str string) string {
str = strings.Replace(str, "\n", "", -1)
str = strings.Replace(str, "\r", "", -1)
str = strings.Replace(str, "\\n", "", -1)
str = strings.TrimSpace(str)
str = StrDeleteSpace(str)
return str
}
// StrDeleteSpace 删除字符串前后的空格
func StrDeleteSpace(str string) string {
strList := []byte(str)
spaceCount, count := 0, len(strList)
for i := 0; i <= len(strList)-1; i++ {
if strList[i] == 32 {
spaceCount++
} else {
break
}
}
strList = strList[spaceCount:]
spaceCount, count = 0, len(strList)
for i := count - 1; i >= 0; i-- {
if strList[i] == 32 {
spaceCount++
} else {
break
}
}
return string(strList[:count-spaceCount])
}
// EncodeByte encode byte
func EncodeByte(v any) []byte {
switch value := v.(type) {
case int, int8, int16, int32:
return Int2Byte(value.(int))
case int64:
return Int642Byte(value)
case string:
return Str2Byte(value)
case bool:
return Bool2Byte(value)
case float32:
return Float322Byte(value)
case float64:
return Float642Byte(value)
}
return []byte("")
}
// DecodeByte decode byte
func DecodeByte(b []byte) ([]byte, error) {
rse := make([]byte, 0)
buf := bytes.NewBuffer(b)
err := binary.Read(buf, binary.BigEndian, rse)
return rse, err
}
// deepCopy 深copy
func deepCopy[T any](dst, src T) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
return err
}
return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
}
func DeepCopy[T any](dst, src T) error {
return deepCopy(dst, src)
}
// PanicToError panic -> error
func PanicToError(fn func()) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic error: %v", r)
}
}()
fn()
return
}
// ByteToBinaryString 字节 -> 二进制字符串
func ByteToBinaryString(data byte) (str string) {
var a byte
for i := 0; i < 8; i++ {
a = data
data <<= 1
data >>= 1
switch a {
case data:
str += "0"
default:
str += "1"
}
data <<= 1
}
return str
}
// P2E panic -> error
func P2E() {
defer func() {
if r := recover(); r != nil {
Error("Panic error: %v", r)
}
}()
}
// IP2Binary IP str -> binary int64
func IP2Binary(ip string) string {
rse := IP2Int64(ip)
return strconv.FormatInt(rse, 2)
}
// UInt32ToIP uint32 -> net.IP
func UInt32ToIP(ip uint32) net.IP {
var b [4]byte
b[0] = byte(ip & 0xFF)
b[1] = byte((ip >> 8) & 0xFF)
b[2] = byte((ip >> 16) & 0xFF)
b[3] = byte((ip >> 24) & 0xFF)
return net.IPv4(b[3], b[2], b[1], b[0])
}
// IP2Int64 IP str -> int64
func IP2Int64(ip string) int64 {
address := net.ParseIP(ip)
if address == nil {
Error("ip地址不正确")
return 0
}
bits := strings.Split(ip, ".")
b0, b1, b2, b3 := 0, 0, 0, 0
if len(bits) >= 1 {
b0, _ = strconv.Atoi(bits[0])
}
if len(bits) >= 2 {
b1, _ = strconv.Atoi(bits[1])
}
if len(bits) >= 3 {
b2, _ = strconv.Atoi(bits[2])
}
if len(bits) >= 4 {
b3, _ = strconv.Atoi(bits[3])
}
var sum int64
sum += int64(b0) << 24
sum += int64(b1) << 16
sum += int64(b2) << 8
sum += int64(b3)
return sum
}
// Charset 字符集类型
type Charset string
const (
UTF8 = Charset("UTF-8")
GB18030 = Charset("GB18030")
GBK = Charset("GBK")
GB2312 = Charset("GB2312")
)
// ConvertByte2String 编码转换
func ConvertByte2String(byte []byte, charset Charset) string {
var str string
switch charset {
case GB18030:
var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
str = string(decodeBytes)
case GBK:
var decodeBytes, _ = simplifiedchinese.GBK.NewDecoder().Bytes(byte)
str = string(decodeBytes)
case GB2312:
var decodeBytes, _ = simplifiedchinese.HZGB2312.NewDecoder().Bytes(byte)
str = string(decodeBytes)
case UTF8:
fallthrough
default:
str = string(byte)
}
return str
}
func UnicodeDec(raw string) string {
str, err := strconv.Unquote(strings.Replace(strconv.Quote(raw), `\\u`, `\u`, -1))
if err != nil {
return ""
}
return str
}
func UnicodeDecByte(raw []byte) []byte {
rawStr := string(raw)
return []byte(UnicodeDec(rawStr))
}
// UnescapeUnicode Unicode 转码
func UnescapeUnicode(raw []byte) ([]byte, error) {
str, err := strconv.Unquote(strings.Replace(strconv.Quote(string(raw)), `\\u`, `\u`, -1))
if err != nil {
return nil, err
}
return []byte(str), nil
}
// Base64Encode base64 编码
func Base64Encode(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}
// Base64Decode base64 解码
func Base64Decode(str string) (string, error) {
b, err := base64.StdEncoding.DecodeString(str)
return string(b), err
}
// Base64UrlEncode base64 url 编码
func Base64UrlEncode(str string) string {
return base64.URLEncoding.EncodeToString([]byte(str))
}
// Base64UrlDecode base64 url 解码
func Base64UrlDecode(str string) (string, error) {
b, err := base64.URLEncoding.DecodeString(str)
return string(b), err
}
// convert
func convert(dstCharset string, srcCharset string, src string) (dst string, err error) {
if dstCharset == srcCharset {
return src, nil
}
dst = src
// Converting `src` to UTF-8.
if srcCharset != "UTF-8" {
if e := getEncoding(srcCharset); e != nil {
tmp, err := io.ReadAll(
transform.NewReader(bytes.NewReader([]byte(src)), e.NewDecoder()),
)
if err != nil {
return "", err
}
src = string(tmp)
} else {
return dst, err
}
}
// Do the converting from UTF-8 to `dstCharset`.
if dstCharset != "UTF-8" {
if e := getEncoding(dstCharset); e != nil {
tmp, err := io.ReadAll(
transform.NewReader(bytes.NewReader([]byte(src)), e.NewEncoder()),
)
if err != nil {
return "", err
}
dst = string(tmp)
} else {
return dst, err
}
} else {
dst = src
}
return dst, nil
}
var (
// Alias for charsets.
charsetAlias = map[string]string{
"HZGB2312": "HZ-GB-2312",
"hzgb2312": "HZ-GB-2312",
"GB2312": "HZ-GB-2312",
"gb2312": "HZ-GB-2312",
}
)
func getEncoding(charset string) encoding.Encoding {
if c, ok := charsetAlias[charset]; ok {
charset = c
}
enc, err := ianaindex.MIB.Encoding(charset)
if err != nil {
log.Println(err)
}
return enc
}
// ToUTF8 to utf8
func ToUTF8(srcCharset string, src string) (dst string, err error) {
return convert("UTF-8", srcCharset, src)
}
// UTF8To utf8 to
func UTF8To(dstCharset string, src string) (dst string, err error) {
return convert(dstCharset, "UTF-8", src)
}
// ToUTF16 to utf16
func ToUTF16(srcCharset string, src string) (dst string, err error) {
return convert("UTF-16", srcCharset, src)
}
// UTF16To utf16 to
func UTF16To(dstCharset string, src string) (dst string, err error) {
return convert(dstCharset, "UTF-16", src)
}
// ToBIG5 to big5
func ToBIG5(srcCharset string, src string) (dst string, err error) {
return convert("big5", srcCharset, src)
}
// BIG5To big to
func BIG5To(dstCharset string, src string) (dst string, err error) {
return convert(dstCharset, "big5", src)
}
// ToGDK to gdk
func ToGDK(srcCharset string, src string) (dst string, err error) {
return convert("gbk", srcCharset, src)
}
// GDKTo gdk to
func GDKTo(dstCharset string, src string) (dst string, err error) {
return convert(dstCharset, "gbk", src)
}
// ToGB18030 to gb18030
func ToGB18030(srcCharset string, src string) (dst string, err error) {
return convert("gb18030", srcCharset, src)
}
// GB18030To gb18030 to
func GB18030To(dstCharset string, src string) (dst string, err error) {
return convert(dstCharset, "gb18030", src)
}
// ToGB2312 to gb2312
func ToGB2312(srcCharset string, src string) (dst string, err error) {
return convert("GB2312", srcCharset, src)
}
// GB2312To gb2312 to
func GB2312To(dstCharset string, src string) (dst string, err error) {
return convert(dstCharset, "GB2312", src)
}
// ToHZGB2312 to hzgb2312
func ToHZGB2312(srcCharset string, src string) (dst string, err error) {
return convert("HZGB2312", srcCharset, src)
}
// HZGB2312To hzgb2312 to
func HZGB2312To(dstCharset string, src string) (dst string, err error) {
return convert(dstCharset, "HZGB2312", src)
}
// IF 三元表达式
func IF[T any](condition bool, a, b T) T {
if condition {
return a
}
return b
}
// ReplaceAllToOne 批量统一替换字符串
func ReplaceAllToOne(str string, from []string, to string) string {
arr := make([]string, len(from)*2)
for i, s := range from {
arr[i*2] = s
arr[i*2+1] = to
}
r := strings.NewReplacer(arr...)
return r.Replace(str)
}
// 字节换算
const (
KiB = 1024
MiB = KiB * 1024
GiB = MiB * 1024
TiB = GiB * 1024
)
func HumanFriendlyTraffic(bytes uint64) string {
if bytes <= KiB {
return fmt.Sprintf("%d B", bytes)
}
if bytes <= MiB {
return fmt.Sprintf("%.2f KiB", float32(bytes)/KiB)
}
if bytes <= GiB {
return fmt.Sprintf("%.2f MiB", float32(bytes)/MiB)
}
if bytes <= TiB {
return fmt.Sprintf("%.2f GiB", float32(bytes)/GiB)
}
return fmt.Sprintf("%.2f TiB", float32(bytes)/TiB)
}
func StrToSize(sizeStr string) int64 {
i := 0
for ; i < len(sizeStr); i++ {
if sizeStr[i] == '.' || (sizeStr[i] >= '0' && sizeStr[i] <= '9') {
continue
} else {
break
}
}
var (
unit = sizeStr[i:]
number, _ = strconv.ParseFloat(sizeStr[:i], 64)
)
if unit == "" {
return int64(number)
}
switch strings.ToLower(unit) {
case "b", "bytes":
return int64(number)
case "k", "kb", "ki", "kib", "kilobyte":
return int64(number * 1024)
case "m", "mb", "mi", "mib", "mebibyte":
return int64(number * 1024 * 1024)
case "g", "gb", "gi", "gib", "gigabyte":
return int64(number * 1024 * 1024 * 1024)
case "t", "tb", "ti", "tib", "terabyte":
return int64(number * 1024 * 1024 * 1024 * 1024)
case "p", "pb", "pi", "pib", "petabyte":
return int64(number * 1024 * 1024 * 1024 * 1024 * 1024)
case "e", "eb", "ei", "eib", "exabyte":
return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)
case "z", "zb", "zi", "zib", "zettabyte":
return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)
case "y", "yb", "yi", "yib", "yottabyte":
return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)
case "bb", "brontobyte":
return int64(number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)
}
return -1
}
func GzipCompress(src []byte) []byte {
var in bytes.Buffer
w := gzip.NewWriter(&in)
_, _ = w.Write(src)
_ = w.Close()
return in.Bytes()
}
func GzipDecompress(src []byte) []byte {
dst := make([]byte, 0)
br := bytes.NewReader(src)
gr, err := gzip.NewReader(br)
if err != nil {
return dst
}
defer func() {
_ = gr.Close()
}()
tmp, err := io.ReadAll(gr)
if err != nil {
return dst
}
dst = tmp
return dst
}
// ConvertStr2GBK 将utf-8编码的字符串转换为GBK编码
func ConvertStr2GBK(str string) string {
ret, err := simplifiedchinese.GBK.NewEncoder().String(str)
if err != nil {
ret = str
}
return ret
}