33[[ Source] ( https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Trim.swift ) |
44 [ Tests] ( https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/TrimTests.swift )]
55
6- Returns a ` SubSequence ` formed by discarding all elements at the start and end
7- of the collection which satisfy the given predicate.
6+ A group of methods that return the ` SubSequence ` formed by discarding elements
7+ at the start and/or end of the collection which satisfy the given predicate.
88
99This example uses ` trimming(while:) ` to get a substring without the white space
1010at the beginning and end of the string.
@@ -17,24 +17,44 @@ let results = [2, 10, 11, 15, 20, 21, 100].trimming(while: { $0.isMultiple(of: 2
1717print (results) // [11, 15, 20, 21]
1818```
1919
20+ The ` Algorithms ` library also includes methods that trim from each end,
21+ as well as mutating versions of all three methods.
22+
2023## Detailed Design
2124
22- A new method is added to ` BidirectionalCollection ` :
25+ New methods are added to ` Collection ` and ` BidirectionalCollection ` :
2326
2427``` swift
28+ extension Collection {
29+ func trimmingPrefix (while predicate : (Element ) throws -> Bool ) rethrows -> SubSequence
30+ }
31+
2532extension BidirectionalCollection {
26- public func trimming (while predicate : (Element ) throws -> Bool ) rethrows -> SubSequence
33+ func trimmingSuffix (while predicate : (Element ) throws -> Bool ) rethrows -> SubSequence
34+
35+ func trimming (while predicate : (Element ) throws -> Bool ) rethrows -> SubSequence
36+ }
37+
38+ extension Collection where Self : RangeReplaceableCollection {
39+ mutating func trimPrefix (while predicate : (Element ) throws -> Bool ) rethrows
40+ }
41+
42+ extension BidirectionalCollection where Self : RangeReplaceableCollection {
43+ mutating func trimSuffix (while predicate : (Element ) throws -> Bool ) rethrows
44+
45+ mutating func trim (while predicate : (Element ) throws -> Bool ) rethrows
2746}
2847```
2948
30- This method requires ` BidirectionalCollection ` for an efficient implementation
31- which visits as few elements as possible .
49+ There are also overloads of the mutating methods when ` Self == Self.SubSequence ` ,
50+ for non-range-replaceable self-slicing types .
3251
33- A less-efficient implementation is _ possible_ for any ` Collection ` , which would
34- involve always traversing the entire collection. This implementation is not
35- provided, as it would mean developers of generic algorithms who forget to add
36- the ` BidirectionalCollection ` constraint will receive that inefficient
37- implementation:
52+ Though the ` trimming ` and ` trimmingSuffix ` methods are declared on
53+ ` BidirectionalCollection ` , a less-efficient implementation is _ possible_ for
54+ any ` Collection ` , which would involve always traversing the entire collection.
55+ This implementation is not provided, as it would mean developers of generic
56+ algorithms who forget to add the ` BidirectionalCollection ` constraint will
57+ receive that inefficient implementation:
3858
3959``` swift
4060func myAlgorithm <Input >(input : Input) where Input: Collection {
@@ -50,28 +70,29 @@ Swift provides the `BidirectionalCollection` protocol for marking types which
5070support reverse traversal, and generic types and algorithms which want to make
5171use of that should add it to their constraints.
5272
53- ### >= 0.3.0
73+ #### Supporting Methods
5474
55- In ` v0.3.0 ` new methods are added to allow discarding all the elements matching
56- the predicate at the beginning (prefix) or at the ending (suffix) of the
57- collection.
58- - ` trimmingSuffix(while:) ` can only be run on collections conforming to the
59- ` BidirectionalCollection ` protocol.
60- - ` trimmingPrefix(while:) ` can be run also on collections conforming to the
61- ` Collection ` protocol.
75+ The ` endOfPrefix(while:) ` and ` startOfSuffix(while:) ` methods are used
76+ in the implementation of the trimming methods described above. As these
77+ supporting methods are independently useful, they are included in the library
78+ as well.
6279
6380``` swift
64- let myString = " hello, world "
65- print (myString.trimmingPrefix (while : \.isWhitespace )) // "hello, world "
81+ extension Collection {
82+ /// Returns the exclusive upper bound of the prefix of elements that satisfy
83+ /// the predicate.
84+ func endOfPrefix (
85+ while predicate : (Element ) throws -> Bool
86+ ) rethrows -> Index
87+ }
6688
67- print (myString.trimmingSuffix (while : \.isWhitespace )) // " hello, world"
68- ```
69- Also mutating variants for all the methods already existing and the new ones are
70- added.
71- ``` swift
72- var myString = " hello, world "
73- myString.trim (while : \.isWhitespace )
74- print (myString) // "hello, world"
89+ extension BidirectionalCollection {
90+ /// Returns the inclusive lower bound of the suffix of elements that satisfy
91+ /// the predicate.
92+ func startOfSuffix (
93+ while predicate : (Element ) throws -> Bool
94+ ) rethrows -> Index
95+ }
7596```
7697
7798### Complexity
0 commit comments