|
2 | 2 | // |
3 | 3 | // This source file is part of the Swift.org open source project |
4 | 4 | // |
5 | | -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception |
7 | 7 | // |
8 | 8 | // See https://swift.org/LICENSE.txt for license information |
@@ -630,6 +630,7 @@ extension Substring: LosslessStringConvertible { |
630 | 630 |
|
631 | 631 | extension Substring { |
632 | 632 | @frozen |
| 633 | + @_addressableForDependencies |
633 | 634 | public struct UTF8View: Sendable { |
634 | 635 | @usableFromInline |
635 | 636 | internal var _slice: Slice<String.UTF8View> |
@@ -749,6 +750,34 @@ extension Substring.UTF8View: BidirectionalCollection { |
749 | 750 | } |
750 | 751 | } |
751 | 752 |
|
| 753 | +extension Substring.UTF8View { |
| 754 | + |
| 755 | + @available(SwiftStdlib 6.2, *) |
| 756 | + public var span: Span<UTF8.CodeUnit> { |
| 757 | + @lifetime(borrow self) |
| 758 | + borrowing get { |
| 759 | + let start = _slice._startIndex._encodedOffset |
| 760 | + let end = _slice._endIndex._encodedOffset |
| 761 | + if _wholeGuts.isSmall { |
| 762 | + let a = Builtin.addressOfBorrow(self) |
| 763 | + let offset = start &+ (MemoryLayout<String.Index>.stride &<< 1) |
| 764 | + let address = unsafe UnsafePointer<UTF8.CodeUnit>(a).advanced(by: offset) |
| 765 | + let span = unsafe Span(_unsafeStart: address, count: end &- start) |
| 766 | + return unsafe _overrideLifetime(span, borrowing: self) |
| 767 | + } |
| 768 | + else if _wholeGuts.isFastUTF8 { |
| 769 | + let buffer = unsafe _wholeGuts._object.fastUTF8.extracting(start..<end) |
| 770 | + let count = end &- start |
| 771 | + _internalInvariant(count == buffer.count) |
| 772 | + let span = unsafe Span(_unsafeElements: buffer) |
| 773 | + return unsafe _overrideLifetime(span, borrowing: self) |
| 774 | + } |
| 775 | + // handle other Objective-C bridging cases here |
| 776 | + fatalError("Some bridged Strings are not supported at this time") |
| 777 | + } |
| 778 | + } |
| 779 | +} |
| 780 | + |
752 | 781 | extension Substring { |
753 | 782 | @inlinable |
754 | 783 | public var utf8: UTF8View { |
|
0 commit comments