@@ -106,121 +106,6 @@ extension SplitCollection: Sequence {
106
106
}
107
107
}
108
108
109
- //extension SplitCollection: Collection {
110
- // public struct Index {
111
- // var start: Base.Index
112
- // var base: RangesCollection<Searcher>.Index
113
- // var isEndIndex: Bool
114
- // }
115
- //
116
- // public var startIndex: Index {
117
- // let base = ranges.startIndex
118
- // return Index(start: ranges.base.startIndex, base: base, isEndIndex: false)
119
- // }
120
- //
121
- // public var endIndex: Index {
122
- // Index(start: ranges.base.endIndex, base: ranges.endIndex, isEndIndex: true)
123
- // }
124
- //
125
- // public func formIndex(after index: inout Index) {
126
- // guard !index.isEndIndex else { fatalError("Cannot advance past endIndex") }
127
- //
128
- // if let range = index.base.range {
129
- // let newStart = range.upperBound
130
- // ranges.formIndex(after: &index.base)
131
- // index.start = newStart
132
- // } else {
133
- // index.isEndIndex = true
134
- // }
135
- // }
136
- //
137
- // public func index(after index: Index) -> Index {
138
- // var index = index
139
- // formIndex(after: &index)
140
- // return index
141
- // }
142
- //
143
- // public subscript(index: Index) -> Base.SubSequence {
144
- // guard !index.isEndIndex else {
145
- // fatalError("Cannot subscript using endIndex")
146
- // }
147
- // let end = index.base.range?.lowerBound ?? ranges.base.endIndex
148
- // return ranges.base[index.start..<end]
149
- // }
150
- //}
151
- //
152
- //extension SplitCollection.Index: Comparable {
153
- // static func == (lhs: Self, rhs: Self) -> Bool {
154
- // switch (lhs.isEndIndex, rhs.isEndIndex) {
155
- // case (false, false):
156
- // return lhs.start == rhs.start
157
- // case (let lhs, let rhs):
158
- // return lhs == rhs
159
- // }
160
- // }
161
- //
162
- // static func < (lhs: Self, rhs: Self) -> Bool {
163
- // switch (lhs.isEndIndex, rhs.isEndIndex) {
164
- // case (true, _):
165
- // return false
166
- // case (_, true):
167
- // return true
168
- // case (false, false):
169
- // return lhs.start < rhs.start
170
- // }
171
- // }
172
- //}
173
-
174
- // MARK: `ReversedSplitCollection`
175
-
176
- struct ReversedSplitCollection < Searcher: BackwardCollectionSearcher > {
177
- public typealias Base = Searcher . BackwardSearched
178
-
179
- let ranges : ReversedRangesCollection < Searcher >
180
-
181
- init ( ranges: ReversedRangesCollection < Searcher > ) {
182
- self . ranges = ranges
183
- }
184
-
185
- init ( base: Base , searcher: Searcher ) {
186
- self . ranges = base. _rangesFromBack ( of: searcher)
187
- }
188
- }
189
-
190
- extension ReversedSplitCollection : Sequence {
191
- public struct Iterator : IteratorProtocol {
192
- let base : Base
193
- var index : Base . Index
194
- var ranges : ReversedRangesCollection < Searcher > . Iterator
195
- var isDone : Bool
196
-
197
- init ( ranges: ReversedRangesCollection < Searcher > ) {
198
- self . base = ranges. base
199
- self . index = base. endIndex
200
- self . ranges = ranges. makeIterator ( )
201
- self . isDone = false
202
- }
203
-
204
- public mutating func next( ) -> Base . SubSequence ? {
205
- guard !isDone else { return nil }
206
-
207
- guard let range = ranges. next ( ) else {
208
- isDone = true
209
- return base [ ..< index]
210
- }
211
-
212
- defer { index = range. lowerBound }
213
- return base [ range. upperBound..< index]
214
- }
215
- }
216
-
217
- public func makeIterator( ) -> Iterator {
218
- Iterator ( ranges: ranges)
219
- }
220
- }
221
-
222
- // TODO: `Collection` conformance
223
-
224
109
// MARK: `CollectionSearcher` algorithms
225
110
226
111
extension Collection {
@@ -237,16 +122,6 @@ extension Collection {
237
122
}
238
123
}
239
124
240
- extension BidirectionalCollection {
241
- func splitFromBack< Searcher: BackwardCollectionSearcher > (
242
- by separator: Searcher
243
- ) -> ReversedSplitCollection < Searcher >
244
- where Searcher. BackwardSearched == Self
245
- {
246
- ReversedSplitCollection ( base: self , searcher: separator)
247
- }
248
- }
249
-
250
125
// MARK: Predicate algorithms
251
126
252
127
extension Collection {
@@ -260,14 +135,6 @@ extension Collection {
260
135
}
261
136
}
262
137
263
- extension BidirectionalCollection where Element: Equatable {
264
- func splitFromBack(
265
- whereSeparator predicate: @escaping ( Element ) -> Bool
266
- ) -> ReversedSplitCollection < PredicateConsumer < Self > > {
267
- splitFromBack ( by: PredicateConsumer ( predicate: predicate) )
268
- }
269
- }
270
-
271
138
// MARK: Single element algorithms
272
139
273
140
extension Collection where Element: Equatable {
@@ -280,14 +147,6 @@ extension Collection where Element: Equatable {
280
147
}
281
148
}
282
149
283
- extension BidirectionalCollection where Element: Equatable {
284
- func splitFromBack(
285
- by separator: Element
286
- ) -> ReversedSplitCollection < PredicateConsumer < Self > > {
287
- splitFromBack ( whereSeparator: { $0 == separator } )
288
- }
289
- }
290
-
291
150
// MARK: Fixed pattern algorithms
292
151
293
152
extension Collection where Element: Equatable {
@@ -399,21 +258,6 @@ extension StringProtocol where SubSequence == Substring {
399
258
400
259
@available ( SwiftStdlib 5 . 7 , * )
401
260
extension BidirectionalCollection where SubSequence == Substring {
402
- @_disfavoredOverload
403
- func split< R: RegexComponent > (
404
- by separator: R ,
405
- maxSplits: Int ,
406
- omittingEmptySubsequences: Bool
407
- ) -> SplitCollection < RegexConsumer < R , Self > > {
408
- split ( by: RegexConsumer ( separator) , maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
409
- }
410
-
411
- func splitFromBack< R: RegexComponent > (
412
- by separator: R
413
- ) -> ReversedSplitCollection < RegexConsumer < R , Self > > {
414
- splitFromBack ( by: RegexConsumer ( separator) )
415
- }
416
-
417
261
// TODO: Is this @_disfavoredOverload necessary?
418
262
// It prevents split(separator: String) from choosing this overload instead
419
263
// of the collection-based version when String has RegexComponent conformance
@@ -431,9 +275,34 @@ extension BidirectionalCollection where SubSequence == Substring {
431
275
maxSplits: Int = . max,
432
276
omittingEmptySubsequences: Bool = true
433
277
) -> [ SubSequence ] {
434
- Array ( split (
435
- by: RegexConsumer ( separator) ,
436
- maxSplits: maxSplits,
437
- omittingEmptySubsequences: omittingEmptySubsequences) )
278
+ var result : [ SubSequence ] = [ ]
279
+ var subSequenceStart = startIndex
280
+
281
+ func appendSubsequence( end: Index ) -> Bool {
282
+ if subSequenceStart == end && omittingEmptySubsequences {
283
+ return false
284
+ }
285
+ result. append ( self [ subSequenceStart..< end] )
286
+ return true
287
+ }
288
+
289
+ guard maxSplits > 0 && !isEmpty else {
290
+ _ = appendSubsequence ( end: endIndex)
291
+ return result
292
+ }
293
+
294
+ for match in _matches ( of: separator) {
295
+ defer { subSequenceStart = match. range. upperBound }
296
+ let didAppend = appendSubsequence ( end: match. range. lowerBound)
297
+ if didAppend && result. count == maxSplits {
298
+ break
299
+ }
300
+ }
301
+
302
+ if subSequenceStart != endIndex || !omittingEmptySubsequences {
303
+ result. append ( self [ subSequenceStart..< endIndex] )
304
+ }
305
+
306
+ return result
438
307
}
439
308
}
0 commit comments