Skip to content

Commit 58fb4ef

Browse files
natecook1000tkremenek
authored andcommitted
[stdlib] Clean up various documentation issues (#3804)
* [stdlib] Clean up some documentation formatting * [stdlib] Update example code for SE-0103 * [stdlib] Fix UnsafeBufferPointer documentation typo * [stdlib] Collection documentation cleanup * [stdlib] Fix String.init?(_:UTF16View) description * [stdlib] Documentation fixes for SE-0091 * [stdlib] Add param info for String(repeating:count:)
1 parent 48c450e commit 58fb4ef

11 files changed

+83
-105
lines changed

stdlib/public/core/Collection.swift

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@ public protocol Indexable : IndexableBase {
188188
/// to `index(before:)`.
189189
///
190190
/// - SeeAlso: `index(_:offsetBy:limitedBy:)`, `formIndex(_:offsetBy:)`
191-
/// - Precondition:
192-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
193-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
194191
/// - Complexity: O(1) if the collection conforms to
195192
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
196193
/// value of `n`.
@@ -234,9 +231,6 @@ public protocol Indexable : IndexableBase {
234231
/// the method returns `nil`.
235232
///
236233
/// - SeeAlso: `index(_:offsetBy:)`, `formIndex(_:offsetBy:limitedBy:)`
237-
/// - Precondition:
238-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
239-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
240234
/// - Complexity: O(1) if the collection conforms to
241235
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
242236
/// value of `n`.
@@ -256,9 +250,6 @@ public protocol Indexable : IndexableBase {
256250
/// collection conforms to the `BidirectionalCollection` protocol.
257251
///
258252
/// - SeeAlso: `index(_:offsetBy:)`, `formIndex(_:offsetBy:limitedBy:)`
259-
/// - Precondition:
260-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
261-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
262253
/// - Complexity: O(1) if the collection conforms to
263254
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
264255
/// value of `n`.
@@ -412,18 +403,18 @@ public struct IndexingIterator<
412403
/// A sequence whose elements can be traversed multiple times,
413404
/// nondestructively, and accessed by indexed subscript.
414405
///
415-
/// Collections are used extensively throughout the standard library. When
416-
/// you use arrays, dictionaries, views of a string's contents and other
417-
/// types, you benefit from the operations that the `Collection` protocol
418-
/// declares and implements.
406+
/// Collections are used extensively throughout the standard library. When you
407+
/// use arrays, dictionaries, views of a string's contents and other types,
408+
/// you benefit from the operations that the `Collection` protocol declares
409+
/// and implements.
419410
///
420411
/// In addition to the methods that collections inherit from the `Sequence`
421412
/// protocol, you gain access to methods that depend on accessing an element
422413
/// at a specific position when using a collection.
423414
///
424-
/// For example, if you want to print only the first word in a string,
425-
/// search for the index of the first space, and then create a subsequence up
426-
/// to that position.
415+
/// For example, if you want to print only the first word in a string, search
416+
/// for the index of the first space and then create a subsequence up to that
417+
/// position.
427418
///
428419
/// let text = "Buffalo buffalo buffalo buffalo."
429420
/// if let firstSpace = text.characters.index(of: " ") {
@@ -464,24 +455,24 @@ public struct IndexingIterator<
464455
/// print(firstChar)
465456
/// // Prints "B"
466457
///
467-
/// The `Collection` protocol declares and provides default implementations
468-
/// for many operations that depend on elements being accessible by their
469-
/// subscript. For example, you can also access the first character of
470-
/// `text` using the `first` property, which has the value of the first
471-
/// element of the collection, or `nil` if the collection is empty.
458+
/// The `Collection` protocol declares and provides default implementations for
459+
/// many operations that depend on elements being accessible by their
460+
/// subscript. For example, you can also access the first character of `text`
461+
/// using the `first` property, which has the value of the first element of
462+
/// the collection, or `nil` if the collection is empty.
472463
///
473464
/// print(text.characters.first)
474465
/// // Prints "Optional("B")"
475466
///
476-
/// Traversing a Collection
467+
/// Traversing a Collection
477468
/// =======================
478469
///
479-
/// While a sequence may be consumed as it is traversed, a collection is
480-
/// guaranteed to be multi-pass: Any element may be repeatedly accessed by
481-
/// saving its index. Moreover, a collection's indices form a finite range
482-
/// of the positions of the collection's elements. This guarantees the
483-
/// safety of operations that depend on a sequence being finite, such as
484-
/// checking to see whether a collection contains an element.
470+
/// Although a sequence can be consumed as it is traversed, a collection is
471+
/// guaranteed to be multipass: Any element may be repeatedly accessed by
472+
/// saving its index. Moreover, a collection's indices form a finite range of
473+
/// the positions of the collection's elements. This guarantees the safety of
474+
/// operations that depend on a sequence being finite, such as checking to see
475+
/// whether a collection contains an element.
485476
///
486477
/// Iterating over the elements of a collection by their positions yields the
487478
/// same elements in the same order as iterating over that collection using
@@ -498,7 +489,7 @@ public struct IndexingIterator<
498489
/// // Prints "i"
499490
/// // Prints "f"
500491
/// // Prints "t"
501-
///
492+
///
502493
/// for i in word.characters.indices {
503494
/// print(word.characters[i])
504495
/// }
@@ -508,26 +499,28 @@ public struct IndexingIterator<
508499
/// // Prints "f"
509500
/// // Prints "t"
510501
///
511-
/// Conforming to the Collection Protocol
502+
/// Conforming to the Collection Protocol
512503
/// =====================================
513504
///
514505
/// If you create a custom sequence that can provide repeated access to its
515-
/// elements, conformance to the `Collection` protocol gives your custom type
516-
/// a more useful and more efficient interface for sequence and collection
517-
/// operations. To add `Collection` conformance to your type, declare
518-
/// `startIndex` and `endIndex` properties, a subscript that provides at least
519-
/// read-only access to your type's elements, and the `index(after:)` method
520-
/// for advancing your collection's indices.
506+
/// elements, make sure that its type conforms to the `Collection` protocol in
507+
/// order to give a more useful and more efficient interface for sequence and
508+
/// collection operations. To add `Collection` conformance to your type, you
509+
/// must declare at least the four following requirements:
510+
///
511+
/// - the `startIndex` and `endIndex` properties,
512+
/// - a subscript that provides at least read-only access to your type's
513+
/// elements, and
514+
/// - the `index(after:)` method for advancing an index into your collection.
521515
///
522516
/// Expected Performance
523517
/// ====================
524518
///
525-
/// Types that conform to `Collection` are expected to provide the
526-
/// `startIndex` and `endIndex` properties and subscript access to elements
527-
/// as O(1) operations. Types that are not able to guarantee that expected
528-
/// performance must document the departure, because many collection operations
529-
/// depend on O(1) subscripting performance for their own performance
530-
/// guarantees.
519+
/// Types that conform to `Collection` are expected to provide the `startIndex`
520+
/// and `endIndex` properties and subscript access to elements as O(1)
521+
/// operations. Types that are not able to guarantee that expected performance
522+
/// must document the departure, because many collection operations depend on
523+
/// O(1) subscripting performance for their own performance guarantees.
531524
///
532525
/// The performance of some collection operations depends on the type of index
533526
/// that the collection provides. For example, a random-access collection,
@@ -696,7 +689,6 @@ public protocol Collection : Indexable, Sequence {
696689
/// `start` must be a valid index of the collection.
697690
/// - Returns: A subsequence starting at the `start` position.
698691
///
699-
/// - Precondition: `start >= self.startIndex && start <= self.endIndex`
700692
/// - Complexity: O(1)
701693
func suffix(from start: Index) -> SubSequence
702694

@@ -790,9 +782,6 @@ public protocol Collection : Indexable, Sequence {
790782
/// to `index(before:)`.
791783
///
792784
/// - SeeAlso: `index(_:offsetBy:limitedBy:)`, `formIndex(_:offsetBy:)`
793-
/// - Precondition:
794-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
795-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
796785
/// - Complexity: O(1) if the collection conforms to
797786
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
798787
/// value of `n`.
@@ -836,9 +825,6 @@ public protocol Collection : Indexable, Sequence {
836825
/// the method returns `nil`.
837826
///
838827
/// - SeeAlso: `index(_:offsetBy:)`, `formIndex(_:offsetBy:limitedBy:)`
839-
/// - Precondition:
840-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
841-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
842828
/// - Complexity: O(1) if the collection conforms to
843829
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
844830
/// value of `n`.
@@ -856,8 +842,8 @@ public protocol Collection : Indexable, Sequence {
856842
/// - end: Another valid index of the collection. If `end` is equal to
857843
/// `start`, the result is zero.
858844
/// - Returns: The distance between `start` and `end`. The result can be
859-
/// negative only if the collection conforms to the `BidirectionalCollection`
860-
/// protocol.
845+
/// negative only if the collection conforms to the
846+
/// `BidirectionalCollection` protocol.
861847
///
862848
/// - Complexity: O(1) if the collection conforms to
863849
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the
@@ -926,9 +912,6 @@ extension Indexable {
926912
/// to `index(before:)`.
927913
///
928914
/// - SeeAlso: `index(_:offsetBy:limitedBy:)`, `formIndex(_:offsetBy:)`
929-
/// - Precondition:
930-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
931-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
932915
/// - Complexity: O(1) if the collection conforms to
933916
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
934917
/// value of `n`.
@@ -974,9 +957,6 @@ extension Indexable {
974957
/// the method returns `nil`.
975958
///
976959
/// - SeeAlso: `index(_:offsetBy:)`, `formIndex(_:offsetBy:limitedBy:)`
977-
/// - Precondition:
978-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
979-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
980960
/// - Complexity: O(1) if the collection conforms to
981961
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
982962
/// value of `n`.
@@ -998,9 +978,6 @@ extension Indexable {
998978
/// collection conforms to the `BidirectionalCollection` protocol.
999979
///
1000980
/// - SeeAlso: `index(_:offsetBy:)`, `formIndex(_:offsetBy:limitedBy:)`
1001-
/// - Precondition:
1002-
/// - If `n > 0`, `n <= self.distance(from: i, to: self.endIndex)`
1003-
/// - If `n < 0`, `n >= self.distance(from: i, to: self.startIndex)`
1004981
/// - Complexity: O(1) if the collection conforms to
1005982
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the absolute
1006983
/// value of `n`.
@@ -1414,7 +1391,6 @@ extension Collection {
14141391
/// `end` must be a valid index of the collection.
14151392
/// - Returns: A subsequence up to, but not including, the `end` position.
14161393
///
1417-
/// - Precondition: `end >= self.startIndex && end <= self.endIndex`
14181394
/// - Complexity: O(1)
14191395
/// - SeeAlso: `prefix(through:)`
14201396
public func prefix(upTo end: Index) -> SubSequence {
@@ -1442,7 +1418,6 @@ extension Collection {
14421418
/// subsequence. `start` must be a valid index of the collection.
14431419
/// - Returns: A subsequence starting at the `start` position.
14441420
///
1445-
/// - Precondition: `start >= self.startIndex && start <= self.endIndex`
14461421
/// - Complexity: O(1)
14471422
public func suffix(from start: Index) -> SubSequence {
14481423
return self[start..<endIndex]

stdlib/public/core/Comparable.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@
6363
/// - `a < b` and `b < c` implies `a < c` (Transitivity)
6464
///
6565
/// To add `Comparable` conformance to your custom types, define the `<` and
66-
/// `==` operators. The `==` operator is a requirement of the `Equatable`
67-
/// protocol, which `Comparable` extends---see that protocol's documentation
68-
/// for more information about equality in Swift. Because default
69-
/// implementations of the remainder of the relational operators are provided
70-
/// by the standard library, you'll be able to use `!=`, `>`, `<=`, and `>=`
71-
/// with instances of your type without any further code.
66+
/// `==` operators as static methods of your types. The `==` operator is a
67+
/// requirement of the `Equatable` protocol, which `Comparable` extends---see
68+
/// that protocol's documentation for more information about equality in
69+
/// Swift. Because default implementations of the remainder of the relational
70+
/// operators are provided by the standard library, you'll be able to use
71+
/// `!=`, `>`, `<=`, and `>=` with instances of your type without any further
72+
/// code.
7273
///
7374
/// As an example, here's an implementation of a `Date` structure that stores
7475
/// the year, month, and day of a date:
@@ -92,7 +93,6 @@
9293
/// return lhs.day < rhs.day
9394
/// }
9495
/// }
95-
/// }
9696
///
9797
/// This function uses the least specific nonmatching property of the date to
9898
/// determine the result of the comparison. For example, if the two `year`
@@ -102,9 +102,10 @@
102102
/// Next, implement the `==` operator function, the requirement inherited from
103103
/// the `Equatable` protocol.
104104
///
105-
/// func == (lhs: Date, rhs: Date) -> Bool {
106-
/// return lhs.year == rhs.year && lhs.month == rhs.month
107-
/// && lhs.day == rhs.day
105+
/// static func == (lhs: Date, rhs: Date) -> Bool {
106+
/// return lhs.year == rhs.year && lhs.month == rhs.month
107+
/// && lhs.day == rhs.day
108+
/// }
108109
/// }
109110
///
110111
/// Two `Date` instances are equal if each of their corresponding properties is

stdlib/public/core/CompilerProtocols.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public protocol _ExpressibleByBuiltinUnicodeScalarLiteral {
262262
/// // Prints "ñ"
263263
///
264264
/// Conforming to ExpressibleByUnicodeScalarLiteral
265-
/// =============================================
265+
/// ===============================================
266266
///
267267
/// To add `ExpressibleByUnicodeScalarLiteral` conformance to your custom type,
268268
/// implement the required initializer.
@@ -305,7 +305,7 @@ public protocol _ExpressibleByBuiltinExtendedGraphemeClusterLiteral
305305
/// // Prints "❄︎"
306306
///
307307
/// Conforming to ExpressibleByExtendedGraphemeClusterLiteral
308-
/// =======================================================
308+
/// =========================================================
309309
///
310310
/// To add `ExpressibleByExtendedGraphemeClusterLiteral` conformance to your
311311
/// custom type, implement the required initializer.
@@ -349,7 +349,7 @@ public protocol _ExpressibleByBuiltinUTF16StringLiteral
349349
/// let picnicGuest = "Deserving porcupine"
350350
///
351351
/// Conforming to ExpressibleByStringLiteral
352-
/// ======================================
352+
/// ========================================
353353
///
354354
/// To add `ExpressibleByStringLiteral` conformance to your custom type,
355355
/// implement the required initializer.
@@ -456,7 +456,7 @@ public protocol ExpressibleByStringLiteral
456456
/// // Prints "Zero integers: []"
457457
///
458458
/// Conforming to ExpressibleByArrayLiteral
459-
/// =====================================
459+
/// =======================================
460460
///
461461
/// Add the capability to be initialized with an array literal to your own
462462
/// custom types by declaring an `init(arrayLiteral:)` initializer. The
@@ -514,7 +514,7 @@ public protocol ExpressibleByArrayLiteral {
514514
/// by assigning an instance of one of these types.
515515
///
516516
/// Conforming to the ExpressibleByDictionaryLiteral Protocol
517-
/// =======================================================
517+
/// =========================================================
518518
///
519519
/// To add the capability to be initialized with a dictionary literal to your
520520
/// own custom types, declare an `init(dictionaryLiteral:)` initializer. The
@@ -565,7 +565,7 @@ public protocol ExpressibleByDictionaryLiteral {
565565
/// // Prints "One cookie: $2, 3 cookies: $6."
566566
///
567567
/// Conforming to the ExpressibleByStringInterpolation Protocol
568-
/// =========================================================
568+
/// ===========================================================
569569
///
570570
/// To use string interpolation to initialize instances of your custom type,
571571
/// implement the required initializers for `ExpressibleByStringInterpolation`

stdlib/public/core/Equatable.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
/// `Comparable` protocols, which allow more uses of your custom type, such as
4949
/// constructing sets or sorting the elements of a collection.
5050
///
51-
/// To adopt the `Equatable` protocol, implement the equal-to operator (`==`).
52-
/// The standard library provides an implementation for the not-equal-to
53-
/// operator (`!=`) for any `Equatable` type, which calls the custom `==`
54-
/// function and negates its result.
51+
/// To adopt the `Equatable` protocol, implement the equal-to operator (`==`)
52+
/// as a static method of your type. The standard library provides an
53+
/// implementation for the not-equal-to operator (`!=`) for any `Equatable`
54+
/// type, which calls the custom `==` function and negates its result.
5555
///
5656
/// As an example, consider a `StreetAddress` structure that holds the parts of
5757
/// a street address: a house or building number, the street name, and an
@@ -76,12 +76,12 @@
7676
/// `Equatable`.
7777
///
7878
/// extension StreetAddress: Equatable {
79-
/// static func == (lhs: StreetAddress, rhs: StreetAddress) -> Bool {
80-
/// return
81-
/// lhs.number == rhs.number &&
82-
/// lhs.street == rhs.street &&
83-
/// lhs.unit == rhs.unit
84-
/// }
79+
/// static func == (lhs: StreetAddress, rhs: StreetAddress) -> Bool {
80+
/// return
81+
/// lhs.number == rhs.number &&
82+
/// lhs.street == rhs.street &&
83+
/// lhs.unit == rhs.unit
84+
/// }
8585
/// }
8686
///
8787
/// The `StreetAddress` type now conforms to `Equatable`. You can use `==` to

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ internal struct _UnmanagedAnyObjectArray {
368368
/// print(primes.intersection(favoriteNumbers))
369369
/// // Prints "[5, 7]"
370370
///
371-
///
372371
/// Sequence and Collection Operations
373372
/// ==================================
374373
///

stdlib/public/core/LazySequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
/// /// - Complexity: O(N)
4848
/// func scan<ResultElement>(
4949
/// _ initial: ResultElement,
50-
/// _ nextPartialResult: @noescape (ResultElement, Iterator.Element) -> ResultElement
50+
/// _ nextPartialResult: (ResultElement, Iterator.Element) -> ResultElement
5151
/// ) -> [ResultElement] {
5252
/// var result = [initial]
5353
/// for x in self {

0 commit comments

Comments
 (0)