-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy path_Strings.kt
2485 lines (2311 loc) · 87.3 KB
/
_Strings.kt
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
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("StringsKt")
package kotlin.text
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.random.*
/**
* Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public expect fun CharSequence.elementAt(index: Int): Char
/**
* Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
*
* @sample samples.collections.Collections.Elements.elementAtOrElse
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char {
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
/**
* Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence.
*
* @sample samples.collections.Collections.Elements.elementAtOrNull
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.elementAtOrNull(index: Int): Char? {
return this.getOrNull(index)
}
/**
* Returns the first character matching the given [predicate], or `null` if no such character was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? {
return firstOrNull(predicate)
}
/**
* Returns the last character matching the given [predicate], or `null` if no such character was found.
*
* @sample samples.collections.Collections.Elements.find
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? {
return lastOrNull(predicate)
}
/**
* Returns the first character.
*
* @throws NoSuchElementException if the char sequence is empty.
*/
public fun CharSequence.first(): Char {
if (isEmpty())
throw NoSuchElementException("Char sequence is empty.")
return this[0]
}
/**
* Returns the first character matching the given [predicate].
* @throws [NoSuchElementException] if no such character is found.
*/
public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("Char sequence contains no character matching the predicate.")
}
/**
* Returns the first non-null value produced by [transform] function being applied to characters of this char sequence in iteration order,
* or throws [NoSuchElementException] if no non-null value was produced.
*
* @sample samples.collections.Collections.Transformations.firstNotNullOf
*/
@SinceKotlin("1.5")
@kotlin.internal.InlineOnly
public inline fun <R : Any> CharSequence.firstNotNullOf(transform: (Char) -> R?): R {
return firstNotNullOfOrNull(transform) ?: throw NoSuchElementException("No element of the char sequence was transformed to a non-null value.")
}
/**
* Returns the first non-null value produced by [transform] function being applied to characters of this char sequence in iteration order,
* or `null` if no non-null value was produced.
*
* @sample samples.collections.Collections.Transformations.firstNotNullOf
*/
@SinceKotlin("1.5")
@kotlin.internal.InlineOnly
public inline fun <R : Any> CharSequence.firstNotNullOfOrNull(transform: (Char) -> R?): R? {
for (element in this) {
val result = transform(element)
if (result != null) {
return result
}
}
return null
}
/**
* Returns the first character, or `null` if the char sequence is empty.
*/
public fun CharSequence.firstOrNull(): Char? {
return if (isEmpty()) null else this[0]
}
/**
* Returns the first character matching the given [predicate], or `null` if character was not found.
*/
public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char? {
for (element in this) if (predicate(element)) return element
return null
}
/**
* Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
/**
* Returns a character at the given [index] or `null` if the [index] is out of bounds of this char sequence.
*
* @sample samples.collections.Collections.Elements.getOrNull
*/
public fun CharSequence.getOrNull(index: Int): Char? {
return if (index >= 0 && index <= lastIndex) get(index) else null
}
/**
* Returns index of the first character matching the given [predicate], or -1 if the char sequence does not contain such character.
*/
public inline fun CharSequence.indexOfFirst(predicate: (Char) -> Boolean): Int {
for (index in indices) {
if (predicate(this[index])) {
return index
}
}
return -1
}
/**
* Returns index of the last character matching the given [predicate], or -1 if the char sequence does not contain such character.
*/
public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int {
for (index in indices.reversed()) {
if (predicate(this[index])) {
return index
}
}
return -1
}
/**
* Returns the last character.
*
* @throws NoSuchElementException if the char sequence is empty.
*
* @sample samples.text.Strings.last
*/
public fun CharSequence.last(): Char {
if (isEmpty())
throw NoSuchElementException("Char sequence is empty.")
return this[lastIndex]
}
/**
* Returns the last character matching the given [predicate].
*
* @throws NoSuchElementException if no such character is found.
*
* @sample samples.text.Strings.last
*/
public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
for (index in this.indices.reversed()) {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Char sequence contains no character matching the predicate.")
}
/**
* Returns the last character, or `null` if the char sequence is empty.
*
* @sample samples.text.Strings.last
*/
public fun CharSequence.lastOrNull(): Char? {
return if (isEmpty()) null else this[length - 1]
}
/**
* Returns the last character matching the given [predicate], or `null` if no such character was found.
*
* @sample samples.text.Strings.last
*/
public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? {
for (index in this.indices.reversed()) {
val element = this[index]
if (predicate(element)) return element
}
return null
}
/**
* Returns a random character from this char sequence.
*
* @throws NoSuchElementException if this char sequence is empty.
*/
@SinceKotlin("1.3")
@kotlin.internal.InlineOnly
public inline fun CharSequence.random(): Char {
return random(Random)
}
/**
* Returns a random character from this char sequence using the specified source of randomness.
*
* @throws NoSuchElementException if this char sequence is empty.
*/
@SinceKotlin("1.3")
public fun CharSequence.random(random: Random): Char {
if (isEmpty())
throw NoSuchElementException("Char sequence is empty.")
return get(random.nextInt(length))
}
/**
* Returns a random character from this char sequence, or `null` if this char sequence is empty.
*/
@SinceKotlin("1.4")
@WasExperimental(ExperimentalStdlibApi::class)
@kotlin.internal.InlineOnly
public inline fun CharSequence.randomOrNull(): Char? {
return randomOrNull(Random)
}
/**
* Returns a random character from this char sequence using the specified source of randomness, or `null` if this char sequence is empty.
*/
@SinceKotlin("1.4")
@WasExperimental(ExperimentalStdlibApi::class)
public fun CharSequence.randomOrNull(random: Random): Char? {
if (isEmpty())
return null
return get(random.nextInt(length))
}
/**
* Returns the single character, or throws an exception if the char sequence is empty or has more than one character.
*/
public fun CharSequence.single(): Char {
return when (length) {
0 -> throw NoSuchElementException("Char sequence is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Char sequence has more than one element.")
}
}
/**
* Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character.
*/
public inline fun CharSequence.single(predicate: (Char) -> Boolean): Char {
var single: Char? = null
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Char sequence contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Char sequence contains no character matching the predicate.")
@Suppress("UNCHECKED_CAST")
return single as Char
}
/**
* Returns single character, or `null` if the char sequence is empty or has more than one character.
*/
public fun CharSequence.singleOrNull(): Char? {
return if (length == 1) this[0] else null
}
/**
* Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found.
*/
public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? {
var single: Char? = null
var found = false
for (element in this) {
if (predicate(element)) {
if (found) return null
single = element
found = true
}
}
if (!found) return null
return single
}
/**
* Returns a subsequence of this char sequence with the first [n] characters removed.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.drop
*/
public fun CharSequence.drop(n: Int): CharSequence {
require(n >= 0) { "Requested character count $n is less than zero." }
return subSequence(n.coerceAtMost(length), length)
}
/**
* Returns a string with the first [n] characters removed.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.drop
*/
public fun String.drop(n: Int): String {
require(n >= 0) { "Requested character count $n is less than zero." }
return substring(n.coerceAtMost(length))
}
/**
* Returns a subsequence of this char sequence with the last [n] characters removed.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.drop
*/
public fun CharSequence.dropLast(n: Int): CharSequence {
require(n >= 0) { "Requested character count $n is less than zero." }
return take((length - n).coerceAtLeast(0))
}
/**
* Returns a string with the last [n] characters removed.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.drop
*/
public fun String.dropLast(n: Int): String {
require(n >= 0) { "Requested character count $n is less than zero." }
return take((length - n).coerceAtLeast(0))
}
/**
* Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.drop
*/
public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in lastIndex downTo 0)
if (!predicate(this[index]))
return subSequence(0, index + 1)
return ""
}
/**
* Returns a string containing all characters except last characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.drop
*/
public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String {
for (index in lastIndex downTo 0)
if (!predicate(this[index]))
return substring(0, index + 1)
return ""
}
/**
* Returns a subsequence of this char sequence containing all characters except first characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.drop
*/
public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in this.indices)
if (!predicate(this[index]))
return subSequence(index, length)
return ""
}
/**
* Returns a string containing all characters except first characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.drop
*/
public inline fun String.dropWhile(predicate: (Char) -> Boolean): String {
for (index in this.indices)
if (!predicate(this[index]))
return substring(index)
return ""
}
/**
* Returns a char sequence containing only those characters from the original char sequence that match the given [predicate].
*
* @sample samples.text.Strings.filter
*/
public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence {
return filterTo(StringBuilder(), predicate)
}
/**
* Returns a string containing only those characters from the original string that match the given [predicate].
*
* @sample samples.text.Strings.filter
*/
public inline fun String.filter(predicate: (Char) -> Boolean): String {
return filterTo(StringBuilder(), predicate).toString()
}
/**
* Returns a char sequence containing only those characters from the original char sequence that match the given [predicate].
* @param [predicate] function that takes the index of a character and the character itself
* and returns the result of predicate evaluation on the character.
*
* @sample samples.collections.Collections.Filtering.filterIndexed
*/
public inline fun CharSequence.filterIndexed(predicate: (index: Int, Char) -> Boolean): CharSequence {
return filterIndexedTo(StringBuilder(), predicate)
}
/**
* Returns a string containing only those characters from the original string that match the given [predicate].
* @param [predicate] function that takes the index of a character and the character itself
* and returns the result of predicate evaluation on the character.
*
* @sample samples.collections.Collections.Filtering.filterIndexed
*/
public inline fun String.filterIndexed(predicate: (index: Int, Char) -> Boolean): String {
return filterIndexedTo(StringBuilder(), predicate).toString()
}
/**
* Appends all characters matching the given [predicate] to the given [destination].
* @param [predicate] function that takes the index of a character and the character itself
* and returns the result of predicate evaluation on the character.
*
* @sample samples.collections.Collections.Filtering.filterIndexedTo
*/
public inline fun <C : Appendable> CharSequence.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C {
forEachIndexed { index, element ->
if (predicate(index, element)) destination.append(element)
}
return destination
}
/**
* Returns a char sequence containing only those characters from the original char sequence that do not match the given [predicate].
*
* @sample samples.text.Strings.filterNot
*/
public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence {
return filterNotTo(StringBuilder(), predicate)
}
/**
* Returns a string containing only those characters from the original string that do not match the given [predicate].
*
* @sample samples.text.Strings.filterNot
*/
public inline fun String.filterNot(predicate: (Char) -> Boolean): String {
return filterNotTo(StringBuilder(), predicate).toString()
}
/**
* Appends all characters not matching the given [predicate] to the given [destination].
*
* @sample samples.collections.Collections.Filtering.filterTo
*/
public inline fun <C : Appendable> CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.append(element)
return destination
}
/**
* Appends all characters matching the given [predicate] to the given [destination].
*
* @sample samples.collections.Collections.Filtering.filterTo
*/
public inline fun <C : Appendable> CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C {
for (index in 0 until length) {
val element = get(index)
if (predicate(element)) destination.append(element)
}
return destination
}
/**
* Returns a char sequence containing characters of the original char sequence at the specified range of [indices].
*/
public fun CharSequence.slice(indices: IntRange): CharSequence {
if (indices.isEmpty()) return ""
return subSequence(indices)
}
/**
* Returns a string containing characters of the original string at the specified range of [indices].
*/
public fun String.slice(indices: IntRange): String {
if (indices.isEmpty()) return ""
return substring(indices)
}
/**
* Returns a char sequence containing characters of the original char sequence at specified [indices].
*/
public fun CharSequence.slice(indices: Iterable<Int>): CharSequence {
val size = indices.collectionSizeOrDefault(10)
if (size == 0) return ""
val result = StringBuilder(size)
for (i in indices) {
result.append(get(i))
}
return result
}
/**
* Returns a string containing characters of the original string at specified [indices].
*/
@kotlin.internal.InlineOnly
public inline fun String.slice(indices: Iterable<Int>): String {
return (this as CharSequence).slice(indices).toString()
}
/**
* Returns a subsequence of this char sequence containing the first [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.take
*/
public fun CharSequence.take(n: Int): CharSequence {
require(n >= 0) { "Requested character count $n is less than zero." }
return subSequence(0, n.coerceAtMost(length))
}
/**
* Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.take
*/
public fun String.take(n: Int): String {
require(n >= 0) { "Requested character count $n is less than zero." }
return substring(0, n.coerceAtMost(length))
}
/**
* Returns a subsequence of this char sequence containing the last [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.take
*/
public fun CharSequence.takeLast(n: Int): CharSequence {
require(n >= 0) { "Requested character count $n is less than zero." }
val length = length
return subSequence(length - n.coerceAtMost(length), length)
}
/**
* Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter.
*
* @throws IllegalArgumentException if [n] is negative.
*
* @sample samples.text.Strings.take
*/
public fun String.takeLast(n: Int): String {
require(n >= 0) { "Requested character count $n is less than zero." }
val length = length
return substring(length - n.coerceAtMost(length))
}
/**
* Returns a subsequence of this char sequence containing last characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.take
*/
public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return subSequence(index + 1, length)
}
}
return subSequence(0, length)
}
/**
* Returns a string containing last characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.take
*/
public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String {
for (index in lastIndex downTo 0) {
if (!predicate(this[index])) {
return substring(index + 1)
}
}
return this
}
/**
* Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.take
*/
public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in 0 until length)
if (!predicate(get(index))) {
return subSequence(0, index)
}
return subSequence(0, length)
}
/**
* Returns a string containing the first characters that satisfy the given [predicate].
*
* @sample samples.text.Strings.take
*/
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0 until length)
if (!predicate(get(index))) {
return substring(0, index)
}
return this
}
/**
* Returns a char sequence with characters in reversed order.
*/
public fun CharSequence.reversed(): CharSequence {
return StringBuilder(this).reverse()
}
/**
* Returns a string with characters in reversed order.
*/
@kotlin.internal.InlineOnly
public inline fun String.reversed(): String {
return (this as CharSequence).reversed().toString()
}
/**
* Returns a [Map] containing key-value pairs provided by [transform] function
* applied to characters of the given char sequence.
*
* If any of two pairs would have the same key the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original char sequence.
*
* @sample samples.text.Strings.associate
*/
public inline fun <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
val capacity = mapCapacity(length).coerceAtLeast(16)
return associateTo(LinkedHashMap<K, V>(capacity), transform)
}
/**
* Returns a [Map] containing the characters from the given char sequence indexed by the key
* returned from [keySelector] function applied to each character.
*
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original char sequence.
*
* @sample samples.text.Strings.associateBy
*/
public inline fun <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K, Char> {
val capacity = mapCapacity(length).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, Char>(capacity), keySelector)
}
/**
* Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to characters of the given char sequence.
*
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original char sequence.
*
* @sample samples.text.Strings.associateByWithValueTransform
*/
public inline fun <K, V> CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
val capacity = mapCapacity(length).coerceAtLeast(16)
return associateByTo(LinkedHashMap<K, V>(capacity), keySelector, valueTransform)
}
/**
* Populates and returns the [destination] mutable map with key-value pairs,
* where key is provided by the [keySelector] function applied to each character of the given char sequence
* and value is the character itself.
*
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
*
* @sample samples.text.Strings.associateByTo
*/
public inline fun <K, M : MutableMap<in K, in Char>> CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M {
for (element in this) {
destination.put(keySelector(element), element)
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs,
* where key is provided by the [keySelector] function and
* and value is provided by the [valueTransform] function applied to characters of the given char sequence.
*
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
*
* @sample samples.text.Strings.associateByToWithValueTransform
*/
public inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M {
for (element in this) {
destination.put(keySelector(element), valueTransform(element))
}
return destination
}
/**
* Populates and returns the [destination] mutable map with key-value pairs
* provided by [transform] function applied to each character of the given char sequence.
*
* If any of two pairs would have the same key the last one gets added to the map.
*
* @sample samples.text.Strings.associateTo
*/
public inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateTo(destination: M, transform: (Char) -> Pair<K, V>): M {
for (element in this) {
destination += transform(element)
}
return destination
}
/**
* Returns a [Map] where keys are characters from the given char sequence and values are
* produced by the [valueSelector] function applied to each character.
*
* If any two characters are equal, the last one gets added to the map.
*
* The returned map preserves the entry iteration order of the original char sequence.
*
* @sample samples.text.Strings.associateWith
*/
@SinceKotlin("1.3")
public inline fun <V> CharSequence.associateWith(valueSelector: (Char) -> V): Map<Char, V> {
val result = LinkedHashMap<Char, V>(mapCapacity(length.coerceAtMost(128)).coerceAtLeast(16))
return associateWithTo(result, valueSelector)
}
/**
* Populates and returns the [destination] mutable map with key-value pairs for each character of the given char sequence,
* where key is the character itself and value is provided by the [valueSelector] function applied to that key.
*
* If any two characters are equal, the last one overwrites the former value in the map.
*
* @sample samples.text.Strings.associateWithTo
*/
@SinceKotlin("1.3")
public inline fun <V, M : MutableMap<in Char, in V>> CharSequence.associateWithTo(destination: M, valueSelector: (Char) -> V): M {
for (element in this) {
destination.put(element, valueSelector(element))
}
return destination
}
/**
* Appends all characters to the given [destination] collection.
*/
public fun <C : MutableCollection<in Char>> CharSequence.toCollection(destination: C): C {
for (item in this) {
destination.add(item)
}
return destination
}
/**
* Returns a new [HashSet] of all characters.
*/
public fun CharSequence.toHashSet(): HashSet<Char> {
return toCollection(HashSet<Char>(mapCapacity(length.coerceAtMost(128))))
}
/**
* Returns a [List] containing all characters.
*/
public fun CharSequence.toList(): List<Char> {
return when (length) {
0 -> emptyList()
1 -> listOf(this[0])
else -> this.toMutableList()
}
}
/**
* Returns a new [MutableList] filled with all characters of this char sequence.
*/
public fun CharSequence.toMutableList(): MutableList<Char> {
return toCollection(ArrayList<Char>(length))
}
/**
* Returns a [Set] of all characters.
*
* The returned set preserves the element iteration order of the original char sequence.
*/
public fun CharSequence.toSet(): Set<Char> {
return when (length) {
0 -> emptySet()
1 -> setOf(this[0])
else -> toCollection(LinkedHashSet<Char>(mapCapacity(length.coerceAtMost(128))))
}
}
/**
* Returns a single list of all elements yielded from results of [transform] function being invoked on each character of original char sequence.
*
* @sample samples.collections.Collections.Transformations.flatMap
*/
public inline fun <R> CharSequence.flatMap(transform: (Char) -> Iterable<R>): List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns a single list of all elements yielded from results of [transform] function being invoked on each character
* and its index in the original char sequence.
*
* @sample samples.collections.Collections.Transformations.flatMapIndexed
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.jvm.JvmName("flatMapIndexedIterable")
@kotlin.internal.InlineOnly
public inline fun <R> CharSequence.flatMapIndexed(transform: (index: Int, Char) -> Iterable<R>): List<R> {
return flatMapIndexedTo(ArrayList<R>(), transform)
}
/**
* Appends all elements yielded from results of [transform] function being invoked on each character
* and its index in the original char sequence, to the given [destination].
*/
@SinceKotlin("1.4")
@OptIn(kotlin.experimental.ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
@kotlin.jvm.JvmName("flatMapIndexedIterableTo")
@kotlin.internal.InlineOnly
public inline fun <R, C : MutableCollection<in R>> CharSequence.flatMapIndexedTo(destination: C, transform: (index: Int, Char) -> Iterable<R>): C {
var index = 0
for (element in this) {
val list = transform(index++, element)
destination.addAll(list)
}
return destination
}
/**
* Appends all elements yielded from results of [transform] function being invoked on each character of original char sequence, to the given [destination].
*/
public inline fun <R, C : MutableCollection<in R>> CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable<R>): C {
for (element in this) {
val list = transform(element)
destination.addAll(list)
}
return destination
}
/**
* Groups characters of the original char sequence by the key returned by the given [keySelector] function
* applied to each character and returns a map where each group key is associated with a list of corresponding characters.
*
* The returned map preserves the entry iteration order of the keys produced from the original char sequence.
*
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K> CharSequence.groupBy(keySelector: (Char) -> K): Map<K, List<Char>> {
return groupByTo(LinkedHashMap<K, MutableList<Char>>(), keySelector)
}
/**
* Groups values returned by the [valueTransform] function applied to each character of the original char sequence
* by the key returned by the given [keySelector] function applied to the character
* and returns a map where each group key is associated with a list of corresponding values.
*
* The returned map preserves the entry iteration order of the keys produced from the original char sequence.
*
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V> CharSequence.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, List<V>> {
return groupByTo(LinkedHashMap<K, MutableList<V>>(), keySelector, valueTransform)
}
/**
* Groups characters of the original char sequence by the key returned by the given [keySelector] function
* applied to each character and puts to the [destination] map each group key associated with a list of corresponding characters.
*
* @return The [destination] map.
*
* @sample samples.collections.Collections.Transformations.groupBy
*/
public inline fun <K, M : MutableMap<in K, MutableList<Char>>> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K): M {
for (element in this) {
val key = keySelector(element)
val list = destination.getOrPut(key) { ArrayList<Char>() }
list.add(element)
}
return destination
}
/**
* Groups values returned by the [valueTransform] function applied to each character of the original char sequence
* by the key returned by the given [keySelector] function applied to the character
* and puts to the [destination] map each group key associated with a list of corresponding values.
*
* @return The [destination] map.
*
* @sample samples.collections.Collections.Transformations.groupByKeysAndValues
*/
public inline fun <K, V, M : MutableMap<in K, MutableList<V>>> CharSequence.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M {
for (element in this) {
val key = keySelector(element)
val list = destination.getOrPut(key) { ArrayList<V>() }
list.add(valueTransform(element))
}
return destination
}
/**
* Creates a [Grouping] source from a char sequence to be used later with one of group-and-fold operations
* using the specified [keySelector] function to extract a key from each character.
*
* @sample samples.collections.Grouping.groupingByEachCount
*/
@SinceKotlin("1.1")
public inline fun <K> CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping<Char, K> {
return object : Grouping<Char, K> {
override fun sourceIterator(): Iterator<Char> = [email protected]()
override fun keyOf(element: Char): K = keySelector(element)
}
}
/**
* Returns a list containing the results of applying the given [transform] function
* to each character in the original char sequence.
*
* @sample samples.text.Strings.map
*/
public inline fun <R> CharSequence.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(length), transform)
}
/**
* Returns a list containing the results of applying the given [transform] function
* to each character and its index in the original char sequence.
* @param [transform] function that takes the index of a character and the character itself
* and returns the result of the transform applied to the character.
*/
public inline fun <R> CharSequence.mapIndexed(transform: (index: Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(length), transform)
}
/**
* Returns a list containing only the non-null results of applying the given [transform] function
* to each character and its index in the original char sequence.
* @param [transform] function that takes the index of a character and the character itself
* and returns the result of the transform applied to the character.
*/
public inline fun <R : Any> CharSequence.mapIndexedNotNull(transform: (index: Int, Char) -> R?): List<R> {
return mapIndexedNotNullTo(ArrayList<R>(), transform)
}
/**
* Applies the given [transform] function to each character and its index in the original char sequence
* and appends only the non-null results to the given [destination].
* @param [transform] function that takes the index of a character and the character itself
* and returns the result of the transform applied to the character.
*/
public inline fun <R : Any, C : MutableCollection<in R>> CharSequence.mapIndexedNotNullTo(destination: C, transform: (index: Int, Char) -> R?): C {
forEachIndexed { index, element -> transform(index, element)?.let { destination.add(it) } }