Skip to content

Commit b8a0cd7

Browse files
gribozavrMax Moiseev
authored andcommitted
stdlib: move 'String : Hashable' conformance to a separate file
I'm about to add more code around it.
1 parent efa6dd9 commit b8a0cd7

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
@@ -112,6 +112,7 @@ set(SWIFTLIB_ESSENTIAL
112112
StringBuffer.swift
113113
StringComparable.swift
114114
StringCore.swift
115+
StringHashable.swift
115116
StringInterpolation.swift.gyb
116117
StringLegacy.swift
117118
StringRangeReplaceableCollection.swift.gyb

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"StringCharacterView.swift",
1414
"StringComparable.swift",
1515
"StringCore.swift",
16+
"StringHashable.swift",
1617
"StringIndexConversions.swift",
1718
"StringInterpolation.swift",
1819
"StringLegacy.swift",

stdlib/public/core/String.swift

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -528,52 +528,6 @@ extension String {
528528
}
529529
}
530530

531-
#if _runtime(_ObjC)
532-
@_silgen_name("swift_stdlib_NSStringHashValue")
533-
func _stdlib_NSStringHashValue(_ str: AnyObject, _ isASCII: Bool) -> Int
534-
535-
@_silgen_name("swift_stdlib_NSStringHashValuePointer")
536-
func _stdlib_NSStringHashValuePointer(_ str: OpaquePointer, _ isASCII: Bool) -> Int
537-
#endif
538-
539-
extension String : Hashable {
540-
/// The string's hash value.
541-
///
542-
/// Hash values are not guaranteed to be equal across different executions of
543-
/// your program. Do not save hash values to use during a future execution.
544-
public var hashValue: Int {
545-
#if _runtime(_ObjC)
546-
// Mix random bits into NSString's hash so that clients don't rely on
547-
// Swift.String.hashValue and NSString.hash being the same.
548-
#if arch(i386) || arch(arm)
549-
let hashOffset = Int(bitPattern: 0x88dd_cc21)
550-
#else
551-
let hashOffset = Int(bitPattern: 0x429b_1266_88dd_cc21)
552-
#endif
553-
// If we have a contiguous string then we can use the stack optimization.
554-
let core = self._core
555-
let isASCII = core.isASCII
556-
if core.hasContiguousStorage {
557-
let stackAllocated = _NSContiguousString(core)
558-
return hashOffset ^ stackAllocated._unsafeWithNotEscapedSelfPointer {
559-
return _stdlib_NSStringHashValuePointer($0, isASCII)
560-
}
561-
} else {
562-
let cocoaString = unsafeBitCast(
563-
self._bridgeToObjectiveCImpl(), to: _NSStringCore.self)
564-
return hashOffset ^ _stdlib_NSStringHashValue(cocoaString, isASCII)
565-
}
566-
#else
567-
if self._core.isASCII {
568-
return _swift_stdlib_unicode_hash_ascii(
569-
_core.startASCII, Int32(_core.count))
570-
} else {
571-
return _swift_stdlib_unicode_hash(_core.startUTF16, Int32(_core.count))
572-
}
573-
#endif
574-
}
575-
}
576-
577531
extension String {
578532
@effects(readonly)
579533
@_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)