-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.go
909 lines (742 loc) · 19.3 KB
/
methods.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
// ssg Project
// Copyright (C) 2021 ALiwoto
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of the source code.
package ssg
import (
"math/rand"
"reflect"
"strconv"
"strings"
"time"
)
//---------------------------------------------------------
// _setValue will set the bytes value of the StrongString.
func (s *StrongString) _setValue(str string) {
if s._value == nil {
s._value = make([]rune, BaseIndex)
}
for _, current := range str {
s._value = append(s._value, current)
}
}
// GetValue will give you the real value of this StrongString.
func (s *StrongString) GetValue() string {
return string(s._value)
}
// length method, will give you the length-as-int of this StrongString.
func (s *StrongString) Length() int {
return len(s._value)
}
// isEmpty will check if this StrongString is empty or not.
func (s *StrongString) IsEmpty() bool {
return s._value == nil || len(s._value) == BaseIndex
}
// isEqual will check if the passed-by-value in the arg is equal to this
// StrongString or not.
func (s *StrongString) IsEqual(q QString) bool {
if reflect.TypeOf(q) != reflect.TypeOf(s) {
return q.GetValue() == s.GetValue()
}
strong, _ok := q.(*StrongString)
if !_ok {
return false
}
// check if the length of them are equal or not.
if len(s._value) != len(strong._value) {
return false
}
for i := 0; i < len(s._value); i++ {
if s._value[i] != strong._value[i] {
return false
}
}
return true
}
// GetIndexV method will give you the rune in index.
// if the passed-by `index` is out of range, it will return
// zero index of the value.
func (s *StrongString) GetIndexV(index int) rune {
if s.IsEmpty() {
return BaseIndex
}
l := len(s._value)
if index >= l || l < BaseIndex {
return s._value[BaseIndex]
}
return s._value[index]
}
// HasSuffix will check if at least there is one suffix is
// presents in this StrongString not.
// the StrongString should ends with at least one of these suffixes.
func (s *StrongString) HasSuffix(values ...string) bool {
for _, str := range values {
if strings.HasSuffix(s.GetValue(), str) {
return true
}
}
return false
}
// HasSuffixes will check if all of the suffixes are
// present in this StrongString or not.
// the StrongString should ends with all of these suffixes.
// usage of this method is not recommended, since you can use
// HasSuffix method with only one string (the longest string).
// this way you will just use too much cpu resources.
func (s *StrongString) HasSuffixes(values ...string) bool {
for _, str := range values {
if !strings.HasSuffix(s.GetValue(), str) {
return false
}
}
return true
}
// HasPrefix will check if at least there is one prefix is
// presents in this StrongString or not.
// the StrongString should starts with at least one of these prefixes.
func (s *StrongString) HasPrefix(values ...string) bool {
v := s.GetValue()
if len(v) == BaseIndex {
return false
}
for _, str := range values {
if strings.HasPrefix(v, str) {
return true
}
}
return false
}
// HasRunePrefix will check if at least there is one prefix is
// presents in this StrongString or not.
// the StrongString should starts with at least one of these prefixes.
func (s *StrongString) HasRunePrefix(values ...rune) bool {
if len(s._value) == BaseIndex {
return false
}
for _, r := range values {
if s._value[BaseIndex] == r {
return true
}
}
return false
}
// HasPrefixes will check if all of the prefixes are
// present in this StrongString or not.
// the StrongString should starts with all of these suffixes.
// usage of this method is not recommended, since you can use
// HasSuffix method with only one string (the longest string).
// this way you will just use too much cpu resources.
func (s *StrongString) HasPrefixes(values ...string) bool {
v := s.GetValue()
if len(v) == BaseIndex {
return false
}
for _, str := range values {
if !strings.HasPrefix(v, str) {
return false
}
}
return true
}
// HasRunePrefixes will check if all of the prefixes are
// present in this StrongString or not.
// the StrongString should starts with all of these suffixes.
// usage of this method is not recommended, since you can use
// HasSuffix method with only one string (the longest string).
// this way you will just use too much cpu resources.
func (s *StrongString) HasRunePrefixes(values ...rune) bool {
if len(s._value) == BaseIndex {
return false
}
for _, r := range values {
if s._value[BaseIndex] != r {
return false
}
}
return true
}
func (s *StrongString) Split(qs ...QString) []QString {
strs := SplitSlice(s.GetValue(), ToStrSlice(qs))
return ToQSlice(strs)
}
func (s *StrongString) SplitN(n int, qs ...QString) []QString {
strs := SplitSliceN(s.GetValue(), ToStrSlice(qs), n)
return ToQSlice(strs)
}
func (s *StrongString) SplitFirst(qs ...QString) []QString {
strs := SplitSliceN(s.GetValue(), ToStrSlice(qs),
BaseTwoIndex)
return ToQSlice(strs)
}
func (s *StrongString) SplitStr(qs ...string) []QString {
strs := SplitSlice(s.GetValue(), qs)
return ToQSlice(strs)
}
func (s *StrongString) SplitStrN(n int, qs ...string) []QString {
strs := SplitSliceN(s.GetValue(), qs, n)
return ToQSlice(strs)
}
func (s *StrongString) SplitStrFirst(qs ...string) []QString {
strs := SplitSliceN(s.GetValue(), qs, BaseTwoIndex)
return ToQSlice(strs)
}
func (s *StrongString) ToQString() QString {
return s
}
func (s *StrongString) Contains(qs ...QString) bool {
v := s.GetValue()
for _, current := range qs {
if strings.Contains(v, current.GetValue()) {
return true
}
}
return false
}
func (s *StrongString) ContainsStr(str ...string) bool {
v := s.GetValue()
for _, current := range str {
if strings.Contains(v, current) {
return true
}
}
return false
}
func (s *StrongString) ContainsAll(qs ...QString) bool {
v := s.GetValue()
for _, current := range qs {
if !strings.Contains(v, current.GetValue()) {
return false
}
}
return true
}
func (s *StrongString) ContainsStrAll(str ...string) bool {
v := s.GetValue()
for _, current := range str {
if !strings.Contains(v, current) {
return false
}
}
return true
}
func (s *StrongString) TrimPrefix(qs ...QString) QString {
final := s.GetValue()
for _, current := range qs {
final = strings.TrimPrefix(final, current.GetValue())
}
return SsPtr(final)
}
func (s *StrongString) TrimPrefixStr(qs ...string) QString {
final := s.GetValue()
for _, current := range qs {
final = strings.TrimPrefix(final, current)
}
return SsPtr(final)
}
func (s *StrongString) TrimSuffix(qs ...QString) QString {
final := s.GetValue()
for _, current := range qs {
final = strings.TrimSuffix(final, current.GetValue())
}
return SsPtr(final)
}
func (s *StrongString) TrimSuffixStr(qs ...string) QString {
final := s.GetValue()
for _, current := range qs {
final = strings.TrimSuffix(final, current)
}
return SsPtr(final)
}
func (s *StrongString) Trim(qs ...QString) QString {
final := s.GetValue()
for _, current := range qs {
final = strings.Trim(final, current.GetValue())
}
return SsPtr(final)
}
func (s *StrongString) ToBool() bool {
return ToBool(s.GetValue())
}
func (s *StrongString) TrimStr(qs ...string) QString {
final := s.GetValue()
for _, current := range qs {
final = strings.Trim(final, current)
}
return SsPtr(final)
}
func (s *StrongString) Replace(qs, newS QString) QString {
return s.ReplaceStr(qs.GetValue(), newS.GetValue())
}
func (s *StrongString) ReplaceStr(qs, newS string) QString {
final := s.GetValue()
final = strings.ReplaceAll(final, qs, newS)
return SsPtr(final)
}
// LockSpecial will lock all the defined special characters.
// This way, you don't actually have to be worry about
// some normal mistakes in splitting strings, cut them out,
// check them. join them, etc...
// WARNING: this method is so dangerous, it's really
// dangerous. we can't say that it's unsafe actually,
// but still it's really dangerous, so if you don't know what the
// fuck are you doing, then please don't use this method.
// this method will not return you a new value, it will effect the
// current value. please consider using it carefully.
func (s *StrongString) LockSpecial() {
final := s.GetValue()
// replacing escaped string characters
// (I mean escaped double question mark) is necessary before
// repairing value.
final = strings.ReplaceAll(final, BACK_STR, JA_STR)
// let it repair the string.
// this function is for repairing these special characters
// and strings:
// '=', ':' and "=="
// it will escape them.
// if it wasn't for this function, members had to
// escape all of these bullshits themselves...
final = repairString(final)
final = strings.ReplaceAll(final, BACK_FLAG, JA_FLAG)
final = strings.ReplaceAll(final, BACK_EQUALITY, JA_EQUALITY)
final = strings.ReplaceAll(final, BACK_DDOT, JA_DDOT)
s._value = make([]rune, BaseIndex)
for _, c := range final {
if c != BaseIndex {
s._value = append(s._value, c)
}
}
}
// UnlockSpecial will unlock all the defined special characters.
// it will return them to their normal form.
func (s *StrongString) UnlockSpecial() {
final := s.GetValue()
final = strings.ReplaceAll(final, JA_FLAG, FLAG_PREFIX)
final = strings.ReplaceAll(final, JA_STR, STR_SIGN)
final = strings.ReplaceAll(final, JA_EQUALITY, EqualStr)
final = strings.ReplaceAll(final, JA_DDOT, DdotSign)
s._value = make([]rune, BaseIndex)
for _, c := range final {
if c != BaseIndex {
s._value = append(s._value, c)
}
}
}
//---------------------------------------------------------
func (l *ListW[T]) Find(element T) int {
for i, v := range l._values {
if v == element {
return i
}
}
return LIST_INDEX_NOTFOUND
}
func (l *ListW[T]) Count(element T) int {
count := 0
for _, v := range l._values {
if v == element {
count++
}
}
return count
}
func (l *ListW[T]) Counts(element ...T) int {
count := 0
for _, v := range l._values {
for _, current := range element {
if v == current {
count++
}
}
}
return count
}
func (l *ListW[T]) Contains(element T) bool {
return l.Find(element) != LIST_INDEX_NOTFOUND
}
func (l *ListW[T]) ContainsAll(elements ...T) bool {
for _, current := range elements {
if !l.Contains(current) {
return false
}
}
return true
}
func (l *ListW[T]) ContainsOne(elements ...T) bool {
for _, current := range elements {
if l.Contains(current) {
return true
}
}
return false
}
func (l *ListW[T]) Change(index int, element T) {
if index < 0 || index >= len(l._values) {
return
}
l._values[index] = element
}
func (l *ListW[T]) Exists(element T) bool {
return l.Find(element) != LIST_INDEX_NOTFOUND
}
func (l *ListW[T]) Append(elements ...T) {
l._values = append(l._values, elements...)
}
func (l *ListW[T]) Add(elements ...T) {
l._values = append(l._values, elements...)
}
func (l *ListW[T]) RemoveAt(index int) {
l._values = append(l._values[:index], l._values[index+1:]...)
}
func (l *ListW[T]) RemoveOnce(element T) {
index := l.Find(element)
if index != LIST_INDEX_NOTFOUND {
l.RemoveAt(index)
}
}
func (l *ListW[T]) RemoveAll(element ...T) {
var newVal []T
for _, current := range element {
for _, v := range l._values {
if v != current {
newVal = append(newVal, v)
}
}
}
l._values = newVal
}
func (l *ListW[T]) Remove(element T) {
l.RemoveOnce(element)
}
// AsArray returns a copy of the value of this list as an array.
// please do notice that if you make changes to the underlying values of
// that array, change won't be applied to the list.
func (l *ListW[T]) AsArray() []T {
var arr = make([]T, len(l._values))
copy(arr, l._values)
return arr
}
// ToArray is equivalent to AsArray method in any way.
// it returns a copy of the value of this list as an array.
// please do notice that if you make changes to the underlying values of
// that array, change won't be applied to the list.
func (l *ListW[T]) ToArray() []T {
return l.AsArray()
}
// Clear method clears the whole list.
func (l *ListW[T]) Clear() {
l._values = nil
}
func (l *ListW[T]) Get(index int) T {
return l._values[index]
}
func (l *ListW[T]) IsThreadSafe() bool {
return true
}
func (l *ListW[T]) IsEmpty() bool {
return len(l._values) == 0
}
func (l *ListW[T]) Length() int {
return len(l._values)
}
func (l *ListW[T]) IsValid() bool {
return len(l._values) > 0
}
//---------------------------------------------------------
func (s *AdvancedMap[TKey, TValue]) lock() {
s.mut.Lock()
}
func (s *AdvancedMap[TKey, TValue]) unlock() {
s.mut.Unlock()
}
func (s *AdvancedMap[TKey, TValue]) Exists(key TKey) bool {
s.lock()
b := len(s.values) != 0 && s.values[key] != nil
s.unlock()
return b
}
func (s *AdvancedMap[TKey, TValue]) Add(key TKey, value *TValue) {
s.lock()
_, exists := s.values[key]
s.values[key] = value
if exists {
s.unlock()
return
}
s.keys = append(s.keys, key)
// store the index of the map key
index := len(s.keys) - 1
s.sliceKeyIndex[key] = index
s.unlock()
}
func (s *AdvancedMap[TKey, TValue]) GetRandom() *TValue {
if s.IsEmpty() {
return nil
}
s.lock()
randomIndex := rand.Intn(len(s.keys))
key := s.keys[randomIndex]
value := s.values[key]
s.unlock()
return value
}
func (s *AdvancedMap[TKey, TValue]) ForEach(fn func(TKey, *TValue) ForEachOperation) {
if fn == nil {
return
}
s.lock()
myFor:
for key, value := range s.values {
switch fn(key, value) {
case ForEachOperationContinue:
continue
case ForEachOperationBreak:
break myFor
case ForEachOperationRemove:
s.delete(key, false)
case ForEachOperationRemoveBreak:
s.delete(key, false)
break myFor
}
}
s.unlock()
}
func (s *AdvancedMap[TKey, TValue]) GetRandomValue() TValue {
if s.IsEmpty() {
return s._default
}
s.lock()
randomIndex := rand.Intn(len(s.keys))
key := s.keys[randomIndex]
value := s.values[key]
s.unlock()
if value == nil {
return s._default
}
return *value
}
func (s *AdvancedMap[TKey, TValue]) GetRandomKey() (key TKey, ok bool) {
if s.IsEmpty() {
return
}
ok = true
s.lock()
key = s.keys[rand.Intn(len(s.keys))]
s.unlock()
return
}
func (s *AdvancedMap[TKey, TValue]) ToArray() []TValue {
var array []TValue
s.lock()
for _, v := range s.values {
if v == nil {
array = append(array, s._default)
continue
}
array = append(array, *v)
}
s.unlock()
return array
}
func (s *AdvancedMap[TKey, TValue]) ToPointerArray() []*TValue {
var array []*TValue
s.lock()
for _, v := range s.values {
if v == nil {
// most likely impossible, this checker is here just for more safety.
continue
}
array = append(array, v)
}
s.unlock()
return array
}
func (s *AdvancedMap[TKey, TValue]) ToList() GenericList[*TValue] {
list := GetEmptyList[*TValue]()
s.lock()
for _, v := range s.values {
if v == nil {
// most likely impossible, this checker is here just for more safety.
continue
}
list.Add(v)
}
s.unlock()
return list
}
func (s *AdvancedMap[TKey, TValue]) AddList(keyGetter func(*TValue) TKey, elements ...TValue) {
if len(elements) == 0 || keyGetter == nil {
return
}
for _, current := range elements {
s.Add(keyGetter(¤t), ¤t)
}
}
func (s *AdvancedMap[TKey, TValue]) AddPointerList(keyGetter func(*TValue) TKey, elements ...*TValue) {
if len(elements) == 0 || keyGetter == nil {
return
}
for _, current := range elements {
s.Add(keyGetter(current), current)
}
}
func (s *AdvancedMap[TKey, TValue]) delete(key TKey, useLock bool) {
if useLock {
s.lock()
}
// get index in key slice for key
index, exists := s.sliceKeyIndex[key]
if !exists {
if useLock {
s.unlock()
}
// item does not exist
return
}
delete(s.sliceKeyIndex, key)
wasLastIndex := len(s.keys)-1 == index
// remove key from slice of keys
s.keys[index] = s.keys[len(s.keys)-1]
s.keys = s.keys[:len(s.keys)-1]
// we just swapped the last element to another position.
// so we need to update it's index (if it was not in last position)
if !wasLastIndex {
otherKey := s.keys[index]
s.sliceKeyIndex[otherKey] = index
}
delete(s.values, key)
if useLock {
s.unlock()
}
}
func (s *AdvancedMap[TKey, TValue]) Delete(key TKey) {
s.delete(key, true)
}
func (s *AdvancedMap[TKey, TValue]) Get(key TKey) *TValue {
s.lock()
value := s.values[key]
s.unlock()
return value
}
func (s *AdvancedMap[TKey, TValue]) GetValue(key TKey) TValue {
s.lock()
value := s.values[key]
s.unlock()
if value == nil {
return s._default
}
return *value
}
func (s *AdvancedMap[TKey, TValue]) SetDefault(value TValue) {
s._default = value
}
// Set function sets the key of type TKey in this safe map to the value.
// the value should be of type TValue or *TValue, otherwise this function won't
// do anything at all.
func (s *AdvancedMap[TKey, TValue]) Set(key TKey, value any) {
correctValue, ok := value.(*TValue)
if !ok {
anotherValue, ok := value.(TValue)
if !ok {
return
}
correctValue = &anotherValue
}
s.Add(key, correctValue)
}
// Clear will clear the whole map.
func (s *AdvancedMap[TKey, TValue]) Clear() {
s.lock()
if len(s.values) != 0 {
s.values = make(map[TKey]*TValue)
}
s.unlock()
}
func (s *AdvancedMap[TKey, TValue]) Length() int {
s.lock()
l := len(s.values)
s.unlock()
return l
}
func (s *AdvancedMap[TKey, TValue]) IsEmpty() bool {
return s.Length() == 0
}
func (s *AdvancedMap[TKey, TValue]) ToNormalMap() map[TKey]TValue {
m := make(map[TKey]TValue)
s.lock()
for k, v := range s.values {
if v == nil {
m[k] = s._default
continue
}
m[k] = *v
}
s.unlock()
return m
}
func (s *AdvancedMap[TKey, TValue]) IsThreadSafe() bool {
return true
}
func (s *AdvancedMap[TKey, TValue]) IsValid() bool {
return s.Length() > 0
}
//---------------------------------------------------------
func (e *ExpiringValue[T]) SetTime(t time.Time) {
e._t = t
}
func (e *ExpiringValue[T]) GetTime() time.Time {
return e._t
}
func (e *ExpiringValue[T]) Reset() {
e.SetTime(time.Now())
}
func (e *ExpiringValue[T]) IsExpired(duration time.Duration) bool {
return time.Since(e._t) > duration
}
func (e *ExpiringValue[T]) SetValue(value T) {
e._value = value
}
func (e *ExpiringValue[T]) GetValue() T {
e.Reset()
return e._value
}
//---------------------------------------------------------
func (e *EndpointError) Error() string {
return strconv.Itoa(e.ErrorCode) + ": " + e.Message
}
//---------------------------------------------------------
// Next method returns the next id and will move the current cursor to
// the next id and will return that.
// current integer won't be available after this method call.
func (n *NumIdGenerator[T]) Next() T {
n.mut.Lock()
defer n.mut.Unlock()
n.current++
return n.current
}
// Set method will set the current value to the given value.
// please do notice that with calling n.Next() method, you will get
// the next id and not the current value.
func (n *NumIdGenerator[T]) Set(current T) {
n.mut.Lock()
defer n.mut.Unlock()
n.current = current
}
// SafeSet method will set the current value to the given value
// if and only if the passed value is more than current value of
// the generator.
// please do notice that with calling n.Next() method, you will get
// the next id and not the current value.
func (n *NumIdGenerator[T]) SafeSet(current T) {
n.mut.Lock()
defer n.mut.Unlock()
if current > n.current {
n.current = current
}
}
// Reset method will set the current value to 0.
func (n *NumIdGenerator[T]) Reset() {
n.mut.Lock()
defer n.mut.Unlock()
n.current = 0
}
//---------------------------------------------------------