@@ -759,40 +759,28 @@ extension Substring.UTF8View {
759
759
#if _runtime(_ObjC)
760
760
// handle non-UTF8 Objective-C bridging cases here
761
761
if !_wholeGuts. isFastUTF8 && _wholeGuts. _object. hasObjCBridgeableObject {
762
- let foreign = String ( _wholeGuts)
763
- let dts = foreign. distance ( from: foreign. startIndex, to: startIndex)
764
- let dte = foreign. distance ( from: startIndex, to: endIndex)
765
-
766
- let storage : __StringStorage
767
- if let associated = _wholeGuts. getAssociatedStorage ( ) {
768
- storage = associated
769
- }
770
- else {
771
- storage = _wholeGuts. getOrAllocateAssociatedStorage ( )
772
- }
773
- let native = storage. asString
774
- let nativeStart = native. index ( native. startIndex, offsetBy: dts)
775
- let startOffset = nativeStart. _encodedOffset
776
- let endOffset = native. index ( nativeStart, offsetBy: dte) . _encodedOffset
777
-
778
- let ( start, count) = unsafe ( storage. start, storage. count)
779
- var span = unsafe Span( _unsafeStart: start, count: count)
780
- span = unsafe span. _extracting ( unchecked: startOffset..< endOffset)
762
+ let base : String . UTF8View = self . _base
763
+ let first = base. _foreignDistance ( from: base. startIndex, to: startIndex)
764
+ let count = base. _foreignDistance ( from: startIndex, to: endIndex)
765
+ let span = unsafe base. span . _extracting (
766
+ unchecked: Range ( _uncheckedBounds: ( first, first &+ count) )
767
+ )
781
768
return unsafe _override Lifetime ( span, borrowing: self )
782
769
}
783
770
#endif
784
- let start = _slice. _startIndex. _encodedOffset
771
+ let first = _slice. _startIndex. _encodedOffset
785
772
let end = _slice. _endIndex. _encodedOffset
786
773
if _wholeGuts. isSmall {
787
774
let a = Builtin . addressOfBorrow ( self )
788
- let offset = start &+ ( MemoryLayout < String . Index > . stride &<< 1 )
789
- let address = unsafe UnsafePointer< UTF8 . CodeUnit > ( a) . advanced ( by: offset)
790
- let span = unsafe Span( _unsafeStart: address , count: end &- start )
775
+ let offset = first &+ ( 2 &* MemoryLayout< String . Index> . stride)
776
+ let start = unsafe UnsafePointer< UTF8 . CodeUnit > ( a) . advanced ( by: offset)
777
+ let span = unsafe Span( _unsafeStart: start , count: end &- first )
791
778
return unsafe _override Lifetime ( span, borrowing: self )
792
779
}
793
- _precondition ( _wholeGuts. isFastUTF8)
794
- let buffer = unsafe _wholeGuts . _object . fastUTF8 . extracting( start..< end)
795
- _internalInvariant ( ( end &- start) == buffer. count)
780
+ _internalInvariant ( _wholeGuts. isFastUTF8)
781
+ let buffer = unsafe _wholeGuts . _object . fastUTF8 . extracting(
782
+ Range ( _uncheckedBounds: ( first, end) )
783
+ )
796
784
let span = unsafe Span( _unsafeElements: buffer)
797
785
return unsafe _override Lifetime ( span, borrowing: self )
798
786
}
0 commit comments