@@ -143,7 +143,7 @@ public final class _DataStorage {
143
143
case . mutable:
144
144
return try apply ( UnsafeRawBufferPointer ( start: _bytes? . advanced ( by: range. lowerBound - _offset) , count: Swift . min ( range. count, _length) ) )
145
145
case . customReference( let d) :
146
- if __NSDataIsCompact ( d ) {
146
+ if d . _isCompact ( ) {
147
147
let len = d. length
148
148
guard len > 0 else {
149
149
return try apply ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
@@ -155,26 +155,28 @@ public final class _DataStorage {
155
155
let sliceRange = NSRange ( location: range. lowerBound - _offset, length: range. count)
156
156
var enumerated = 0
157
157
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 {
165
172
stop. pointee = true
166
- } else {
167
- memcpy ( buffer. baseAddress!. advanced ( by: enumerated) , ptr, effectiveRange. length)
168
173
}
169
- enumerated += byteRange. length
170
- } else if sliceRange. location + sliceRange. length < byteRange. location {
171
- stop. pointee = true
172
174
}
173
175
}
174
176
return try apply ( UnsafeRawBufferPointer ( buffer) )
175
177
}
176
178
case . customMutableReference( let d) :
177
- if __NSDataIsCompact ( d ) {
179
+ if d . _isCompact ( ) {
178
180
let len = d. length
179
181
guard len > 0 else {
180
182
return try apply ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
@@ -186,19 +188,22 @@ public final class _DataStorage {
186
188
let sliceRange = NSRange ( location: range. lowerBound - _offset, length: range. count)
187
189
var enumerated = 0
188
190
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 {
195
205
stop. pointee = true
196
- } else {
197
- memcpy ( buffer. baseAddress!. advanced ( by: enumerated) , ptr, effectiveRange. length)
198
206
}
199
- enumerated += byteRange. length
200
- } else if sliceRange. location + sliceRange. length < byteRange. location {
201
- stop. pointee = true
202
207
}
203
208
}
204
209
return try apply ( UnsafeRawBufferPointer ( buffer) )
@@ -518,7 +523,7 @@ public final class _DataStorage {
518
523
case . mutable:
519
524
return _bytes!. advanced ( by: index - _offset) . assumingMemoryBound ( to: UInt8 . self) . pointee
520
525
case . customReference( let d) :
521
- if __NSDataIsCompact ( d ) {
526
+ if d . _isCompact ( ) {
522
527
return d. bytes. advanced ( by: index - _offset) . assumingMemoryBound ( to: UInt8 . self) . pointee
523
528
} else {
524
529
var byte : UInt8 = 0
@@ -532,7 +537,7 @@ public final class _DataStorage {
532
537
return byte
533
538
}
534
539
case . customMutableReference( let d) :
535
- if __NSDataIsCompact ( d ) {
540
+ if d . _isCompact ( ) {
536
541
return d. bytes. advanced ( by: index - _offset) . assumingMemoryBound ( to: UInt8 . self) . pointee
537
542
} else {
538
543
var byte : UInt8 = 0
@@ -863,7 +868,7 @@ public final class _DataStorage {
863
868
}
864
869
865
870
public func withInteriorPointerReference< T> ( _ range: Range < Int > , _ work: ( NSData ) throws -> T ) rethrows -> T {
866
- if range. isEmpty {
871
+ if range. count == 0 {
867
872
return try work ( NSData ( ) ) // zero length data can be optimized as a singleton
868
873
}
869
874
@@ -895,7 +900,7 @@ public final class _DataStorage {
895
900
}
896
901
897
902
public func bridgedReference( _ range: Range < Int > ) -> NSData {
898
- if range. isEmpty {
903
+ if range. count == 0 {
899
904
return NSData ( ) // zero length data can be optimized as a singleton
900
905
}
901
906
@@ -1003,7 +1008,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1003
1008
1004
1009
public typealias Index = Int
1005
1010
// 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 >
1007
1012
1008
1013
@_versioned internal var _backing : _DataStorage
1009
1014
@_versioned internal var _sliceRange : Range < Index >
@@ -1435,17 +1440,17 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1435
1440
let nsRange : NSRange
1436
1441
if let r = range {
1437
1442
_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)
1439
1444
} else {
1440
- nsRange = NSRange ( location: 0 , length: _backing . length )
1445
+ nsRange = NSRange ( location: 0 , length: count )
1441
1446
}
1442
1447
let result = _backing. withInteriorPointerReference ( _sliceRange) {
1443
1448
$0. range ( of: dataToFind, options: options, in: nsRange)
1444
1449
}
1445
1450
if result. location == NSNotFound {
1446
1451
return nil
1447
1452
}
1448
- return result. location..< ( result. location + result. length)
1453
+ return ( result. location + startIndex ) ..< ( ( result. location + startIndex ) + result. length)
1449
1454
}
1450
1455
1451
1456
/// Enumerate the contents of the data.
@@ -1474,7 +1479,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1474
1479
/// - parameter buffer: The buffer of bytes to append. The size is calculated from `SourceType` and `buffer.count`.
1475
1480
@inline ( __always)
1476
1481
public mutating func append< SourceType> ( _ buffer : UnsafeBufferPointer < SourceType > ) {
1477
- if buffer. isEmpty { return }
1482
+ if buffer. count == 0 { return }
1478
1483
if !isKnownUniquelyReferenced( & _backing) {
1479
1484
_backing = _backing. mutableCopy ( _sliceRange)
1480
1485
}
@@ -1598,7 +1603,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1598
1603
@inline ( __always)
1599
1604
public func subdata( in range: Range < Index > ) -> Data {
1600
1605
_validateRange ( range)
1601
- if isEmpty {
1606
+ if count == 0 {
1602
1607
return Data ( )
1603
1608
}
1604
1609
return _backing. subdata ( in: range)
@@ -1744,7 +1749,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1744
1749
return i + 1
1745
1750
}
1746
1751
1747
- public var indices : CountableRange < Int > {
1752
+ public var indices : Range < Int > {
1748
1753
@inline ( __always)
1749
1754
get {
1750
1755
return startIndex..< endIndex
0 commit comments