|
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 |
@@ -89,6 +89,7 @@ extension String { |
89 | 89 | /// print(String(s1.utf8.prefix(15))!) |
90 | 90 | /// // Prints "They call me 'B" |
91 | 91 | @frozen |
| 92 | + @_addressableForDependencies |
92 | 93 | public struct UTF8View: Sendable { |
93 | 94 | @usableFromInline |
94 | 95 | internal var _guts: _StringGuts |
@@ -317,6 +318,31 @@ extension String.UTF8View { |
317 | 318 | } |
318 | 319 | } |
319 | 320 |
|
| 321 | +extension String.UTF8View { |
| 322 | + |
| 323 | + @available(SwiftStdlib 6.2, *) |
| 324 | + public var span: Span<UTF8.CodeUnit> { |
| 325 | + @lifetime(borrow self) |
| 326 | + borrowing get { |
| 327 | + let count = _guts.count |
| 328 | + if _guts.isSmall { |
| 329 | + let a = Builtin.addressOfBorrow(self) |
| 330 | + let address = unsafe UnsafePointer<UTF8.CodeUnit>(a) |
| 331 | + let span = unsafe Span(_unsafeStart: address, count: count) |
| 332 | + return unsafe _overrideLifetime(span, borrowing: self) |
| 333 | + } |
| 334 | + else if _guts.isFastUTF8 { |
| 335 | + let buffer = unsafe _guts._object.fastUTF8 |
| 336 | + _internalInvariant(count == buffer.count) |
| 337 | + let span = unsafe Span(_unsafeElements: buffer) |
| 338 | + return unsafe _overrideLifetime(span, borrowing: self) |
| 339 | + } |
| 340 | + // handle other Objective-C bridging cases here |
| 341 | + fatalError("Some bridged Strings are not supported at this time") |
| 342 | + } |
| 343 | + } |
| 344 | +} |
| 345 | + |
320 | 346 | // Index conversions |
321 | 347 | extension String.UTF8View.Index { |
322 | 348 | /// Creates an index in the given UTF-8 view that corresponds exactly to the |
|
0 commit comments