Skip to content

Commit a7a902a

Browse files
authored
Merge pull request #930 from phausler/data_slice_iteration
2 parents 95f2c1e + ed9e003 commit a7a902a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Foundation/Data.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
11261126
@inline(__always)
11271127
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: NSRange) {
11281128
if range.length == 0 { return }
1129-
memcpy(UnsafeMutableRawPointer(pointer), _backing.bytes!.advanced(by: range.location + _sliceRange.lowerBound), range.length)
1129+
memcpy(UnsafeMutableRawPointer(pointer), _backing.bytes!.advanced(by: range.location), range.length)
11301130
}
11311131

11321132
/// Copy a subset of the contents of the data to a pointer.

TestFoundation/TestNSData.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class TestNSData: XCTestCase {
9696
("test_sliceAppending", test_sliceAppending),
9797
("test_replaceSubrange", test_replaceSubrange),
9898
("test_sliceWithUnsafeBytes", test_sliceWithUnsafeBytes),
99+
("test_sliceIteration", test_sliceIteration),
99100
]
100101
}
101102

@@ -1091,5 +1092,16 @@ extension TestNSData {
10911092
}
10921093
XCTAssertEqual(segment, [UInt8(2), UInt8(3)])
10931094
}
1095+
1096+
func test_sliceIteration() {
1097+
let base = Data([0, 1, 2, 3, 4, 5])
1098+
let slice = base[2..<4]
1099+
var found = [UInt8]()
1100+
for byte in slice {
1101+
found.append(byte)
1102+
}
1103+
XCTAssertEqual(found[0], 2)
1104+
XCTAssertEqual(found[1], 3)
1105+
}
10941106
}
10951107

0 commit comments

Comments
 (0)