forked from phR0ze/n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.go
1442 lines (1296 loc) · 38.2 KB
/
str.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
package n
import (
"regexp"
"sort"
"strings"
"unicode"
"github.com/pkg/errors"
)
var (
// ReGraphicalOnly is a regex to filter on graphical runes only
ReGraphicalOnly = regexp.MustCompile(`[^[:graph:]]+`)
)
// Str wraps the Go []rune and implements the Slice interface providing
// convenience methods on par with rapid development languages.
type Str []rune
// NewStr creates a new *Str which will never be nil
// Supports: Str *Str, string *string, []byte *[]byte, rune *rune, []rune *[]rune, []string *[]string ...
func NewStr(obj interface{}) *Str {
return ToStr(obj)
}
// NewStrV creates a new *Str from the given variadic elements. Returned *Str
// will never be nil.
// Supports: Str *Str, string *string, []byte *[]byte, rune *rune, []rune *[]rune, []string *[]string ...
func NewStrV(elems ...interface{}) *Str {
return ToStr(elems)
}
// A exports the Str as a Go string
func (p *Str) A() string {
if p == nil {
return ""
}
return string(*p)
}
// All tests if this Slice contains all of the given variadic elements.
// Incompatible types will return false.
// Supports: Str *Str, string *string, rune []rune as a string, []byte as string
func (p *Str) All(elems ...interface{}) bool {
if p == nil || len(*p) == 0 || len(elems) == 0 {
return false
}
str := p.A()
result := true
for i := range elems {
if !strings.Contains(str, ToString(elems[i])) {
return false
}
}
return result
}
// AllS tests if this Slice contains all of the given Slice's elements.
// Incompatible types will return false.
// Supports: []Str *[]Str, []string *[]string, StringSlice *StringSlice, []rune *[]rune as chars, []byte *[]byte as chars
func (p *Str) AllS(slice interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
str := p.A()
result := true
o := DeReference(slice)
switch x := o.(type) {
case byte:
if !strings.ContainsRune(str, rune(x)) {
return false
}
case []byte:
SetOnTrueB(&result, false, len(x) == 0)
for i := range x {
if !strings.ContainsRune(str, rune(x[i])) {
return false
}
}
case rune:
if !strings.ContainsRune(str, x) {
return false
}
case []rune:
SetOnTrueB(&result, false, len(x) == 0)
for i := range x {
if !strings.ContainsRune(str, x[i]) {
return false
}
}
default:
elems := *ToStringSlice(x)
SetOnTrueB(&result, false, len(elems) == 0)
for i := range elems {
if !strings.Contains(str, elems[i]) {
return false
}
}
}
return result
}
// Any tests if this Slice is not empty or optionally if it contains
// any of the given variadic elements. Incompatible types will return false.
// Supports: Str *Str, string *string, rune []rune as a string, []byte as string...
func (p *Str) Any(elems ...interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
// Not looking for anything
if len(elems) == 0 {
return true
}
// Looking for something specific returns false if incompatible type
return p.AnyS(elems)
}
// AnyS tests if this Slice contains any of the given Slice's elements.
// Incompatible types will return false.
// Supports: []Str *[]Str, []string *[]string, StringSlice *StringSlice, []rune *[]rune as chars, []byte *[]byte as chars
func (p *Str) AnyS(slice interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
str := p.A()
result := false
o := DeReference(slice)
switch x := o.(type) {
case byte:
if strings.ContainsRune(str, rune(x)) {
return true
}
case []byte:
for i := range x {
if strings.ContainsRune(str, rune(x[i])) {
return true
}
}
case rune:
if strings.ContainsRune(str, x) {
return true
}
case []rune:
for i := range x {
if strings.ContainsRune(str, x[i]) {
return true
}
}
default:
elems := *ToStringSlice(x)
for i := range elems {
if strings.Contains(str, elems[i]) {
return true
}
}
}
return result
}
// AnyW tests if this Slice contains any that match the lambda selector.
func (p *Str) AnyW(sel func(O) bool) bool {
return p.CountW(sel) != 0
}
// Append an element to the end of this Slice and returns a reference to this Slice.
// Supports: Str *Str, string *string, []byte *[]byte, rune *rune, []rune *[]rune, []string *[]string ...
func (p *Str) Append(elem interface{}) ISlice {
if p == nil {
p = NewStrV()
}
*p = append(*p, (*ToStr(elem))...)
return p
}
// AppendV appends the variadic elements to the end of this Slice and returns a reference to this Slice.
// Supports: Str *Str, string *string, []byte *[]byte, rune *rune, []rune *[]rune, []string *[]string ...
func (p *Str) AppendV(elems ...interface{}) ISlice {
if p == nil {
p = NewStrV()
}
for _, elem := range elems {
*p = append(*p, (*ToStr(elem))...)
}
return p
}
// Ascii converts the string to pure ASCII
func (p *Str) Ascii() *Str {
if p == nil {
return A("")
}
return A(ReGraphicalOnly.ReplaceAllString(string(*p), " "))
}
// AsciiA converts the string to pure ASCII
func (p *Str) AsciiA() string {
if p == nil {
return ""
}
return ReGraphicalOnly.ReplaceAllString(string(*p), " ")
}
// AsciiOnly checks to see if this is an ASCII only string
func (p *Str) AsciiOnly() bool {
if p == nil {
return true
}
return len(*p) == len(ReGraphicalOnly.ReplaceAllString(string(*p), ""))
}
// At returns the element at the given index location. Allows for negative notation.
func (p *Str) At(i int) (elem *Object) {
elem = &Object{}
if p == nil {
return
}
if i = absIndex(len(*p), i); i == -1 {
return
}
elem.o = ToChar((*p)[i])
return
}
// B exports the Str as a Go []byte
func (p *Str) B() []byte {
if p == nil {
return []byte{}
}
return []byte(string(*p))
}
// C exports the Str as a Char
func (p *Str) C() *Char {
return NewChar(p)
}
// Clear modifies this Slice to clear out all elements and returns a reference to this Slice.
func (p *Str) Clear() ISlice {
if p == nil {
p = NewStrV()
} else {
p.Drop()
}
return p
}
// Concat returns a new Slice by appending the given Slice to this Slice.
// Supports Str, *Str, []string or *[]string
func (p *Str) Concat(slice interface{}) (new ISlice) {
return p.Copy().ConcatM(slice)
}
// ConcatA calls Concat and returns it as a string for brevity
func (p *Str) ConcatA(str interface{}) string {
return p.Concat(str).A()
}
// ConcatM modifies this Slice by appending the given and returns a reference to this Slice.
// Supports: Str *Str, string *string, []byte *[]byte, rune *rune, []rune *[]rune, []string *[]string ...
func (p *Str) ConcatM(slice interface{}) ISlice {
return p.Append(slice)
}
// Contains checks if the given str is contained in this Str.
// Pass through for strings.Contains
func (p *Str) Contains(str string) bool {
if p == nil {
return false
}
return strings.Contains(string(*p), str)
}
// ContainsAny checks if any of the given chars exist in this Str.
// Pass through for strings.ContainsAny
func (p *Str) ContainsAny(chars string) bool {
if p == nil {
return false
}
return strings.ContainsAny(string(*p), chars)
}
// ContainsRune checks if the given rune exists in this Str.
// Pass through for strings.ContainsRune
func (p *Str) ContainsRune(r rune) bool {
if p == nil {
return false
}
return strings.ContainsRune(string(*p), r)
}
// Copy returns a new Slice with the indicated range of elements copied from this Slice.
// Expects nothing, in which case everything is copied, or two indices i and j, in which
// case positive and negative notation is supported and uses an inclusive behavior such
// that Slice(0, -1) includes index -1 as opposed to Go's exclusive behavior. Out of
// bounds indices will be moved within bounds.
//
// An empty Slice is returned if indicies are mutually exclusive or nothing can be returned.
func (p *Str) Copy(indices ...int) (new ISlice) {
if p == nil || len(*p) == 0 {
return NewStrV()
}
// Handle index manipulation
i, j, err := absIndices(len(*p), indices...)
if err != nil {
return NewStrV()
}
// Copy elements over to new Slice
x := make([]rune, j-i, j-i)
copy(x, []rune(*p)[i:j])
return ToStr(x)
}
// CopyA calls Copy and returns it as a string for brevity
func (p *Str) CopyA(indices ...int) (new string) {
return p.Copy(indices...).A()
}
// Count counts the number of non-overlapping instances of substr in this string.
// Supports: Str, *Str, string, *string, rune, []rune as a string, []byte as string.
//
// Pass through for strings.Count
func (p *Str) Count(elem interface{}) (cnt int) {
if p == nil || len(*p) == 0 || elem == nil {
return
}
return strings.Count(string(*p), string(*ToStr(elem)))
}
// CountW counts the number of elements in this Slice that match the lambda selector.
func (p *Str) CountW(sel func(O) bool) (cnt int) {
if p == nil || len(*p) == 0 {
return
}
for i := range *p {
if sel(Char((*p)[i])) {
cnt++
}
}
return
}
// Drop modifies this Slice to delete the indicated range of elements and returns a referece to this Slice.
// Expects nothing, in which case everything is dropped, or two indices i and j, in which case positive and
// negative notation is supported and uses an inclusive behavior such that DropAt(0, -1) includes index -1
// as opposed to Go's exclusive behavior. Out of bounds indices will be moved within bounds.
func (p *Str) Drop(indices ...int) ISlice {
if p == nil || len(*p) == 0 {
return NewStrV()
}
// Handle index manipulation
i, j, err := absIndices(len(*p), indices...)
if err != nil {
return p
}
// Execute
n := j - i
if i+n < len(*p) {
*p = append((*p)[:i], (*p)[i+n:]...)
} else {
*p = (*p)[:i]
}
return p
}
// DropAt modifies this Slice to delete the element at the given index location. Allows for negative notation.
// Returns a reference to this Slice.
func (p *Str) DropAt(i int) ISlice {
return p.Drop(i, i)
}
// DropFirst modifies this Slice to delete the first element and returns a reference to this Slice.
func (p *Str) DropFirst() ISlice {
return p.Drop(0, 0)
}
// DropFirstN modifies this Slice to delete the first n elements and returns a reference to this Slice.
func (p *Str) DropFirstN(n int) ISlice {
if n == 0 {
return p
}
return p.Drop(0, abs(n)-1)
}
// DropLast modifies this Slice to delete the last element and returns a reference to this Slice.
func (p *Str) DropLast() ISlice {
return p.Drop(-1, -1)
}
// DropLastN modifies thi Slice to delete the last n elements and returns a reference to this Slice.
func (p *Str) DropLastN(n int) ISlice {
if n == 0 {
return p
}
return p.Drop(absNeg(n), -1)
}
// DropW modifies this Slice to delete the elements that match the lambda selector and returns a reference to this Slice.
// The slice is updated instantly when lambda expression is evaluated not after DropW completes.
func (p *Str) DropW(sel func(O) bool) ISlice {
if p == nil || len(*p) == 0 {
return p
}
l := len(*p)
for i := 0; i < l; i++ {
if sel(Char((*p)[i])) {
p.DropAt(i)
l--
i--
}
}
return p
}
// Each calls the given lambda once for each element in this Slice, passing in that element
// as a parameter. Element will be a *Char. Returns a reference to this Slice
func (p *Str) Each(action func(O)) ISlice {
if p == nil {
return p
}
for i := range *p {
action(ToChar((*p)[i]))
}
return p
}
// EachE calls the given lambda once for each element in this Slice, passing in that element
// as a parameter. Element will be a *Char. Returns a reference to this Slice and any error from the lambda.
func (p *Str) EachE(action func(O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := range *p {
if err = action(ToChar((*p)[i])); err != nil {
return p, err
}
}
return p, err
}
// EachI calls the given lambda once for each element in this Slice, passing in the index and element
// as a parameter. Element will be a *Char. Returns a reference to this Slice
func (p *Str) EachI(action func(int, O)) ISlice {
if p == nil {
return p
}
for i := range *p {
action(i, ToChar((*p)[i]))
}
return p
}
// EachIE calls the given lambda once for each element in this Slice, passing in the index and element
// as a parameter. Element will be a *Char. Returns a reference to this Slice and any error from the lambda.
func (p *Str) EachIE(action func(int, O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := range *p {
if err = action(i, ToChar((*p)[i])); err != nil {
return p, err
}
}
return p, err
}
// EachR calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Element will be a *Char. Returns a reference to this Slice
func (p *Str) EachR(action func(O)) ISlice {
if p == nil {
return p
}
for i := len(*p) - 1; i >= 0; i-- {
action(ToChar((*p)[i]))
}
return p
}
// EachRE calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Element will be a *Char. Returns a reference to this Slice and any error from the lambda.
func (p *Str) EachRE(action func(O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := len(*p) - 1; i >= 0; i-- {
if err = action(ToChar((*p)[i])); err != nil {
return p, err
}
}
return p, err
}
// EachRI calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Element will be a *Char. Returns a reference to this Slice
func (p *Str) EachRI(action func(int, O)) ISlice {
if p == nil {
return p
}
for i := len(*p) - 1; i >= 0; i-- {
action(i, ToChar((*p)[i]))
}
return p
}
// EachRIE calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Element will be a *Char. Returns a reference to this Slice and any error from the lambda.
func (p *Str) EachRIE(action func(int, O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := len(*p) - 1; i >= 0; i-- {
if err = action(i, ToChar((*p)[i])); err != nil {
return p, err
}
}
return p, err
}
// Empty tests if this Slice is empty.
func (p *Str) Empty() bool {
if p == nil || len(*p) == 0 {
return true
}
return false
}
// Equal tests if this Str is equal to the given string
func (p *Str) Equal(val interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
return string(*p) == string(*ToStr(val))
}
// Fields splits the Str around each instance of one or more consecutive white space characters
// as defined by unicode.IsSpace, returning a Slice of substrings or an empty Slice if only
// white space is found or the Str is nil or empty.
func (p *Str) Fields() (new *StringSlice) {
if p == nil || len(*p) == 0 {
return ToStringSlice(p)
}
return ToStringSlice(strings.Fields(string(*p)))
}
// FieldsW splits the Str where the lambda f returns true, returning a Slice of substrings or
// an empty Slice if nothing returns true or the Str is nil or empty.
func (p *Str) FieldsW(f func(rune) bool) (new *StringSlice) {
new = NewStringSliceV()
if p == nil || len(*p) == 0 {
return
}
i := 0
capture := false
for j := range *p {
if f((*p)[j]) {
if capture {
capture = false
*new = append(*new, string((*p)[i:j]))
}
} else if !capture {
capture = true
i = j
}
}
if capture {
*new = append(*new, p.Slice(i, -1).A())
}
return
}
// First returns the first element in this Slice as Object.
// Object.Nil() == true will be returned when there are no elements in the slice.
func (p *Str) First() (elem *Object) {
return p.At(0)
}
// FirstN returns the first n elements in this slice as a Slice reference to the original.
// Best effort is used such that as many as can be will be returned up until the request is satisfied.
func (p *Str) FirstN(n int) ISlice {
if n == 0 {
return NewStrV()
}
return p.Slice(0, abs(n)-1)
}
// G returns the underlying data structure as a builtin Go type
func (p *Str) G() string {
return p.O().(string)
}
// HasAnyPrefix checks if the string has any of the given prefixes
func (p *Str) HasAnyPrefix(prefixes interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
str := p.A()
slice := ToStringSlice(prefixes)
for i := range *slice {
x := (*slice)[i]
if len(x) > 0 && strings.HasPrefix(str, x) {
return true
}
}
return false
}
// HasAnyPrefixV checks if the string has any of the given prefixes
func (p *Str) HasAnyPrefixV(prefixes ...interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
str := p.A()
for i := range prefixes {
x := ToString(prefixes[i])
if len(x) > 0 && strings.HasPrefix(str, x) {
return true
}
}
return false
}
// HasAnySuffix checks if the string has any of the given prefixes
func (p *Str) HasAnySuffix(prefixes interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
str := p.A()
slice := ToStringSlice(prefixes)
for i := range *slice {
x := (*slice)[i]
if len(x) > 0 && strings.HasSuffix(str, x) {
return true
}
}
return false
}
// HasAnySuffixV checks if the string has any of the given prefixes
func (p *Str) HasAnySuffixV(prefixes ...interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
str := p.A()
for i := range prefixes {
x := ToString(prefixes[i])
if len(x) > 0 && strings.HasSuffix(str, x) {
return true
}
}
return false
}
// HasPrefix checks if the Str has the given prefix.
// Pass through to strings.HasPrefix
func (p *Str) HasPrefix(prefix interface{}) bool {
x := ToStr(prefix)
if p == nil || len(*p) == 0 || len(*x) == 0 {
return false
}
return strings.HasPrefix(string(*p), string(*x))
}
// HasSuffix checks if the Str has the given prefix.
// Pass through to strings.HasSuffix
func (p *Str) HasSuffix(prefix interface{}) bool {
x := ToStr(prefix)
if p == nil || len(*p) == 0 || len(*x) == 0 {
return false
}
return strings.HasSuffix(string(*p), string(*x))
}
// Index returns the index of the first substr in this Str, or -1 if substr is not present.
// Pass through to strings.Index
func (p *Str) Index(substr interface{}) (loc int) {
loc = -1
if p == nil || len(*p) == 0 {
return
}
str := string(*p)
x := ToString(substr)
return strings.Index(str, x)
}
// IndexAny returns the index of the first rune in the given elems found, or -1 if not found.
// Pass through to strings.IndexAny
func (p *Str) IndexAny(elems interface{}) (loc int) {
loc = -1
if p == nil || len(*p) == 0 {
return
}
str := string(*p)
x := ToString(elems)
return strings.IndexAny(str, x)
}
// IndexChar returns the index of the first instance of char in this Str, or -1 if char is not present.
// Specifically handles byte, rune and Char
func (p *Str) IndexChar(char interface{}) (loc int) {
loc = -1
if p == nil || len(*p) == 0 {
return
}
str := string(*p)
x := ToChar(char).G()
return strings.IndexRune(str, x)
}
// Insert modifies this Slice to insert the given element before the element with the given index.
// Negative indices count backwards from the end of the slice, where -1 is the last element. If a
// negative index is used, the given element will be inserted after that element, so using an index
// of -1 will insert the element at the end of the slice. Slice is returned for chaining. Invalid
// index locations will not change the slice.
func (p *Str) Insert(i int, obj interface{}) ISlice {
if p == nil || len(*p) == 0 {
return p.ConcatM(obj)
}
// Insert the item before j if pos and after j if neg
j := i
if j = absIndex(len(*p), j); j == -1 {
return p
}
if i < 0 {
j++
}
x := ToStr(obj)
if j == 0 {
*p = append(*x, *p...)
} else if j < len(*p) {
*p = append(*p, *x...) // ensures enough space exists
copy((*p)[j+len(*x):], (*p)[j:]) // shifts right elements drop added
copy((*p)[j:], *x) // set new in locations vacated
} else {
*p = append(*p, *x...)
}
return p
}
// InterSlice returns true if the underlying implementation is a RefSlice
func (p *Str) InterSlice() bool {
return false
}
// Join converts each element into a string then joins them together using the given separator or comma by default.
func (p *Str) Join(separator ...string) (str *Object) {
if p == nil || len(*p) == 0 {
str = &Object{""}
return
}
sep := ","
if len(separator) > 0 {
sep = separator[0]
}
var builder strings.Builder
for i := range *p {
builder.WriteString(ToChar((*p)[i]).A())
if i+1 < len(*p) {
builder.WriteString(sep)
}
}
str = &Object{builder.String()}
return
}
// Last returns the last element in this Slice as an Object.
// Object.Nil() == true will be returned if there are no elements in the slice.
func (p *Str) Last() (elem *Object) {
return p.At(-1)
}
// LastIndex returns the index of the last substr in this Str, or -1 if substr is not present.
// Pass through to strings.LastIndex
func (p *Str) LastIndex(substr interface{}) (loc int) {
loc = -1
if p == nil || len(*p) == 0 {
return
}
str := string(*p)
x := ToString(substr)
return strings.LastIndex(str, x)
}
// LastIndexAny returns the index of the first rune in the given elems found, or -1 if not found.
// Pass through to strings.LastIndexAny
func (p *Str) LastIndexAny(elems interface{}) (loc int) {
loc = -1
if p == nil || len(*p) == 0 {
return
}
str := string(*p)
x := ToString(elems)
return strings.LastIndexAny(str, x)
}
// LastIndexChar returns the index of the first instance of char in this Str, or -1 if char is not present.
// Specifically handles byte, rune and Char
func (p *Str) LastIndexChar(char interface{}) (loc int) {
loc = -1
if p == nil || len(*p) == 0 {
return
}
str := string(*p)
x := string(ToChar(char).G())
return strings.LastIndexAny(str, x)
}
// LastN returns the last n elements in this Slice as a Slice reference to the original.
// Best effort is used such that as many as can be will be returned up until the request is satisfied.
func (p *Str) LastN(n int) ISlice {
if n == 0 {
return NewStrV()
}
return p.Slice(absNeg(n), -1)
}
// Len returns the number of elements in this Slice
func (p *Str) Len() int {
if p == nil {
return 0
}
return len(*p)
}
// Less returns true if the element indexed by i is less than the element indexed by j.
func (p *Str) Less(i, j int) bool {
if p == nil || len(*p) < 2 || i < 0 || j < 0 || i >= len(*p) || j >= len(*p) {
return false
}
return ToChar((*p)[i]).Less((*p)[j])
}
// Map creates a new slice with the modified elements from the lambda.
func (p *Str) Map(mod func(O) O) ISlice {
var slice ISlice
if p == nil || len(*p) == 0 {
return NewStrV()
}
for i := range *p {
v := mod((*p)[i])
if slice == nil {
slice = Slice(v)
} else {
slice.Append(v)
}
}
return slice
}
// Nil tests if this Slice is nil
func (p *Str) Nil() bool {
if p == nil {
return true
}
return false
}
// O returns the underlying data structure as is
func (p *Str) O() interface{} {
if p == nil {
return ""
}
return string(*p)
}
// Pair simply returns the first and second Slice elements as Objects
func (p *Str) Pair() (first, second *Object) {
first, second = &Object{}, &Object{}
if p == nil {
return
}
if len(*p) > 0 {
first = p.At(0)
}
if len(*p) > 1 {
second = p.At(1)
}
return
}
// Pop modifies this Slice to remove the last element and returns the removed element as an Object.
func (p *Str) Pop() (elem *Object) {
elem = p.Last()
p.DropLast()
return
}
// PopN modifies this Slice to remove the last n elements and returns the removed elements as a new Slice.
func (p *Str) PopN(n int) (new ISlice) {
if n == 0 {
return NewStrV()
}
new = p.Copy(absNeg(n), -1)
p.DropLastN(n)
return
}
// Prepend modifies this Slice to add the given element at the begining and returns a reference to this Slice.
func (p *Str) Prepend(elem interface{}) ISlice {
return p.Insert(0, elem)
}
// RefSlice returns true if the underlying implementation is a RefSlice
func (p *Str) RefSlice() bool {
return false
}
// Replace returns a copy of the string s with the first n non-overlapping instances of old
// replaced by new. If old is empty, it matches at the beginning of the string and after each
// UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no
// limit on the number of replacements. Pass through to strings.Replace
func (p *Str) Replace(old, new interface{}, n int) *Str {
if p == nil || len(*p) == 0 {
return NewStrV()
}
str := p.A()
x := ToString(old)
y := ToString(new)
return ToStr(strings.Replace(str, x, y, n))
}
// ReplaceAll returns a copy of the string s with all non-overlapping instances of old
// replaced by new. If old is empty, it matches at the beginning of the string and after each
// UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. Pass through
// to strings.ReplaceAll
func (p *Str) ReplaceAll(old, new interface{}) *Str {
if p == nil || len(*p) == 0 {
return NewStrV()
}
str := p.A()
x := ToString(old)
y := ToString(new)
return ToStr(strings.ReplaceAll(str, x, y))
}
// Reverse returns a new Slice with the order of the elements reversed.
func (p *Str) Reverse() (new ISlice) {
if p == nil || len(*p) < 2 {
return p.Copy()
}
return p.Copy().ReverseM()
}
// ReverseM modifies this Slice reversing the order of the elements and returns a reference to this Slice.
func (p *Str) ReverseM() ISlice {
if p == nil || len(*p) == 0 {
return p
}
for i, j := 0, len(*p)-1; i < j; i, j = i+1, j-1 {
p.Swap(i, j)
}
return p
}
// S is an alias to ToStringSlice
func (p *Str) S() (slice *StringSlice) {
return ToStringSlice(p.O())
}
// Select creates a new slice with the elements that match the lambda selector.
func (p *Str) Select(sel func(O) bool) (new ISlice) {
slice := NewStrV()
if p == nil || len(*p) == 0 {
return slice
}
for i := range *p {
if sel(ToChar((*p)[i])) {
*slice = append(*slice, (*p)[i])
}
}
return slice
}
// Set the element(s) at the given index location to the given element(s). Allows for negative notation.
// Returns a reference to this Slice and swallows any errors.
func (p *Str) Set(i int, elem interface{}) ISlice {
slice, _ := p.SetE(i, elem)
return slice
}
// SetE the element(s) at the given index location to the given element. Allows for negative notation.
// Returns a reference to this Slice and an error if out of bounds or elem is the wrong type.
func (p *Str) SetE(i int, elems interface{}) (ISlice, error) {
var err error
if p == nil {
return NewStrV(), err
}
if i = absIndex(len(*p), i); i == -1 {
err = errors.Errorf("slice assignment is out of bounds")