Skip to content

Commit 31d3bb6

Browse files
authored
Merge pull request #4612 from apple/stdlib-string-hashing-move
stdlib: move 'String : Hashable' conformance to a separate file
2 parents 897de2c + c314b59 commit 31d3bb6

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ set(SWIFTLIB_ESSENTIAL
114114
StringBridge.swift
115115
StringBuffer.swift
116116
StringCore.swift
117+
StringHashable.swift
117118
StringInterpolation.swift.gyb
118119
StringLegacy.swift
119120
StringRangeReplaceableCollection.swift.gyb

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"StringBuffer.swift",
1313
"StringCharacterView.swift",
1414
"StringCore.swift",
15+
"StringHashable.swift",
1516
"StringIndexConversions.swift",
1617
"StringInterpolation.swift",
1718
"StringLegacy.swift",

stdlib/public/core/String.swift

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -654,52 +654,6 @@ extension String {
654654
}
655655
}
656656

657-
#if _runtime(_ObjC)
658-
@_silgen_name("swift_stdlib_NSStringHashValue")
659-
func _stdlib_NSStringHashValue(_ str: AnyObject, _ isASCII: Bool) -> Int
660-
661-
@_silgen_name("swift_stdlib_NSStringHashValuePointer")
662-
func _stdlib_NSStringHashValuePointer(_ str: OpaquePointer, _ isASCII: Bool) -> Int
663-
#endif
664-
665-
extension String : Hashable {
666-
/// The string's hash value.
667-
///
668-
/// Hash values are not guaranteed to be equal across different executions of
669-
/// your program. Do not save hash values to use during a future execution.
670-
public var hashValue: Int {
671-
#if _runtime(_ObjC)
672-
// Mix random bits into NSString's hash so that clients don't rely on
673-
// Swift.String.hashValue and NSString.hash being the same.
674-
#if arch(i386) || arch(arm)
675-
let hashOffset = Int(bitPattern: 0x88dd_cc21)
676-
#else
677-
let hashOffset = Int(bitPattern: 0x429b_1266_88dd_cc21)
678-
#endif
679-
// If we have a contiguous string then we can use the stack optimization.
680-
let core = self._core
681-
let isASCII = core.isASCII
682-
if core.hasContiguousStorage {
683-
let stackAllocated = _NSContiguousString(core)
684-
return hashOffset ^ stackAllocated._unsafeWithNotEscapedSelfPointer {
685-
return _stdlib_NSStringHashValuePointer($0, isASCII)
686-
}
687-
} else {
688-
let cocoaString = unsafeBitCast(
689-
self._bridgeToObjectiveCImpl(), to: _NSStringCore.self)
690-
return hashOffset ^ _stdlib_NSStringHashValue(cocoaString, isASCII)
691-
}
692-
#else
693-
if self._core.isASCII {
694-
return _swift_stdlib_unicode_hash_ascii(
695-
_core.startASCII, Int32(_core.count))
696-
} else {
697-
return _swift_stdlib_unicode_hash(_core.startUTF16, Int32(_core.count))
698-
}
699-
#endif
700-
}
701-
}
702-
703657
extension String {
704658
@effects(readonly)
705659
@_semantics("string.concat")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftShims
14+
15+
#if _runtime(_ObjC)
16+
@_silgen_name("swift_stdlib_NSStringHashValue")
17+
func _stdlib_NSStringHashValue(_ str: AnyObject, _ isASCII: Bool) -> Int
18+
19+
@_silgen_name("swift_stdlib_NSStringHashValuePointer")
20+
func _stdlib_NSStringHashValuePointer(_ str: OpaquePointer, _ isASCII: Bool) -> Int
21+
#endif
22+
23+
extension String : Hashable {
24+
/// The string's hash value.
25+
///
26+
/// Hash values are not guaranteed to be equal across different executions of
27+
/// your program. Do not save hash values to use during a future execution.
28+
public var hashValue: Int {
29+
#if _runtime(_ObjC)
30+
// Mix random bits into NSString's hash so that clients don't rely on
31+
// Swift.String.hashValue and NSString.hash being the same.
32+
#if arch(i386) || arch(arm)
33+
let hashOffset = Int(bitPattern: 0x88dd_cc21)
34+
#else
35+
let hashOffset = Int(bitPattern: 0x429b_1266_88dd_cc21)
36+
#endif
37+
// If we have a contiguous string then we can use the stack optimization.
38+
let core = self._core
39+
let isASCII = core.isASCII
40+
if core.hasContiguousStorage {
41+
let stackAllocated = _NSContiguousString(core)
42+
return hashOffset ^ stackAllocated._unsafeWithNotEscapedSelfPointer {
43+
return _stdlib_NSStringHashValuePointer($0, isASCII)
44+
}
45+
} else {
46+
let cocoaString = unsafeBitCast(
47+
self._bridgeToObjectiveCImpl(), to: _NSStringCore.self)
48+
return hashOffset ^ _stdlib_NSStringHashValue(cocoaString, isASCII)
49+
}
50+
#else
51+
if self._core.isASCII {
52+
return _swift_stdlib_unicode_hash_ascii(
53+
_core.startASCII, Int32(_core.count))
54+
} else {
55+
return _swift_stdlib_unicode_hash(_core.startUTF16, Int32(_core.count))
56+
}
57+
#endif
58+
}
59+
}
60+

0 commit comments

Comments
 (0)