Skip to content

Correct Data slice iteration to be indexed by the startIndex to the endIndex #930

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 1 commit into from
Apr 3, 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
2 changes: 1 addition & 1 deletion Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
@inline(__always)
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: NSRange) {
if range.length == 0 { return }
memcpy(UnsafeMutableRawPointer(pointer), _backing.bytes!.advanced(by: range.location + _sliceRange.lowerBound), range.length)
memcpy(UnsafeMutableRawPointer(pointer), _backing.bytes!.advanced(by: range.location), range.length)
}

/// Copy a subset of the contents of the data to a pointer.
Expand Down
12 changes: 12 additions & 0 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class TestNSData: XCTestCase {
("test_sliceAppending", test_sliceAppending),
("test_replaceSubrange", test_replaceSubrange),
("test_sliceWithUnsafeBytes", test_sliceWithUnsafeBytes),
("test_sliceIteration", test_sliceIteration),
]
}

Expand Down Expand Up @@ -1091,5 +1092,16 @@ extension TestNSData {
}
XCTAssertEqual(segment, [UInt8(2), UInt8(3)])
}

func test_sliceIteration() {
let base = Data([0, 1, 2, 3, 4, 5])
let slice = base[2..<4]
var found = [UInt8]()
for byte in slice {
found.append(byte)
}
XCTAssertEqual(found[0], 2)
XCTAssertEqual(found[1], 3)
}
}