Skip to content

[SDK] Use withContiguousStorageIfAvailable in some overlay methods #22001

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

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 14 additions & 12 deletions stdlib/public/Darwin/AppKit/NSGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ extension Sequence where Iterator.Element == NSRect {
NSGraphicsContext.current?.compositingOperation ?? .sourceOver) {
precondition(NSGraphicsContext.current != nil,
"There must be a set current NSGraphicsContext")
let rects = Array(self)
let count = rects.count
guard count > 0 else { return }
rects.withUnsafeBufferPointer { rectBufferPointer in
guard let rectArray = rectBufferPointer.baseAddress else { return }
__NSRectFillListUsingOperation(rectArray, count, operation)
func _fill(buffer: UnsafeBufferPointer<NSRect>) {
guard let bufferAddress = buffer.baseAddress, !buffer.isEmpty else {
return
}
__NSRectFillListUsingOperation(bufferAddress, buffer.count, operation)
}
withContiguousStorageIfAvailable(_fill)
?? Array(self).withUnsafeBufferPointer(_fill)
}

/// Modifies the current graphics context clipping path by intersecting it
Expand All @@ -83,13 +84,14 @@ extension Sequence where Iterator.Element == NSRect {
public func clip() {
precondition(NSGraphicsContext.current != nil,
"There must be a set current NSGraphicsContext")
let rects = Array(self)
let count = rects.count
guard count > 0 else { return }
rects.withUnsafeBufferPointer { rectBufferPointer in
guard let rectArray = rectBufferPointer.baseAddress else { return }
__NSRectClipList(rectArray, count)
func _clip(buffer: UnsafeBufferPointer<NSRect>) {
guard let bufferAddress = buffer.baseAddress, !buffer.isEmpty else {
return
}
__NSRectClipList(bufferAddress, buffer.count)
}
withContiguousStorageIfAvailable(_clip)
?? Array(self).withUnsafeBufferPointer(_clip)
}
}

Expand Down
30 changes: 15 additions & 15 deletions stdlib/public/Darwin/Foundation/NSStringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,22 @@ extension String {
/// - Parameters:
/// - bytes: A sequence of bytes to interpret using `encoding`.
/// - encoding: The ecoding to use to interpret `bytes`.
public init?<S: Sequence>(bytes: __shared S, encoding: Encoding)
where S.Iterator.Element == UInt8 {
let byteArray = Array(bytes)
if encoding == .utf8,
let str = byteArray.withUnsafeBufferPointer({ String._tryFromUTF8($0) })
{
public init? <S: Sequence>(bytes: __shared S, encoding: Encoding)
where S.Iterator.Element == UInt8 {
func _makeString(buffer: UnsafeBufferPointer<UInt8>) -> String? {
guard let address = buffer.baseAddress else { return nil }
if encoding == .utf8, let str = String._tryFromUTF8(buffer) {
return str
}
return NSString(bytes: address, length: buffer.count,
encoding: encoding.rawValue)
.map(String._unconditionallyBridgeFromObjectiveC)
}
guard let str = bytes.withContiguousStorageIfAvailable(_makeString)
?? Array(bytes).withUnsafeBufferPointer(_makeString) else {
return nil
}
self = str
return
}

if let ns = NSString(
bytes: byteArray, length: byteArray.count, encoding: encoding.rawValue) {
self = String._unconditionallyBridgeFromObjectiveC(ns)
} else {
return nil
}
}

// - (instancetype)
Expand Down