Skip to content

SR-5810: Fix slice data range calculation when inserting/deleting an element #1202

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 1 commit 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
4 changes: 2 additions & 2 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,8 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl

@_versioned
internal init(backing: _DataStorage, range: Range<Index>) {
_backing = backing
_sliceRange = range
_backing = backing.mutableCopy(range)
_sliceRange = 0..<range.count
}

@_versioned
Expand Down
29 changes: 25 additions & 4 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TestNSData: XCTestCase {
("testCopyBytes_oversized", testCopyBytes_oversized),
("testCopyBytes_ranges", testCopyBytes_ranges),
("testCopyBytes_undersized", testCopyBytes_undersized),
("testCopyBytes", testCopyBytes),
("testCopyBytes", testCopyBytes),
("testCustomDeallocator", testCustomDeallocator),
("testDataInSet", testDataInSet),
("testEquality", testEquality),
Expand All @@ -65,8 +65,6 @@ class TestNSData: XCTestCase {
("testReplaceSubrange3", testReplaceSubrange3),
("testReplaceSubrange4", testReplaceSubrange4),
("testReplaceSubrange5", testReplaceSubrange5),


("test_description", test_description),
("test_emptyDescription", test_emptyDescription),
("test_longDescription", test_longDescription),
Expand Down Expand Up @@ -95,11 +93,12 @@ class TestNSData: XCTestCase {
("test_initDataWithCount", test_initDataWithCount),
("test_emptyStringToData", test_emptyStringToData),
("test_repeatingValueInitialization", test_repeatingValueInitialization),

("test_sliceAppending", test_sliceAppending),
("test_replaceSubrange", test_replaceSubrange),
("test_sliceWithUnsafeBytes", test_sliceWithUnsafeBytes),
("test_sliceIteration", test_sliceIteration),
("test_sliceInsertion", test_sliceInsertion),
("test_sliceDeletion", test_sliceDeletion),
]
}

Expand Down Expand Up @@ -1164,5 +1163,27 @@ extension TestNSData {
XCTAssertEqual(found[0], 2)
XCTAssertEqual(found[1], 3)
}

func test_sliceInsertion() {
// https://bugs.swift.org/browse/SR-5810
let baseData = Data([0, 1, 2, 3, 4, 5])
var sliceData = baseData[2..<4]
let elementToInsert: UInt8 = 0x07
sliceData.insert(elementToInsert, at: sliceData.startIndex)
XCTAssertEqual(sliceData.first, elementToInsert)
XCTAssertEqual(sliceData.startIndex, 0)
XCTAssertEqual(sliceData.endIndex, Array(sliceData).count)
}

func test_sliceDeletion() {
// https://bugs.swift.org/browse/SR-5810
let baseData = Data([0, 1, 2, 3, 4, 5])
let sliceData = baseData[2..<4]
var mutableSliceData = sliceData
mutableSliceData.removeSubrange(mutableSliceData.startIndex..<mutableSliceData.startIndex.advanced(by: 1))
XCTAssertEqual(sliceData[sliceData.startIndex.advanced(by: 1)], mutableSliceData.first)
XCTAssertEqual(mutableSliceData.startIndex, 0)
XCTAssertEqual(mutableSliceData.endIndex, Array(mutableSliceData).count)
}
}