@@ -158,21 +158,25 @@ extension _AbstractStringStorage {
158158
159159 // At this point we've proven that it is a non-Swift NSString
160160 let otherUTF16Length = _stdlib_binary_CFStringGetLength ( other)
161-
161+
162+ if UTF16Length != otherUTF16Length {
163+ return 0
164+ }
165+
162166 // CFString will only give us ASCII bytes here, but that's fine.
163167 // We already handled non-ASCII UTF8 strings earlier since they're Swift.
164168 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 ) )
170170 } ) {
171171 return asciiEqual ? 1 : 0
172172 }
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
176180 }
177181
178182 /*
@@ -189,7 +193,11 @@ extension __StringStorage {
189193 @objc ( length)
190194 final internal var UTF16Length : Int {
191195 @_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
193201 }
194202 }
195203
@@ -235,7 +243,7 @@ extension __StringStorage {
235243 _ requiresNulTermination: Int8 ,
236244 _ outUTF8Length: UnsafeMutablePointer < UInt >
237245 ) -> UnsafePointer < UInt8 > ? {
238- outUTF8Length. pointee = UInt ( count)
246+ unsafe outUTF8 Length. pointee = UInt ( count)
239247 return unsafe start
240248 }
241249
@@ -301,7 +309,11 @@ extension __SharedStringStorage {
301309 @objc ( length)
302310 final internal var UTF16Length : Int {
303311 @_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
305317 }
306318 }
307319
@@ -363,7 +375,7 @@ extension __SharedStringStorage {
363375 _ requiresNulTermination: Int8 ,
364376 _ outUTF8Length: UnsafeMutablePointer < UInt >
365377 ) -> UnsafePointer < UInt8 > ? {
366- outUTF8Length. pointee = UInt ( count)
378+ unsafe outUTF8 Length. pointee = UInt ( count)
367379 return unsafe start
368380 }
369381
0 commit comments