Skip to content

Commit 6171d68

Browse files
author
Itai Ferber
authored
Merge pull request #25065 from ikesyo/fix-dataprotocol-lastrange
Fix `DataProtocol.lastRange(of:)` (and cherry-pick Data.swift changes from swift-5.0-branch)
2 parents ef069a3 + 55b300f commit 6171d68

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

stdlib/public/Darwin/Foundation/Data.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
688688
return Int(length)
689689
}
690690
set(newValue) {
691-
precondition(newValue <= MemoryLayout<Buffer>.size)
691+
assert(newValue <= MemoryLayout<Buffer>.size)
692692
length = UInt8(newValue)
693693
}
694694
}
@@ -777,7 +777,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
777777
assert(subrange.upperBound <= MemoryLayout<Buffer>.size)
778778
assert(count - (subrange.upperBound - subrange.lowerBound) + replacementLength <= MemoryLayout<Buffer>.size)
779779
precondition(subrange.lowerBound <= length, "index \(subrange.lowerBound) is out of bounds of 0..<\(length)")
780-
precondition(subrange.upperBound <= length, "index \(subrange.lowerBound) is out of bounds of 0..<\(length)")
780+
precondition(subrange.upperBound <= length, "index \(subrange.upperBound) is out of bounds of 0..<\(length)")
781781
let currentLength = count
782782
let resultingLength = currentLength - (subrange.upperBound - subrange.lowerBound) + replacementLength
783783
let shift = resultingLength - currentLength
@@ -2188,7 +2188,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
21882188

21892189
@inlinable // This is @inlinable as trivially forwarding.
21902190
internal func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: Range<Int>) {
2191-
if range.upperBound - range.lowerBound == 0 { return }
2191+
if range.isEmpty { return }
21922192
_representation.copyBytes(to: pointer, from: range)
21932193
}
21942194

stdlib/public/Darwin/Foundation/DataProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extension DataProtocol {
8686
}
8787

8888
public func lastRange<D: DataProtocol>(of data: D) -> Range<Index>? {
89-
return self.firstRange(of: data, in: self.startIndex ..< self.endIndex)
89+
return self.lastRange(of: data, in: self.startIndex ..< self.endIndex)
9090
}
9191

9292
@discardableResult

0 commit comments

Comments
 (0)