Skip to content

Commit 4ba3605

Browse files
committed
Make firstRange and lastRange methods of the DataProtocol mirror macOS foundation when data is empty
1 parent 2232811 commit 4ba3605

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Sources/Foundation/DataProtocol.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ extension DataProtocol {
137137
}
138138

139139
public func firstRange<D: DataProtocol, R: RangeExpression>(of data: D, in range: R) -> Range<Index>? where R.Bound == Index {
140+
guard !data.isEmpty else {
141+
return nil
142+
}
140143
let r = range.relative(to: self)
141144
let rangeCount = distance(from: r.lowerBound, to: r.upperBound)
142145
if rangeCount < data.count {
@@ -166,6 +169,9 @@ extension DataProtocol {
166169
}
167170

168171
public func lastRange<D: DataProtocol, R: RangeExpression>(of data: D, in range: R) -> Range<Index>? where R.Bound == Index {
172+
guard !data.isEmpty else {
173+
return nil
174+
}
169175
let r = range.relative(to: self)
170176
let rangeCount = distance(from: r.lowerBound, to: r.upperBound)
171177
if rangeCount < data.count {

0 commit comments

Comments
 (0)