Skip to content
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
16 changes: 9 additions & 7 deletions Sources/Foundation/DataProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ extension DataProtocol {
return nil
}
var haystackIndex = r.lowerBound
let haystackEnd = index(r.upperBound, offsetBy: -data.count)
while haystackIndex < haystackEnd {
let haystackEnd = r.upperBound
let haystackSearchEnd = index(r.upperBound, offsetBy: -data.count)
while haystackIndex <= haystackSearchEnd {
var compareIndex = haystackIndex
var needleIndex = data.startIndex
let needleEnd = data.endIndex
Expand Down Expand Up @@ -172,19 +173,20 @@ extension DataProtocol {
return nil
}
var haystackIndex = r.upperBound
let haystackStart = index(r.lowerBound, offsetBy: data.count)
while haystackIndex > haystackStart {
let haystackStart = r.lowerBound
let haystackSearchStart = index(r.lowerBound, offsetBy: data.count)
while haystackIndex >= haystackSearchStart {
var compareIndex = haystackIndex
var needleIndex = data.endIndex
let needleStart = data.startIndex
var matched = true
while compareIndex > haystackStart && needleIndex > needleStart {
while compareIndex >= haystackStart && needleIndex > needleStart {
needleIndex = data.index(before: needleIndex)
compareIndex = index(before: compareIndex)
if self[compareIndex] != data[needleIndex] {
matched = false
break
}
needleIndex = data.index(before: needleIndex)
compareIndex = index(before: compareIndex)
}
if matched {
return compareIndex..<haystackIndex
Expand Down
24 changes: 24 additions & 0 deletions Tests/Foundation/Tests/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class TestNSData: LoopbackServerTest {
("testCustomDeallocator", testCustomDeallocator),
("testDataInSet", testDataInSet),
("testEquality", testEquality),
("testFirstRange", testFirstRange),
("testLastRange", testLastRange),
("testGenericAlgorithms", testGenericAlgorithms),
("testInitializationWithArray", testInitializationWithArray),
("testInsertData", testInsertData),
Expand Down Expand Up @@ -1181,6 +1183,28 @@ extension TestNSData {
XCTAssertTrue(d1 == d2, "Data should be equal")
}

func testFirstRange() {
let d = Data([1, 2, 3, 1, 2, 3, 0])
XCTAssertEqual(d.firstRange(of: Data([1])), 0..<1)
XCTAssertEqual(d.firstRange(of: Data([2])), 1..<2)
XCTAssertEqual(d.firstRange(of: Data([3])), 2..<3)
XCTAssertEqual(d.firstRange(of: Data([0])), 6..<7)
XCTAssertEqual(d.firstRange(of: Data([2, 3, 0])), 4..<7)
XCTAssertEqual(d.firstRange(of: Data([3, 1, 2])), 2..<5)
XCTAssertEqual(d.firstRange(of: Data([1, 2, 3, 1, 2, 3, 0])), 0..<7)
}

func testLastRange() {
let d = Data([0, 1, 2, 3, 1, 2, 3])
XCTAssertEqual(d.lastRange(of: Data([1])), 4..<5)
XCTAssertEqual(d.lastRange(of: Data([2])), 5..<6)
XCTAssertEqual(d.lastRange(of: Data([3])), 6..<7)
XCTAssertEqual(d.lastRange(of: Data([0])), 0..<1)
XCTAssertEqual(d.lastRange(of: Data([0, 1, 2])), 0..<3)
XCTAssertEqual(d.lastRange(of: Data([1, 2, 3])), 4..<7)
XCTAssertEqual(d.lastRange(of: Data([0, 1, 2, 3, 1, 2, 3])), 0..<7)
}

func testDataInSet() {
let d1 = dataFrom("Hello")
let d2 = dataFrom("Hello")
Expand Down