Skip to content

[stdlib] Remove _HasContiguousBytes #22028

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions stdlib/public/Darwin/Foundation/NSStringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,7 @@ extension String {
/// Unicode characters using a given `encoding`.
public init?(data: __shared Data, encoding: Encoding) {
if encoding == .utf8,
let str = data.withUnsafeBytes({
String._tryFromUTF8($0.bindMemory(to: UInt8.self))
}) {
let str = data.withUnsafeBytes(String._tryFromUTF8) {
self = str
return
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/AssertCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal func _assertionFailure(
) -> Never {
prefix.withUTF8Buffer {
(prefix) -> Void in
message._withUnsafeBufferPointerToUTF8 {
message._withUTF8 {
(messageUTF8) -> Void in
file.withUTF8Buffer {
(file) -> Void in
Expand Down Expand Up @@ -145,7 +145,7 @@ internal func _assertionFailure(
) -> Never {
prefix.withUTF8Buffer {
(prefix) -> Void in
message._withUnsafeBufferPointerToUTF8 {
message._withUTF8 {
(messageUTF8) -> Void in
_swift_stdlib_reportFatalError(
prefix.baseAddress!, CInt(prefix.count),
Expand Down
91 changes: 3 additions & 88 deletions stdlib/public/core/ContiguouslyStored.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,7 @@
//
//===----------------------------------------------------------------------===//

@usableFromInline
internal protocol _HasContiguousBytes {
func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R

var _providesContiguousBytesNoCopy: Bool { get }
}
extension _HasContiguousBytes {
@inlinable
var _providesContiguousBytesNoCopy: Bool {
@inline(__always) get { return true }
}
}
extension Array: _HasContiguousBytes {
@inlinable
var _providesContiguousBytesNoCopy: Bool {
@inline(__always) get {
#if _runtime(_ObjC)
return _buffer._isNative
#else
return true
#endif
}
}
}
extension ContiguousArray: _HasContiguousBytes {}
extension UnsafeBufferPointer: _HasContiguousBytes {
@inlinable @inline(__always)
func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R {
let ptr = UnsafeRawPointer(self.baseAddress._unsafelyUnwrappedUnchecked)
let len = self.count &* MemoryLayout<Element>.stride
return try body(UnsafeRawBufferPointer(start: ptr, count: len))
}
}
extension UnsafeMutableBufferPointer: _HasContiguousBytes {
@inlinable @inline(__always)
func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R {
let ptr = UnsafeRawPointer(self.baseAddress._unsafelyUnwrappedUnchecked)
let len = self.count &* MemoryLayout<Element>.stride
return try body(UnsafeRawBufferPointer(start: ptr, count: len))
}
}
extension UnsafeRawBufferPointer: _HasContiguousBytes {
@inlinable @inline(__always)
func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R {
return try body(self)
}
}
extension UnsafeMutableRawBufferPointer: _HasContiguousBytes {
@inlinable @inline(__always)
func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R {
return try body(UnsafeRawBufferPointer(self))
}
}
extension String: _HasContiguousBytes {
@inlinable
internal var _providesContiguousBytesNoCopy: Bool {
@inline(__always) get { return self._guts.isFastUTF8 }
}
extension String {

@inlinable @inline(__always)
internal func _withUTF8<R>(
Expand All @@ -93,20 +26,9 @@ extension String: _HasContiguousBytes {
try body($0)
}
}

@inlinable @inline(__always)
internal func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R {
return try self._withUTF8 { return try body(UnsafeRawBufferPointer($0)) }
}
}
extension Substring: _HasContiguousBytes {
@inlinable
internal var _providesContiguousBytesNoCopy: Bool {
@inline(__always) get { return self._wholeGuts.isFastUTF8 }
}

extension Substring {

@inlinable @inline(__always)
internal func _withUTF8<R>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> R
Expand All @@ -121,11 +43,4 @@ extension Substring: _HasContiguousBytes {
try body($0)
}
}

@inlinable @inline(__always)
internal func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R {
return try self._withUTF8 { return try body(UnsafeRawBufferPointer($0)) }
}
}
13 changes: 13 additions & 0 deletions stdlib/public/core/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ public protocol Sequence {
func withContiguousStorageIfAvailable<R>(
_ body: (UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R?

func _withRawContiguousStorageIfAvailable<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R?
}

// Provides a default associated type witness for Iterator when the
Expand Down Expand Up @@ -1111,6 +1115,15 @@ extension Sequence {
) rethrows -> R? {
return nil
}

@inlinable
public func _withRawContiguousStorageIfAvailable<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R? {
return try withContiguousStorageIfAvailable {
try body(UnsafeRawBufferPointer($0))
}
}
}

// FIXME(ABI)#182
Expand Down
11 changes: 8 additions & 3 deletions stdlib/public/core/SmallString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ extension _SmallString {

// Direct from UTF-8
@inlinable @inline(__always)
internal init?(_ input: UnsafeBufferPointer<UInt8>) {
internal init?(_ input: UnsafeRawBufferPointer) {
let count = input.count
guard count <= _SmallString.capacity else { return nil }

Expand All @@ -254,6 +254,11 @@ extension _SmallString {
self.init(leading: leading, trailing: trailing, count: count)
}

@inlinable @inline(__always)
internal init?(_ input: UnsafeBufferPointer<UInt8>) {
self.init(UnsafeRawBufferPointer(input))
}

@usableFromInline // @testable
internal init?(_ base: _SmallString, appending other: _SmallString) {
let totalCount = base.count + other.count
Expand Down Expand Up @@ -319,7 +324,7 @@ extension UInt64 {

@inlinable @inline(__always)
internal func _bytesToUInt64(
_ input: UnsafePointer<UInt8>,
_ input: UnsafeRawPointer,
_ c: Int
) -> UInt64 {
// FIXME: This should be unified with _loadPartialUnalignedUInt64LE.
Expand All @@ -328,7 +333,7 @@ internal func _bytesToUInt64(
var r: UInt64 = 0
var shift: Int = 0
for idx in 0..<c {
r = r | (UInt64(input[idx]) &<< shift)
r = r | (UInt64(input.load(fromByteOffset: idx, as: UInt8.self)) &<< shift)
shift = shift &+ 8
}
return r
Expand Down
16 changes: 5 additions & 11 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,11 @@ extension String {
public init<C: Collection, Encoding: Unicode.Encoding>(
decoding codeUnits: C, as sourceEncoding: Encoding.Type
) where C.Iterator.Element == Encoding.CodeUnit {
if let contigBytes = codeUnits as? _HasContiguousBytes,
sourceEncoding == UTF8.self,
contigBytes._providesContiguousBytesNoCopy
{
self = contigBytes.withUnsafeBytes { rawBufPtr in
let ptr = rawBufPtr.baseAddress._unsafelyUnwrappedUnchecked
return String._fromUTF8Repairing(
UnsafeBufferPointer(
start: ptr.assumingMemoryBound(to: UInt8.self),
count: rawBufPtr.count)).0
}
if sourceEncoding == UTF8.self,
let str = codeUnits._withRawContiguousStorageIfAvailable({
buf -> String in String._fromUTF8Repairing(buf).0
}) {
self = str
return
}

Expand Down
Loading