Skip to content

Commit f650e0a

Browse files
committed
[stdlib] String and range expressions
* finish string documentation revisions * revise examples throughout to use range expressions instead of e.g. prefix(upTo: _)
1 parent 6dd52c5 commit f650e0a

11 files changed

+399
-186
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ if True:
9595
///
9696
/// let midpoint = absences.count / 2
9797
///
98-
/// let firstHalf = absences.prefix(upTo: midpoint)
99-
/// let secondHalf = absences.suffix(from: midpoint)
98+
/// let firstHalf = absences[..<midpoint]
99+
/// let secondHalf = absences[midpoint...]
100100
///
101101
/// Neither the `firstHalf` nor `secondHalf` slices allocate any new storage
102102
/// of their own. Instead, each presents a view onto the storage of the
@@ -151,7 +151,7 @@ if True:
151151
/// Here's an implementation of those steps:
152152
///
153153
/// if let i = absences.index(where: { $0 > 0 }) { // 1
154-
/// let absencesAfterFirst = absences.suffix(from: i + 1) // 2
154+
/// let absencesAfterFirst = absences[(i + 1)...] // 2
155155
/// if let j = absencesAfterFirst.index(where: { $0 > 0 }) { // 3
156156
/// print("The first day with absences had \(absences[i]).") // 4
157157
/// print("The second day with absences had \(absences[j]).")

