Skip to content

Commit 3ef626d

Browse files
authored
Merge pull request #905 from johnno1962a/master
2 parents fc9b635 + 9b7de0a commit 3ef626d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Foundation/NSData.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,8 @@ open class NSMutableData : NSData {
950950
}
951951

952952
open func replaceBytes(in range: NSRange, withBytes replacementBytes: UnsafeRawPointer?, length replacementLength: Int) {
953-
if let replacementBytes = replacementBytes {
954-
let bytePtr = replacementBytes.bindMemory(to: UInt8.self, capacity: replacementLength)
955-
CFDataReplaceBytes(_cfMutableObject, CFRangeMake(range.location, range.length), bytePtr, replacementLength)
956-
}
953+
let bytePtr = replacementBytes?.bindMemory(to: UInt8.self, capacity: replacementLength)
954+
CFDataReplaceBytes(_cfMutableObject, CFRangeMake(range.location, range.length), bytePtr, replacementLength)
957955
}
958956
}
959957

TestFoundation/TestNSData.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class TestNSData: XCTestCase {
8787
("test_rangeOfData",test_rangeOfData),
8888
("test_initMutableDataWithLength", test_initMutableDataWithLength),
8989
("test_replaceBytes", test_replaceBytes),
90+
("test_replaceBytesWithNil", test_replaceBytesWithNil),
9091
("test_initDataWithCapacity", test_initDataWithCapacity),
9192
("test_initDataWithCount", test_initDataWithCount),
9293
("test_emptyStringToData", test_emptyStringToData),
@@ -426,6 +427,17 @@ class TestNSData: XCTestCase {
426427
XCTAssertEqual(mData, expected)
427428
}
428429

430+
func test_replaceBytesWithNil() {
431+
func makeData(_ data: [UInt8]) -> NSMutableData {
432+
return NSMutableData(bytes: data, length: data.count)
433+
}
434+
435+
let mData = makeData([1, 2, 3, 4, 5])
436+
mData.replaceBytes(in: NSMakeRange(1, 3), withBytes: nil, length: 0)
437+
let expected = makeData([1, 5])
438+
XCTAssertEqual(mData, expected)
439+
}
440+
429441
func test_initDataWithCapacity() {
430442
let data = Data(capacity: 123)
431443
XCTAssertEqual(data.count, 0)

0 commit comments

Comments
 (0)