Skip to content

Commit 435b6a9

Browse files
authored
Merge pull request #2618 from spevans/pr_sync_dataprotocol_52
[5.2] Sync NSData+DataProtocol from the SDK overlay
2 parents 415d1a6 + abc49a1 commit 435b6a9

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

Foundation/NSData+DataProtocol.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212

1313

1414
extension NSData : DataProtocol {
15-
15+
1616
@nonobjc
1717
public var startIndex: Int { return 0 }
18-
18+
1919
@nonobjc
2020
public var endIndex: Int { return length }
21-
21+
2222
@nonobjc
2323
public func lastRange<D, R>(of data: D, in r: R) -> Range<Int>? where D : DataProtocol, R : RangeExpression, NSData.Index == R.Bound {
2424
return Range<Int>(range(of: Data(data), options: .backwards, in: NSRange(r)))
2525
}
26-
26+
2727
@nonobjc
2828
public func firstRange<D, R>(of data: D, in r: R) -> Range<Int>? where D : DataProtocol, R : RangeExpression, NSData.Index == R.Bound {
2929
return Range<Int>(range(of: Data(data), in: NSRange(r)))
3030
}
31-
31+
3232
@nonobjc
3333
public var regions: [Data] {
3434
var datas = [Data]()
@@ -39,13 +39,15 @@ extension NSData : DataProtocol {
3939
}
4040
return datas
4141
}
42-
42+
4343
@nonobjc
4444
public subscript(position: Int) -> UInt8 {
4545
var byte = UInt8(0)
46+
var offset = position
4647
enumerateBytes { (ptr, range, stop) in
47-
if range.location <= position && position < range.upperBound {
48-
byte = ptr.load(fromByteOffset: range.location - position, as: UInt8.self)
48+
offset -= range.lowerBound
49+
if range.contains(position) {
50+
byte = ptr.load(fromByteOffset: offset, as: UInt8.self)
4951
stop.pointee = true
5052
}
5153
}

TestFoundation/TestNSData.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ class TestNSData: LoopbackServerTest {
528528
("test_replaceSubrangeReferencingMutable", test_replaceSubrangeReferencingMutable),
529529
("test_replaceSubrangeReferencingImmutable", test_replaceSubrangeReferencingImmutable),
530530
("test_rangeOfSlice", test_rangeOfSlice),
531+
("test_nsdataSequence", test_nsdataSequence),
532+
("test_dispatchSequence", test_dispatchSequence),
531533
]
532534
}
533535

@@ -4528,5 +4530,38 @@ extension TestNSData {
45284530
let decodedData = NSData(coder: unarchiver)
45294531
XCTAssertEqual(data, decodedData)
45304532
}
4533+
4534+
func test_nsdataSequence() {
4535+
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
4536+
let bytes: [UInt8] = Array(0x00...0xFF)
4537+
let data = bytes.withUnsafeBytes { NSData(bytes: $0.baseAddress, length: $0.count) }
4538+
4539+
for byte in bytes {
4540+
expectEqual(data[Int(byte)], byte)
4541+
}
4542+
}
4543+
}
4544+
4545+
func test_dispatchSequence() {
4546+
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
4547+
let bytes1: [UInt8] = Array(0x00..<0xF0)
4548+
let bytes2: [UInt8] = Array(0xF0..<0xFF)
4549+
var data = DispatchData.empty
4550+
bytes1.withUnsafeBytes {
4551+
data.append($0)
4552+
}
4553+
bytes2.withUnsafeBytes {
4554+
data.append($0)
4555+
}
4556+
4557+
for byte in bytes1 {
4558+
expectEqual(data[Int(byte)], byte)
4559+
}
4560+
for byte in bytes2 {
4561+
expectEqual(data[Int(byte)], byte)
4562+
}
4563+
}
4564+
}
4565+
45314566
}
45324567

0 commit comments

Comments
 (0)