Skip to content

Commit 2014fc3

Browse files
author
Frederick Kellison-Linn
committed
Correct behavior for NSMutableData's replaceBytes(in:withBytes:length:) method for withBytes: nil case
1 parent 9a5d842 commit 2014fc3

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Foundation/NSData.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,10 @@ open class NSMutableData : NSData {
942942
}
943943

944944
}
945-
945+
946946
open func replaceBytes(in range: NSRange, withBytes replacementBytes: UnsafeRawPointer?, length replacementLength: Int) {
947-
if let replacementBytes = replacementBytes {
948-
let bytePtr = replacementBytes.bindMemory(to: UInt8.self, capacity: replacementLength)
949-
CFDataReplaceBytes(_cfMutableObject, CFRangeMake(range.location, range.length), bytePtr, replacementLength)
950-
}
947+
let bytePtr = replacementBytes?.bindMemory(to: UInt8.self, capacity: replacementLength)
948+
CFDataReplaceBytes(_cfMutableObject, CFRangeMake(range.location, range.length), bytePtr, replacementLength)
951949
}
952950
}
953951

TestFoundation/TestNSData.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,14 @@ class TestNSData: XCTestCase {
421421
let replacement = makeData([8, 9, 10])
422422
mData.replaceBytes(in: NSMakeRange(1, 3), withBytes: replacement.bytes,
423423
length: 3)
424-
let expected = makeData([0, 8, 9, 10, 0])
424+
var expected = makeData([0, 8, 9, 10, 0])
425425
XCTAssertEqual(mData, expected)
426+
427+
// test removing bytes with nil replacementBytes parameter
428+
mData.replaceBytes(in: NSMakeRange(1, 3), withBytes: nil, length: 0)
429+
expected = makeData([0, 0])
430+
XCTAssertEqual(mData, expected)
431+
426432
}
427433

428434
func test_initDataWithCapacity() {

0 commit comments

Comments
 (0)