-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcvts_tool.go
810 lines (713 loc) · 21.2 KB
/
cvts_tool.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
package evendeep
import (
"fmt"
"log"
"math"
"reflect"
"strconv"
"strings"
"time"
"gopkg.in/hedzr/errors.v3"
"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/internal/cl"
"github.com/hedzr/evendeep/internal/syscalls"
"github.com/hedzr/evendeep/ref"
)
// rForBool transform bool -> string.
func rForBool(v reflect.Value) (ret reflect.Value) {
vs := strconv.FormatBool(v.Bool())
ret = reflect.ValueOf(vs)
return
}
// rToBool transform string (or anything) -> bool.
func rToBool(v reflect.Value) (ret reflect.Value) { //nolint:revive
var b bool
if !v.IsValid() || ref.IsNil(v) || ref.IsZero(v) {
return reflect.ValueOf(b)
}
k := v.Kind()
switch k { //nolint:exhaustive //no need
case reflect.Bool:
b = v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
b = v.Int() != 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
b = v.Uint() != 0
case reflect.Float32, reflect.Float64:
b = math.Float64bits(v.Float()) != 0
case reflect.Complex64, reflect.Complex128:
c := v.Complex()
b = math.Float64bits(real(c)) != 0 || math.Float64bits(imag(c)) != 0
case reflect.Array:
b = !ref.ArrayIsZero(v)
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
b = !ref.IsNil(v)
case reflect.Struct:
b = !ref.StructIsZero(v)
case reflect.String:
b = internalToBool(v.String())
}
ret = reflect.ValueOf(b)
return
}
func internalToBool(s string) (b bool) {
switch val := strings.ToLower(s); val {
case "y", "t", "1", "yes", "true", "on", "ok", "m", "male":
b = true
}
return
}
// rForInteger transform integer -> string.
func rForInteger(v reflect.Value) (ret reflect.Value) {
int64typ := reflect.TypeOf((*int64)(nil)).Elem()
if !v.IsValid() {
v = reflect.Zero(int64typ) //nolint:revive
} else if k := v.Kind(); k < reflect.Int || k > reflect.Int64 {
if ref.CanConvert(&v, int64typ) {
v = v.Convert(int64typ) //nolint:revive
} else {
v = reflect.Zero(int64typ) //nolint:revive
}
}
vs := strconv.FormatInt(v.Int(), 10)
ret = reflect.ValueOf(vs)
return
}
const uintSize = 32 << (^uint(0) >> 32 & 1)
//nolint:dupl //don't
func rToInteger(v reflect.Value, desiredType reflect.Type) (ret reflect.Value, err error) {
genret := func(ival int64, desiredTypeKind reflect.Kind) (ret reflect.Value) {
switch desiredTypeKind { //nolint:exhaustive //no need
case reflect.Int:
ret = reflect.ValueOf(int(ival))
case reflect.Int32:
ret = reflect.ValueOf(int32(ival))
case reflect.Int16:
ret = reflect.ValueOf(int16(ival))
case reflect.Int8:
ret = reflect.ValueOf(int8(ival))
default:
ret = reflect.ValueOf(ival)
}
return
}
ret, err = toTypeConverter(v, desiredType, 10, //nolint:gomnd //no need
func(str string, base, bitSize int) (ret reflect.Value, err error) {
var ival int64
ival, err = strconv.ParseInt(str, base, bitSize)
if err == nil {
k := desiredType.Kind()
ret = genret(ival, k)
} else {
var fval float64
fval, err = strconv.ParseFloat(str, bitSize)
if err == nil {
ival = int64(math.Floor(fval + 0.5)) //nolint:gomnd //no need
k := desiredType.Kind()
ret = genret(ival, k)
}
}
return
})
return
}
// rForUInteger transform uint64 -> string.
func rForUInteger(v reflect.Value) (ret reflect.Value) {
uint64typ := reflect.TypeOf((*uint64)(nil)).Elem()
if !v.IsValid() {
v = reflect.Zero(uint64typ) //nolint:revive
} else if k := v.Kind(); k < reflect.Uint || k > reflect.Uintptr {
if ref.CanConvert(&v, uint64typ) {
v = v.Convert(uint64typ) //nolint:revive
} else {
v = reflect.Zero(uint64typ) //nolint:revive
}
}
vs := strconv.FormatUint(v.Uint(), 10)
ret = reflect.ValueOf(vs)
return
}
//nolint:dupl //don't
func rToUInteger(v reflect.Value, desiredType reflect.Type) (ret reflect.Value, err error) {
genret := func(ival uint64, desiredTypeKind reflect.Kind) (ret reflect.Value) {
switch desiredTypeKind { //nolint:exhaustive //no need
case reflect.Uint:
ret = reflect.ValueOf(uint(ival))
case reflect.Uint32:
ret = reflect.ValueOf(uint32(ival))
case reflect.Uint16:
ret = reflect.ValueOf(uint16(ival))
case reflect.Uint8:
ret = reflect.ValueOf(uint8(ival))
default:
ret = reflect.ValueOf(ival)
}
return
}
ret, err = toTypeConverter(v, desiredType, 10, //nolint:gomnd //no need
func(str string, base, bitSize int) (ret reflect.Value, err error) {
var ival uint64
ival, err = strconv.ParseUint(str, base, bitSize)
if err == nil {
k := desiredType.Kind()
ret = genret(ival, k)
} else {
var fval float64
fval, err = strconv.ParseFloat(str, bitSize)
if err == nil {
ival = uint64(math.Floor(fval + 0.5)) //nolint:gomnd //no need
k := desiredType.Kind()
ret = genret(ival, k)
}
}
return
})
return
}
// rForUIntegerHex transform uintptr/... -> string.
func rForUIntegerHex(u uintptr) (ret reflect.Value) {
vs := syscalls.UintptrToString(u)
ret = reflect.ValueOf(vs)
return
}
func rToUIntegerHex(s reflect.Value, desiredType reflect.Type) (ret reflect.Value) {
vs := syscalls.UintptrFromString(s.String())
log.Printf("vs : %v, k: %v\n", vs, desiredType.Kind())
switch k := desiredType.Kind(); k { //nolint:exhaustive //no need
case reflect.Uintptr:
ret = reflect.ValueOf(vs)
case reflect.UnsafePointer, reflect.Ptr:
ret = reflect.ValueOf(vs)
// u := getPointerAsUintptr(ret)
}
return
}
func getPointerAsUintptr(v reflect.Value) uintptr { //nolint:unused // intergrality
var p uintptr
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
p = v.Pointer()
}
return p
}
// rForFloat transform float -> string.
func rForFloat(v reflect.Value) (ret reflect.Value) {
float64typ := reflect.TypeOf((*float64)(nil)).Elem()
if !v.IsValid() {
v = reflect.Zero(float64typ) //nolint:revive
} else if k := v.Kind(); k < reflect.Float32 || k > reflect.Float64 {
if ref.CanConvert(&v, float64typ) {
v = v.Convert(float64typ) //nolint:revive
} else {
v = reflect.Zero(float64typ) //nolint:revive
}
}
vs := strconv.FormatFloat(v.Float(), 'g', -1, 64)
ret = reflect.ValueOf(vs)
return
}
func rToFloat(v reflect.Value, desiredType reflect.Type) (ret reflect.Value, err error) {
toFloat := func(fval float64, desiredType reflect.Type) (ret reflect.Value) {
if desiredType.Kind() == reflect.Float64 {
ret = reflect.ValueOf(fval)
} else {
ret = reflect.ValueOf(float32(fval))
}
return
}
ret, err = toTypeConverter(v, desiredType, 10, //nolint:gomnd //no need
func(str string, base, bitSize int) (ret reflect.Value, err error) { //nolint:revive
var fval float64
fval, err = strconv.ParseFloat(str, bitSize)
if err == nil {
return toFloat(fval, desiredType), nil
}
var ival int64
ival, err = strconv.ParseInt(str, 10, bitSize)
if err == nil {
return toFloat(float64(ival), desiredType), nil
}
var uval uint64
uval, err = strconv.ParseUint(str, 10, bitSize)
if err == nil {
return toFloat(float64(uval), desiredType), nil
}
var cval complex128
cval, err = cl.ParseComplex(str)
if err == nil {
fval = real(cval)
ret = toFloat(fval, desiredType)
}
return
})
return
}
// rForComplex transform complex -> string.
func rForComplex(v reflect.Value) (ret reflect.Value) {
complex128typ := reflect.TypeOf((*complex128)(nil)).Elem()
if !v.IsValid() {
v = reflect.Zero(complex128typ) //nolint:revive
} else if k := v.Kind(); k < reflect.Complex64 || k > reflect.Complex128 {
// if canConvert(&v, complex128typ) {
// v = v.Convert(complex128typ)
// } else {
switch k { //nolint:exhaustive //no need
case reflect.Float64, reflect.Float32:
v = reflect.ValueOf(complex(v.Float(), 0.0)) //nolint:revive
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v = reflect.ValueOf(complex(float64(v.Int()), 0.0)) //nolint:revive
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
v = reflect.ValueOf(complex(float64(v.Uint()), 0.0)) //nolint:revive
default:
v = reflect.Zero(complex128typ) //nolint:revive
}
// }
}
vs := cl.FormatComplex(v.Complex(), 'g', -1, 128) //nolint:gomnd //no need
ret = reflect.ValueOf(vs)
return
}
func rToComplex(v reflect.Value, desiredType reflect.Type) (ret reflect.Value, err error) {
toComplex := func(cval complex128, desiredType reflect.Type) (ret reflect.Value) {
if desiredType.Kind() == reflect.Complex128 {
ret = reflect.ValueOf(cval)
} else {
ret = reflect.ValueOf(complex64(cval))
}
return
}
ret, err = toTypeConverter(v, desiredType, 10, //nolint:gomnd //no need
func(str string, base, bitSize int) (ret reflect.Value, err error) { //nolint:revive
var cval complex128
if str[0] != '(' {
str = "(" + str
}
if lastch := str[len(str)-1]; lastch != ')' {
if lastch != 'i' {
str += "+0i"
}
str += ")"
}
cval, err = cl.ParseComplex(str)
if err == nil {
ret = toComplex(cval, desiredType)
}
return
})
return
}
func toTypeConverter(v reflect.Value, desiredType reflect.Type, base int, //nolint:unparam
converter func(str string, int, bitSize int) (ret reflect.Value, err error),
) (ret reflect.Value, err error) {
if !v.IsValid() || ref.IsNil(v) || ref.IsZero(v) {
ret = reflect.Zero(desiredType)
return
}
if ref.CanConvert(&v, desiredType) {
ret = v.Convert(desiredType)
return
}
val := v.String()
bitSize := 64
switch k := desiredType.Kind(); k { //nolint:exhaustive //no need
case reflect.Int, reflect.Uint:
bitSize = uintSize
case reflect.Int32, reflect.Uint32, reflect.Float32:
bitSize = 32
case reflect.Int16, reflect.Uint16:
bitSize = 16
case reflect.Int8, reflect.Uint8:
bitSize = 8
case reflect.Complex128:
bitSize = 128
// case reflect.Float64, reflect.Complex64:
// bitSize = 64
}
ret, err = converter(val, base, bitSize)
return
}
func tryStringerIt(source reflect.Value, desiredType reflect.Type) (target reflect.Value, processed bool, err error) { //nolint:unparam
val := source.Interface()
if ss, ok := val.(interface{ String() string }); ok {
nv := ss.String()
target = reflect.ValueOf(nv)
processed = true
return
}
if ref.CanConvert(&source, desiredType) {
nv := source.Convert(desiredType)
// target.Set(nv)
target = nv
processed = true
} else {
nv := fmt.Sprintf("%v", val)
// target.Set(reflect.ValueOf(nv))
target = reflect.ValueOf(nv)
processed = true
}
return
}
func rToString(source reflect.Value, desiredType reflect.Type) (target reflect.Value, err error) { //nolint:revive
if source.IsValid() {
switch k := source.Kind(); k { //nolint:exhaustive //no need
case reflect.Bool:
target = rForBool(source)
case reflect.Int64:
var processed bool
if target, processed, err = tryStringerIt(source, desiredType); processed {
return
}
fallthrough
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
target = rForInteger(source)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
target = rForUInteger(source)
case reflect.Uintptr:
target = rForUIntegerHex(uintptr(source.Uint()))
// case reflect.UnsafePointer:
// target = rForUIntegerHex(uintptr(source.Uint()))
// case reflect.Ptr:
// target = rForUIntegerHex(source.Pointer())
case reflect.Float32, reflect.Float64:
target = rForFloat(source)
case reflect.Complex64, reflect.Complex128:
target = rForComplex(source)
case reflect.String:
target = reflect.ValueOf(source.String())
// reflect.Array
// reflect.Chan
// reflect.Func
// reflect.Interface
// reflect.Map
// reflect.Slice
// reflect.Struct
default:
target, _, err = tryStringerIt(source, desiredType)
}
} else {
target = reflect.Zero(ref.StringType)
}
return
}
//nolint:unparam,unused,deadcode,lll //reserved
func rToArray(ctx *ValueConverterContext, sources reflect.Value, desiredType reflect.Type, targetLength int) (target reflect.Value, err error) { //nolint:revive,lll
eltyp := desiredType.Elem() // length := desiredType.Len()
dbglog.Log(" desiredType: %v, el.type: %v", ref.Typfmt(desiredType), ref.Typfmt(eltyp))
count, length := sources.Len(), targetLength
if length <= 0 {
length = count
}
if count > length {
count = length
}
target = reflect.New(reflect.ArrayOf(length, eltyp)).Elem()
for ix := 0; ix < count; ix++ {
src := sources.Index(ix)
if ix < length && src.IsValid() {
if src.Type().AssignableTo(eltyp) {
target.Index(ix).Set(src)
} else if src.Type().ConvertibleTo(eltyp) {
target.Index(ix).Set(src.Convert(eltyp))
}
}
}
_ = ctx
return
}
//nolint:unparam,unused,deadcode,lll //reserved
func rToSlice(ctx *ValueConverterContext, sources reflect.Value, desiredType reflect.Type, targetLength int) (target reflect.Value, err error) { //nolint:revive,lll
eltyp := desiredType.Elem() // length := desiredType.Len()
dbglog.Log(" desiredType: %v, el.type: %v", ref.Typfmt(desiredType), ref.Typfmt(eltyp))
count, length := sources.Len(), targetLength
if length <= 0 {
length = count
}
if count > length {
count = length
}
target = reflect.MakeSlice(desiredType, length, length)
for ix := 0; ix < count; ix++ {
src := sources.Index(ix)
if ix < length && src.IsValid() {
if src.Type().AssignableTo(eltyp) {
target.Index(ix).Set(src)
} else if src.Type().ConvertibleTo(eltyp) {
target.Index(ix).Set(src.Convert(eltyp))
}
}
}
_ = ctx
return
}
//nolint:unparam,unused,deadcode,lll //reserved
func rToMap(ctx *ValueConverterContext, source reflect.Value, fromFuncType, desiredType reflect.Type) (target reflect.Value, err error) {
ec := errors.New("cannot transform item into map")
defer ec.Defer(&err)
dtyp := desiredType.Elem()
styp := source.Type()
// srceltyp := styp.Elem()
target = reflect.MakeMap(desiredType)
if source.IsValid() {
for ix, key := range source.MapKeys() {
val := source.MapIndex(key)
if err = rSetMapValue(ix, target, key, val, styp, dtyp); err != nil {
ec.Attach(err)
continue
}
}
}
_, _ = ctx, fromFuncType
// for i := 0; i < fromFuncType.NumOut(); i++ {
// if i >= len(sources) {
// continue
// }
//
// styp := fromFuncType.Out(i)
// sname := styp.Name()
// var key reflect.Value
// if key, err = nameToMapKey(sname, desiredType); err != nil {
// ec.Attach(err)
// continue
// }
//
// if err = rSetMapValue(target, key, sources[i], styp, dtyp); err != nil {
// ec.Attach(err)
// continue
// }
// }
return
}
//nolint:unused //future
func rSetMapValue(ix int, target, key, srcVal reflect.Value, sTyp, dTyp reflect.Type) (err error) {
if sTyp.AssignableTo(dTyp) { //nolint:gocritic // no need to switch to 'switch' clause
target.SetMapIndex(key, srcVal)
} else if sTyp.ConvertibleTo(dTyp) {
target.SetMapIndex(key, srcVal.Convert(dTyp))
} else {
dstval := target.MapIndex(key)
err = errors.New("cannot set map[%v] since transforming/converting failed: %v -> %v",
ref.Valfmt(&key), ref.Valfmt(&srcVal), ref.Valfmt(&dstval))
}
_ = ix
return
}
//nolint:unused //future
func nameToMapKey(name string, mapType reflect.Type) (key reflect.Value, err error) {
nameval := reflect.ValueOf(name)
nametyp := nameval.Type()
if keytyp := mapType.Key(); nametyp.AssignableTo(keytyp) { //nolint:gocritic // no need to switch to 'switch' clause
key = nameval
} else if nametyp.ConvertibleTo(keytyp) {
key = nameval.Convert(keytyp)
} else {
cvt := fromStringConverter{}
key, err = cvt.Transform(nil, nameval, keytyp)
}
return
}
//nolint:unused,deadcode,lll //reserved
func rToStruct(ctx *ValueConverterContext, source reflect.Value, fromFuncType, desiredType reflect.Type) (target reflect.Value, err error) {
// result (source) -> struct (target)
_, _, _, _ = ctx, source, fromFuncType, desiredType
return
}
//nolint:unused,deadcode,lll //reserved
func rToFunc(ctx *ValueConverterContext, source reflect.Value, fromFuncType, desiredType reflect.Type) (target reflect.Value, err error) {
_, _, _, _ = ctx, source, fromFuncType, desiredType
return
}
//
//
//
func intToString[T Integers](val T) string {
return intToStringEx(val, 10)
}
func intToStringEx[T Integers](val T, base int) string {
return strconv.FormatInt(int64(val), base)
}
func uintToString[T Uintegers](val T) string {
return uintToStringEx(val, 10)
}
func uintToStringEx[T Uintegers](val T, base int) string {
return strconv.FormatUint(uint64(val), base)
}
func floatToString[T Floats](val T) string {
return floatToStringEx(float64(val), 'f', -1, 64)
}
func floatToStringEx[T Floats](val T, format byte, prec, bitSize int) string {
return strconv.FormatFloat(float64(val), format, prec, bitSize)
}
func complexToString[T Complexes](val T) string {
return complexToStringEx(val, 'f', -1, 128)
}
func complexToStringEx[T Complexes](val T, format byte, prec, bitSize int) string {
return strconv.FormatComplex(complex128(val), format, prec, bitSize)
}
func boolToString(b bool) string {
return strconv.FormatBool(b)
}
func timeToString(tm time.Time) string {
const layout = time.RFC3339Nano
return tm.Format(layout)
}
func durationToString(dur time.Duration) string {
return dur.String()
}
//
func bytesToString(data []byte) string {
b := make([]byte, 0, 3+len(data)*4)
b = append(b, data...)
b = append(b, []byte(" [")...)
for i := 0; i < len(data); {
if i > 0 {
b = append(b, ' ')
}
strconv.AppendInt(b, int64(data[i]), 16)
}
b = append(b, []byte("]")...)
return string(b)
}
func intSliceToString[T Integers](val IntSlice[T]) string {
b := make([]byte, 0, len(val)*8) // 8: assume integer need 8 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
b = strconv.AppendInt(b, int64(val[i]), 10)
}
b = append(b, []byte("]")...)
return string(b)
}
func uintSliceToString[T Uintegers](val UintSlice[T]) string {
b := make([]byte, 0, len(val)*8) // 8: assume unsigned integer need 8 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
b = strconv.AppendUint(b, uint64(val[i]), 10)
}
b = append(b, []byte("]")...)
return string(b)
}
func floatSliceToString[T Floats](val FloatSlice[T]) string {
b := make([]byte, 0, len(val)*16+2) // 8: assume floats need 16 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
b = strconv.AppendFloat(b, float64(val[i]), 'f', -1, 64)
}
b = append(b, []byte("]")...)
return string(b)
}
func complexSliceToString[T Complexes](val ComplexSlice[T]) string {
b := make([]byte, 0, len(val)*32+2) // 8: assume complex need 32 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
num := strconv.FormatComplex(complex128(val[i]), 'f', -1, 128)
b = append(b, []byte(num)...)
}
b = append(b, []byte("]")...)
return string(b)
}
// func complexSliceToString[T Complexes](val ComplexSlice[T]) string {
// var b = make([]byte, 0, len(val)*32+2) // 8: assume complex need 32 runes
// b = append(b, []byte("[")...)
// for i := range val {
// if i > 0 {
// b = append(b, []byte(",")...)
// }
// b=strconv.AppendComplex(b, complex128(val[i]), 'f', -1, 128)
// }
// b = append(b, []byte("]")...)
// return string(b)
// }
func stringSliceToString(val []string) string {
b := make([]byte, 0, len(val)*32+2) // 8: assume integer need 32 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
b = strconv.AppendQuote(b, val[i])
}
b = append(b, []byte("]")...)
return string(b)
}
func boolSliceToString(val []bool) string {
b := make([]byte, 0, len(val)*8) // 8: assume bool need 5 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
b = strconv.AppendBool(b, val[i])
}
b = append(b, []byte("]")...)
return string(b)
}
func timeSliceToString(val []time.Time) string {
b := make([]byte, 0, len(val)*32+2) // 8: assume time need 24 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
b = strconv.AppendQuote(b, val[i].Format(time.RFC3339Nano))
}
b = append(b, []byte("]")...)
return string(b)
}
func durationSliceToString(val []time.Duration) string {
b := make([]byte, 0, len(val)*16+2) // 8: assume duration need 16 runes
b = append(b, []byte("[")...)
for i := range val {
if i > 0 {
b = append(b, []byte(",")...)
}
b = strconv.AppendQuote(b, val[i].String())
}
b = append(b, []byte("]")...)
return string(b)
}
//
type Stringer interface {
String() string
}
type ToString interface {
ToString(args ...any) string
}
type Integers interface {
int | int8 | int16 | int32 | int64
}
type Uintegers interface {
uint | uint8 | uint16 | uint32 | uint64
}
type Floats interface {
float32 | float64
}
type Complexes interface {
complex64 | complex128
}
type Numerics interface {
Integers | Uintegers | Floats | Complexes
}
type IntSlice[T Integers] []T
type UintSlice[T Uintegers] []T
type FloatSlice[T Floats] []T
type ComplexSlice[T Complexes] []T
type StringSlice[T string] []T
type BoolSlice[T bool] []T
type Slice[T Integers | Uintegers | Floats] []T
// const (
// TimeNoNano = "15:04:05Z07:00"
// TimeNano = "15:04:05.000000Z07:00"
// DateTime = "2006-01-0215:04:05Z07:00"
// RFC3339Nano = "2006-01-02T15:04:05.000000Z07:00"
// RFC3339NanoOrig = "2006-01-02T15:04:05.999999999Z07:00"
// )