index(n, stepsFrom: i)

M-x findr-query-replace

\<advance(\([^:]+?\),\([
]+\)by: *\([^(),]*\|[^(),]+([^()]*)[^(),]*\)\(,\(?:[
]+\)limit: *\(?:[^()]*\|[^()]+([^()]*)[^()]*\)\)?)

index(\3,\2stepsFrom: \1\4)
This commit is contained in:
Dave Abrahams
2016-03-25 17:54:39 -07:00
parent 11259d1d14
commit 01127b32d5
31 changed files with 157 additions and 157 deletions

View File

@@ -23,8 +23,8 @@ public struct SubscriptRangeTest {
public func bounds<C : Collection>(in c: C) -> Range<C.Index> {
let i = c.startIndex
return c.advance(i, by: numericCast(bounds.lowerBound)) ..<
c.advance(i, by: numericCast(bounds.upperBound))
return c.index(numericCast(bounds.lowerBound), stepsFrom: i) ..<
c.index(numericCast(bounds.upperBound), stepsFrom: i)
}
public init(
@@ -378,10 +378,10 @@ if resiliencyChecks.creatingOutOfBoundsIndicesBehavior != .none {
let index = c.endIndex
if resiliencyChecks.creatingOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
_blackHole(c.advance(index, by: numericCast(outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(outOfBoundsIndexOffset), stepsFrom: index))
} else {
expectFailure {
_blackHole(c.advance(index, by: numericCast(outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(outOfBoundsIndexOffset), stepsFrom: index))
}
}
}
@@ -391,10 +391,10 @@ if resiliencyChecks.creatingOutOfBoundsIndicesBehavior != .none {
let index = c.endIndex
if resiliencyChecks.creatingOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
_blackHole(c.advance(index, by: numericCast(outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(outOfBoundsIndexOffset), stepsFrom: index))
} else {
expectFailure {
_blackHole(c.advance(index, by: numericCast(outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(outOfBoundsIndexOffset), stepsFrom: index))
}
}
}
@@ -410,11 +410,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.endIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
}
}
@@ -425,11 +425,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.endIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
}
}
@@ -464,11 +464,11 @@ if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
var index = c.endIndex
if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
}
}
@@ -479,11 +479,11 @@ if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
var index = c.endIndex
if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
}
}
@@ -678,7 +678,7 @@ self.test("\(testNamePrefix).split/semantics") {
self.test("\(testNamePrefix).prefix(through:)/semantics") {
for test in prefixThroughTests {
let c = makeWrappedCollection(test.collection.map(OpaqueValue.init))
let index = c.advance(c.startIndex, by: numericCast(test.position))
let index = c.index(numericCast(test.position), stepsFrom: c.startIndex)
let result = c.prefix(through: index)
expectEqualSequence(test.expected, result.map(extractValue).map { $0.value },
stackTrace: SourceLocStack().with(test.loc))
@@ -692,7 +692,7 @@ self.test("\(testNamePrefix).prefix(through:)/semantics") {
self.test("\(testNamePrefix).prefix(upTo:)/semantics") {
for test in prefixUpToTests {
let c = makeWrappedCollection(test.collection.map(OpaqueValue.init))
let index = c.advance(c.startIndex, by: numericCast(test.end))
let index = c.index(numericCast(test.end), stepsFrom: c.startIndex)
let result = c.prefix(upTo: index)
expectEqualSequence(test.expected, result.map(extractValue).map { $0.value },
stackTrace: SourceLocStack().with(test.loc))
@@ -706,7 +706,7 @@ self.test("\(testNamePrefix).prefix(upTo:)/semantics") {
self.test("\(testNamePrefix).suffix(from:)/semantics") {
for test in suffixFromTests {
let c = makeWrappedCollection(test.collection.map(OpaqueValue.init))
let index = c.advance(c.startIndex, by: numericCast(test.start))
let index = c.index(numericCast(test.start), stepsFrom: c.startIndex)
let result = c.suffix(from: index)
expectEqualSequence(test.expected, result.map(extractValue).map { $0.value },
stackTrace: SourceLocStack().with(test.loc))
@@ -763,7 +763,7 @@ self.test("\(testNamePrefix).removeFirst(n: Int)/slice/semantics") {
var slice = c[c.startIndex..<c.endIndex]
let survivingIndices = _allIndices(
into: slice,
in: slice.advance(slice.startIndex, by: numericCast(test.numberToRemove)) ..<
in: slice.index(numericCast(test.numberToRemove), stepsFrom: slice.startIndex) ..<
slice.endIndex
)
slice.removeFirst(test.numberToRemove)
@@ -945,7 +945,7 @@ self.test("\(testNamePrefix).removeLast()/slice/semantics") {
let survivingIndices = _allIndices(
into: slice,
in: slice.startIndex ..<
slice.advance(slice.endIndex, by: numericCast(-test.numberToRemove))
slice.index(numericCast(-test.numberToRemove), stepsFrom: slice.endIndex)
)
let removedElement = slice.removeLast()
expectEqual(
@@ -989,7 +989,7 @@ self.test("\(testNamePrefix).removeLast(n: Int)/slice/semantics") {
let survivingIndices = _allIndices(
into: slice,
in: slice.startIndex ..<
slice.advance(slice.endIndex, by: numericCast(-test.numberToRemove))
slice.index(numericCast(-test.numberToRemove), stepsFrom: slice.endIndex)
)
slice.removeLast(test.numberToRemove)
expectEqualSequence(
@@ -1045,7 +1045,7 @@ self.test("\(testNamePrefix).popLast()/slice/semantics") {
let survivingIndices = _allIndices(
into: slice,
in: slice.startIndex ..<
slice.advance(slice.endIndex, by: numericCast(-test.numberToRemove))
slice.index(numericCast(-test.numberToRemove), stepsFrom: slice.endIndex)
)
let removedElement = slice.popLast()!
expectEqual(
@@ -1088,10 +1088,10 @@ if resiliencyChecks.creatingOutOfBoundsIndicesBehavior != .none {
let index = c.startIndex
if resiliencyChecks.creatingOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
_blackHole(c.advance(index, by: numericCast(-outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(-outOfBoundsIndexOffset), stepsFrom: index))
} else {
expectFailure {
_blackHole(c.advance(index, by: numericCast(-outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(-outOfBoundsIndexOffset), stepsFrom: index))
}
}
}
@@ -1101,10 +1101,10 @@ if resiliencyChecks.creatingOutOfBoundsIndicesBehavior != .none {
let index = c.startIndex
if resiliencyChecks.creatingOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
_blackHole(c.advance(index, by: numericCast(-outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(-outOfBoundsIndexOffset), stepsFrom: index))
} else {
expectFailure {
_blackHole(c.advance(index, by: numericCast(-outOfBoundsIndexOffset)))
_blackHole(c.index(numericCast(-outOfBoundsIndexOffset), stepsFrom: index))
}
}
}
@@ -1120,11 +1120,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.startIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
}
}
@@ -1135,11 +1135,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.startIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index])
}
}
@@ -1156,11 +1156,11 @@ if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
var index = c.startIndex
if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
}
}
@@ -1171,11 +1171,11 @@ if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
var index = c.startIndex
if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
} else {
expectFailure {
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
_blackHole(c[index..<index])
}
}

View File

@@ -153,11 +153,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.endIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
} else {
expectFailure {
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
}
}
@@ -168,11 +168,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.endIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
} else {
expectFailure {
index = c.advance(index, by: numericCast(outOfBoundsSubscriptOffset))
index = c.index(numericCast(outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
}
}
@@ -517,11 +517,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.startIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
} else {
expectFailure {
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
}
}
@@ -532,11 +532,11 @@ if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior != .none {
var index = c.startIndex
if resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior == .trap {
expectCrashLater()
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
} else {
expectFailure {
index = c.advance(index, by: numericCast(-outOfBoundsSubscriptOffset))
index = c.index(numericCast(-outOfBoundsSubscriptOffset), stepsFrom: index)
c[index] = wrapValue(OpaqueValue(9999))
}
}

View File

@@ -26,15 +26,15 @@ internal enum RangeSelection {
case .leftEdge: return c.startIndex..<c.startIndex
case .rightEdge: return c.endIndex..<c.endIndex
case .middle:
let start = c.advance(c.startIndex, by: c.count / 4)
let end = c.advance(c.startIndex, by: 3 * c.count / 4 + 1)
let start = c.index(c.count / 4, stepsFrom: c.startIndex)
let end = c.index(3 * c.count / 4 + 1, stepsFrom: c.startIndex)
return start..<end
case .leftHalf:
let start = c.startIndex
let end = c.advance(start, by: c.count / 2)
let end = c.index(c.count / 2, stepsFrom: start)
return start..<end
case .rightHalf:
let start = c.advance(c.startIndex, by: c.count / 2)
let start = c.index(c.count / 2, stepsFrom: c.startIndex)
let end = c.endIndex
return start..<end
}
@@ -50,9 +50,9 @@ internal enum IndexSelection {
internal func index<C : Collection>(in c: C) -> C.Index {
switch self {
case .start: return c.startIndex
case .middle: return c.advance(c.startIndex, by: c.count / 2)
case .middle: return c.index(c.count / 2, stepsFrom: c.startIndex)
case .end: return c.endIndex
case .last: return c.advance(c.startIndex, by: c.count - 1)
case .last: return c.index(c.count - 1, stepsFrom: c.startIndex)
}
}
}

View File

@@ -108,7 +108,7 @@ extension TestSuite {
var c = makeWrappedCollection(test.collection.map(OpaqueValue.init))
let survivingIndices = _allIndices(
into: c,
in: c.advance(c.startIndex, by: numericCast(test.numberToRemove)) ..<
in: c.index(numericCast(test.numberToRemove), stepsFrom: c.startIndex) ..<
c.endIndex
)
c.removeFirst(test.numberToRemove)
@@ -257,7 +257,7 @@ extension TestSuite {
let survivingIndices = _allIndices(
into: c,
in: c.startIndex ..<
c.advance(c.endIndex, by: numericCast(-test.numberToRemove))
c.index(numericCast(-test.numberToRemove), stepsFrom: c.endIndex)
)
c.removeLast(test.numberToRemove)
expectEqualSequence(

View File

@@ -420,13 +420,13 @@ public struct ${Self}<
@warn_unused_result
public func advance(i: Index, by n: IndexDistance) -> Index {
Log.advance[selfType] += 1
return base.advance(i, by: n)
return base.index(n, stepsFrom: i)
}
@warn_unused_result
public func advance(i: Index, by n: IndexDistance, limit: Index) -> Index {
Log.advanceLimit[selfType] += 1
return base.advance(i, by: n, limit: limit)
return base.index(n, stepsFrom: i, limit: limit)
}
@warn_unused_result

View File

@@ -634,12 +634,12 @@ FIXME: swift-3-indexing-model: move this code to an appropriate place.
public subscript(i: MinimalIndex) -> T {
get {
_expectCompatibleIndices(advance(startIndex, by: i.position), i)
_expectCompatibleIndices(index(i.position, stepsFrom: startIndex), i)
return _elements[i.position]
}
% if Mutable:
set {
_expectCompatibleIndices(advance(startIndex, by: i.position), i)
_expectCompatibleIndices(index(i.position, stepsFrom: startIndex), i)
_elements[i.position] = newValue
}
% end

View File

@@ -1841,7 +1841,7 @@ public func checkStrideable<
}
public func nthIndex<C: Collection>(x: C, _ n: Int) -> C.Index {
return x.advance(x.startIndex, by: numericCast(n))
return x.index(numericCast(n), stepsFrom: x.startIndex)
}
public func nth<C: Collection>(x: C, _ n: Int) -> C.Iterator.Element {

View File

@@ -59,5 +59,5 @@ public func pickRandom<
C : RandomAccessCollection
>(c: C) -> C.Iterator.Element {
let i = Int(rand32(exclusiveUpperBound: numericCast(c.count)))
return c[c.advance(c.startIndex, by: numericCast(i))]
return c[c.index(numericCast(i), stepsFrom: c.startIndex)]
}

View File

@@ -201,7 +201,7 @@ extension BidirectionalCollection where SubSequence == Self {
_precondition(n >= 0, "number of elements to remove should be non-negative")
_precondition(count >= numericCast(n),
"can't remove more items from a collection than it contains")
self = self[startIndex..<advance(endIndex, by: numericCast(-n))]
self = self[startIndex..<index(numericCast(-n), stepsFrom: endIndex)]
}
}
@@ -214,7 +214,7 @@ extension BidirectionalCollection {
public func dropLast(n: Int) -> SubSequence {
_precondition(
n >= 0, "Can't drop a negative number of elements from a collection")
let end = advance(endIndex, by: numericCast(-n), limit: startIndex)
let end = index(numericCast(-n), stepsFrom: endIndex, limit: startIndex)
return self[startIndex..<end]
}
@@ -231,7 +231,7 @@ extension BidirectionalCollection {
_precondition(
maxLength >= 0,
"Can't take a suffix of negative length from a collection")
let start = advance(endIndex, by: numericCast(-maxLength), limit: startIndex)
let start = index(numericCast(-maxLength), stepsFrom: endIndex, limit: startIndex)
return self[start..<endIndex]
}
}

View File

@@ -613,7 +613,7 @@ extension Collection {
@warn_unused_result
public func dropFirst(n: Int) -> SubSequence {
_precondition(n >= 0, "Can't drop a negative number of elements from a collection")
let start = advance(startIndex, by: numericCast(n), limit: endIndex)
let start = index(numericCast(n), stepsFrom: startIndex, limit: endIndex)
return self[start..<endIndex]
}
@@ -626,7 +626,7 @@ extension Collection {
_precondition(
n >= 0, "Can't drop a negative number of elements from a collection")
let amount = Swift.max(0, numericCast(count) - n)
let end = advance(startIndex, by: numericCast(amount), limit: endIndex)
let end = index(numericCast(amount), stepsFrom: startIndex, limit: endIndex)
return self[startIndex..<end]
}
@@ -643,7 +643,7 @@ extension Collection {
_precondition(
maxLength >= 0,
"Can't take a prefix of negative length from a collection")
let end = advance(startIndex, by: numericCast(maxLength), limit: endIndex)
let end = index(numericCast(maxLength), stepsFrom: startIndex, limit: endIndex)
return self[startIndex..<end]
}
@@ -661,7 +661,7 @@ extension Collection {
maxLength >= 0,
"Can't take a suffix of negative length from a collection")
let amount = Swift.max(0, numericCast(count) - maxLength)
let start = advance(startIndex, by: numericCast(amount), limit: endIndex)
let start = index(numericCast(amount), stepsFrom: startIndex, limit: endIndex)
return self[start..<endIndex]
}
@@ -808,7 +808,7 @@ extension Collection where SubSequence == Self {
_precondition(n >= 0, "number of elements to remove should be non-negative")
_precondition(count >= numericCast(n),
"can't remove more items from a collection than it contains")
self = self[advance(startIndex, by: numericCast(n))..<endIndex]
self = self[index(numericCast(n), stepsFrom: startIndex)..<endIndex]
}
}

View File

@@ -154,7 +154,7 @@ ${orderingRequirementForComparable}
return unsafeBufferPivot - bufferPointer.startIndex
}
if let offset = maybeOffset {
return advance(startIndex, by: numericCast(offset))
return index(numericCast(offset), stepsFrom: startIndex)
}
% if preds:

View File

@@ -174,13 +174,13 @@ extension LazyCollection : Collection {
// TODO: swift-3-indexing-model - add docs
@warn_unused_result
public func advance(i: Index, by n: Base.IndexDistance) -> Index {
return _base.advance(i, by: n)
return _base.index(n, stepsFrom: i)
}
// TODO: swift-3-indexing-model - add docs
@warn_unused_result
public func advance(i: Index, by n: Base.IndexDistance, limit: Index) -> Index {
return _base.advance(i, by: n, limit: limit)
return _base.index(n, stepsFrom: i, limit: limit)
}
// TODO: swift-3-indexing-model - add docs

View File

@@ -164,12 +164,12 @@ public struct ${Self}<
@warn_unused_result
public func advance(i: Index, by n: Base.IndexDistance) -> Index {
return _base.advance(i, by: n)
return _base.index(n, stepsFrom: i)
}
@warn_unused_result
public func advance(i: Index, by n: Base.IndexDistance, limit: Index) -> Index {
return _base.advance(i, by: n, limit: limit)
return _base.index(n, stepsFrom: i, limit: limit)
}
@warn_unused_result

View File

@@ -446,9 +446,9 @@ extension Mirror {
position = children.index { $0.label == label } ?? children.endIndex
}
else if let offset = (e as? Int).map({ IntMax($0) }) ?? (e as? IntMax) {
position = children.advance(
position = children.index(offset,
stepsFrom:
children.startIndex,
by: offset,
limit: children.endIndex)
}
else {

View File

@@ -291,7 +291,7 @@ extension RangeReplaceableCollection {
_precondition(n >= 0, "number of elements to remove should be non-negative")
_precondition(count >= numericCast(n),
"can't remove more items from a collection than it has")
let end = advance(startIndex, by: numericCast(n))
let end = index(numericCast(n), stepsFrom: startIndex)
removeSubrange(startIndex..<end)
}
@@ -336,7 +336,7 @@ extension RangeReplaceableCollection where SubSequence == Self {
_precondition(n >= 0, "number of elements to remove should be non-negative")
_precondition(count >= numericCast(n),
"can't remove more items from a collection than it contains")
self = self[advance(startIndex, by: numericCast(n))..<endIndex]
self = self[index(numericCast(n), stepsFrom: startIndex)..<endIndex]
}
}
@@ -386,7 +386,7 @@ extension RangeReplaceableCollection
@warn_unused_result
public mutating func _customRemoveLast(n: Int) -> Bool {
self = self[startIndex..<advance(endIndex, by: numericCast(-n))]
self = self[startIndex..<index(numericCast(-n), stepsFrom: endIndex)]
return true
}
}
@@ -417,7 +417,7 @@ extension RangeReplaceableCollection where Self : BidirectionalCollection {
return
}
let end = endIndex
removeSubrange(advance(end, by: numericCast(-n))..<end)
removeSubrange(index(numericCast(-n), stepsFrom: end)..<end)
}
}
@@ -453,7 +453,7 @@ extension RangeReplaceableCollection
return
}
let end = endIndex
removeSubrange(advance(end, by: numericCast(-n))..<end)
removeSubrange(index(numericCast(-n), stepsFrom: end)..<end)
}
}

View File

@@ -105,13 +105,13 @@ public struct ReversedCollection<
@warn_unused_result
public func advance(i: Index, by n: IndexDistance) -> Index {
// FIXME: swift-3-indexing-model: `-n` can trap on Int.min.
return ReversedIndex(_base.advance(i.base, by: -n))
return ReversedIndex(_base.index(-n, stepsFrom: i.base))
}
@warn_unused_result
public func advance(i: Index, by n: IndexDistance, limit: Index) -> Index {
// FIXME: swift-3-indexing-model: `-n` can trap on Int.min.
//return ReversedIndex(_base.advance(i.base, by: -n, limit: ???)
//return ReversedIndex(_base.index(-n, stepsFrom: i.base, limit: ???)
fatalError("FIXME: swift-3-indexing-model")
}
@@ -219,14 +219,14 @@ public struct ReversedRandomAccessCollection<
public func advance(i: Index, by n: IndexDistance) -> Index {
// FIXME: swift-3-indexing-model: `-n` can trap on Int.min.
// FIXME: swift-3-indexing-model: tests.
return ReversedRandomAccessIndex(_base.advance(i.base, by: -n))
return ReversedRandomAccessIndex(_base.index(-n, stepsFrom: i.base))
}
@warn_unused_result
public func advance(i: Index, by n: IndexDistance, limit: Index) -> Index {
// FIXME: swift-3-indexing-model: `-n` can trap on Int.min.
// FIXME: swift-3-indexing-model: tests.
return Index(_base.advance(i.base, by: -n, limit: limit.base))
return Index(_base.index(-n, stepsFrom: i.base, limit: limit.base))
}
@warn_unused_result

View File

@@ -137,12 +137,12 @@ public struct ${Self}<Base : ${BaseRequirements}>
@warn_unused_result
public func advance(i: Index, by n: IndexDistance) -> Index {
return _base.advance(i, by: n)
return _base.index(n, stepsFrom: i)
}
@warn_unused_result
public func advance(i: Index, by n: IndexDistance, limit: Index) -> Index {
return _base.advance(i, by: n, limit: limit)
return _base.index(n, stepsFrom: i, limit: limit)
}
@warn_unused_result

View File

@@ -227,7 +227,7 @@ func _siftDown<
if countToIndex + 1 >= countFromIndex {
return
}
let left = elements.advance(index, by: countToIndex + 1)
let left = elements.index(countToIndex + 1, stepsFrom: index)
var largest = index
if ${cmp("elements[largest]", "elements[left]", p)} {
largest = left
@@ -265,9 +265,9 @@ func _heapify<
// We skip the rightmost half of the array, because these nodes don't have
// any children.
let root = range.lowerBound
var node = elements.advance(
root,
by: elements.distance(from: range.lowerBound, to: range.upperBound) / 2)
var node = elements.index(elements.distance(from: range.lowerBound, to: range.upperBound) / 2,
stepsFrom:
root)
while node != root {
elements.formPredecessor(&node)
_siftDown(

View File

@@ -562,13 +562,13 @@ extension String {
// TODO: swift-3-indexing-model - add docs
@warn_unused_result
public func advance(i: Index, by n: IndexDistance) -> Index {
return characters.advance(i, by: n)
return characters.index(n, stepsFrom: i)
}
// TODO: swift-3-indexing-model - add docs
@warn_unused_result
public func advance(i: Index, by n: IndexDistance, limit: Index) -> Index {
return characters.advance(i, by: n, limit: limit)
return characters.index(n, stepsFrom: i, limit: limit)
}
// TODO: swift-3-indexing-model - add docs

View File

@@ -399,13 +399,13 @@ extension String.UTF16View.Indices : BidirectionalCollection {
@warn_unused_result
public func advance(i: Index, by n: IndexDistance) -> Index {
// FIXME: swift-3-indexing-model: range check i?
return _elements.advance(i, by: n)
return _elements.index(n, stepsFrom: i)
}
@warn_unused_result
public func advance(i: Index, by n: IndexDistance, limit: Index) -> Index {
// FIXME: swift-3-indexing-model: range check i?
return _elements.advance(i, by: n, limit: limit)
return _elements.index(n, stepsFrom: i, limit: limit)
}
// TODO: swift-3-indexing-model - add docs

View File

@@ -721,13 +721,13 @@ internal func _transcodeSomeUTF16AsUTF8<
// Replace it with U+FFFD.
r = 0xbdbfef
scalarUtf8Length = 3
} else if _slowPath(input.advance(nextIndex, by: 1) == endIndex) {
} else if _slowPath(input.index(1, stepsFrom: nextIndex) == endIndex) {
// We have seen a high-surrogate and EOF, so we have an ill-formed
// sequence. Replace it with U+FFFD.
r = 0xbdbfef
scalarUtf8Length = 3
} else {
let unit1 = UInt(input[input.advance(nextIndex, by: 1)])
let unit1 = UInt(input[input.index(1, stepsFrom: nextIndex)])
if _fastPath((unit1 >> 10) == 0b1101_11) {
// `unit1` is a low-surrogate. We have a well-formed surrogate
// pair.
@@ -755,7 +755,7 @@ internal func _transcodeSomeUTF16AsUTF8<
result |= numericCast(r) << shift
utf8Count += scalarUtf8Length
}
nextIndex = input.advance(nextIndex, by: utf16Length)
nextIndex = input.index(utf16Length, stepsFrom: nextIndex)
}
// FIXME: Annoying check, courtesy of <rdar://problem/16740169>
if utf8Count < sizeofValue(result) {

View File

@@ -62,7 +62,7 @@ func find(substring: String, within domain: String) -> String.Index? {
if (domainCount < substringCount) { return nil }
var sliceStart = domain.startIndex
var sliceEnd = domain.advance(sliceStart, by: substringCount)
var sliceEnd = domain.index(substringCount, stepsFrom: sliceStart)
var i = 0
while true {
if domain[sliceStart..<sliceEnd] == substring {

View File

@@ -533,8 +533,8 @@ NSStringAPIs.test("enumerateLines(_:)") {
NSStringAPIs.test("enumerateLinguisticTagsIn(_:scheme:options:orthography:_:") {
let s = "Абв. Глокая куздра штеко будланула бокра и кудрячит бокрёнка. Абв."
let startIndex = s.advance(s.startIndex, by: 5)
let endIndex = s.advance(s.startIndex, by: 62)
let startIndex = s.index(5, stepsFrom: s.startIndex)
let endIndex = s.index(62, stepsFrom: s.startIndex)
var tags: [String] = []
var tokens: [String] = []
var sentences: [String] = []
@@ -562,8 +562,8 @@ NSStringAPIs.test("enumerateLinguisticTagsIn(_:scheme:options:orthography:_:") {
NSStringAPIs.test("enumerateSubstringsIn(_:options:_:)") {
let s = "\u{304b}\u{3099}お\u{263a}\u{fe0f}😀😊"
let startIndex = s.advance(s.startIndex, by: 1)
let endIndex = s.advance(s.startIndex, by: 5)
let startIndex = s.index(1, stepsFrom: s.startIndex)
let endIndex = s.index(5, stepsFrom: s.startIndex)
do {
var substrings: [String] = []
s.enumerateSubstrings(in: startIndex..<endIndex,
@@ -600,8 +600,8 @@ NSStringAPIs.test("fastestEncoding") {
NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remaining:)") {
let s = "abc абв def где gh жз zzz"
let startIndex = s.advance(s.startIndex, by: 8)
let endIndex = s.advance(s.startIndex, by: 22)
let startIndex = s.index(8, stepsFrom: s.startIndex)
let endIndex = s.index(22, stepsFrom: s.startIndex)
do {
// 'maxLength' is limiting.
let bufferLength = 100
@@ -619,7 +619,7 @@ NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remain
expectTrue(result)
expectEqualSequence(expectedStr, buffer)
expectEqual(11, usedLength)
expectEqual(remainingRange.lowerBound, s.advance(startIndex, by: 8))
expectEqual(remainingRange.lowerBound, s.index(8, stepsFrom: startIndex))
expectEqual(remainingRange.upperBound, endIndex)
}
do {
@@ -640,7 +640,7 @@ NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remain
expectTrue(result)
expectEqualSequence(expectedStr, buffer)
expectEqual(4, usedLength)
expectEqual(remainingRange.lowerBound, s.advance(startIndex, by: 4))
expectEqual(remainingRange.lowerBound, s.index(4, stepsFrom: startIndex))
expectEqual(remainingRange.upperBound, endIndex)
}
do {
@@ -680,7 +680,7 @@ NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remain
expectTrue(result)
expectEqualSequence(expectedStr, buffer)
expectEqual(4, usedLength)
expectEqual(remainingRange.lowerBound, s.advance(startIndex, by: 4))
expectEqual(remainingRange.lowerBound, s.index(4, stepsFrom: startIndex))
expectEqual(remainingRange.upperBound, endIndex)
}
}
@@ -733,7 +733,7 @@ NSStringAPIs.test("getCString(_:maxLength:encoding:)") {
NSStringAPIs.test("getLineStart(_:end:contentsEnd:forRange:)") {
let s = "Глокая куздра\nштеко будланула\nбокра и кудрячит\nбокрёнка."
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
let r = s.index(16, stepsFrom: s.startIndex)..<s.index(35, stepsFrom: s.startIndex)
do {
var outStartIndex = s.startIndex
var outLineEndIndex = s.startIndex
@@ -749,7 +749,7 @@ NSStringAPIs.test("getLineStart(_:end:contentsEnd:forRange:)") {
NSStringAPIs.test("getParagraphStart(_:end:contentsEnd:forRange:)") {
let s = "Глокая куздра\nштеко будланула\u{2028}бокра и кудрячит\u{2028}бокрёнка.\n Абв."
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
let r = s.index(16, stepsFrom: s.startIndex)..<s.index(35, stepsFrom: s.startIndex)
do {
var outStartIndex = s.startIndex
var outEndIndex = s.startIndex
@@ -878,7 +878,7 @@ NSStringAPIs.test("lengthOfBytesUsingEncoding(_:)") {
NSStringAPIs.test("lineRangeFor(_:)") {
let s = "Глокая куздра\nштеко будланула\nбокра и кудрячит\nбокрёнка."
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
let r = s.index(16, stepsFrom: s.startIndex)..<s.index(35, stepsFrom: s.startIndex)
do {
let result = s.lineRange(for: r)
expectEqual("штеко будланула\nбокра и кудрячит\n", s[result])
@@ -887,8 +887,8 @@ NSStringAPIs.test("lineRangeFor(_:)") {
NSStringAPIs.test("linguisticTagsIn(_:scheme:options:orthography:tokenRanges:)") {
let s = "Абв. Глокая куздра штеко будланула бокра и кудрячит бокрёнка. Абв."
let startIndex = s.advance(s.startIndex, by: 5)
let endIndex = s.advance(s.startIndex, by: 17)
let startIndex = s.index(5, stepsFrom: s.startIndex)
let endIndex = s.index(17, stepsFrom: s.startIndex)
var tokenRanges: [Range<String.Index>] = []
var tags = s.linguisticTags(in: startIndex..<endIndex,
scheme: NSLinguisticTagSchemeTokenType,
@@ -1044,7 +1044,7 @@ NSStringAPIs.test("maximumLengthOfBytesUsingEncoding(_:)") {
NSStringAPIs.test("paragraphRangeFor(_:)") {
let s = "Глокая куздра\nштеко будланула\u{2028}бокра и кудрячит\u{2028}бокрёнка.\n Абв."
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
let r = s.index(16, stepsFrom: s.startIndex)..<s.index(35, stepsFrom: s.startIndex)
do {
let result = s.paragraphRange(for: r)
expectEqual("штеко будланула\u{2028}бокра и кудрячит\u{2028}бокрёнка.\n", s[result])
@@ -1100,8 +1100,8 @@ NSStringAPIs.test("rangeOfCharacterFrom(_:options:range:)") {
do {
let s = "Глокая куздра"
let r = s.rangeOfCharacter(from: charset)!
expectEqual(s.advance(s.startIndex, by: 4), r.lowerBound)
expectEqual(s.advance(s.startIndex, by: 5), r.upperBound)
expectEqual(s.index(4, stepsFrom: s.startIndex), r.lowerBound)
expectEqual(s.index(5, stepsFrom: s.startIndex), r.upperBound)
}
do {
expectEmpty("клмн".rangeOfCharacter(from: charset))
@@ -1110,15 +1110,15 @@ NSStringAPIs.test("rangeOfCharacterFrom(_:options:range:)") {
let s = "абвклмнабвклмн"
let r = s.rangeOfCharacter(from: charset,
options: .backwardsSearch)!
expectEqual(s.advance(s.startIndex, by: 9), r.lowerBound)
expectEqual(s.advance(s.startIndex, by: 10), r.upperBound)
expectEqual(s.index(9, stepsFrom: s.startIndex), r.lowerBound)
expectEqual(s.index(10, stepsFrom: s.startIndex), r.upperBound)
}
do {
let s = "абвклмнабв"
let r = s.rangeOfCharacter(from: charset,
range: s.advance(s.startIndex, by: 3)..<s.endIndex)!
expectEqual(s.advance(s.startIndex, by: 7), r.lowerBound)
expectEqual(s.advance(s.startIndex, by: 8), r.upperBound)
range: s.index(3, stepsFrom: s.startIndex)..<s.endIndex)!
expectEqual(s.index(7, stepsFrom: s.startIndex), r.lowerBound)
expectEqual(s.index(8, stepsFrom: s.startIndex), r.upperBound)
}
}
@@ -1149,20 +1149,20 @@ NSStringAPIs.test("rangeOfComposedCharacterSequence(at:)") {
expectEqual("\u{1F601}", s[s.rangeOfComposedCharacterSequence(
at: s.startIndex)])
expectEqual("a", s[s.rangeOfComposedCharacterSequence(
at: s.advance(s.startIndex, by: 1))])
at: s.index(1, stepsFrom: s.startIndex))])
expectEqual("\u{305f}\u{3099}", s[s.rangeOfComposedCharacterSequence(
at: s.advance(s.startIndex, by: 5))])
at: s.index(5, stepsFrom: s.startIndex))])
expectEqual(" ", s[s.rangeOfComposedCharacterSequence(
at: s.advance(s.startIndex, by: 6))])
at: s.index(6, stepsFrom: s.startIndex))])
}
NSStringAPIs.test("rangeOfComposedCharacterSequences(for:)") {
let s = "\u{1F601}abc さ\u{3099}し\u{3099}す\u{3099}せ\u{3099}そ\u{3099}"
expectEqual("\u{1F601}a", s[s.rangeOfComposedCharacterSequences(
for: s.startIndex..<s.advance(s.startIndex, by: 2))])
for: s.startIndex..<s.index(2, stepsFrom: s.startIndex))])
expectEqual("\u{3099}そ\u{3099}", s[s.rangeOfComposedCharacterSequences(
for: s.advance(s.startIndex, by: 8)..<s.advance(s.startIndex, by: 10))])
for: s.index(8, stepsFrom: s.startIndex)..<s.index(10, stepsFrom: s.startIndex))])
}
func toIntRange(
@@ -1478,15 +1478,15 @@ NSStringAPIs.test("replacingCharacters(in:with:)") {
expectEqual(
"\u{3099}せ\u{3099}そ\u{3099}",
s.replacingCharacters(
in: s.startIndex..<s.advance(s.startIndex, by: 7), with: ""))
in: s.startIndex..<s.index(7, stepsFrom: s.startIndex), with: ""))
expectEqual(
"zzzす\u{3099}せ\u{3099}そ\u{3099}",
s.replacingCharacters(
in: s.startIndex..<s.advance(s.startIndex, by: 7), with: "zzz"))
in: s.startIndex..<s.index(7, stepsFrom: s.startIndex), with: "zzz"))
expectEqual(
"\u{1F602}す\u{3099}せ\u{3099}そ\u{3099}",
s.replacingCharacters(
in: s.startIndex..<s.advance(s.startIndex, by: 7), with: "\u{1F602}"))
in: s.startIndex..<s.index(7, stepsFrom: s.startIndex), with: "\u{1F602}"))
expectEqual("\u{1F601}", s.replacingCharacters(
in: s.successor(of: s.startIndex)..<s.endIndex, with: ""))
@@ -1498,15 +1498,15 @@ NSStringAPIs.test("replacingCharacters(in:with:)") {
expectEqual(
"\u{1F601}aす\u{3099}せ\u{3099}そ\u{3099}",
s.replacingCharacters(
in: s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 7), with: ""))
in: s.index(2, stepsFrom: s.startIndex)..<s.index(7, stepsFrom: s.startIndex), with: ""))
expectEqual(
"\u{1F601}azzzす\u{3099}せ\u{3099}そ\u{3099}",
s.replacingCharacters(
in: s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 7), with: "zzz"))
in: s.index(2, stepsFrom: s.startIndex)..<s.index(7, stepsFrom: s.startIndex), with: "zzz"))
expectEqual(
"\u{1F601}a\u{1F602}す\u{3099}せ\u{3099}そ\u{3099}",
s.replacingCharacters(
in: s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 7),
in: s.index(2, stepsFrom: s.startIndex)..<s.index(7, stepsFrom: s.startIndex),
with: "\u{1F602}"))
}
@@ -1575,12 +1575,12 @@ NSStringAPIs.test("replacingOccurrences(of:with:options:range:)") {
s.replacingOccurrences(
of: "\u{1F601}", with: "\u{1F602}\u{1F603}",
options: NSStringCompareOptions.literalSearch,
range: s.startIndex..<s.advance(s.startIndex, by: 1)))
range: s.startIndex..<s.index(1, stepsFrom: s.startIndex)))
expectEqual(s, s.replacingOccurrences(
of: "\u{1F601}", with: "\u{1F602}\u{1F603}",
options: NSStringCompareOptions.literalSearch,
range: s.advance(s.startIndex, by: 1)..<s.advance(s.startIndex, by: 3)))
range: s.index(1, stepsFrom: s.startIndex)..<s.index(3, stepsFrom: s.startIndex)))
}
NSStringAPIs.test("replacingPercentEscapes(usingEncoding:)") {
@@ -1666,8 +1666,8 @@ NSStringAPIs.test("substring(from:)") {
expectEqual(s, s.substring(from: s.startIndex))
expectEqual("\u{3099}そ\u{3099}",
s.substring(from: s.advance(s.startIndex, by: 8)))
expectEqual("", s.substring(from: s.advance(s.startIndex, by: 10)))
s.substring(from: s.index(8, stepsFrom: s.startIndex)))
expectEqual("", s.substring(from: s.index(10, stepsFrom: s.startIndex)))
}
NSStringAPIs.test("substring(to:)") {
@@ -1675,8 +1675,8 @@ NSStringAPIs.test("substring(to:)") {
expectEqual("", s.substring(to: s.startIndex))
expectEqual("\u{1F601}abc さ\u{3099}し\u{3099}す\u{3099}",
s.substring(to: s.advance(s.startIndex, by: 8)))
expectEqual(s, s.substring(to: s.advance(s.startIndex, by: 10)))
s.substring(to: s.index(8, stepsFrom: s.startIndex)))
expectEqual(s, s.substring(to: s.index(10, stepsFrom: s.startIndex)))
}
NSStringAPIs.test("substring(with:)") {
@@ -1685,12 +1685,12 @@ NSStringAPIs.test("substring(with:)") {
expectEqual("", s.substring(with: s.startIndex..<s.startIndex))
expectEqual(
"",
s.substring(with: s.advance(s.startIndex, by: 1)..<s.advance(s.startIndex, by: 1)))
s.substring(with: s.index(1, stepsFrom: s.startIndex)..<s.index(1, stepsFrom: s.startIndex)))
expectEqual("", s.substring(with: s.endIndex..<s.endIndex))
expectEqual(s, s.substring(with: s.startIndex..<s.endIndex))
expectEqual(
"\u{3099}し\u{3099}す\u{3099}",
s.substring(with: s.advance(s.startIndex, by: 5)..<s.advance(s.startIndex, by: 8)))
s.substring(with: s.index(5, stepsFrom: s.startIndex)..<s.index(8, stepsFrom: s.startIndex)))
}
NSStringAPIs.test("localizedUppercase") {
@@ -1892,7 +1892,7 @@ NSStringAPIs.test("CompareStringsWithUnpairedSurrogates")
let acceptor = "\u{1f601}\u{1f602}\u{1f603}"
expectEqual("\u{fffd}\u{1f602}\u{fffd}",
acceptor[donor.advance(donor.startIndex, by: 1)..<donor.advance(donor.startIndex, by: 5)])
acceptor[donor.index(1, stepsFrom: donor.startIndex)..<donor.index(5, stepsFrom: donor.startIndex)])
}
NSStringAPIs.test("copy construction") {

View File

@@ -104,8 +104,8 @@ func nonASCII() {
// Slicing the String does not allocate
// CHECK-NEXT: String(Contiguous(owner: .cocoa@[[utf16address]], count: 6))
let i2 = newNSUTF16.advance(newNSUTF16.startIndex, by: 2)
let i8 = newNSUTF16.advance(newNSUTF16.startIndex, by: 6)
let i2 = newNSUTF16.index(2, stepsFrom: newNSUTF16.startIndex)
let i8 = newNSUTF16.index(6, stepsFrom: newNSUTF16.startIndex)
print(" \(repr(newNSUTF16[i2..<i8]))")
// Representing a slice as an NSString requires a new object
@@ -147,8 +147,8 @@ func ascii() {
// CHECK: --- ASCII slicing ---
print("--- ASCII slicing ---")
let i3 = newNSASCII.advance(newNSASCII.startIndex, by: 3)
let i6 = newNSASCII.advance(newNSASCII.startIndex, by: 6)
let i3 = newNSASCII.index(3, stepsFrom: newNSASCII.startIndex)
let i6 = newNSASCII.index(6, stepsFrom: newNSASCII.startIndex)
// Slicing the String
print(" \(repr(newNSASCII[i3..<i6]))")

View File

@@ -319,8 +319,8 @@ StringTests.test("CompareStringsWithUnpairedSurrogates")
expectEqual("\u{fffd}\u{1f602}\u{fffd}",
acceptor[
donor.advance(donor.startIndex, by: 1) ..<
donor.advance(donor.startIndex, by: 5)
donor.index(1, stepsFrom: donor.startIndex) ..<
donor.index(5, stepsFrom: donor.startIndex)
]
)
}

View File

@@ -4,7 +4,7 @@
func test(s: String)
{
print(s)
var s2 = s[s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 4)]
var s2 = s[s.index(2, stepsFrom: s.startIndex)..<s.index(4, stepsFrom: s.startIndex)]
print(s2)
var s3 = s2[s2.startIndex..<s2.startIndex]
var s4 = s3[s2.startIndex..<s2.startIndex]

View File

@@ -61,7 +61,7 @@ internal func _splitRandomAccessIndexRange<
if length < 2 {
return [ range ]
}
let middle = elements.advance(startIndex, by: C.IndexDistance(length / 2))
let middle = elements.index(C.IndexDistance(length / 2), stepsFrom: startIndex)
return [startIndex ..< middle, middle ..< endIndex]
}

View File

@@ -601,7 +601,7 @@ extension MyForwardCollectionType {
@warn_unused_result
public func dropFirst(n: Int) -> SubSequence {
_precondition(n >= 0, "Can't drop a negative number of elements from a collection")
let start = advance(startIndex, by: numericCast(n), limit: endIndex)
let start = index(numericCast(n), stepsFrom: startIndex, limit: endIndex)
return self[start..<*endIndex]
}
@@ -609,14 +609,14 @@ extension MyForwardCollectionType {
public func dropLast(n: Int) -> SubSequence {
_precondition(n >= 0, "Can't drop a negative number of elements from a collection")
let amount = max(0, numericCast(count) - n)
let end = advance(startIndex, by: numericCast(amount), limit: endIndex)
let end = index(numericCast(amount), stepsFrom: startIndex, limit: endIndex)
return self[startIndex..<*end]
}
@warn_unused_result
public func prefix(maxLength: Int) -> SubSequence {
_precondition(maxLength >= 0, "Can't take a prefix of negative length from a collection")
let end = advance(startIndex, by: numericCast(maxLength), limit: endIndex)
let end = index(numericCast(maxLength), stepsFrom: startIndex, limit: endIndex)
return self[startIndex..<*end]
}
@@ -624,7 +624,7 @@ extension MyForwardCollectionType {
public func suffix(maxLength: Int) -> SubSequence {
_precondition(maxLength >= 0, "Can't take a suffix of negative length from a collection")
let amount = max(0, numericCast(count) - maxLength)
let start = advance(startIndex, by: numericCast(amount), limit: endIndex)
let start = index(numericCast(amount), stepsFrom: startIndex, limit: endIndex)
return self[start..<*endIndex]
}
}
@@ -670,7 +670,7 @@ extension MyForwardCollectionType
@warn_unused_result
public func successor(of i: Index) -> Index {
return advance(i, by: 1)
return index(1, stepsFrom: i)
}
@warn_unused_result
@@ -781,7 +781,7 @@ extension MyBidirectionalCollectionType
@warn_unused_result
public func predecessor(of i: Index) -> Index {
return advance(i, by: -1)
return index(-1, stepsFrom: i)
}
@warn_unused_result
@@ -1287,7 +1287,7 @@ extension MyRandomAccessCollectionType
var subrangeCount = count
while subrangeCount != 0 {
let midOffset = subrangeCount / 2
let mid = advance(low, by: midOffset)
let mid = index(midOffset, stepsFrom: low)
if isOrderedBefore(self[mid], element) {
low = successor(of: mid)
subrangeCount -= midOffset + 1

View File

@@ -561,7 +561,7 @@ CollectionTypeTests.test("subscript(_: Range<Index>)/writeback") {
var collection = MinimalMutableRandomAccessCollection(
elements: [ 5, 4, 3, 2, 1, 0, -1, -2, -3, -4 ])
var i = collection.startIndex
var j = collection.advance(i, by: 5)
var j = collection.index(5, stepsFrom: i)
collection[i..<j].sort()
expectEqualSequence(
[ 1, 2, 3, 4, 5, 0, -1, -2, -3, -4 ], collection)

View File

@@ -133,7 +133,7 @@ struct InstrumentedCollection<
func advance(i: Index, by n: IndexDistance) -> Index {
callCounts["advance(_:by:)"]! += 1
return base.advance(i, by: n)
return base.index(n, stepsFrom: i)
}
func distance(from start: Index, to end: Index) -> IndexDistance {

View File

@@ -29,7 +29,7 @@ import ObjectiveC
extension Collection {
func indexAt(offset offset: Int) -> Index {
return self.advance(self.startIndex, by: numericCast(offset))
return self.index(numericCast(offset), stepsFrom: self.startIndex)
}
}
@@ -164,13 +164,13 @@ Index.test("${TraversalCollection}/distance(to:)/dispatch") {
Index.test("${TraversalCollection}/advance(_:by:)/dispatch") {
let c = ${TraversalCollection}Log.dispatchTester(Array(0..<10))
_ = c.advance(c.startIndex, by: 10)
_ = c.index(10, stepsFrom: c.startIndex)
expectCustomizable(c, c.log.advance)
}
Index.test("${TraversalCollection}/advance(_:by:limit:)/dispatch") {
let c = ${TraversalCollection}Log.dispatchTester(Array(0..<10))
_ = c.advance(c.startIndex, by: 10, limit: 5)
_ = c.index(10, stepsFrom: c.startIndex, limit: 5)
expectCustomizable(c, c.log.advanceLimit)
}
@@ -261,7 +261,7 @@ Index.test(
let c = ${Kind}Collection(Array(0..<10))
let i = c.indexAt(offset: test.startIndex)
let result = c.advance(i, by: test.distance)
let result = c.index(test.distance, stepsFrom: i)
expectEqual(0, c.timesSuccessorCalled.value)
expectEqual(0, c.timesPredecessorCalled.value)
}
@@ -276,7 +276,7 @@ Index.test(
let i = c.indexAt(offset: test.startIndex)
let limit = c.indexAt(offset: test.limit.unsafelyUnwrapped)
let result = c.advance(i, by: test.distance, limit: limit)
let result = c.index(test.distance, stepsFrom: i, limit: limit)
expectEqual(0, c.timesSuccessorCalled.value)
expectEqual(0, c.timesPredecessorCalled.value)
}