Skip to content

Commit bb43874

Browse files
authored
Merge pull request #3047 from austinzheng/az-fix-tests
2 parents 2526da3 + 9138b73 commit bb43874

File tree

3 files changed

+145
-173
lines changed

3 files changed

+145
-173
lines changed

stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ extension TestSuite {
17651765
}
17661766
} else {
17671767
expectCrashLater()
1768-
c.formIndex(&new, offsetBy: numericCast(test.distance), limitedBy: limit)
1768+
_ = c.formIndex(&new, offsetBy: numericCast(test.distance), limitedBy: limit)
17691769
}
17701770
}
17711771
}

stdlib/public/core/ClosedRange.swift

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -38,75 +38,6 @@ public struct ClosedRangeIndex<Bound> : Comparable
3838

3939
/// Creates a position `p` for which `r[p] == x`.
4040
internal init(_ x: Bound) { _value = .inRange(x) }
41-
42-
internal func _successor(upperBound limit: Bound) -> ClosedRangeIndex {
43-
switch _value {
44-
case .inRange(let x): return x == limit
45-
? ClosedRangeIndex() : ClosedRangeIndex(x.advanced(by: 1))
46-
case .pastEnd: _preconditionFailure("Incrementing past end index")
47-
}
48-
}
49-
50-
internal func _predecessor(
51-
lowerBound: Bound, upperBound limit: Bound
52-
) -> ClosedRangeIndex {
53-
switch _value {
54-
case .inRange(let x):
55-
_precondition(x > lowerBound, "Incrementing past start index")
56-
return ClosedRangeIndex(x.advanced(by: -1))
57-
case .pastEnd:
58-
_precondition(limit >= lowerBound, "Incrementing past start index")
59-
return ClosedRangeIndex(limit)
60-
}
61-
}
62-
63-
internal func _advanced(
64-
by n: Bound.Stride, lowerBound: Bound, upperBound limit: Bound
65-
) -> ClosedRangeIndex {
66-
switch _value {
67-
case .inRange(let x):
68-
let d = x.distance(to: limit)
69-
if n <= d {
70-
let newPosition = x.advanced(by: n)
71-
_precondition(newPosition >= lowerBound,
72-
"Advancing past start index")
73-
return ClosedRangeIndex(newPosition)
74-
}
75-
if d - -1 == n { return ClosedRangeIndex() }
76-
_preconditionFailure("Advancing past end index")
77-
case .pastEnd:
78-
if n == 0 {
79-
return self
80-
} else if n > 0 {
81-
_preconditionFailure("Advancing past end index")
82-
} else {
83-
return ClosedRangeIndex(limit)._advanced(
84-
by: (n + 1),
85-
lowerBound: lowerBound,
86-
upperBound: limit
87-
)
88-
}
89-
}
90-
}
91-
92-
internal func _distance(
93-
to: ClosedRangeIndex<Bound>, upperBound limit: Bound
94-
) -> Bound.Stride {
95-
switch (_value, to._value) {
96-
case let (.inRange(left), .inRange(right)):
97-
// in range <--> in range
98-
return left.distance(to: right)
99-
case let (.inRange(left), .pastEnd):
100-
// in range --> end
101-
return 1 + left.distance(to: limit)
102-
case let (.pastEnd, .inRange(right)):
103-
// in range <-- end
104-
return limit.distance(to: right) - 1
105-
case (.pastEnd, .pastEnd):
106-
// end <--> end
107-
return 0
108-
}
109-
}
11041

11142
internal var _value: _ClosedRangeIndexRepresentation<Bound>
11243
internal var _dereferenced : Bound {
@@ -258,19 +189,65 @@ public struct CountableClosedRange<Bound> : RandomAccessCollection
258189
}
259190

260191
public func index(after i: Index) -> Index {
261-
return i._successor(upperBound: upperBound)
192+
switch i._value {
193+
case .inRange(let x):
194+
return x == upperBound
195+
? ClosedRangeIndex()
196+
: ClosedRangeIndex(x.advanced(by: 1))
197+
case .pastEnd:
198+
_preconditionFailure("Incrementing past end index")
199+
}
262200
}
263201

264202
public func index(before i: Index) -> Index {
265-
return i._predecessor(lowerBound: lowerBound, upperBound: upperBound)
203+
switch i._value {
204+
case .inRange(let x):
205+
_precondition(x > lowerBound, "Incrementing past start index")
206+
return ClosedRangeIndex(x.advanced(by: -1))
207+
case .pastEnd:
208+
_precondition(upperBound >= lowerBound, "Incrementing past start index")
209+
return ClosedRangeIndex(upperBound)
210+
}
266211
}
267212

268213
public func index(_ i: Index, offsetBy n: IndexDistance) -> Index {
269-
return i._advanced(by: n, lowerBound: lowerBound, upperBound: upperBound)
214+
switch i._value {
215+
case .inRange(let x):
216+
let d = x.distance(to: upperBound)
217+
if n <= d {
218+
let newPosition = x.advanced(by: n)
219+
_precondition(newPosition >= lowerBound,
220+
"Advancing past start index")
221+
return ClosedRangeIndex(newPosition)
222+
}
223+
if d - -1 == n { return ClosedRangeIndex() }
224+
_preconditionFailure("Advancing past end index")
225+
case .pastEnd:
226+
if n == 0 {
227+
return i
228+
} else if n > 0 {
229+
_preconditionFailure("Advancing past end index")
230+
} else {
231+
return index(ClosedRangeIndex(upperBound), offsetBy: (n + 1))
232+
}
233+
}
270234
}
271235

272236
public func distance(from start: Index, to end: Index) -> IndexDistance {
273-
return start._distance(to: end, upperBound: upperBound)
237+
switch (start._value, end._value) {
238+
case let (.inRange(left), .inRange(right)):
239+
// in range <--> in range
240+
return left.distance(to: right)
241+
case let (.inRange(left), .pastEnd):
242+
// in range --> end
243+
return 1 + left.distance(to: upperBound)
244+
case let (.pastEnd, .inRange(right)):
245+
// in range <-- end
246+
return upperBound.distance(to: right) - 1
247+
case (.pastEnd, .pastEnd):
248+
// end <--> end
249+
return 0
250+
}
274251
}
275252

276253
/// Accesses the element at specified position.

0 commit comments

Comments
 (0)