Skip to content

Commit 279c036

Browse files
committed
[stdlib] document limitations for Substring’s span
1 parent 0115ffc commit 279c036

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

stdlib/public/core/Substring.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,27 @@ extension Substring.UTF8View: BidirectionalCollection {
751751

752752
extension Substring.UTF8View {
753753

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.
754775
@available(SwiftStdlib 6.2, *)
755776
public var span: Span<UTF8.CodeUnit> {
756777
@lifetime(borrow self)

0 commit comments

Comments
 (0)