@@ -200,17 +200,50 @@ extension String {
200
200
}
201
201
}
202
202
203
+ @available ( SwiftStdlib 6 . 2 , * )
204
+ private var _span : Span < UTF8 . CodeUnit > {
205
+ @lifetime ( borrow self)
206
+ borrowing get {
207
+ #if _runtime(_ObjC)
208
+ // handle non-UTF8 Objective-C bridging cases here
209
+ if !_guts. isFastUTF8, _guts. _object. hasObjCBridgeableObject {
210
+ let storage = _guts. _getOrAllocateAssociatedStorage ( )
211
+ let ( start, count) = unsafe ( storage. start, storage. count)
212
+ let span = unsafe Span( _unsafeStart: start, count: count)
213
+ return unsafe _override Lifetime ( span, borrowing: self )
214
+ }
215
+ #endif
216
+ let count = _guts. count
217
+ if _guts. isSmall {
218
+ let a = Builtin . addressOfBorrow ( self )
219
+ let address = unsafe UnsafePointer< UTF8 . CodeUnit > ( a)
220
+ let span = unsafe Span( _unsafeStart: address, count: count)
221
+ return unsafe _override Lifetime ( span, borrowing: self )
222
+ }
223
+ let isFastUTF8 = _guts. isFastUTF8
224
+ _precondition ( isFastUTF8, " String must be contiguous UTF8 " )
225
+ let buffer = unsafe _guts. _object . fastUTF8
226
+ let span = unsafe Span( _unsafeElements: buffer)
227
+ return unsafe _override Lifetime ( span, borrowing: self )
228
+ }
229
+ }
230
+
231
+ /// A UTF8span over the code units that make up this string.
232
+ ///
233
+ /// - Note: In the case of bridged UTF16 String instances (on Apple
234
+ /// platforms,) this property transcodes the code units the first time
235
+ /// it is called. The transcoded buffer is cached, and subsequent calls
236
+ /// to `span` can reuse the buffer.
237
+ ///
238
+ /// Returns: a `UTF8Span` over the code units of this String.
239
+ ///
240
+ /// Complexity: O(1) for native UTF8 Strings,
241
+ /// amortized O(1) for bridged UTF16 Strings.
203
242
@available ( SwiftStdlib 6 . 2 , * )
204
243
public var utf8Span : UTF8Span {
205
244
@lifetime ( borrow self)
206
245
borrowing get {
207
- let isKnownASCII = _guts. isASCII
208
- let utf8 = self . utf8
209
- let span = utf8. span
210
- let result = unsafe UTF8 Span(
211
- unchecked: span,
212
- isKnownASCII: isKnownASCII)
213
- return unsafe _overrideLifetime ( result, borrowing: self )
246
+ unsafe UTF8Span( unchecked: _span, isKnownASCII: _guts. isASCII)
214
247
}
215
248
}
216
249
}
0 commit comments