Skip to content

Commit 0ab8bb3

Browse files
committed
[Foundation] improve hashing of NSRanges to collide less in reasonable values (and use the length too)
1 parent 0fd0be8 commit 0ab8bb3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

stdlib/public/SDK/Foundation/NSRange.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
@_exported import Foundation // Clang module
1414

1515
extension NSRange : Hashable {
16-
public var hashValue: Int {
17-
return Int(bitPattern: UInt(location) ^ UInt(location))
16+
public var hashValue: Int {
17+
#if arch(i386) || arch(arm)
18+
return Int(bitPattern: (UInt(bitPattern: location) | (UInt(bitPattern: length) << 16)))
19+
#elseif arch(x86_64) || arch(arm64)
20+
return Int(bitPattern: (UInt(bitPattern: location) | (UInt(bitPattern: length) << 32)))
21+
#endif
1822
}
1923

2024
public static func==(_ lhs: NSRange, _ rhs: NSRange) -> Bool {

0 commit comments

Comments
 (0)