Skip to content

Commit 42b387c

Browse files
authored
Merge pull request swiftlang#2310 from ikesyo/fix-dataprotocol-lastrange
Fix `DataProtocol.lastRange(of:)` (and cherry-pick Data.swift changes from the overlay)
2 parents 3ab5b52 + 8f21aa7 commit 42b387c

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Foundation/Data.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
721721
return Int(length)
722722
}
723723
set(newValue) {
724-
precondition(newValue <= MemoryLayout<Buffer>.size)
724+
assert(newValue <= MemoryLayout<Buffer>.size)
725725
length = UInt8(newValue)
726726
}
727727
}
@@ -810,7 +810,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
810810
assert(subrange.upperBound <= MemoryLayout<Buffer>.size)
811811
assert(count - (subrange.upperBound - subrange.lowerBound) + replacementLength <= MemoryLayout<Buffer>.size)
812812
precondition(subrange.lowerBound <= length, "index \(subrange.lowerBound) is out of bounds of 0..<\(length)")
813-
precondition(subrange.upperBound <= length, "index \(subrange.lowerBound) is out of bounds of 0..<\(length)")
813+
precondition(subrange.upperBound <= length, "index \(subrange.upperBound) is out of bounds of 0..<\(length)")
814814
let currentLength = count
815815
let resultingLength = currentLength - (subrange.upperBound - subrange.lowerBound) + replacementLength
816816
let shift = resultingLength - currentLength
@@ -2223,7 +2223,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
22232223

22242224
@inlinable // This is @inlinable as trivially forwarding.
22252225
internal func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: Range<Int>) {
2226-
if range.upperBound - range.lowerBound == 0 { return }
2226+
if range.isEmpty { return }
22272227
_representation.copyBytes(to: pointer, from: range)
22282228
}
22292229

Foundation/DataProtocol.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension DataProtocol {
8080
}
8181

8282
public func lastRange<D: DataProtocol>(of data: D) -> Range<Index>? {
83-
return self.firstRange(of: data, in: self.startIndex ..< self.endIndex)
83+
return self.lastRange(of: data, in: self.startIndex ..< self.endIndex)
8484
}
8585

8686
@discardableResult

0 commit comments

Comments
 (0)