Skip to content

Commit

Permalink
Merge pull request swiftlang#24289 from Catfish-Man/bulk-discount
Browse files Browse the repository at this point in the history
SR-10555 foreignCopyUTF8 should do bulk access
  • Loading branch information
Catfish-Man authored Apr 30, 2019
2 parents 316a3b2 + fd0d4d8 commit b6d0362
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
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

0 comments on commit b6d0362

Please sign in to comment.