From 3669ff0c1182ff953f46f404ce37002cd790496c Mon Sep 17 00:00:00 2001 From: Oliver Atkinson Date: Fri, 6 Nov 2020 15:33:03 +0000 Subject: [PATCH] offsetBackward n is positive and remove init? from Index --- Sources/Algorithms/Stride.swift | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Sources/Algorithms/Stride.swift b/Sources/Algorithms/Stride.swift index 4f4ef53..4e35ebd 100644 --- a/Sources/Algorithms/Stride.swift +++ b/Sources/Algorithms/Stride.swift @@ -85,11 +85,6 @@ extension Stride: Collection where Base: Collection { self.base = base } - init?(_ base: Base.Index?) { - guard let base = base else { return nil } - self.base = base - } - public static func < (lhs: Index, rhs: Index) -> Bool { lhs.base < rhs.base } @@ -122,7 +117,7 @@ extension Stride: Collection where Base: Collection { return n > 0 ? offsetForward(i, offsetBy: n, limitedBy: limit) - : offsetBackward(i, offsetBy: n, limitedBy: limit) + : offsetBackward(i, offsetBy: -n, limitedBy: limit) } private func offsetForward( @@ -160,15 +155,13 @@ extension Stride: Collection where Base: Collection { limitedBy limit: Index ) -> Index? { let distance = i == endIndex - ? -((base.count - 1) % stride + 1) + (n + 1) * stride - : n * stride - return Index( - base.index( + ? -((base.count - 1) % stride + 1) + (1 - n) * stride + : n * -stride + return base.index( i.base, offsetBy: distance, limitedBy: limit.base - ) - ) + ).map(Index.init) } public var count: Int {