@@ -158,21 +158,25 @@ extension _AbstractStringStorage {
158
158
159
159
// At this point we've proven that it is a non-Swift NSString
160
160
let otherUTF16Length = _stdlib_binary_CFStringGetLength ( other)
161
-
161
+
162
+ if UTF16Length != otherUTF16Length {
163
+ return 0
164
+ }
165
+
162
166
// CFString will only give us ASCII bytes here, but that's fine.
163
167
// We already handled non-ASCII UTF8 strings earlier since they're Swift.
164
168
if let asciiEqual = unsafe withCocoaASCIIPointer( other, work: { ( ascii) -> Bool in
165
- // UTF16 length == UTF8 length iff ASCII
166
- if otherUTF16Length == self . count {
167
- return unsafe ( start == ascii || ( memcmp ( start, ascii, self . count) == 0 ) )
168
- }
169
- return false
169
+ return unsafe ( start == ascii || ( memcmp ( start, ascii, self . count) == 0 ) )
170
170
} ) {
171
171
return asciiEqual ? 1 : 0
172
172
}
173
-
174
- if self . UTF16Length != otherUTF16Length {
175
- return 0
173
+
174
+ if let utf16Ptr = unsafe _stdlib_binary_CFStringGetCharactersPtr( other) {
175
+ let utf16Buffer = unsafe UnsafeBufferPointer(
176
+ start: utf16Ptr,
177
+ count: otherUTF16Length
178
+ )
179
+ return unsafe asString. utf16 . elementsEqual ( utf16Buffer) ? 1 : 0
176
180
}
177
181
178
182
/*
@@ -189,7 +193,11 @@ extension __StringStorage {
189
193
@objc ( length)
190
194
final internal var UTF16Length : Int {
191
195
@_effects ( readonly) @inline ( __always) get {
192
- return asString. utf16. count // UTF16View special-cases ASCII for us.
196
+ // UTF16View does this, but there's still a little overhead
197
+ if isASCII {
198
+ return count
199
+ }
200
+ return asString. utf16. count
193
201
}
194
202
}
195
203
@@ -235,7 +243,7 @@ extension __StringStorage {
235
243
_ requiresNulTermination: Int8 ,
236
244
_ outUTF8Length: UnsafeMutablePointer < UInt >
237
245
) -> UnsafePointer < UInt8 > ? {
238
- outUTF8Length. pointee = UInt ( count)
246
+ unsafe outUTF8 Length. pointee = UInt ( count)
239
247
return unsafe start
240
248
}
241
249
@@ -301,7 +309,11 @@ extension __SharedStringStorage {
301
309
@objc ( length)
302
310
final internal var UTF16Length : Int {
303
311
@_effects ( readonly) get {
304
- return asString. utf16. count // UTF16View special-cases ASCII for us.
312
+ // UTF16View does this, but there's still a little overhead
313
+ if isASCII {
314
+ return count
315
+ }
316
+ return asString. utf16. count
305
317
}
306
318
}
307
319
@@ -363,7 +375,7 @@ extension __SharedStringStorage {
363
375
_ requiresNulTermination: Int8 ,
364
376
_ outUTF8Length: UnsafeMutablePointer < UInt >
365
377
) -> UnsafePointer < UInt8 > ? {
366
- outUTF8Length. pointee = UInt ( count)
378
+ unsafe outUTF8 Length. pointee = UInt ( count)
367
379
return unsafe start
368
380
}
369
381
0 commit comments