@@ -102,31 +102,37 @@ extension RangeReplaceableCollection where Self: BidirectionalCollection {
102
102
// MARK: Predicate algorithms
103
103
104
104
extension Collection {
105
- // TODO: Non-escaping and throwing
106
- func trimmingPrefix(
107
- while predicate: @escaping ( Element ) -> Bool
108
- ) -> SubSequence {
109
- trimmingPrefix ( ManyConsumer ( base: PredicateConsumer ( predicate: predicate) ) )
105
+ fileprivate func endOfPrefix( while predicate: ( Element ) throws -> Bool ) rethrows -> Index {
106
+ try firstIndex ( where: { try ! predicate( $0) } ) ?? endIndex
107
+ }
108
+
109
+ @available ( SwiftStdlib 5 . 7 , * )
110
+ public func trimmingPrefix(
111
+ while predicate: ( Element ) throws -> Bool
112
+ ) rethrows -> SubSequence {
113
+ let end = try endOfPrefix ( while: predicate)
114
+ return self [ end... ]
110
115
}
111
116
}
112
117
113
118
extension Collection where SubSequence == Self {
114
119
@available ( SwiftStdlib 5 . 7 , * )
115
120
public mutating func trimPrefix(
116
- while predicate: @escaping ( Element ) -> Bool
117
- ) {
118
- trimPrefix ( ManyConsumer (
119
- base : PredicateConsumer < SubSequence > ( predicate : predicate ) ) )
121
+ while predicate: ( Element ) throws -> Bool
122
+ ) throws {
123
+ let end = try endOfPrefix ( while : predicate )
124
+ self = self [ end ... ]
120
125
}
121
126
}
122
127
123
128
extension RangeReplaceableCollection {
124
129
@_disfavoredOverload
125
130
@available ( SwiftStdlib 5 . 7 , * )
126
131
public mutating func trimPrefix(
127
- while predicate: @escaping ( Element ) -> Bool
128
- ) {
129
- trimPrefix ( ManyConsumer ( base: PredicateConsumer ( predicate: predicate) ) )
132
+ while predicate: ( Element ) throws -> Bool
133
+ ) rethrows {
134
+ let end = try endOfPrefix ( while: predicate)
135
+ removeSubrange ( startIndex..< end)
130
136
}
131
137
}
132
138
0 commit comments