Skip to content

Commit 6855203

Browse files
committed
Aliging Data in Foundation into sync with the version in the overlay
1 parent d1272be commit 6855203

File tree

2 files changed

+361
-44
lines changed

2 files changed

+361
-44
lines changed

Foundation/Data.swift

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public final class _DataStorage {
143143
case .mutable:
144144
return try apply(UnsafeRawBufferPointer(start: _bytes?.advanced(by: range.lowerBound - _offset), count: Swift.min(range.count, _length)))
145145
case .customReference(let d):
146-
if __NSDataIsCompact(d) {
146+
if d._isCompact() {
147147
let len = d.length
148148
guard len > 0 else {
149149
return try apply(UnsafeRawBufferPointer(start: nil, count: 0))
@@ -155,26 +155,28 @@ public final class _DataStorage {
155155
let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
156156
var enumerated = 0
157157
d.enumerateBytes { (ptr, byteRange, stop) in
158-
if NSIntersectionRange(sliceRange, byteRange).length > 0 {
159-
let lower = Swift.max(byteRange.location, sliceRange.location)
160-
let upper = Swift.min(byteRange.location + byteRange.length, sliceRange.location + sliceRange.length)
161-
let offset = lower - byteRange.location
162-
let effectiveRange = NSRange(location: lower, length: upper - lower)
163-
if effectiveRange == sliceRange {
164-
memcpy(buffer.baseAddress!, ptr, effectiveRange.length)
158+
if byteRange.upperBound - _offset < range.lowerBound {
159+
// before the range that we are looking for...
160+
} else if byteRange.lowerBound - _offset > range.upperBound {
161+
stop.pointee = true // we are past the range in question so we need to stop
162+
} else {
163+
// the byteRange somehow intersects the range in question that we are looking for...
164+
let lower = Swift.max(byteRange.lowerBound - _offset, range.lowerBound)
165+
let upper = Swift.min(byteRange.upperBound - _offset, range.upperBound)
166+
167+
let len = upper - lower
168+
memcpy(buffer.baseAddress!.advanced(by: enumerated), ptr.advanced(by: lower - (byteRange.lowerBound - _offset)), len)
169+
enumerated += len
170+
171+
if upper == range.upperBound {
165172
stop.pointee = true
166-
} else {
167-
memcpy(buffer.baseAddress!.advanced(by: enumerated), ptr, effectiveRange.length)
168173
}
169-
enumerated += byteRange.length
170-
} else if sliceRange.location + sliceRange.length < byteRange.location {
171-
stop.pointee = true
172174
}
173175
}
174176
return try apply(UnsafeRawBufferPointer(buffer))
175177
}
176178
case .customMutableReference(let d):
177-
if __NSDataIsCompact(d) {
179+
if d._isCompact() {
178180
let len = d.length
179181
guard len > 0 else {
180182
return try apply(UnsafeRawBufferPointer(start: nil, count: 0))
@@ -186,19 +188,22 @@ public final class _DataStorage {
186188
let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
187189
var enumerated = 0
188190
d.enumerateBytes { (ptr, byteRange, stop) in
189-
if NSIntersectionRange(sliceRange, byteRange).length > 0 {
190-
let lower = Swift.max(byteRange.location, sliceRange.location)
191-
let upper = Swift.min(byteRange.location + byteRange.length, sliceRange.location + sliceRange.length)
192-
let effectiveRange = NSRange(location: lower, length: upper - lower)
193-
if effectiveRange == sliceRange {
194-
memcpy(buffer.baseAddress!, ptr, effectiveRange.length)
191+
if byteRange.upperBound - _offset < range.lowerBound {
192+
// before the range that we are looking for...
193+
} else if byteRange.lowerBound - _offset > range.upperBound {
194+
stop.pointee = true // we are past the range in question so we need to stop
195+
} else {
196+
// the byteRange somehow intersects the range in question that we are looking for...
197+
let lower = Swift.max(byteRange.lowerBound - _offset, range.lowerBound)
198+
let upper = Swift.min(byteRange.upperBound - _offset, range.upperBound)
199+
200+
let len = upper - lower
201+
memcpy(buffer.baseAddress!.advanced(by: enumerated), ptr.advanced(by: lower - (byteRange.lowerBound - _offset)), len)
202+
enumerated += len
203+
204+
if upper == range.upperBound {
195205
stop.pointee = true
196-
} else {
197-
memcpy(buffer.baseAddress!.advanced(by: enumerated), ptr, effectiveRange.length)
198206
}
199-
enumerated += byteRange.length
200-
} else if sliceRange.location + sliceRange.length < byteRange.location {
201-
stop.pointee = true
202207
}
203208
}
204209
return try apply(UnsafeRawBufferPointer(buffer))
@@ -518,7 +523,7 @@ public final class _DataStorage {
518523
case .mutable:
519524
return _bytes!.advanced(by: index - _offset).assumingMemoryBound(to: UInt8.self).pointee
520525
case .customReference(let d):
521-
if __NSDataIsCompact(d) {
526+
if d._isCompact() {
522527
return d.bytes.advanced(by: index - _offset).assumingMemoryBound(to: UInt8.self).pointee
523528
} else {
524529
var byte: UInt8 = 0
@@ -532,7 +537,7 @@ public final class _DataStorage {
532537
return byte
533538
}
534539
case .customMutableReference(let d):
535-
if __NSDataIsCompact(d) {
540+
if d._isCompact() {
536541
return d.bytes.advanced(by: index - _offset).assumingMemoryBound(to: UInt8.self).pointee
537542
} else {
538543
var byte: UInt8 = 0
@@ -863,7 +868,7 @@ public final class _DataStorage {
863868
}
864869

865870
public func withInteriorPointerReference<T>(_ range: Range<Int>, _ work: (NSData) throws -> T) rethrows -> T {
866-
if range.isEmpty {
871+
if range.count == 0 {
867872
return try work(NSData()) // zero length data can be optimized as a singleton
868873
}
869874

@@ -895,7 +900,7 @@ public final class _DataStorage {
895900
}
896901

897902
public func bridgedReference(_ range: Range<Int>) -> NSData {
898-
if range.isEmpty {
903+
if range.count == 0 {
899904
return NSData() // zero length data can be optimized as a singleton
900905
}
901906

@@ -1003,7 +1008,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
10031008

10041009
public typealias Index = Int
10051010
// FIXME: switch back to Range once swift 5.0 branch has PR #13342
1006-
public typealias Indices = CountableRange<Int>
1011+
public typealias Indices = Range<Int>
10071012

10081013
@_versioned internal var _backing : _DataStorage
10091014
@_versioned internal var _sliceRange: Range<Index>
@@ -1435,17 +1440,17 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
14351440
let nsRange : NSRange
14361441
if let r = range {
14371442
_validateRange(r)
1438-
nsRange = NSRange(location: r.lowerBound, length: r.upperBound - r.lowerBound)
1443+
nsRange = NSRange(location: r.lowerBound - startIndex, length: r.upperBound - r.lowerBound)
14391444
} else {
1440-
nsRange = NSRange(location: 0, length: _backing.length)
1445+
nsRange = NSRange(location: 0, length: count)
14411446
}
14421447
let result = _backing.withInteriorPointerReference(_sliceRange) {
14431448
$0.range(of: dataToFind, options: options, in: nsRange)
14441449
}
14451450
if result.location == NSNotFound {
14461451
return nil
14471452
}
1448-
return result.location..<(result.location + result.length)
1453+
return (result.location + startIndex)..<((result.location + startIndex) + result.length)
14491454
}
14501455

14511456
/// Enumerate the contents of the data.
@@ -1474,7 +1479,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
14741479
/// - parameter buffer: The buffer of bytes to append. The size is calculated from `SourceType` and `buffer.count`.
14751480
@inline(__always)
14761481
public mutating func append<SourceType>(_ buffer : UnsafeBufferPointer<SourceType>) {
1477-
if buffer.isEmpty { return }
1482+
if buffer.count == 0 { return }
14781483
if !isKnownUniquelyReferenced(&_backing) {
14791484
_backing = _backing.mutableCopy(_sliceRange)
14801485
}
@@ -1598,7 +1603,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
15981603
@inline(__always)
15991604
public func subdata(in range: Range<Index>) -> Data {
16001605
_validateRange(range)
1601-
if isEmpty {
1606+
if count == 0 {
16021607
return Data()
16031608
}
16041609
return _backing.subdata(in: range)
@@ -1744,7 +1749,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
17441749
return i + 1
17451750
}
17461751

1747-
public var indices: CountableRange<Int> {
1752+
public var indices: Range<Int> {
17481753
@inline(__always)
17491754
get {
17501755
return startIndex..<endIndex

0 commit comments

Comments
 (0)