Skip to content

Commit 7e58b81

Browse files
committed
Call out time complexity of String.count.
This discussion borrows wording from the docs for the default implementation of Collection.count[1], which are inherited by many other symbols, but with a rewrite here because String doesn't guarantee random-access performance (doesn't conform to RandomAccessCollection), so accessing the count is never an O(1) operation. 1: https://developer.apple.com/documentation/swift/collection/count-4l4qk
1 parent 8c03ace commit 7e58b81

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

stdlib/public/core/StringCharacterView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ extension String: BidirectionalCollection {
3434
public var endIndex: Index { return _guts.endIndex }
3535

3636
/// The number of characters in a string.
37+
///
38+
/// To check whether a string is empty,
39+
/// use its `isEmpty` property instead of comparing `count` to zero.
40+
///
41+
/// - Complexity: O(n), where n is the length of the string.
3742
@inline(__always)
3843
public var count: Int {
3944
return distance(from: startIndex, to: endIndex)

0 commit comments

Comments
 (0)