File tree 1 file changed +21
-0
lines changed
1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -751,6 +751,27 @@ extension Substring.UTF8View: BidirectionalCollection {
751
751
752
752
extension Substring . UTF8View {
753
753
754
+ /// A span over the UTF8 code units that make up this substring.
755
+ ///
756
+ /// - Note: In the case of bridged UTF16 String instances (on Apple
757
+ /// platforms,) this property needs to transcode the code units every time
758
+ /// it is called.
759
+ /// For example, if `string` has the bridged UTF16 representation,
760
+ /// for word in string.split(separator: " ") {
761
+ /// useSpan(word.span)
762
+ /// }
763
+ /// is accidentally quadratic because of this issue. A workaround is to
764
+ /// explicitly convert the string into its native UTF8 representation:
765
+ /// var nativeString = consume string
766
+ /// nativeString.makeContiguousUTF8()
767
+ /// for word in nativeString.split(separator: " ") {
768
+ /// useSpan(word.span)
769
+ /// }
770
+ /// This second option has linear time complexity, as expected.
771
+ ///
772
+ /// Returns: a `Span` over the UTF8 code units of this Substring.
773
+ ///
774
+ /// Complexity: O(1) for native UTF8 Strings, O(n) for bridged UTF16 Strings.
754
775
@available ( SwiftStdlib 6 . 2 , * )
755
776
public var span : Span < UTF8 . CodeUnit > {
756
777
@lifetime ( borrow self)
You can’t perform that action at this time.
0 commit comments