stdlib/public/core/Collection.swift

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,15 @@ public struct IndexingIterator<
453453
///
454454
/// let text = "Buffalo buffalo buffalo buffalo."
455455
/// if let firstSpace = text.index(of: " ") {
456-
/// print(text.prefix(upTo: firstSpace))
456+
/// print(text[..<firstSpace])
457457
/// }
458458
/// // Prints "Buffalo"
459459
///
460-
/// The `firstSpace` constant is an index into the `text` string---the position of the first space in the
461-
/// string. You can store indices in variables, and pass them to
462-
/// collection algorithms or use them later to access the corresponding
463-
/// element. In the example above, `firstSpace` is used to extract the prefix
464-
/// that contains elements up to that index.
460+
/// The `firstSpace` constant is an index into the `text` string---the position
461+
/// of the first space in the string. You can store indices in variables, and
462+
/// pass them to collection algorithms or use them later to access the
463+
/// corresponding element. In the example above, `firstSpace` is used to
464+
/// extract the prefix that contains elements up to that index.
465465
///
466466
/// You can pass only valid indices to collection operations. You can find a
467467
/// complete set of a collection's valid indices by starting with the
@@ -514,20 +514,18 @@ public struct IndexingIterator<
514514
/// // Prints "Buffalo"
515515
///
516516
/// You can retrieve the same slice using other methods, such as finding the
517-
/// terminating index, and then using the `prefix(upTo:)` method, which takes
518-
/// an index as its parameter, or by using the view's ranged subscript.
517+
/// terminating index, and the using the string's ranged subscript, or by
518+
/// using the `prefix(upTo:)` method, which takes an index as its parameter.
519519
///
520520
/// if let firstSpace = text.index(of: " ") {
521-
/// print(text.prefix(upTo: firstSpace))
521+
/// print(text[..<firstSpace]
522522
/// // Prints "Buffalo"
523523
///
524-
/// let start = text.startIndex
525-
/// print(text[start..<firstSpace])
524+
/// print(text.prefix(upTo: firstSpace))
526525
/// // Prints "Buffalo"
527526
/// }
528527
///
529-
/// The retrieved slice of `text` is equivalent in each of these
530-
/// cases.
528+
/// The retrieved slice of `text` is equivalent in each of these cases.
531529
///
532530
/// Slices Share Indices
533531
/// --------------------
@@ -795,6 +793,15 @@ public protocol Collection : _Indexable, Sequence
795793
/// print(numbers.prefix(upTo: numbers.startIndex))
796794
/// // Prints "[]"
797795
///
796+
/// Using the `prefix(upTo:)` method is equivalent to using a partial
797+
/// half-open range as the collection's subscript. The subscript notation is
798+
/// preferred over `prefix(upTo:)`.
799+
///
800+
/// if let i = numbers.index(of: 40) {
801+
/// print(numbers[..<i])
802+
/// }
803+
/// // Prints "[10, 20, 30]"
804+
///
798805
/// - Parameter end: The "past the end" index of the resulting subsequence.
799806
/// `end` must be a valid index of the collection.
800807
/// - Returns: A subsequence up to, but not including, the `end` position.
@@ -822,6 +829,15 @@ public protocol Collection : _Indexable, Sequence
822829
/// print(numbers.suffix(from: numbers.endIndex))
823830
/// // Prints "[]"
824831
///
832+
/// Using the `suffix(from:)` method is equivalent to using a partial range
833+
/// from the index as the collection's subscript. The subscript notation is
834+
/// preferred over `suffix(from:)`.
835+
///
836+
/// if let i = numbers.index(of: 40) {
837+
/// print(numbers[i...])
838+
/// }
839+
/// // Prints "[40, 50, 60]"
840+
///
825841
/// - Parameter start: The index at which to start the resulting subsequence.
826842
/// `start` must be a valid index of the collection.
827843
/// - Returns: A subsequence starting at the `start` position.
@@ -843,6 +859,15 @@ public protocol Collection : _Indexable, Sequence
843859
/// }
844860
/// // Prints "[10, 20, 30, 40]"
845861
///
862+
/// Using the `prefix(through:)` method is equivalent to using a partial
863+
/// closed range as the collection's subscript. The subscript notation is
864+
/// preferred over `prefix(through:)`.
865+
///
866+
/// if let i = numbers.index(of: 40) {
867+
/// print(numbers[...i])
868+
/// }
869+
/// // Prints "[10, 20, 30, 40]"
870+
///
846871
/// - Parameter end: The index of the last element to include in the
847872
/// resulting subsequence. `end` must be a valid index of the collection
848873
/// that is not equal to the `endIndex` property.
@@ -1619,6 +1644,15 @@ extension Collection {
16191644
/// print(numbers.prefix(upTo: numbers.startIndex))
16201645
/// // Prints "[]"
16211646
///
1647+
/// Using the `prefix(upTo:)` method is equivalent to using a partial
1648+
/// half-open range as the collection's subscript. The subscript notation is
1649+
/// preferred over `prefix(upTo:)`.
1650+
///
1651+
/// if let i = numbers.index(of: 40) {
1652+
/// print(numbers[..<i])
1653+
/// }
1654+
/// // Prints "[10, 20, 30]"
1655+
///
16221656
/// - Parameter end: The "past the end" index of the resulting subsequence.
16231657
/// `end` must be a valid index of the collection.
16241658
/// - Returns: A subsequence up to, but not including, the `end` position.
@@ -1649,6 +1683,15 @@ extension Collection {
16491683
/// print(numbers.suffix(from: numbers.endIndex))
16501684
/// // Prints "[]"
16511685
///
1686+
/// Using the `suffix(from:)` method is equivalent to using a partial range
1687+
/// from the index as the collection's subscript. The subscript notation is
1688+
/// preferred over `suffix(from:)`.
1689+
///
1690+
/// if let i = numbers.index(of: 40) {
1691+
/// print(numbers[i...])
1692+
/// }
1693+
/// // Prints "[40, 50, 60]"
1694+
///
16521695
/// - Parameter start: The index at which to start the resulting subsequence.
16531696
/// `start` must be a valid index of the collection.
16541697
/// - Returns: A subsequence starting at the `start` position.
@@ -1673,6 +1716,15 @@ extension Collection {
16731716
/// }
16741717
/// // Prints "[10, 20, 30, 40]"
16751718
///
1719+
/// Using the `prefix(through:)` method is equivalent to using a partial
1720+
/// closed range as the collection's subscript. The subscript notation is
1721+
/// preferred over `prefix(through:)`.
1722+
///
1723+
/// if let i = numbers.index(of: 40) {
1724+
/// print(numbers[...i])
1725+
/// }
1726+
/// // Prints "[10, 20, 30, 40]"
1727+
///
16761728
/// - Parameter end: The index of the last element to include in the
16771729
/// resulting subsequence. `end` must be a valid index of the collection
16781730
/// that is not equal to the `endIndex` property.

stdlib/public/core/MutableCollection.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,13 @@ public protocol MutableCollection : _MutableIndexable, Collection
267267
/// // numbers == [30, 10, 20, 30, 30, 60, 40]
268268
///
269269
/// The `numbers` array is now arranged in two partitions. The first
270-
/// partition, `numbers.prefix(upTo: p)`, is made up of the elements that
271-
/// are not greater than 30. The second partition, `numbers.suffix(from: p)`,
270+
/// partition, `numbers[..<p]`, is made up of the elements that
271+
/// are not greater than 30. The second partition, `numbers[p...]`,
272272
/// is made up of the elements that *are* greater than 30.
273273
///
274-
/// let first = numbers.prefix(upTo: p)
274+
/// let first = numbers[..<p]
275275
/// // first == [30, 10, 20, 30, 30]
276-
/// let second = numbers.suffix(from: p)
276+
/// let second = numbers[p...]
277277
/// // second == [60, 40]
278278
///
279279
/// - Parameter belongsInSecondPartition: A predicate used to partition

stdlib/public/core/OptionSet.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// A type that presents a mathematical set interface to a bit mask.
13+
/// A type that presents a mathematical set interface to a bitset.
1414
///
15-
/// You use the `OptionSet` protocol to represent bit mask types, where
16-
/// individual bits represent members of the set. Adopting this protocol in
15+
/// You use the `OptionSet` protocol to represent bitset types, where
16+
/// individual bits represent members of a set. Adopting this protocol in
1717
/// your custom types lets you perform set-related operations such as
1818
/// membership tests, unions, and intersections on those types. What's more,
1919
/// when implemented using specific criteria, adoption of this protocol
@@ -30,8 +30,8 @@
3030
/// For example, consider a custom type called `ShippingOptions` that is an
3131
/// option set of the possible ways to ship a customer's purchase.
3232
/// `ShippingOptions` includes a `rawValue` property of type `Int` that stores
33-
/// the bit mask of available shipping options. The static members `NextDay`,
34-
/// `SecondDay`, `Priority`, and `Standard` are unique, individual options.
33+
/// the bit mask of available shipping options. The static members `nextDay`,
34+
/// `secondDay`, `priority`, and `standard` are unique, individual options.
3535
///
3636
/// struct ShippingOptions: OptionSet {
3737
/// let rawValue: Int

0 commit comments

Comments
 (0)