-
Notifications
You must be signed in to change notification settings - Fork 11
/
tplfunc.go
872 lines (797 loc) · 18.5 KB
/
tplfunc.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
/*
Copyright 2016 Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tplfunc
import (
"bytes"
"fmt"
"html/template"
"log"
"math"
"net/url"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/webx-top/captcha"
"github.com/webx-top/com"
)
func New() (r template.FuncMap) {
r = template.FuncMap{}
for name, function := range TplFuncMap {
r[name] = function
}
return
}
var TplFuncMap template.FuncMap = template.FuncMap{
// ======================
// time
// ======================
"Now": Now,
"ElapsedMemory": com.ElapsedMemory, //内存消耗
"TotalRunTime": com.TotalRunTime, //运行时长(从启动服务时算起)
"CaptchaForm": CaptchaForm, //验证码图片
"FormatByte": com.FormatByte, //字节转为适合理解的格式
"FriendlyTime": FriendlyTime,
"FormatPastTime": com.FormatPastTime, //以前距离现在多长时间
"DateFormat": com.DateFormat,
"DateFormatShort": com.DateFormatShort,
"Ts2time": TsToTime, // 时间戳数字转time.Time
"Ts2date": TsToDate, // 时间戳数字转日期字符串
// ======================
// compare
// ======================
"Eq": Eq,
"Add": Add,
"Sub": Sub,
"Div": Div,
"Mul": Mul,
"IsNil": IsNil,
"IsEmpty": IsEmpty,
"NotEmpty": NotEmpty,
"IsNaN": IsNaN,
"IsInf": IsInf,
// ======================
// conversion type
// ======================
"Html": ToHTML,
"Js": ToJS,
"Css": ToCSS,
"ToJS": ToJS,
"ToCSS": ToCSS,
"ToURL": ToURL,
"ToHTML": ToHTML,
"ToHTMLAttr": ToHTMLAttr,
"ToHTMLAttrs": ToHTMLAttrs,
"ToStrSlice": ToStrSlice,
"ToDuration": ToDuration,
"Str": com.Str,
"Int": com.Int,
"Int32": com.Int32,
"Int64": com.Int64,
"Uint": com.Uint,
"Uint32": com.Uint32,
"Uint64": com.Uint64,
"Float32": com.Float32,
"Float64": com.Float64,
"ToFloat64": ToFloat64,
"ToFixed": ToFixed,
"Math": Math,
"NumberFormat": NumberFormat,
"DurationFormat": DurationFormat,
// ======================
// string
// ======================
"Contains": strings.Contains,
"HasPrefix": strings.HasPrefix,
"HasSuffix": strings.HasSuffix,
"Trim": strings.TrimSpace,
"TrimLeft": strings.TrimLeft,
"TrimRight": strings.TrimRight,
"TrimPrefix": strings.TrimPrefix,
"TrimSuffix": strings.TrimSuffix,
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
"Title": strings.Title,
"LowerCaseFirst": com.LowerCaseFirst,
"CamelCase": com.CamelCase,
"PascalCase": com.PascalCase,
"SnakeCase": com.SnakeCase,
"Reverse": com.Reverse,
"Dir": filepath.Dir,
"Base": filepath.Base,
"Ext": filepath.Ext,
"Dirname": path.Dir,
"Basename": path.Base,
"Extension": path.Ext,
"InExt": InExt,
"Concat": Concat,
"Replace": strings.Replace, //strings.Replace(s, old, new, n)
"Split": strings.Split,
"Join": strings.Join,
"Substr": com.Substr,
"StripTags": com.StripTags,
"Nl2br": NlToBr, // \n替换为<br>
"AddSuffix": AddSuffix,
// ======================
// encode & decode
// ======================
"JSONEncode": JSONEncode,
"JSONDecode": JSONDecode,
"URLEncode": com.URLEncode,
"URLDecode": URLDecode,
"Base64Encode": com.Base64Encode,
"Base64Decode": Base64Decode,
"UnicodeDecode": UnicodeDecode,
"SafeBase64Encode": com.SafeBase64Encode,
"SafeBase64Decode": SafeBase64Decode,
"Hash": Hash,
"Unquote": Unquote,
"Quote": strconv.Quote,
// ======================
// map & slice
// ======================
"InSlice": com.InSlice,
"InSlicex": com.InSliceIface,
"Set": Set,
"Append": Append,
"InStrSlice": InStrSlice,
"SearchStrSlice": SearchStrSlice,
"URLValues": URLValues,
"ToSlice": ToSlice,
"StrToSlice": StrToSlice,
// ======================
// regexp
// ======================
"Regexp": regexp.MustCompile,
"RegexpPOSIX": regexp.MustCompilePOSIX,
// ======================
// other
// ======================
"Ignore": Ignore,
"Default": Default,
}
var (
HashSalt = time.Now().Format(time.RFC3339)
HashClipPositions = []uint{1, 3, 8, 9}
)
func Hash(text, salt string, positions ...uint) string {
if len(salt) < 1 {
salt = HashSalt
}
if len(positions) < 1 {
positions = HashClipPositions
}
return com.MakePassword(text, salt, positions...)
}
func Unquote(s string) string {
r, _ := strconv.Unquote(`"` + s + `"`)
return r
}
func UnicodeDecode(str string) string {
buf := bytes.NewBuffer(nil)
i, j := 0, len(str)
for i < j {
x := i + 6
if x > j {
buf.WriteString(str[i:])
break
}
if str[i] == '\\' && str[i+1] == 'u' {
hex := str[i+2 : x]
r, err := strconv.ParseUint(hex, 16, 64)
if err == nil {
buf.WriteRune(rune(r))
} else {
buf.WriteString(str[i:x])
}
i = x
} else {
buf.WriteByte(str[i])
i++
}
}
return buf.String()
}
func JSONEncode(s interface{}, indents ...string) string {
r, _ := com.JSONEncode(s, indents...)
return string(r)
}
func JSONDecode(s string) map[string]interface{} {
r := map[string]interface{}{}
e := com.JSONDecode([]byte(s), &r)
if e != nil {
log.Println(e)
}
return r
}
func URLDecode(s string) string {
r, e := com.URLDecode(s)
if e != nil {
log.Println(e)
}
return r
}
func Base64Decode(s string) string {
r, e := com.Base64Decode(s)
if e != nil {
log.Println(e)
}
return r
}
func SafeBase64Decode(s string) string {
r, e := com.SafeBase64Decode(s)
if e != nil {
log.Println(e)
}
return r
}
func Ignore(_ interface{}) interface{} {
return nil
}
func URLValues(values ...interface{}) url.Values {
v := url.Values{}
var k string
for i, j := 0, len(values); i < j; i++ {
if i%2 == 0 {
k = fmt.Sprint(values[i])
continue
}
v.Add(k, fmt.Sprint(values[i]))
k = ``
}
if len(k) > 0 {
v.Add(k, ``)
k = ``
}
return v
}
func ToStrSlice(s ...string) []string {
return s
}
func ToSlice(s ...interface{}) []interface{} {
return s
}
func StrToSlice(s string, sep string) []interface{} {
ss := strings.Split(s, sep)
r := make([]interface{}, len(ss))
for i, s := range ss {
r[i] = s
}
return r
}
func Concat(s ...string) string {
return strings.Join(s, ``)
}
func InExt(fileName string, exts ...string) bool {
ext := filepath.Ext(fileName)
ext = strings.ToLower(ext)
for _, _ext := range exts {
if ext == strings.ToLower(_ext) {
return true
}
}
return false
}
func Default(defaultV interface{}, v interface{}) interface{} {
switch val := v.(type) {
case nil:
return defaultV
case string:
if len(val) == 0 {
return defaultV
}
case uint8, int8, uint, int, uint32, int32, int64, uint64:
if val == 0 {
return defaultV
}
case float32, float64:
if val == 0.0 {
return defaultV
}
default:
if len(com.Str(v)) == 0 {
return defaultV
}
}
return v
}
func Set(renderArgs map[string]interface{}, key string, value interface{}) string {
renderArgs[key] = value
return ``
}
func Append(renderArgs map[string]interface{}, key string, value interface{}) string {
if renderArgs[key] == nil {
renderArgs[key] = []interface{}{value}
} else {
renderArgs[key] = append(renderArgs[key].([]interface{}), value)
}
return ``
}
//NlToBr Replaces newlines with <br />
func NlToBr(text string) template.HTML {
return template.HTML(Nl2br(text))
}
//CaptchaForm 验证码表单域
func CaptchaForm(args ...interface{}) template.HTML {
id := "captcha"
msg := "页面验证码已经失效,必须重新请求当前页面。确定要刷新本页面吗?"
onErr := "if(this.src.indexOf('?reload=')!=-1 && confirm('%s')) window.location.reload();"
format := `<img id="%[2]sImage" src="/captcha/%[1]s.png" alt="Captcha image" onclick="this.src=this.src.split('?')[0]+'?reload='+Math.random();" onerror="%[3]s" style="cursor:pointer" /><input type="hidden" name="captchaId" id="%[2]sId" value="%[1]s" />`
var customOnErr bool
switch len(args) {
case 3:
switch v := args[2].(type) {
case template.JS:
onErr = string(v)
customOnErr = true
case string:
msg = v
}
fallthrough
case 2:
if args[1] != nil {
v := fmt.Sprint(args[1])
format = v
}
fallthrough
case 1:
switch v := args[0].(type) {
case template.JS:
onErr = string(v)
customOnErr = true
case template.HTML:
format = string(v)
case string:
id = v
}
}
cid := captcha.New()
if !customOnErr {
onErr = fmt.Sprintf(onErr, msg)
}
return template.HTML(fmt.Sprintf(format, cid, id, onErr))
}
//CaptchaVerify 验证码验证
func CaptchaVerify(captchaSolution string, idGet func(string, ...string) string) bool {
//id := r.FormValue("captchaId")
id := idGet("captchaId")
if !captcha.VerifyString(id, captchaSolution) {
return false
}
return true
}
//Nl2br 将换行符替换为<br />
func Nl2br(text string) string {
return com.Nl2br(template.HTMLEscapeString(text))
}
func IsNil(a interface{}) bool {
switch a.(type) {
case nil:
return true
}
return false
}
func interface2Int64(value interface{}) (int64, bool) {
switch v := value.(type) {
case uint:
return int64(v), true
case uint8:
return int64(v), true
case uint16:
return int64(v), true
case uint32:
return int64(v), true
case uint64:
return int64(v), true
case int:
return int64(v), true
case int8:
return int64(v), true
case int16:
return int64(v), true
case int32:
return int64(v), true
case int64:
return v, true
default:
return 0, false
}
}
func interface2Float64(value interface{}) (float64, bool) {
switch v := value.(type) {
case float32:
return float64(v), true
case float64:
return v, true
default:
return 0, false
}
}
func ToFloat64(value interface{}) float64 {
if v, ok := interface2Int64(value); ok {
return float64(v)
}
if v, ok := interface2Float64(value); ok {
return v
}
return com.Float64(value)
}
func Add(left interface{}, right interface{}) interface{} {
var rleft, rright int64
var fleft, fright float64
var isInt bool
rleft, isInt = interface2Int64(left)
if !isInt {
fleft, _ = interface2Float64(left)
}
rright, isInt = interface2Int64(right)
if !isInt {
fright, _ = interface2Float64(right)
}
intSum := rleft + rright
if isInt {
return intSum
}
return fleft + fright + float64(intSum)
}
func Div(left interface{}, right interface{}) interface{} {
return ToFloat64(left) / ToFloat64(right)
}
func Mul(left interface{}, right interface{}) interface{} {
return ToFloat64(left) * ToFloat64(right)
}
func Math(op string, args ...interface{}) interface{} {
length := len(args)
if length < 1 {
return float64(0)
}
switch op {
case `mod`: //模
if length < 2 {
return float64(0)
}
return math.Mod(ToFloat64(args[0]), ToFloat64(args[1]))
case `abs`:
return math.Abs(ToFloat64(args[0]))
case `acos`:
return math.Acos(ToFloat64(args[0]))
case `acosh`:
return math.Acosh(ToFloat64(args[0]))
case `asin`:
return math.Asin(ToFloat64(args[0]))
case `asinh`:
return math.Asinh(ToFloat64(args[0]))
case `atan`:
return math.Atan(ToFloat64(args[0]))
case `atan2`:
if length < 2 {
return float64(0)
}
return math.Atan2(ToFloat64(args[0]), ToFloat64(args[1]))
case `atanh`:
return math.Atanh(ToFloat64(args[0]))
case `cbrt`:
return math.Cbrt(ToFloat64(args[0]))
case `ceil`:
return math.Ceil(ToFloat64(args[0]))
case `copysign`:
if length < 2 {
return float64(0)
}
return math.Copysign(ToFloat64(args[0]), ToFloat64(args[1]))
case `cos`:
return math.Cos(ToFloat64(args[0]))
case `cosh`:
return math.Cosh(ToFloat64(args[0]))
case `dim`:
if length < 2 {
return float64(0)
}
return math.Dim(ToFloat64(args[0]), ToFloat64(args[1]))
case `erf`:
return math.Erf(ToFloat64(args[0]))
case `erfc`:
return math.Erfc(ToFloat64(args[0]))
case `exp`:
return math.Exp(ToFloat64(args[0]))
case `exp2`:
return math.Exp2(ToFloat64(args[0]))
case `floor`:
return math.Floor(ToFloat64(args[0]))
case `max`:
if length < 2 {
return float64(0)
}
return math.Max(ToFloat64(args[0]), ToFloat64(args[1]))
case `min`:
if length < 2 {
return float64(0)
}
return math.Min(ToFloat64(args[0]), ToFloat64(args[1]))
case `pow`: //幂
if length < 2 {
return float64(0)
}
return math.Pow(ToFloat64(args[0]), ToFloat64(args[1]))
case `sqrt`: //平方根
return math.Sqrt(ToFloat64(args[0]))
case `sin`:
return math.Sin(ToFloat64(args[0]))
case `log`:
return math.Log(ToFloat64(args[0]))
case `log2`:
return math.Log2(ToFloat64(args[0]))
case `log10`:
return math.Log10(ToFloat64(args[0]))
case `tan`:
return math.Tan(ToFloat64(args[0]))
case `tanh`:
return math.Tanh(ToFloat64(args[0]))
case `add`: //加
if length < 2 {
return float64(0)
}
return Add(ToFloat64(args[0]), ToFloat64(args[1]))
case `sub`: //减
if length < 2 {
return float64(0)
}
return Sub(ToFloat64(args[0]), ToFloat64(args[1]))
case `mul`: //乘
if length < 2 {
return float64(0)
}
return Mul(ToFloat64(args[0]), ToFloat64(args[1]))
case `div`: //除
if length < 2 {
return float64(0)
}
return Div(ToFloat64(args[0]), ToFloat64(args[1]))
}
return nil
}
func IsNaN(v interface{}) bool {
return math.IsNaN(ToFloat64(v))
}
func IsInf(v interface{}, s interface{}) bool {
return math.IsInf(ToFloat64(v), com.Int(s))
}
func Sub(left interface{}, right interface{}) interface{} {
var rleft, rright int64
var fleft, fright float64
var isInt bool
rleft, isInt = interface2Int64(left)
if !isInt {
fleft, _ = interface2Float64(left)
}
rright, isInt = interface2Int64(right)
if !isInt {
fright, _ = interface2Float64(right)
}
if isInt {
return rleft - rright
}
return fleft + float64(rleft) - (fright + float64(rright))
}
func ToFixed(value interface{}, precision interface{}) string {
return fmt.Sprintf("%.*f", com.Int(precision), ToFloat64(value))
}
func Now() time.Time {
return time.Now()
}
func Eq(left interface{}, right interface{}) bool {
leftIsNil := (left == nil)
rightIsNil := (right == nil)
if leftIsNil || rightIsNil {
if leftIsNil && rightIsNil {
return true
}
return false
}
return fmt.Sprintf("%v", left) == fmt.Sprintf("%v", right)
}
func ToHTML(raw string) template.HTML {
return template.HTML(raw)
}
func ToHTMLAttr(raw string) template.HTMLAttr {
return template.HTMLAttr(raw)
}
func ToHTMLAttrs(raw map[string]interface{}) (r map[template.HTMLAttr]interface{}) {
r = make(map[template.HTMLAttr]interface{})
for k, v := range raw {
r[ToHTMLAttr(k)] = v
}
return
}
func ToJS(raw string) template.JS {
return template.JS(raw)
}
func ToCSS(raw string) template.CSS {
return template.CSS(raw)
}
func ToURL(raw string) template.URL {
return template.URL(raw)
}
func AddSuffix(s string, suffix string, args ...string) string {
beforeChar := `.`
if len(args) > 0 {
beforeChar = args[0]
if beforeChar == `` {
return s + suffix
}
}
p := strings.LastIndex(s, beforeChar)
if p < 0 {
return s
}
return s[0:p] + suffix + s[p:]
}
func IsEmpty(a interface{}) bool {
switch v := a.(type) {
case nil:
return true
case string:
return len(v) == 0
case []interface{}:
return len(v) < 1
default:
switch fmt.Sprintf(`%v`, a) {
case `<nil>`, ``, `[]`:
return true
}
}
return false
}
func NotEmpty(a interface{}) bool {
return !IsEmpty(a)
}
func InStrSlice(values []string, value string) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}
func SearchStrSlice(values []string, value string) int {
for i, v := range values {
if v == value {
return i
}
}
return -1
}
func DurationFormat(lang interface{}, t interface{}, args ...string) *com.Durafmt {
duration := ToDuration(t, args...)
return com.ParseDuration(duration, lang)
}
func ToDuration(t interface{}, args ...string) time.Duration {
td := time.Second
if len(args) > 0 {
switch args[0] {
case `ns`:
td = time.Nanosecond
case `us`:
td = time.Microsecond
case `s`:
td = time.Second
case `ms`:
td = time.Millisecond
case `h`:
td = time.Hour
case `m`:
td = time.Minute
}
}
switch v := t.(type) {
case time.Duration:
return v
case int64:
td = time.Duration(v) * td
case int:
td = time.Duration(v) * td
case uint:
td = time.Duration(v) * td
case int32:
td = time.Duration(v) * td
case uint32:
td = time.Duration(v) * td
case uint64:
td = time.Duration(v) * td
default:
td = time.Duration(com.Int64(t)) * td
}
return td
}
func FriendlyTime(t interface{}, args ...string) string {
var td time.Duration
switch v := t.(type) {
case time.Duration:
td = v
case int64:
td = time.Duration(v)
case int:
td = time.Duration(v)
case uint:
td = time.Duration(v)
case int32:
td = time.Duration(v)
case uint32:
td = time.Duration(v)
case uint64:
td = time.Duration(v)
default:
td = time.Duration(com.Int64(t))
}
return com.FriendlyTime(td, args...)
}
func TsToTime(timestamp interface{}) time.Time {
return TimestampToTime(timestamp)
}
func TsToDate(format string, timestamp interface{}) string {
t := TimestampToTime(timestamp)
if t.IsZero() {
return ``
}
return t.Format(format)
}
func TimestampToTime(timestamp interface{}) time.Time {
var ts int64
switch v := timestamp.(type) {
case int64:
ts = v
case uint:
ts = int64(v)
case int:
ts = int64(v)
case uint32:
ts = int64(v)
case int32:
ts = int64(v)
case uint64:
ts = int64(v)
default:
i, e := strconv.ParseInt(fmt.Sprint(timestamp), 10, 64)
if e != nil {
log.Println(e)
}
ts = i
}
return time.Unix(ts, 0)
}
func NumberFormat(number interface{}, precision int, delim ...string) string {
r := fmt.Sprintf(`%.*f`, precision, ToFloat64(number))
d := `,`
if len(delim) > 0 {
d = delim[0]
if len(d) == 0 {
return r
}
}
i := len(r) - 1 - precision
j := int(math.Ceil(float64(i) / float64(3)))
s := make([]string, j)
v := r[i:]
for i > 0 && j > 0 {
j--
start := i - 3
if start < 0 {
start = 0
}
s[j] = r[start:i]
i = start
}
return strings.Join(s, d) + v
}