Skip to content

NSMutableData compatiblity with macOS #905

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

Merged
merged 3 commits into from
Mar 6, 2017
Merged
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
6 changes: 2 additions & 4 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -950,10 +950,8 @@ open class NSMutableData : NSData {
}

open func replaceBytes(in range: NSRange, withBytes replacementBytes: UnsafeRawPointer?, length replacementLength: Int) {
if let replacementBytes = replacementBytes {
let bytePtr = replacementBytes.bindMemory(to: UInt8.self, capacity: replacementLength)
CFDataReplaceBytes(_cfMutableObject, CFRangeMake(range.location, range.length), bytePtr, replacementLength)
}
let bytePtr = replacementBytes?.bindMemory(to: UInt8.self, capacity: replacementLength)
CFDataReplaceBytes(_cfMutableObject, CFRangeMake(range.location, range.length), bytePtr, replacementLength)
}
}

Expand Down
12 changes: 12 additions & 0 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class TestNSData: XCTestCase {
("test_rangeOfData",test_rangeOfData),
("test_initMutableDataWithLength", test_initMutableDataWithLength),
("test_replaceBytes", test_replaceBytes),
("test_replaceBytesWithNil", test_replaceBytesWithNil),
("test_initDataWithCapacity", test_initDataWithCapacity),
("test_initDataWithCount", test_initDataWithCount),
("test_emptyStringToData", test_emptyStringToData),
Expand Down Expand Up @@ -426,6 +427,17 @@ class TestNSData: XCTestCase {
XCTAssertEqual(mData, expected)
}

func test_replaceBytesWithNil() {
func makeData(_ data: [UInt8]) -> NSMutableData {
return NSMutableData(bytes: data, length: data.count)
}

let mData = makeData([1, 2, 3, 4, 5])
mData.replaceBytes(in: NSMakeRange(1, 3), withBytes: nil, length: 0)
let expected = makeData([1, 5])
XCTAssertEqual(mData, expected)
}

func test_initDataWithCapacity() {
let data = Data(capacity: 123)
XCTAssertEqual(data.count, 0)
Expand Down