@@ -38,6 +38,22 @@ internal func __NSDataIsCompact(_ data: NSData) -> Bool {
38
38
import _SwiftFoundationOverlayShims
39
39
import _SwiftCoreFoundationOverlayShims
40
40
41
+ internal func __NSDataIsCompact( _ data: NSData ) -> Bool {
42
+ if #available( OSX 10 . 10 , iOS 8 . 0 , tvOS 9 . 0 , watchOS 2 . 0 , * ) {
43
+ return data. _isCompact ( )
44
+ } else {
45
+ var compact = true
46
+ let len = data. length
47
+ data. enumerateBytes { ( _, byteRange, stop) in
48
+ if byteRange. length != len {
49
+ compact = false
50
+ }
51
+ stop. pointee = true
52
+ }
53
+ return compact
54
+ }
55
+ }
56
+
41
57
#endif
42
58
43
59
public final class _DataStorage {
@@ -127,7 +143,7 @@ public final class _DataStorage {
127
143
case . mutable:
128
144
return try apply ( UnsafeRawBufferPointer ( start: _bytes? . advanced ( by: range. lowerBound - _offset) , count: Swift . min ( range. count, _length) ) )
129
145
case . customReference( let d) :
130
- if d . _isCompact ( ) {
146
+ if __NSDataIsCompact ( d ) {
131
147
let len = d. length
132
148
guard len > 0 else {
133
149
return try apply ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
@@ -136,6 +152,7 @@ public final class _DataStorage {
136
152
} else {
137
153
var buffer = UnsafeMutableRawBufferPointer . allocate ( byteCount: range. count, alignment: MemoryLayout< UInt> . alignment)
138
154
defer { buffer. deallocate ( ) }
155
+
139
156
let sliceRange = NSRange ( location: range. lowerBound - _offset, length: range. count)
140
157
var enumerated = 0
141
158
d. enumerateBytes { ( ptr, byteRange, stop) in
@@ -160,7 +177,7 @@ public final class _DataStorage {
160
177
return try apply ( UnsafeRawBufferPointer ( buffer) )
161
178
}
162
179
case . customMutableReference( let d) :
163
- if d . _isCompact ( ) {
180
+ if __NSDataIsCompact ( d ) {
164
181
let len = d. length
165
182
guard len > 0 else {
166
183
return try apply ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
@@ -169,6 +186,7 @@ public final class _DataStorage {
169
186
} else {
170
187
var buffer = UnsafeMutableRawBufferPointer . allocate ( byteCount: range. count, alignment: MemoryLayout< UInt> . alignment)
171
188
defer { buffer. deallocate ( ) }
189
+
172
190
let sliceRange = NSRange ( location: range. lowerBound - _offset, length: range. count)
173
191
var enumerated = 0
174
192
d. enumerateBytes { ( ptr, byteRange, stop) in
@@ -507,7 +525,7 @@ public final class _DataStorage {
507
525
case . mutable:
508
526
return _bytes!. advanced ( by: index - _offset) . assumingMemoryBound ( to: UInt8 . self) . pointee
509
527
case . customReference( let d) :
510
- if d . _isCompact ( ) {
528
+ if __NSDataIsCompact ( d ) {
511
529
return d. bytes. advanced ( by: index - _offset) . assumingMemoryBound ( to: UInt8 . self) . pointee
512
530
} else {
513
531
var byte : UInt8 = 0
@@ -521,7 +539,7 @@ public final class _DataStorage {
521
539
return byte
522
540
}
523
541
case . customMutableReference( let d) :
524
- if d . _isCompact ( ) {
542
+ if __NSDataIsCompact ( d ) {
525
543
return d. bytes. advanced ( by: index - _offset) . assumingMemoryBound ( to: UInt8 . self) . pointee
526
544
} else {
527
545
var byte : UInt8 = 0
@@ -1541,7 +1559,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1541
1559
/// - parameter buffer: The replacement bytes.
1542
1560
@inline ( __always)
1543
1561
public mutating func replaceSubrange< SourceType> ( _ subrange: Range < Index > , with buffer: UnsafeBufferPointer < SourceType > ) {
1544
- guard !buffer. isEmpty else { return }
1562
+ guard !buffer. isEmpty else { return }
1545
1563
replaceSubrange ( subrange, with: buffer. baseAddress!, count: buffer. count * MemoryLayout< SourceType> . stride)
1546
1564
}
1547
1565
0 commit comments