@@ -57,22 +57,33 @@ if #available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
57
57
}
58
58
expectEqual ( expected, actual)
59
59
}
60
-
60
+
61
61
let validUTF8 : [ UInt8 ] = [ 0x43 , 0x61 , 0x66 , 0xC3 , 0xA9 ]
62
62
let invalidUTF8 : [ UInt8 ] = [ 0x43 , 0x61 , 0x66 , 0xC3 ]
63
-
64
- let cafe1 = String ( unsafeUninitializedCapacity: validUTF8. count) {
65
- _ = $0. initialize ( from: validUTF8)
66
- return validUTF8. count
67
- }
68
- expectEqual ( " Café " , cafe1)
69
-
70
- let cafe2 = String ( unsafeUninitializedCapacity: invalidUTF8. count) {
71
- _ = $0. initialize ( from: invalidUTF8)
72
- return invalidUTF8. count
63
+ let longerValidUTF8 : [ UInt8 ] = [ 0x21 , 0x21 , 0x21 , 0x21 , 0x21 , 0x21 , 0x21 , 0x21 , 0x21 , 0x21 , 0x43 , 0x61 , 0x66 , 0xC3 , 0xA9 ]
64
+ let longerInvalidUTF8 : [ UInt8 ] = [ 0x21 , 0x21 , 0x43 , 0x61 , 0x66 , 0xC3 , 0x43 , 0x61 , 0x66 , 0xC3 , 0x43 , 0x61 , 0x66 , 0xC3 ]
65
+
66
+ func test( bufferSize: Int , input: [ UInt8 ] , expected: String ) {
67
+ let strs = ( 0 ..< 100 ) . map { _ in
68
+ String ( unsafeUninitializedCapacity: bufferSize) { buffer in
69
+ _ = buffer. initialize ( from: input)
70
+ return input. count
71
+ }
72
+ }
73
+ for str in strs {
74
+ expectEqual ( expected, str)
75
+ }
73
76
}
74
- expectEqual ( " Caf� " , cafe2)
75
-
77
+
78
+ test ( bufferSize: validUTF8. count, input: validUTF8, expected: " Café " )
79
+ test ( bufferSize: invalidUTF8. count, input: invalidUTF8, expected: " Caf� " )
80
+ // Force non-smol strings by using a larger capacity
81
+ test ( bufferSize: 16 , input: validUTF8, expected: " Café " )
82
+ test ( bufferSize: 16 , input: invalidUTF8, expected: " Caf� " )
83
+ test ( bufferSize: longerValidUTF8. count, input: longerValidUTF8, expected: " !!!!!!!!!!Café " )
84
+ test ( bufferSize: longerInvalidUTF8. count, input: longerInvalidUTF8, expected: " !!Caf�Caf�Caf� " )
85
+ test ( bufferSize: 16 , input: longerValidUTF8, expected: " !!!!!!!!!!Café " )
86
+ test ( bufferSize: 16 , input: longerInvalidUTF8, expected: " !!Caf�Caf�Caf� " )
76
87
let empty = String ( unsafeUninitializedCapacity: 16 ) { _ in
77
88
// Can't initialize the buffer (e.g. the capacity is too small).
78
89
return 0
0 commit comments