-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Re-shim tagged NSString creation #26588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,3 +96,28 @@ extension Substring : _ObjectiveCBridgeable { | |
| } | ||
|
|
||
| extension String: CVarArg {} | ||
|
|
||
| /* | ||
| This is on NSObject so that the stdlib can call it in StringBridge.swift | ||
| without having to synthesize a receiver (e.g. lookup a class or allocate) | ||
|
|
||
| In the future (once the Foundation overlay can know about SmallString), we | ||
| should move the caller of this method up into the overlay and avoid this | ||
| indirection. | ||
| */ | ||
| private extension NSObject { | ||
| // The ObjC selector has to start with "new" to get ARC to not autorelease | ||
| @_effects(releasenone) | ||
| @objc(newTaggedNSStringWithASCIIBytes_:length_:) | ||
|
||
| func createTaggedString(bytes: UnsafePointer<UInt8>, | ||
| count: Int) -> AnyObject? { | ||
| //TODO: update this to use _CFStringCreateTaggedPointerString once we can | ||
| return CFStringCreateWithBytes( | ||
| kCFAllocatorSystemDefault, | ||
| bytes, | ||
| count, | ||
| CFStringBuiltInEncodings.UTF8.rawValue, | ||
| false | ||
| ) as NSString as NSString? //just "as AnyObject" inserts unwanted bridging | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,10 @@ internal typealias _CocoaString = AnyObject | |
| options: UInt, | ||
| range: _SwiftNSRange, | ||
| locale: AnyObject?) -> Int | ||
|
|
||
| @objc(newTaggedNSStringWithASCIIBytes_:length_:) | ||
| func createTaggedString(bytes: UnsafePointer<UInt8>, | ||
| count: Int) -> AnyObject? | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -410,19 +414,28 @@ extension String { | |
| } | ||
| } | ||
|
|
||
| @_effects(releasenone) | ||
| private func _createNSString( | ||
| _ receiver: _StringSelectorHolder, | ||
| _ ptr: UnsafePointer<UInt8>, | ||
| _ count: Int, | ||
| _ encoding: UInt32 | ||
| ) -> AnyObject? { | ||
| return receiver.createTaggedString(bytes: ptr, count: count) | ||
| } | ||
|
|
||
| @_effects(releasenone) | ||
| private func _createCFString( | ||
| _ ptr: UnsafePointer<UInt8>, | ||
| _ count: Int, | ||
| _ encoding: UInt32 | ||
| ) -> AnyObject { | ||
| return _swift_stdlib_CFStringCreateWithBytes( | ||
| nil, //ignored in the shim for perf reasons | ||
| ) -> AnyObject? { | ||
| return _createNSString( | ||
| unsafeBitCast(__StringStorage.self as AnyClass, to: _StringSelectorHolder.self), | ||
|
||
| ptr, | ||
| count, | ||
| kCFStringEncodingUTF8, | ||
| 0 | ||
| ) as AnyObject | ||
| encoding | ||
| ) | ||
| } | ||
|
|
||
| extension String { | ||
|
|
@@ -431,13 +444,14 @@ extension String { | |
| func _bridgeToObjectiveCImpl() -> AnyObject { | ||
| // Smol ASCII a) may bridge to tagged pointers, b) can't contain a BOM | ||
| if _guts.isSmallASCII { | ||
| return _guts.asSmall.withUTF8 { bufPtr in | ||
| let maybeTagged = _guts.asSmall.withUTF8 { bufPtr in | ||
|
||
| return _createCFString( | ||
| bufPtr.baseAddress._unsafelyUnwrappedUnchecked, | ||
| bufPtr.count, | ||
| kCFStringEncodingUTF8 | ||
| ) | ||
| } | ||
| if let tagged = maybeTagged { return tagged } | ||
| } | ||
| if _guts.isSmall { | ||
| // We can't form a tagged pointer String, so grow to a non-small String, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear what exactly I'm claiming is not being released here, but it does cause something to not be retain-released