Skip to content

Clean up and restructure String bridging a bit #20623

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

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stdlib/public/SwiftShims/CoreFoundationShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ _swift_stdlib_NSStringGetCStringTrampoline(id _Nonnull obj,
_swift_shims_UInt8 *buffer,
_swift_shims_CFIndex maxLength,
unsigned long encoding);

SWIFT_RUNTIME_STDLIB_API
__swift_uintptr_t
_swift_stdlib_unsafeAddressOfClass(id _Nonnull obj);

#endif // __OBJC2__

Expand Down
108 changes: 73 additions & 35 deletions stdlib/public/core/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ internal func _cocoaCStringUsingEncodingTrampoline(
}




@_effects(releasenone)
internal func _cocoaGetCStringTrampoline(
_ string: _CocoaString,
Expand All @@ -131,6 +133,40 @@ private var kCFStringEncodingUTF8 : _swift_shims_CFStringEncoding {
@inline(__always) get { return 0x8000100 }
}

@_effects(readonly)
private func _unsafeAddressOfCocoaStringClass(_ str: _CocoaString) -> UInt {
return _swift_stdlib_unsafeAddressOfClass(str)
}

internal enum _KnownCocoaString {
case storage
case shared
case cocoa
#if !(arch(i386) || arch(arm))
case tagged
#endif

@inline(__always)
init(_ str: _CocoaString) {

#if !(arch(i386) || arch(arm))
if _isObjCTaggedPointer(str) {
self = .tagged
return
}
#endif

switch _unsafeAddressOfCocoaStringClass(str) {
case unsafeBitCast(_StringStorage.self, to: UInt.self):
self = .storage
case unsafeBitCast(_SharedStringStorage.self, to: UInt.self):
self = .shared
default:
self = .cocoa
}
}
}

#if !(arch(i386) || arch(arm))
// Resiliently write a tagged cocoa string's contents into a buffer
@_effects(releasenone) // @opaque
Expand Down Expand Up @@ -185,47 +221,49 @@ private func _getCocoaStringPointer(
@usableFromInline
@_effects(releasenone) // @opaque
internal func _bridgeCocoaString(_ cocoaString: _CocoaString) -> _StringGuts {
if let abstract = cocoaString as? _AbstractStringStorage {
return abstract.asString._guts
}
switch _KnownCocoaString(cocoaString) {
case .storage:
return _unsafeUncheckedDowncast(cocoaString, to: _StringStorage.self).asString._guts
case .shared:
return _unsafeUncheckedDowncast(cocoaString, to: _SharedStringStorage.self).asString._guts
#if !(arch(i386) || arch(arm))
if _isObjCTaggedPointer(cocoaString) {
return _StringGuts(_SmallString(taggedCocoa: cocoaString))
}
case .tagged:
return _StringGuts(_SmallString(taggedCocoa: cocoaString))
#endif

// "copy" it into a value to be sure nobody will modify behind
// our backs. In practice, when value is already immutable, this
// just does a retain.
//
// TODO: Only in certain circumstances should we emit this call:
// 1) If it's immutable, just retain it.
// 2) If it's mutable with no associated information, then a copy must
// happen; might as well eagerly bridge it in.
// 3) If it's mutable with associated information, must make the call
//
let immutableCopy
= _stdlib_binary_CFStringCreateCopy(cocoaString) as AnyObject

#if !(arch(i386) || arch(arm))
if _isObjCTaggedPointer(immutableCopy) {
return _StringGuts(_SmallString(taggedCocoa: immutableCopy))
}
#endif

let (fastUTF8, isASCII): (Bool, Bool)
switch _getCocoaStringPointer(immutableCopy) {
case .cocoa:
// "copy" it into a value to be sure nobody will modify behind
// our backs. In practice, when value is already immutable, this
// just does a retain.
//
// TODO: Only in certain circumstances should we emit this call:
// 1) If it's immutable, just retain it.
// 2) If it's mutable with no associated information, then a copy must
// happen; might as well eagerly bridge it in.
// 3) If it's mutable with associated information, must make the call
//
let immutableCopy
= _stdlib_binary_CFStringCreateCopy(cocoaString) as AnyObject
#if !(arch(i386) || arch(arm))
if _isObjCTaggedPointer(immutableCopy) {
return _StringGuts(_SmallString(taggedCocoa: immutableCopy))
}
#endif
let (fastUTF8, isASCII): (Bool, Bool)
switch _getCocoaStringPointer(immutableCopy) {
case .ascii(_): (fastUTF8, isASCII) = (true, true)
case .utf8(_): (fastUTF8, isASCII) = (true, false)
default: (fastUTF8, isASCII) = (false, false)
}
let length = _stdlib_binary_CFStringGetLength(immutableCopy)

return _StringGuts(
cocoa: immutableCopy,
providesFastUTF8: fastUTF8,
isASCII: isASCII,
length: length)
}
let length = _stdlib_binary_CFStringGetLength(immutableCopy)

return _StringGuts(
cocoa: immutableCopy,
providesFastUTF8: fastUTF8,
isASCII: isASCII,
length: length)
}

extension String {
Expand Down
Loading