@@ -55,11 +55,15 @@ extension LazySplitSequence.Iterator: IteratorProtocol {
55
55
var currentElement = base. next ( )
56
56
var subsequence : Element = [ ]
57
57
58
+ // Add the next elements of the base sequence to this subsequence, until we
59
+ // reach a separator, unless we've already split the maximum number of
60
+ // times. In all cases, stop at the end of the base sequence.
58
61
while currentElement != nil {
59
62
if separatorCount < maxSplits && isSeparator ( currentElement!) {
60
63
separatorCount += 1
61
64
62
65
if omittingEmptySubsequences && subsequence. isEmpty {
66
+ // Keep going if we don't want to return an empty subsequence.
63
67
currentElement = base. next ( )
64
68
continue
65
69
} else {
@@ -71,22 +75,18 @@ extension LazySplitSequence.Iterator: IteratorProtocol {
71
75
}
72
76
}
73
77
74
- if currentElement == nil {
75
- if sequenceLength < separatorCount + 1 {
76
- if ! subsequence. isEmpty || !omittingEmptySubsequences {
77
- sequenceLength += 1
78
- return subsequence
79
- } else {
80
- return nil
81
- }
82
- } else {
83
- return nil
84
- }
78
+ // We're done iterating when we've reached the end of the base sequence,
79
+ // and we've either returned the maximum number of subsequences (one more
80
+ // than the number of separators), or the only subsequence left to return is
81
+ // empty and we're omitting those.
82
+ if currentElement == nil
83
+ && ( sequenceLength == separatorCount + 1
84
+ || omittingEmptySubsequences && subsequence . isEmpty ) {
85
+ return nil
86
+ } else {
87
+ sequenceLength += 1
88
+ return subsequence
85
89
}
86
-
87
- sequenceLength += 1
88
-
89
- return subsequence
90
90
}
91
91
}
92
92
0 commit comments