Skip to content

[5.1] SR-10555 foreignCopyUTF8 should do bulk access #24648

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
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
21 changes: 21 additions & 0 deletions stdlib/public/core/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ internal func _cocoaStringSubscript(
return _swift_stdlib_CFStringGetCharacterAtIndex(cfSelf, position)
}

@_effects(releasenone)
internal func _cocoaStringCopyUTF8(
_ target: _CocoaString,
into bufPtr: UnsafeMutableBufferPointer<UInt8>
) -> Int? {
let ptr = bufPtr.baseAddress._unsafelyUnwrappedUnchecked
let len = _stdlib_binary_CFStringGetLength(target)
var count = 0
let converted = _swift_stdlib_CFStringGetBytes(
target,
_swift_shims_CFRange(location: 0, length: len),
kCFStringEncodingUTF8,
0,
0,
ptr,
bufPtr.count,
&count
)
return len == converted ? count : nil
}

@_effects(readonly)
internal func _cocoaStringCompare(
_ string: _CocoaString, _ other: _CocoaString
Expand Down
16 changes: 6 additions & 10 deletions stdlib/public/core/StringGuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,12 @@ extension _StringGuts {
internal func _foreignCopyUTF8(
into mbp: UnsafeMutableBufferPointer<UInt8>
) -> Int? {
var ptr = mbp.baseAddress._unsafelyUnwrappedUnchecked
var numWritten = 0
for cu in String(self).utf8 {
guard numWritten < mbp.count else { return nil }
ptr.initialize(to: cu)
ptr += 1
numWritten += 1
}

return numWritten
#if _runtime(_ObjC)
// Currently, foreign means NSString
return _cocoaStringCopyUTF8(_object.cocoaObject, into: mbp)
#else
fatalError("No foreign strings on Linux in this version of Swift")
#endif
}

@inline(__always)
Expand Down