Skip to content

Synchronize the latest revision of Data.swift from the overlay #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ internal func __NSDataIsCompact(_ data: NSData) -> Bool {
import _SwiftFoundationOverlayShims
import _SwiftCoreFoundationOverlayShims

internal func __NSDataIsCompact(_ data: NSData) -> Bool {
if #available(OSX 10.10, iOS 8.0, tvOS 9.0, watchOS 2.0, *) {
return data._isCompact()
} else {
var compact = true
let len = data.length
data.enumerateBytes { (_, byteRange, stop) in
if byteRange.length != len {
compact = false
}
stop.pointee = true
}
return compact
}
}

#endif

public final class _DataStorage {
Expand Down Expand Up @@ -127,7 +143,7 @@ public final class _DataStorage {
case .mutable:
return try apply(UnsafeRawBufferPointer(start: _bytes?.advanced(by: range.lowerBound - _offset), count: Swift.min(range.count, _length)))
case .customReference(let d):
if d._isCompact() {
if __NSDataIsCompact(d) {
let len = d.length
guard len > 0 else {
return try apply(UnsafeRawBufferPointer(start: nil, count: 0))
Expand All @@ -136,6 +152,7 @@ public final class _DataStorage {
} else {
var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: range.count, alignment: MemoryLayout<UInt>.alignment)
defer { buffer.deallocate() }

let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
var enumerated = 0
d.enumerateBytes { (ptr, byteRange, stop) in
Expand All @@ -160,7 +177,7 @@ public final class _DataStorage {
return try apply(UnsafeRawBufferPointer(buffer))
}
case .customMutableReference(let d):
if d._isCompact() {
if __NSDataIsCompact(d) {
let len = d.length
guard len > 0 else {
return try apply(UnsafeRawBufferPointer(start: nil, count: 0))
Expand All @@ -169,6 +186,7 @@ public final class _DataStorage {
} else {
var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: range.count, alignment: MemoryLayout<UInt>.alignment)
defer { buffer.deallocate() }

let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
var enumerated = 0
d.enumerateBytes { (ptr, byteRange, stop) in
Expand Down Expand Up @@ -507,7 +525,7 @@ public final class _DataStorage {
case .mutable:
return _bytes!.advanced(by: index - _offset).assumingMemoryBound(to: UInt8.self).pointee
case .customReference(let d):
if d._isCompact() {
if __NSDataIsCompact(d) {
return d.bytes.advanced(by: index - _offset).assumingMemoryBound(to: UInt8.self).pointee
} else {
var byte: UInt8 = 0
Expand All @@ -521,7 +539,7 @@ public final class _DataStorage {
return byte
}
case .customMutableReference(let d):
if d._isCompact() {
if __NSDataIsCompact(d) {
return d.bytes.advanced(by: index - _offset).assumingMemoryBound(to: UInt8.self).pointee
} else {
var byte: UInt8 = 0
Expand Down Expand Up @@ -993,8 +1011,8 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
public typealias Index = Int
public typealias Indices = Range<Int>

@_versioned internal var _backing : _DataStorage
@_versioned internal var _sliceRange: Range<Index>
@usableFromInline internal var _backing : _DataStorage
@usableFromInline internal var _sliceRange: Range<Index>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it now okay to bring it in?

#1502

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is targeting master and the unit tests seem to be good :)



// A standard or custom deallocator for `Data`.
Expand Down Expand Up @@ -1245,18 +1263,18 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
}
}

@_versioned
@usableFromInline
internal init(backing: _DataStorage, range: Range<Index>) {
_backing = backing
_sliceRange = range
}

@_versioned
@usableFromInline
internal func _validateIndex(_ index: Int, message: String? = nil) {
precondition(_sliceRange.contains(index), message ?? "Index \(index) is out of bounds of range \(_sliceRange)")
}

@_versioned
@usableFromInline
internal func _validateRange<R: RangeExpression>(_ range: R) where R.Bound == Int {
let lower = R.Bound(_sliceRange.lowerBound)
let upper = R.Bound(_sliceRange.upperBound)
Expand Down Expand Up @@ -1541,7 +1559,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
/// - parameter buffer: The replacement bytes.
@inline(__always)
public mutating func replaceSubrange<SourceType>(_ subrange: Range<Index>, with buffer: UnsafeBufferPointer<SourceType>) {
guard !buffer.isEmpty else { return }
guard !buffer.isEmpty else { return }
replaceSubrange(subrange, with: buffer.baseAddress!, count: buffer.count * MemoryLayout<SourceType>.stride)
}

Expand Down