2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
@@ -47,8 +47,8 @@ internal func _stdlib_binary_CFStringGetCharactersPtr(
47
47
mutating: _swift_stdlib_CFStringGetCharactersPtr ( source) )
48
48
}
49
49
50
- /// Copies a slice of a _CocoaString into contiguous storage of
51
- /// sufficient capacity.
50
+ /// Copies a slice of a _CocoaString into contiguous storage of sufficient
51
+ /// capacity.
52
52
@_effects ( releasenone)
53
53
internal func _cocoaStringCopyCharacters(
54
54
from source: _CocoaString ,
@@ -81,54 +81,46 @@ internal func _cocoaStringCompare(
81
81
@_effects ( readonly)
82
82
internal func _cocoaHashString(
83
83
_ string: _CocoaString
84
- ) -> UInt {
84
+ ) -> UInt {
85
85
return _swift_stdlib_CFStringHashNSString ( string)
86
86
}
87
87
88
88
@_effects ( readonly)
89
89
internal func _cocoaHashASCIIBytes(
90
- _ bytes: UnsafePointer < UInt8 > ,
91
- length: Int
92
- ) -> UInt {
90
+ _ bytes: UnsafePointer < UInt8 > , length: Int
91
+ ) -> UInt {
93
92
return _swift_stdlib_CFStringHashCString ( bytes, length)
94
93
}
95
94
96
95
// These "trampolines" are effectively objc_msgSend_super.
97
- // They bypass our implementations to use NSString's
96
+ // They bypass our implementations to use NSString's.
98
97
99
98
@_effects ( readonly)
100
99
internal func _cocoaCStringUsingEncodingTrampoline(
101
- _ string: _CocoaString ,
102
- _ encoding: UInt )
103
- -> UnsafePointer < UInt8 > ? {
104
- return _swift_stdlib_NSStringCStringUsingEncodingTrampoline (
105
- string,
106
- encoding)
100
+ _ string: _CocoaString , _ encoding: UInt
101
+ ) -> UnsafePointer < UInt8 > ? {
102
+ return _swift_stdlib_NSStringCStringUsingEncodingTrampoline ( string, encoding)
107
103
}
108
104
109
-
110
-
111
-
112
105
@_effects ( releasenone)
113
106
internal func _cocoaGetCStringTrampoline(
114
- _ string: _CocoaString ,
115
- _ buffer: UnsafeMutablePointer < UInt8 > ,
116
- _ maxLength: Int ,
117
- _ encoding: UInt )
118
- -> Int8 {
119
- return Int8 ( _swift_stdlib_NSStringGetCStringTrampoline ( string,
120
- buffer,
121
- maxLength,
122
- encoding) )
107
+ _ string: _CocoaString ,
108
+ _ buffer: UnsafeMutablePointer < UInt8 > ,
109
+ _ maxLength: Int ,
110
+ _ encoding: UInt
111
+ ) -> Int8 {
112
+ return Int8 ( _swift_stdlib_NSStringGetCStringTrampoline (
113
+ string, buffer, maxLength, encoding) )
123
114
}
124
115
125
116
//
126
- // Conversion from NSString to Swift's native representation
117
+ // Conversion from NSString to Swift's native representation.
127
118
//
128
119
129
120
private var kCFStringEncodingASCII : _swift_shims_CFStringEncoding {
130
121
@inline ( __always) get { return 0x0600 }
131
122
}
123
+
132
124
private var kCFStringEncodingUTF8 : _swift_shims_CFStringEncoding {
133
125
@inline ( __always) get { return 0x8000100 }
134
126
}
@@ -149,12 +141,12 @@ internal enum _KnownCocoaString {
149
141
@inline ( __always)
150
142
init ( _ str: _CocoaString ) {
151
143
152
- #if !(arch(i386) || arch(arm))
144
+ #if !(arch(i386) || arch(arm))
153
145
if _isObjCTaggedPointer ( str) {
154
146
self = . tagged
155
147
return
156
148
}
157
- #endif
149
+ #endif
158
150
159
151
switch _unsafeAddressOfCocoaStringClass ( str) {
160
152
case unsafeBitCast ( _StringStorage. self, to: UInt . self) :
@@ -168,7 +160,7 @@ internal enum _KnownCocoaString {
168
160
}
169
161
170
162
#if !(arch(i386) || arch(arm))
171
- // Resiliently write a tagged cocoa string 's contents into a buffer
163
+ // Resiliently write a tagged _CocoaString 's contents into a buffer.
172
164
@_effects ( releasenone) // @opaque
173
165
internal func _bridgeTagged(
174
166
_ cocoa: _CocoaString ,
@@ -223,32 +215,33 @@ private func _getCocoaStringPointer(
223
215
internal func _bridgeCocoaString( _ cocoaString: _CocoaString ) -> _StringGuts {
224
216
switch _KnownCocoaString ( cocoaString) {
225
217
case . storage:
226
- return _unsafeUncheckedDowncast ( cocoaString, to: _StringStorage. self) . asString. _guts
218
+ return _unsafeUncheckedDowncast (
219
+ cocoaString, to: _StringStorage. self) . asString. _guts
227
220
case . shared:
228
- return _unsafeUncheckedDowncast ( cocoaString, to: _SharedStringStorage. self) . asString. _guts
221
+ return _unsafeUncheckedDowncast (
222
+ cocoaString, to: _SharedStringStorage. self) . asString. _guts
229
223
#if !(arch(i386) || arch(arm))
230
224
case . tagged:
231
- return _StringGuts ( _SmallString ( taggedCocoa: cocoaString) )
225
+ return _StringGuts ( _SmallString ( taggedCocoa: cocoaString) )
232
226
#endif
233
227
case . cocoa:
234
- // "copy " it into a value to be sure nobody will modify behind
235
- // our backs. In practice, when value is already immutable, this
228
+ // "Copy " it into a value to be sure nobody will modify behind
229
+ // our backs. In practice, when value is already immutable, this
236
230
// just does a retain.
237
231
//
238
232
// TODO: Only in certain circumstances should we emit this call:
239
233
// 1) If it's immutable, just retain it.
240
234
// 2) If it's mutable with no associated information, then a copy must
241
235
// happen; might as well eagerly bridge it in.
242
236
// 3) If it's mutable with associated information, must make the call
243
- //
244
237
let immutableCopy
245
238
= _stdlib_binary_CFStringCreateCopy ( cocoaString) as AnyObject
246
239
247
- #if !(arch(i386) || arch(arm))
240
+ #if !(arch(i386) || arch(arm))
248
241
if _isObjCTaggedPointer ( immutableCopy) {
249
242
return _StringGuts ( _SmallString ( taggedCocoa: immutableCopy) )
250
243
}
251
- #endif
244
+ #endif
252
245
253
246
let ( fastUTF8, isASCII) : ( Bool , Bool )
254
247
switch _getCocoaStringPointer ( immutableCopy) {
@@ -288,9 +281,13 @@ extension String {
288
281
}
289
282
}
290
283
if _guts. _object. isImmortal {
284
+ // TODO: We'd rather emit a valid ObjC object statically than create a
285
+ // shared string class instance.
286
+ let gutsCountAndFlags = _guts. _object. _countAndFlags
291
287
return _SharedStringStorage (
292
288
immortal: _guts. _object. fastUTF8. baseAddress!,
293
- countAndFlags: _guts. _object. _countAndFlags)
289
+ countAndFlags: _StringObject. CountAndFlags (
290
+ sharedCount: _guts. count, isASCII: gutsCountAndFlags. isASCII) )
294
291
}
295
292
296
293
_internalInvariant ( _guts. _object. hasObjCBridgeableObject,
0 commit comments