-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathStringViews.swift
852 lines (724 loc) · 25.6 KB
/
StringViews.swift
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
//===--- StringViews.swift ------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// rdar://84233775
// REQUIRES: rdar84233775
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test
import Swift
import StdlibUnittest
import StdlibUnicodeUnittest
import StdlibCollectionUnittest
#if _runtime(_ObjC)
// FIXME: Foundation leaks through StdlibUnittest. It adds some conformances
// that overload resolution picks up in this code.
import Foundation
#endif
// CHECK: testing...
print("testing...")
let replacementUTF16: UTF16.CodeUnit = 0xFFFD
let replacementUTF8: [UTF8.CodeUnit] = [0xEF, 0xBF, 0xBD]
let replacementScalar = UnicodeScalar(replacementUTF16)!
let replacementCharacter = Character(replacementScalar)
// This string contains a variety of non-ASCII characters, including
// Unicode scalars that must be represented with a surrogate pair in
// UTF16, grapheme clusters composed of multiple Unicode scalars, and
// invalid UTF16 that should be replaced with replacement characters.
let winterUTF16 = Array("🏂☃❅❆❄︎⛄️❄️".utf16) + [0xD83C, 0x0020, 0xDF67, 0xD83C]
var winter = winterUTF16.withUnsafeBufferPointer {
String._fromInvalidUTF16($0)
}
let winterInvalidUTF8: [UTF8.CodeUnit] = replacementUTF8 + ([0x20] as [UTF8.CodeUnit]) + replacementUTF8 + replacementUTF8
let winterUTF8: [UTF8.CodeUnit] = [
0xf0, 0x9f, 0x8f, 0x82, 0xe2, 0x98, 0x83, 0xe2, 0x9d, 0x85, 0xe2,
0x9d, 0x86, 0xe2, 0x9d, 0x84, 0xef, 0xb8, 0x8e, 0xe2, 0x9b, 0x84,
0xef, 0xb8, 0x8f, 0xe2, 0x9d, 0x84, 0xef, 0xb8, 0x8f
] + winterInvalidUTF8
let summer = "school's out!"
let summerBytes: [UInt8] = [
0x73, 0x63, 0x68, 0x6f, 0x6f, 0x6c, 0x27, 0x73, 0x20, 0x6f, 0x75, 0x74, 0x21]
var tests = TestSuite("StringViews")
tests.test("decoding") {
expectEqualSequence(
winterUTF8,
winter.utf8
)
expectEqualSequence(
[0xd83c, 0xdfc2, 0x2603, 0x2745, 0x2746, 0x2744, 0xfe0e, 0x26c4,
0xfe0f, 0x2744, 0xfe0f,
replacementUTF16, 0x0020, replacementUTF16, replacementUTF16
],
winter.utf16
)
expectEqualSequence(
summerBytes,
summer.utf8
)
expectEqualSequence(
summerBytes.map {UTF16.CodeUnit($0)},
summer.utf16
)
}
// winter UTF8 grapheme clusters ([]) and unicode scalars (|)
// [f0 9f 8f 82] [e2 98 83] [e2 9d 85] [e2 9d 86] [e2 9d 84 | ef b8 8e]
// [e2 9b 84 | ef b8 8f] [e2 9d 84 | ef b8 8f]
//===--- To UTF8 ----------------------------------------------------------===//
func checkToUTF8(
_ id: String,
mapIndex: @escaping (String.Index, String.UTF8View)->String.Index?
) {
tests.test("index-mapping/character-to-utf8/\(id)") {
// the first three utf8 code units at the start of each grapheme
// cluster
expectEqualSequence(
[
[0xf0, 0x9f, 0x8f],
[0xe2, 0x98, 0x83],
[0xe2, 0x9d, 0x85],
[0xe2, 0x9d, 0x86],
[0xe2, 0x9d, 0x84],
[0xe2, 0x9b, 0x84],
[0xe2, 0x9d, 0x84],
replacementUTF8,
[0x20] + replacementUTF8[0..<2],
replacementUTF8,
replacementUTF8
] as [[UTF8.CodeUnit]],
winter.indices.map {
i in (0..<3).map {
let w = winter
return w.utf8[w.utf8.index(mapIndex(i, w.utf8)!, offsetBy: $0)]
}
}, sameValue: ==)
expectEqual(
winter.utf8.endIndex, mapIndex(winter.endIndex, winter.utf8))
expectEqualSequence(
summerBytes,
summer.indices.map {
summer.utf8[mapIndex($0, summer.utf8)!]
}
)
expectEqual(
summer.utf8.endIndex, mapIndex(summer.endIndex, summer.utf8))
}
tests.test("index-mapping/unicode-scalar-to-utf8/\(id)") {
// the first three utf8 code units at the start of each unicode
// scalar
expectEqualSequence(
[
[0xf0, 0x9f, 0x8f],
[0xe2, 0x98, 0x83],
[0xe2, 0x9d, 0x85],
[0xe2, 0x9d, 0x86],
[0xe2, 0x9d, 0x84],
[0xef, 0xb8, 0x8e],
[0xe2, 0x9b, 0x84],
[0xef, 0xb8, 0x8f],
[0xe2, 0x9d, 0x84],
[0xef, 0xb8, 0x8f],
replacementUTF8,
[0x20] + replacementUTF8[0..<2],
replacementUTF8,
replacementUTF8
] as [[UTF8.CodeUnit]],
winter.unicodeScalars.indices.map {
i in (0..<3).map {
let w = winter
return w.utf8[w.utf8.index(mapIndex(i, w.utf8)!, offsetBy: $0)]
}
}, sameValue: ==)
expectEqual(
winter.utf8.endIndex,
mapIndex(winter.unicodeScalars.endIndex, winter.utf8))
expectEqualSequence(
summerBytes,
summer.unicodeScalars.indices.map {
summer.utf8[mapIndex($0, summer.utf8)!]
}
)
expectEqual(
summer.utf8.endIndex,
mapIndex(summer.unicodeScalars.endIndex, summer.utf8))
}
tests.test("index-mapping/utf16-to-utf8/\(id)") {
// check the first three utf8 code units at the start of each utf16
// code unit
expectEqualSequence(
[
[0xf0, 0x9f, 0x8f],
// Prior to UTF-8 String, this tested for empty array in "legacy mode" or
// the replacement character otherwise. However, SE-0180 (String Index
// Overhaul) dictates subscript behavior should treat it as emergent
// behavior from its encoded offset, hence we should get the same 3 code
// units as prior for non-scalar-aligned UTF-16 offsets applied to the
// UTF-8 view.
//
// Old code:
// // does not align with any utf8 code unit
// id == "legacy" ? [] : replacementUTF8,
id == "legacy" ? [] : [0xf0, 0x9f, 0x8f],
[0xe2, 0x98, 0x83],
[0xe2, 0x9d, 0x85],
[0xe2, 0x9d, 0x86],
[0xe2, 0x9d, 0x84],
[0xef, 0xb8, 0x8e],
[0xe2, 0x9b, 0x84],
[0xef, 0xb8, 0x8f],
[0xe2, 0x9d, 0x84],
[0xef, 0xb8, 0x8f],
replacementUTF8,
[0x20] + replacementUTF8[0..<2],
replacementUTF8,
replacementUTF8
] as [[UTF8.CodeUnit]],
winter.utf16.indices.map {
i16 in mapIndex(i16, winter.utf8).map {
i8 in (0..<3).map {
winter.utf8[winter.utf8.index(i8, offsetBy: $0)]
}
} ?? []
}, sameValue: ==)
expectNotNil(mapIndex(winter.utf16.endIndex, winter.utf8))
expectEqual(
winter.utf8.endIndex,
mapIndex(winter.utf16.endIndex, winter.utf8)!)
expectEqualSequence(
summerBytes,
summer.utf16.indices.map {
summer.utf8[mapIndex($0, summer.utf8)!]
}
)
expectNotNil(mapIndex(summer.utf16.endIndex, summer.utf8))
expectEqual(
summer.utf8.endIndex,
mapIndex(summer.utf16.endIndex, summer.utf8)!)
}
tests.test("index-mapping/utf8-to-utf8/\(id)") {
// should always succeed
for i in winter.utf8.indices {
expectEqual(i, mapIndex(i, winter.utf8)!)
}
}
}
checkToUTF8("legacy") { $0.samePosition(in: $1) }
checkToUTF8("interchange") { i, _ in i }
//===--- To UTF16 ---------------------------------------------------------===//
func checkToUTF16(
_ id: String,
mapIndex: @escaping (String.Index, String.UTF16View)->String.Index?
) {
func err(_ codeUnit: Unicode.UTF16.CodeUnit) -> Unicode.UTF16.CodeUnit? {
return id == "legacy" ? nil : codeUnit
}
tests.test("index-mapping/character-to-utf16/\(id)") {
expectEqualSequence(
[
0xd83c, // 0xdfc2,
0x2603,
0x2745,
0x2746,
0x2744, // 0xfe0e,
0x26c4, // 0xfe0f,
0x2744, // 0xfe0f,
replacementUTF16, 0x20, replacementUTF16, replacementUTF16
] as [UTF16.CodeUnit],
winter.indices.map {
winter.utf16[mapIndex($0, winter.utf16)!]
},
sameValue: ==)
expectEqual(winter.utf16.endIndex, mapIndex(winter.endIndex, winter.utf16))
expectEqualSequence(
summerBytes.map { UTF16.CodeUnit($0) },
summer.indices.map { summer.utf16[mapIndex($0, summer.utf16)!] }
)
expectEqual(summer.utf16.endIndex, mapIndex(summer.endIndex, summer.utf16))
}
tests.test("index-mapping/unicode-scalar-to-utf16/\(id)") {
expectEqualSequence(
[
0xd83c, // 0xdfc2,
0x2603,
0x2745,
0x2746,
0x2744, 0xfe0e,
0x26c4, 0xfe0f,
0x2744, 0xfe0f,
replacementUTF16, 0x20, replacementUTF16, replacementUTF16
] as [UTF16.CodeUnit],
winter.unicodeScalars.indices.map {
winter.utf16[mapIndex($0, winter.utf16)!]
})
expectEqual(
winter.utf16.endIndex,
mapIndex(winter.unicodeScalars.endIndex, winter.utf16))
expectEqualSequence(
summerBytes.map { UTF16.CodeUnit($0) },
summer.unicodeScalars.indices.map {
summer.utf16[mapIndex($0, summer.utf16)!]
}
)
expectEqual(
summer.utf16.endIndex,
mapIndex(summer.unicodeScalars.endIndex, summer.utf16))
}
tests.test("index-mapping/utf8-to-utf16/\(id)") {
expectEqualSequence(
[
0xd83c, err(0xd83c), err(0xd83c), err(0xd83c),
0x2603, err(0x2603), err(0x2603),
0x2745, err(0x2745), err(0x2745),
0x2746, err(0x2746), err(0x2746),
0x2744, err(0x2744), err(0x2744),
0xfe0e, err(0xfe0e), err(0xfe0e),
0x26c4, err(0x26c4), err(0x26c4),
0xfe0f, err(0xfe0f), err(0xfe0f),
0x2744, err(0x2744), err(0x2744),
0xfe0f, err(0xfe0f), err(0xfe0f),
replacementUTF16, err(replacementUTF16), err(replacementUTF16),
0x20,
replacementUTF16, err(replacementUTF16), err(replacementUTF16),
replacementUTF16, err(replacementUTF16), err(replacementUTF16)
] as [UTF16.CodeUnit?],
winter.utf8.indices.map {
mapIndex($0, winter.utf16).map {
winter.utf16[$0]
}
}, sameValue: ==)
expectNotNil(mapIndex(winter.utf8.endIndex, winter.utf16))
expectEqual(
winter.utf16.endIndex,
mapIndex(winter.utf8.endIndex, winter.utf16)!)
expectEqualSequence(
summerBytes.map { UTF16.CodeUnit($0) },
summer.utf8.indices.map { summer.utf16[mapIndex($0, summer.utf16)!] }
)
expectNotNil(mapIndex(summer.utf8.endIndex, summer.utf16))
expectEqual(
summer.utf16.endIndex,
mapIndex(summer.utf8.endIndex, summer.utf16)!)
}
}
checkToUTF16("legacy") { $0.samePosition(in: $1) }
checkToUTF16("interchange") { i, _ in i }
//===--- To UnicodeScalar -------------------------------------------------===//
func checkToUnicodeScalar(
_ id: String,
mapIndex: @escaping (String.Index, String.UnicodeScalarView)->String.Index?
) {
func err(_ scalarValue: UInt32) -> UnicodeScalar? {
return id == "legacy" ? nil : UnicodeScalar(scalarValue)
}
tests.test("index-mapping/character-to-unicode-scalar/\(id)") {
let winterCharacterUnicodeScalars: [UnicodeScalar] = [
UnicodeScalar(0x1f3c2)!,
UnicodeScalar(0x2603)!,
UnicodeScalar(0x2745)!,
UnicodeScalar(0x2746)!,
UnicodeScalar(0x2744)!, // 0xfe0e,
UnicodeScalar(0x26c4)!, // 0xfe0f,
UnicodeScalar(0x2744)!, // 0xfe0f
replacementScalar, UnicodeScalar(0x20)!, replacementScalar, replacementScalar
]
expectEqualSequence(
winterCharacterUnicodeScalars,
winter.indices.map {
let w = winter
return w.unicodeScalars[mapIndex($0, w.unicodeScalars)!]
})
expectEqual(winter.unicodeScalars.endIndex, mapIndex(winter.endIndex, winter.unicodeScalars))
expectEqualSequence(
summerBytes.map { UnicodeScalar($0) },
summer.indices.map { summer.unicodeScalars[mapIndex($0, summer.unicodeScalars)!] }
)
expectEqual(summer.unicodeScalars.endIndex, mapIndex(summer.endIndex, summer.unicodeScalars))
}
tests.test("index-mapping/utf8-to-unicode-scalar/\(id)") {
// Define expectation separately to help the type-checker, which
// otherwise runs out of time solving.
let winterUtf8UnicodeScalars: [UnicodeScalar?] = [
UnicodeScalar(0x1f3c2), err(0x1f3c2), err(0x1f3c2), err(0x1f3c2),
UnicodeScalar(0x2603), err(0x2603), err(0x2603),
UnicodeScalar(0x2745), err(0x2745), err(0x2745),
UnicodeScalar(0x2746), err(0x2746), err(0x2746),
UnicodeScalar(0x2744), err(0x2744), err(0x2744),
UnicodeScalar(0xfe0e), err(0xfe0e), err(0xfe0e),
UnicodeScalar(0x26c4), err(0x26c4), err(0x26c4),
UnicodeScalar(0xfe0f), err(0xfe0f), err(0xfe0f),
UnicodeScalar(0x2744), err(0x2744), err(0x2744),
UnicodeScalar(0xfe0f), err(0xfe0f), err(0xfe0f),
replacementScalar,
err(replacementScalar.value), err(replacementScalar.value),
UnicodeScalar(0x20),
replacementScalar,
err(replacementScalar.value), err(replacementScalar.value),
replacementScalar,
err(replacementScalar.value), err(replacementScalar.value)
]
expectEqualSequence(
winterUtf8UnicodeScalars,
winter.utf8.indices.map {
i in mapIndex(i, winter.unicodeScalars).map {
winter.unicodeScalars[$0]
}
}, sameValue: ==)
expectNotNil(mapIndex(winter.utf8.endIndex, winter.unicodeScalars))
expectEqual(
winter.unicodeScalars.endIndex,
mapIndex(winter.utf8.endIndex, winter.unicodeScalars)!)
expectEqualSequence(
summerBytes.map { UnicodeScalar($0) as UnicodeScalar? },
summer.utf8.indices.map {
i in mapIndex(i, summer.unicodeScalars).map {
summer.unicodeScalars[$0]
}
}, sameValue: ==)
expectNotNil(mapIndex(summer.utf8.endIndex, summer.unicodeScalars))
expectEqual(
summer.unicodeScalars.endIndex,
mapIndex(summer.utf8.endIndex, summer.unicodeScalars)!)
}
tests.test("index-mapping/utf16-to-unicode-scalar/\(id)") {
let winterUtf16UnicodeScalars: [UnicodeScalar?] = [
UnicodeScalar(0x1f3c2),
// Prior to UTF-8 String, this tested for empty array in "legacy mode" or
// the replacement character otherwise. However, SE-0180 (String Index
// Overhaul) dictates subscript behavior should treat it as emergent
// behavior from its encoded offset, hence we should get the same 3 code
// units as prior for non-scalar-aligned UTF-16 offsets applied to the
// UTF-8 view.
//
// Old code:
// err(replacementScalar.value),
err(0x1f3c2),
UnicodeScalar(0x2603),
UnicodeScalar(0x2745),
UnicodeScalar(0x2746),
UnicodeScalar(0x2744), UnicodeScalar(0xfe0e),
UnicodeScalar(0x26c4), UnicodeScalar(0xfe0f),
UnicodeScalar(0x2744), UnicodeScalar(0xfe0f),
replacementScalar, UnicodeScalar(0x20), replacementScalar, replacementScalar
]
expectEqualSequence(
winterUtf16UnicodeScalars,
winter.utf16.indices.map {
i in mapIndex(i, winter.unicodeScalars).map {
winter.unicodeScalars[$0]
}
}, sameValue: ==)
expectNotNil(mapIndex(winter.utf16.endIndex, winter.unicodeScalars))
expectEqual(
winter.unicodeScalars.endIndex,
mapIndex(winter.utf16.endIndex, winter.unicodeScalars)!)
expectEqualSequence(
summerBytes.map { UnicodeScalar($0) as UnicodeScalar? },
summer.utf16.indices.map {
i in mapIndex(i, summer.unicodeScalars).map {
summer.unicodeScalars[$0]
}
}, sameValue: ==)
expectNotNil(mapIndex(summer.utf16.endIndex, summer.unicodeScalars))
expectEqual(
summer.unicodeScalars.endIndex,
mapIndex(summer.utf16.endIndex, summer.unicodeScalars)!)
}
}
checkToUnicodeScalar("legacy") { $0.samePosition(in: $1) }
checkToUnicodeScalar("interchange") { i, _ in i }
//===--- To Character -----------------------------------------------------===//
func checkToCharacter(
_ id: String,
mapIndex: @escaping (String.Index, String)->String.Index?
) {
func err(_ c: Character) -> Character? {
return id == "legacy" ? nil : c
}
tests.test("index-mapping/unicode-scalar-to-character/\(id)") {
let winterUnicodeScalarCharacters: [Character?] = [
"🏂", "☃", "❅", "❆", "❄︎", err("\u{FE0E}"), "⛄️", err("\u{FE0F}"),
"❄️", err("\u{FE0F}"),
replacementCharacter, "\u{20}", replacementCharacter, replacementCharacter
]
expectEqualSequence(
winterUnicodeScalarCharacters,
winter.unicodeScalars.indices.map {
i in mapIndex(i, winter).map {
winter[$0]
}
}, sameValue: ==)
expectEqual(winter.endIndex, mapIndex(winter.unicodeScalars.endIndex, winter)!)
expectEqualSequence(
summerBytes.map { Character(UnicodeScalar($0)) },
summer.unicodeScalars.indices.map { summer[mapIndex($0, summer)!] }
)
expectEqual(summer.endIndex, mapIndex(summer.unicodeScalars.endIndex, summer)!)
}
tests.test("index-mapping/utf8-to-character/\(id)") {
// Define expectation separately to help the type-checker, which
// otherwise runs out of time solving.
let winterUtf8Characters: [Character?] = [
"🏂", err("🏂"), err("🏂"), err("🏂"),
"☃", err("☃"), err("☃"),
"❅", err("❅"), err("❅"),
"❆", err("❆"), err("❆"),
"❄︎", err("❄︎"), err("❄︎"),
err("\u{fe0e}"), err("\u{fe0e}"), err("\u{fe0e}"),
"⛄️", err("⛄️"), err("⛄️"),
err("\u{fe0f}"), err("\u{fe0f}"), err("\u{fe0f}"),
"❄️", err("❄️"), err("❄️"),
err("\u{fe0f}"), err("\u{fe0f}"), err("\u{fe0f}"),
replacementCharacter, err(replacementCharacter), err(replacementCharacter),
"\u{20}",
replacementCharacter, err(replacementCharacter), err(replacementCharacter),
replacementCharacter, err(replacementCharacter), err(replacementCharacter),
]
expectEqualSequence(
winterUtf8Characters,
winter.utf8.indices.map {
(i:String.Index) -> Character? in mapIndex(i, winter).map {
winter[$0]
}
}, sameValue: ==)
expectNotNil(mapIndex(winter.utf8.endIndex, winter))
expectEqual(
winter.endIndex,
mapIndex(winter.utf8.endIndex, winter)!)
expectEqualSequence(
summerBytes.map { Character(UnicodeScalar($0)) },
summer.utf8.indices.map { summer[mapIndex($0, summer)!] }
)
expectNotNil(mapIndex(summer.utf8.endIndex, summer))
expectEqual(
summer.endIndex,
mapIndex(summer.utf8.endIndex, summer)!)
}
tests.test("index-mapping/utf16-to-character/\(id)") {
let winterUtf16Characters: [Character?] = [
"🏂",
// Prior to UTF-8 String, this tested for empty array in "legacy mode" or
// the replacement character otherwise. However, SE-0180 (String Index
// Overhaul) dictates subscript behavior should treat it as emergent
// behavior from its encoded offset, hence we should get the same 3 code
// units as prior for non-scalar-aligned UTF-16 offsets applied to the
// UTF-8 view. Under a mixed-encoding String model, we necessarily have to
// clamp all indices to the nearest prior scalar boundary...
//
// Old code:
// err(replacementCharacter),
err("🏂"),
"☃", "❅", "❆", "❄︎", err("\u{fe0e}"),
"⛄️", err("\u{fe0f}"), "❄️", err("\u{fe0f}"),
replacementCharacter, "\u{20}", replacementCharacter, replacementCharacter
]
expectEqualSequence(
winterUtf16Characters,
winter.utf16.indices.map {
i in mapIndex(i, winter).map {
winter[$0]
}
}, sameValue: ==)
expectNotNil(mapIndex(winter.utf16.endIndex, winter))
expectEqual(
winter.endIndex,
mapIndex(winter.utf16.endIndex, winter)!)
expectEqualSequence(
summerBytes.map { Character(UnicodeScalar($0)) },
summer.utf16.indices.map {
summer[mapIndex($0, summer)!]
}
)
expectNotNil(mapIndex(summer.utf16.endIndex, summer))
expectEqual(
summer.endIndex,
mapIndex(summer.utf16.endIndex, summer)!)
}
}
checkToCharacter("legacy") { $0.samePosition(in: $1) }
checkToCharacter("interchange") { i, _ in i }
//===----------------------------------------------------------------------===//
// These are rather complicated due to their internal buffers, so
// rigorous tests are required
tests.test("UTF8 indexes") {
// Make sure that equivalent UTF8 indices computed in different ways
// are still equal.
//
// CHECK-NEXT: true
let abc = "abcdefghijklmnop"
do {
let start = String.Index(abc.startIndex, within: abc.utf8)
expectEqual(
abc.utf8.index(after: start!),
String.Index(abc.index(after: abc.startIndex), within: abc.utf8))
}
let diverseCharacters = summer + winter + winter + summer
let s = diverseCharacters.unicodeScalars
let u8 = diverseCharacters.utf8
let u16 = diverseCharacters.utf16
//===--- nested for...in loops ------------------------------------------===//
// Test all valid subranges si0..<si1 of positions in s. ds is
// always si0.distance(to: si1)
for si0 in s.indices {
for (ds, si1) in s.indices[si0..<s.endIndex].enumerated() {
// Map those unicode scalar indices into utf8 indices
let u8i1 = si1.samePosition(in: u8)
let u8i0 = si0.samePosition(in: u8)
//===--- while loop -------------------------------------------------===//
// Advance an index from u8i0 over ds Unicode scalars (thus
// reaching u8i1) by counting leading bytes traversed
var u8i0a = u8i0! // <========== NOTE SOURCE COMPATIBILITY BREAKAGE
var dsa = 0 // number of Unicode scalars it has advanced over
while true {
//===--- loop condition -------------------------------------------===//
let b = u8[u8i0a]
let isLeadingByte = !UTF8.isContinuation(b)
if dsa == ds && isLeadingByte { break } //
//===--------------------------------------------------------------===//
expectNotEqual(u8i0a, u8i1) // We're not there yet
if isLeadingByte { // On a unicode scalar boundary?
let u16i0a = u8i0a.samePosition(in: u16)!
// we should be able to round-trip through UTF16
expectEqual(u8i0a, u16i0a.samePosition(in: u8)!)
if UTF16.isLeadSurrogate(u16[u16i0a]) {
// We only have well-formed UTF16 in this string, so the
// successor points to a trailing surrogate of a pair and
// thus shouldn't convert to a UTF8 position
expectNil(u16.index(after: u16i0a).samePosition(in: u8))
}
dsa = dsa.advanced(by: 1) // we're moving off the beginning of a new Unicode scalar
}
else {
expectNil(u8i0a.samePosition(in: u16))
}
u8i0a = u8.index(u8i0a, offsetBy: 1)
}
expectEqual(u8i0a, u8i1) // We should be there now
// Also check some UTF8 positions between unicode scalars for equality
var u8i0b = u8i0a
for n0 in 0..<8 {
var u8i1b = u8i1
for n1 in 0..<8 {
expectEqualTest(u8i0b, u8i1b, sameValue: n0 == n1 ? (==) : (!=))
if u8i1b == u8.endIndex { break }
u8i1b = u8.index(u8i1b!, offsetBy: 1)
}
if u8i0b == u8.endIndex { break }
u8i0b = u8.index(u8i0b, offsetBy: 1)
}
}
}
}
tests.test("index/Comparable")
.forEach(in: [summer, winter]) { str in
checkComparable(str.indices, oracle: <=>)
checkComparable(str.unicodeScalars.indices, oracle: <=>)
checkComparable(str.utf16.indices, oracle: <=>)
checkComparable(str.utf8.indices, oracle: <=>)
}
tests.test("UTF16->String") {
let s = summer + winter + winter + summer
let v = s.utf16
for i in v.indices {
for j in v.indices[i..<v.endIndex] {
if let si = i.samePosition(in: s) {
if let sj = j.samePosition(in: s) {
expectEqual(s[si..<sj], String(v[i..<j])!)
continue
}
}
}
}
}
tests.test("UTF8->String") {
let s = summer + winter + winter + summer
let v = s.utf8
for i in v.indices {
for j in v.indices[i..<v.endIndex] {
if let si = i.samePosition(in: s) {
if let sj = j.samePosition(in: s) {
expectEqual(
s[si..<sj], String(v[i..<j])!,
"\(String(reflecting: s))[\n \(si..<sj)\n] != String(\n \(String(reflecting: v))[\n \(i..<j)\n ])!"
)
continue
}
}
}
}
}
tests.test("UnicodeScalars->String") {
let s = summer + winter + winter + summer
let v = s.unicodeScalars
for i in s.indices {
for j in s.indices[i..<s.endIndex] {
expectEqual(
s[i..<j],
String(v[i.samePosition(in: v)!..<j.samePosition(in: v)!])
)
}
}
}
// Note: Strideable conformance for UTF16View.Index when Foundation is imported
// has been dropped for Swift 4.
tests.test("String.UTF8View/Collection")
.forEach(in: utfTests) {
test in
checkBidirectionalCollection(test.utf8, test.string.utf8)
}
tests.test("String.UTF16View/BidirectionalCollection")
.forEach(in: utfTests) {
test in
checkBidirectionalCollection(test.utf16, test.string.utf16)
}
tests.test("String.UTF32View/BidirectionalCollection")
.forEach(in: utfTests) {
test in
checkBidirectionalCollection(
test.unicodeScalars, test.string.unicodeScalars)
}
tests.test("String View Setters") {
var string = "abcd🤠👨👨👦👦efg"
string.utf8 = winter.utf8
expectEqual(winter, string)
string.utf8 = summer.utf8
expectEqual(summer, string)
string.utf16 = winter.utf16
expectEqual(winter, string)
string.utf16 = summer.utf16
expectEqual(summer, string)
string.unicodeScalars = winter.unicodeScalars
expectEqual(winter, string)
string.unicodeScalars = summer.unicodeScalars
expectEqual(summer, string)
string = winter
expectEqual(winter, string)
string = summer
expectEqual(summer, string)
}
tests.test("Scalar alignment") {
// Misaligned indices were fixed in 5.1
guard _hasSwift_5_1() else { return }
let str = "😀"
let idx = str.utf8.index(after: str.startIndex)
let substr = str[idx...]
expectEqual(str, substr)
checkBidirectionalCollection(str, substr)
checkBidirectionalCollection(str.utf16, substr.utf16)
checkBidirectionalCollection(str.utf8, substr.utf8)
checkBidirectionalCollection(str.unicodeScalars, substr.unicodeScalars)
let idxBeforeLast = str.utf8.index(before: str.endIndex)
let substr2 = str[idx...]
expectEqual(str, substr2)
expectEqual(substr, substr2)
checkBidirectionalCollection(str, substr2)
checkBidirectionalCollection(str.utf16, substr2.utf16)
checkBidirectionalCollection(str.utf8, substr2.utf8)
checkBidirectionalCollection(str.unicodeScalars, substr2.unicodeScalars)
}
runAllTests()