Skip to content

Commit 6522414

Browse files
committed
Swift 3 API Parity: Data & NSData
Brings the Data and NSData API in s-cl-f to match the public version of Swift3 on Darwin. Reorganizes the implementation of Data/NSData to be a bit saner (versus somewhat vestigially matching the Objective-C categories on Darwin). Also brings a whole host of tests over from our overlay (some disabled for now, since the real focus of this PR is the API parity, which we should have now).
1 parent 31c8b10 commit 6522414

File tree

11 files changed

+1286
-445
lines changed

11 files changed

+1286
-445
lines changed

Foundation/Data.swift

Lines changed: 128 additions & 86 deletions
Large diffs are not rendered by default.

Foundation/NSConcreteValue.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ internal class NSConcreteValue : NSValue {
113113
}
114114

115115
override var description : String {
116-
return Data(bytes: self.value, count: self._size).description
116+
let boundBytes = self.value.bindMemory(to: UInt8.self, capacity: self._size)
117+
return Data(bytes: boundBytes, count: self._size).description
117118
}
118119

119120
convenience required init?(coder aDecoder: NSCoder) {

Foundation/NSData.swift

Lines changed: 328 additions & 338 deletions
Large diffs are not rendered by default.

Foundation/NSFileHandle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ open class FileHandle: NSObject, NSSecureCoding {
118118
open func write(_ data: Data) {
119119
data.enumerateBytes() { (bytes, range, stop) in
120120
do {
121-
try NSData.writeToFileDescriptor(self._fd, path: nil, buf: UnsafeRawPointer(bytes.baseAddress!), length: bytes.count)
121+
try NSData.write(toFileDescriptor: self._fd, path: nil, buf: UnsafeRawPointer(bytes.baseAddress!), length: bytes.count)
122122
} catch {
123123
fatalError("Write failure")
124124
}

Foundation/NSFileManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ open class FileManager: NSObject {
686686

687687
open func createFile(atPath path: String, contents data: Data?, attributes attr: [String : Any]? = [:]) -> Bool {
688688
do {
689-
try (data ?? Data()).write(to: URL(fileURLWithPath: path), options: .dataWritingAtomic)
689+
try (data ?? Data()).write(to: URL(fileURLWithPath: path), options: .atomic)
690690
return true
691691
} catch _ {
692692
return false

Foundation/NSString.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ extension NSString {
831831
return String.Encoding.unicode.rawValue
832832
}
833833

834-
public func data(using encoding: UInt, allowLossyConversion lossy: Bool) -> Data? {
834+
public func data(using encoding: UInt, allowLossyConversion lossy: Bool = false) -> Data? {
835835
let len = length
836836
var reqSize = 0
837837

@@ -844,7 +844,7 @@ extension NSString {
844844
if convertedLen != len {
845845
return nil // Not able to do it all...
846846
}
847-
847+
848848
if 0 < reqSize {
849849
var data = Data(count: reqSize)
850850
data.count = data.withUnsafeMutableBytes { (mutableBytes: UnsafeMutablePointer<UInt8>) -> Int in
@@ -1160,7 +1160,7 @@ extension NSString {
11601160
internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws {
11611161
var data = Data()
11621162
try _getExternalRepresentation(&data, url, enc)
1163-
try data.write(to: url, options: useAuxiliaryFile ? .dataWritingAtomic : [])
1163+
try data.write(to: url, options: useAuxiliaryFile ? .atomic : [])
11641164
}
11651165

11661166
open func write(to url: URL, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws {

Foundation/NSXMLDocument.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ open class XMLDocument : XMLNode {
8282
@abstract Returns a document created from the contents of an XML or HTML URL. Connection problems such as 404, parse errors are returned in <tt>error</tt>.
8383
*/
8484
public convenience init(contentsOf url: URL, options: Options) throws {
85-
let data = try Data(contentsOf: url, options: .dataReadingMappedIfSafe)
85+
let data = try Data(contentsOf: url, options: .mappedIfSafe)
8686

8787
try self.init(data: data, options: options)
8888
}

Foundation/NSXMLParser.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ internal func _NSXMLParserProcessingInstruction(_ ctx: _CFXMLInterface, target:
381381
internal func _NSXMLParserCdataBlock(_ ctx: _CFXMLInterface, value: UnsafePointer<UInt8>, len: Int32) -> Void {
382382
let parser = ctx.parser
383383
if let delegate = parser.delegate {
384-
delegate.parser(parser, foundCDATA: Data(bytes: UnsafeRawPointer(value), count: Int(len)))
384+
delegate.parser(parser, foundCDATA: Data(bytes: value, count: Int(len)))
385385
}
386386
}
387387

@@ -551,9 +551,9 @@ open class XMLParser : NSObject {
551551
_bomChunk = nil
552552

553553
if (totalLength > 4) {
554-
let remainingData = allExistingData.withUnsafeBytes { (bytes: UnsafePointer<Int8>) -> Data in
554+
let remainingData = allExistingData.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) -> Data in
555555
let ptr = bytes.advanced(by: 4)
556-
return Data(bytesNoCopy: UnsafeMutablePointer(mutating: ptr), count: totalLength - 4, deallocator: .none)
556+
return Data(bytesNoCopy: ptr, count: totalLength - 4, deallocator: .none)
557557
}
558558

559559
let _ = parseData(remainingData)

0 commit comments

Comments
 (0)