[stdlib] String: Avoid retain/release operations around use sites of sharedStorage and cocoaObject

This commit is contained in:
Karoy Lorentey
2023-02-15 14:21:46 -08:00
parent 7a11700d7b
commit 3f5dfea4b1
5 changed files with 57 additions and 22 deletions

View File

@@ -249,12 +249,11 @@ extension _StringGuts {
) -> Int? {
#if _runtime(_ObjC)
// Currently, foreign means NSString
if let res = _cocoaStringCopyUTF8(_object.cocoaObject,
into: UnsafeMutableRawBufferPointer(start: mbp.baseAddress,
count: mbp.count)) {
return res
let res = _object.withCocoaObject {
_cocoaStringCopyUTF8($0, into: UnsafeMutableRawBufferPointer(mbp))
}
if let res { return res }
// If the NSString contains invalid UTF8 (e.g. unpaired surrogates), we
// can get nil from cocoaStringCopyUTF8 in situations where a character by
// character loop would get something more useful like repaired contents