Skip to content

Instantly share code, notes, and snippets.

@spevans
Created March 29, 2021 08:57
Show Gist options
  • Save spevans/c2c6a15b4cae1c376564ea434bda3eea to your computer and use it in GitHub Desktop.
Save spevans/c2c6a15b4cae1c376564ea434bda3eea to your computer and use it in GitHub Desktop.
patch
diff --git a/Sources/Foundation/NSAttributedString.swift b/Sources/Foundation/NSAttributedString.swift
index dce57a25..4f7523f1 100644
--- a/Sources/Foundation/NSAttributedString.swift
+++ b/Sources/Foundation/NSAttributedString.swift
@@ -559,7 +559,7 @@ internal func _NSReadIntFromMutableAttributedStringCoding(_ data: NSData, _ star
var value = 0
- withExtendedLifetime(data) {
+ return withExtendedLifetime(data) { _ -> (Int, Int)? in
while offset < length {
let i = Int(data.bytes.load(fromByteOffset: offset, as: UInt8.self))
@@ -581,9 +581,8 @@ internal func _NSReadIntFromMutableAttributedStringCoding(_ data: NSData, _ star
multiplier *= 128
}
+ return nil // Getting to the end of the stream indicates error, since we were still expecting more bytes
}
-
- return nil // Getting to the end of the stream indicates error, since we were still expecting more bytes
}
internal func _NSWriteIntToMutableAttributedStringCoding(_ i: Int, _ data: NSMutableData) {
diff --git a/Sources/Foundation/NSData.swift b/Sources/Foundation/NSData.swift
index e5134c4b..198941c1 100644
--- a/Sources/Foundation/NSData.swift
+++ b/Sources/Foundation/NSData.swift
@@ -394,7 +394,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}
guard var r = result else { return nil }
- result.withUnsafeMutableBytes {
+ r.withUnsafeMutableBytes {
_init(bytes: $0.baseAddress, length: $0.count, copy: true)
}
}
diff --git a/Sources/Foundation/NSString.swift b/Sources/Foundation/NSString.swift
index 306453de..9644eb7a 100644
--- a/Sources/Foundation/NSString.swift
+++ b/Sources/Foundation/NSString.swift
@@ -1368,7 +1368,7 @@ extension NSString {
public convenience init(contentsOf url: URL, encoding enc: UInt) throws {
let readResult = try NSData(contentsOf: url, options: [])
- let cf = try withExtendedLifetime(readResult) {
+ let cf = try withExtendedLifetime(readResult) { _ -> CFString in
let bytePtr = readResult.bytes.bindMemory(to: UInt8.self, capacity: readResult.length)
guard let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr, readResult.length, CFStringConvertNSStringEncodingToEncoding(numericCast(enc)), true) else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
@@ -1395,10 +1395,10 @@ extension NSString {
public convenience init(contentsOf url: URL, usedEncoding enc: UnsafeMutablePointer<UInt>?) throws {
let (readResult, textEncodingNameMaybe) = try NSData.contentsOf(url: url)
- let encoding: UInt
- let offset: Int
- let cf = try withExtendedLifetime(readResult) {
+ let (cf, encoding) = try withExtendedLifetime(readResult) { _ -> (CFString, UInt) in
+ let encoding: UInt
+ let offset: Int
// Look for a BOM (Byte Order Marker) to try and determine the text Encoding, this also skips
// over the bytes. This takes precedence over the textEncoding in the http header
let bytePtr = readResult.bytes.bindMemory(to: UInt8.self, capacity:readResult.length)
@@ -1435,7 +1435,7 @@ extension NSString {
NSDebugDescriptionErrorKey : "Unable to create a string using the specified encoding."
])
}
- return cf
+ return (cf, encoding)
}
var str: String?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment