22//
33// This source file is part of the Swift.org open source project
44//
5- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+ // Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66// Licensed under Apache License v2.0 with Runtime Library Exception
77//
88// See https://swift.org/LICENSE.txt for license information
@@ -47,8 +47,8 @@ internal func _stdlib_binary_CFStringGetCharactersPtr(
4747 mutating: _swift_stdlib_CFStringGetCharactersPtr ( source) )
4848}
4949
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.
5252@_effects ( releasenone)
5353internal func _cocoaStringCopyCharacters(
5454 from source: _CocoaString ,
@@ -81,54 +81,46 @@ internal func _cocoaStringCompare(
8181@_effects ( readonly)
8282internal func _cocoaHashString(
8383 _ string: _CocoaString
84- ) -> UInt {
84+ ) -> UInt {
8585 return _swift_stdlib_CFStringHashNSString ( string)
8686}
8787
8888@_effects ( readonly)
8989internal func _cocoaHashASCIIBytes(
90- _ bytes: UnsafePointer < UInt8 > ,
91- length: Int
92- ) -> UInt {
90+ _ bytes: UnsafePointer < UInt8 > , length: Int
91+ ) -> UInt {
9392 return _swift_stdlib_CFStringHashCString ( bytes, length)
9493}
9594
9695// 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.
9897
9998@_effects ( readonly)
10099internal 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)
107103}
108104
109-
110-
111-
112105@_effects ( releasenone)
113106internal 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) )
123114}
124115
125116//
126- // Conversion from NSString to Swift's native representation
117+ // Conversion from NSString to Swift's native representation.
127118//
128119
129120private var kCFStringEncodingASCII : _swift_shims_CFStringEncoding {
130121 @inline ( __always) get { return 0x0600 }
131122}
123+
132124private var kCFStringEncodingUTF8 : _swift_shims_CFStringEncoding {
133125 @inline ( __always) get { return 0x8000100 }
134126}
@@ -149,12 +141,12 @@ internal enum _KnownCocoaString {
149141 @inline ( __always)
150142 init ( _ str: _CocoaString ) {
151143
152- #if !(arch(i386) || arch(arm))
144+ #if !(arch(i386) || arch(arm))
153145 if _isObjCTaggedPointer ( str) {
154146 self = . tagged
155147 return
156148 }
157- #endif
149+ #endif
158150
159151 switch _unsafeAddressOfCocoaStringClass ( str) {
160152 case unsafeBitCast ( _StringStorage. self, to: UInt . self) :
@@ -168,7 +160,7 @@ internal enum _KnownCocoaString {
168160}
169161
170162#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.
172164@_effects ( releasenone) // @opaque
173165internal func _bridgeTagged(
174166 _ cocoa: _CocoaString ,
@@ -223,32 +215,33 @@ private func _getCocoaStringPointer(
223215internal func _bridgeCocoaString( _ cocoaString: _CocoaString ) -> _StringGuts {
224216 switch _KnownCocoaString ( cocoaString) {
225217 case . storage:
226- return _unsafeUncheckedDowncast ( cocoaString, to: _StringStorage. self) . asString. _guts
218+ return _unsafeUncheckedDowncast (
219+ cocoaString, to: _StringStorage. self) . asString. _guts
227220 case . shared:
228- return _unsafeUncheckedDowncast ( cocoaString, to: _SharedStringStorage. self) . asString. _guts
221+ return _unsafeUncheckedDowncast (
222+ cocoaString, to: _SharedStringStorage. self) . asString. _guts
229223#if !(arch(i386) || arch(arm))
230224 case . tagged:
231- return _StringGuts ( _SmallString ( taggedCocoa: cocoaString) )
225+ return _StringGuts ( _SmallString ( taggedCocoa: cocoaString) )
232226#endif
233227 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
236230 // just does a retain.
237231 //
238232 // TODO: Only in certain circumstances should we emit this call:
239233 // 1) If it's immutable, just retain it.
240234 // 2) If it's mutable with no associated information, then a copy must
241235 // happen; might as well eagerly bridge it in.
242236 // 3) If it's mutable with associated information, must make the call
243- //
244237 let immutableCopy
245238 = _stdlib_binary_CFStringCreateCopy ( cocoaString) as AnyObject
246239
247- #if !(arch(i386) || arch(arm))
240+ #if !(arch(i386) || arch(arm))
248241 if _isObjCTaggedPointer ( immutableCopy) {
249242 return _StringGuts ( _SmallString ( taggedCocoa: immutableCopy) )
250243 }
251- #endif
244+ #endif
252245
253246 let ( fastUTF8, isASCII) : ( Bool , Bool )
254247 switch _getCocoaStringPointer ( immutableCopy) {
@@ -288,9 +281,13 @@ extension String {
288281 }
289282 }
290283 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
291287 return _SharedStringStorage (
292288 immortal: _guts. _object. fastUTF8. baseAddress!,
293- countAndFlags: _guts. _object. _countAndFlags)
289+ countAndFlags: _StringObject. CountAndFlags (
290+ sharedCount: _guts. count, isASCII: gutsCountAndFlags. isASCII) )
294291 }
295292
296293 _internalInvariant ( _guts. _object. hasObjCBridgeableObject,
0 commit comments