Skip to content

Correct behavior for NSMutableData's replaceBytes(in:withBytes:length:) method #735

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

Closed
wants to merge 3 commits into from
Closed
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
23 changes: 18 additions & 5 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -944,12 +944,25 @@ open class NSMutableData : NSData {
}

}


/// Replaces with a given set of bytes a given range within the contents of the receiver.
///
/// If the length of range is not equal to replacementLength, the receiver is resized to
/// accommodate the new bytes. Any bytes past range in the receiver are shifted to accommodate
/// the new bytes. You can therefore pass NULL for replacementBytes and 0 for replacementLength
/// to delete bytes in the receiver in the range range. You can also replace a range (which
/// might be zero-length) with more bytes than the length of the range, which has the effect of
/// insertion (or “replace some and insert more”).
///
/// - Parameter range: range within the receiver's contents to replace with bytes. The range must not exceed the bounds of the receiver.
/// - Parameter replacementBytes: data to insert into the receiver's contents.
/// - Parameter replacementLength: number of bytes to take from `replacementBytes`.
///
/// - Note: `replacementLength` must be less than or equal to the
/// size of the buffer pointed to by `replacementBytes`.
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
8 changes: 7 additions & 1 deletion TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,14 @@ class TestNSData: XCTestCase {
let replacement = makeData([8, 9, 10])
mData.replaceBytes(in: NSMakeRange(1, 3), withBytes: replacement.bytes,
length: 3)
let expected = makeData([0, 8, 9, 10, 0])
var expected = makeData([0, 8, 9, 10, 0])
XCTAssertEqual(mData, expected)

// test removing bytes with nil replacementBytes parameter
mData.replaceBytes(in: NSMakeRange(1, 3), withBytes: nil, length: 0)
expected = makeData([0, 0])
XCTAssertEqual(mData, expected)

}

func test_initDataWithCapacity() {
Expand Down