Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion stdlib/public/core/StringStorageBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ extension _AbstractStringStorage {
// CFString will only give us ASCII bytes here, but that's fine.
// We already handled non-ASCII UTF8 strings earlier since they're Swift.
if let asciiEqual = unsafe withCocoaASCIIPointer(other, work: { (ascii) -> Bool in
return unsafe (start == ascii || (memcmp(start, ascii, self.count) == 0))
// otherUTF16Length is the same as the byte count here since it's ASCII
// self.count could still be utf8
if count != otherUTF16Length {
return false
}
return unsafe (start == ascii || (memcmp(start, ascii, otherUTF16Length) == 0))
}) {
return asciiEqual ? 1 : 0
}
Expand Down
7 changes: 7 additions & 0 deletions test/stdlib/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,11 @@ StringBridgeTests.test("lengthOfBytes(using:)") {
expectEqual(utf8AsMacRomanLen, 43)
}

StringBridgeTests.test("Equal UTF16 lengths but unequal UTF8") {
let utf8 = "чебурашка@ящик-с-апельсинами.рф" //Native, UTF16 count: 31, UTF8 count: 58
let nsascii = "2166002315@874404110.1042078977" as NSString //Non-native, UTF16 count: 31, UTF8 count: 31
let nsutf8 = String(decoding: utf8.utf8, as: UTF8.self) as NSString //bridged native
expectFalse(nsutf8.isEqual(nsascii))
}

runAllTests()