From eaea3f7aae3d72570acb1b8c0258aa562b7499d7 Mon Sep 17 00:00:00 2001 From: kenta okamura Date: Mon, 11 Oct 2021 18:08:54 +0900 Subject: [PATCH 1/2] fix reverseChunked does not work correctly. --- .../PostgresNIO/Data/PostgresData+Numeric.swift | 15 ++++++--------- Tests/IntegrationTests/PostgresNIOTests.swift | 8 ++++++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Sources/PostgresNIO/Data/PostgresData+Numeric.swift b/Sources/PostgresNIO/Data/PostgresData+Numeric.swift index 5e564d6d..a3a9e65e 100644 --- a/Sources/PostgresNIO/Data/PostgresData+Numeric.swift +++ b/Sources/PostgresNIO/Data/PostgresData+Numeric.swift @@ -268,16 +268,13 @@ private extension Collection { // splits the collection into chunks of the supplied size // if the collection is not evenly divisible, the first chunk will be smaller func reverseChunked(by maxSize: Int) -> [SubSequence] { - var lastDistance = 0 + let firstChunkSize = self.count % maxSize + if firstChunkSize == 0 { + return self.chunked(by: maxSize) + } var chunkStartIndex = self.startIndex - return stride(from: 0, to: self.count, by: maxSize).reversed().map { current in - let distance = (self.count - current) - lastDistance - lastDistance = distance - let chunkEndOffset = Swift.min( - self.distance(from: chunkStartIndex, to: self.endIndex), - distance - ) - let chunkEndIndex = self.index(chunkStartIndex, offsetBy: chunkEndOffset) + return stride(from: firstChunkSize, through: self.count, by: maxSize).map { current in + let chunkEndIndex = self.index(self.startIndex, offsetBy: current) defer { chunkStartIndex = chunkEndIndex } return self[chunkStartIndex.. Date: Thu, 14 Oct 2021 21:30:26 +0900 Subject: [PATCH 2/2] simpler with sequence and zip --- .../Data/PostgresData+Numeric.swift | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/Sources/PostgresNIO/Data/PostgresData+Numeric.swift b/Sources/PostgresNIO/Data/PostgresData+Numeric.swift index a3a9e65e..ded3135c 100644 --- a/Sources/PostgresNIO/Data/PostgresData+Numeric.swift +++ b/Sources/PostgresNIO/Data/PostgresData+Numeric.swift @@ -251,33 +251,26 @@ extension PostgresData { } private extension Collection { - // splits the collection into chunks of the supplied size - // if the collection is not evenly divisible, the last chunk will be smaller + /// Split the collection into chunks of `maxSize` length each. The last chunk may contain anywhere between 1 and maxSize elements. `maxSize` must be greater than zero. func chunked(by maxSize: Int) -> [SubSequence] { - return stride(from: 0, to: self.count, by: maxSize).map { current in - let chunkStartIndex = self.index(self.startIndex, offsetBy: current) - let chunkEndOffset = Swift.min( - self.distance(from: chunkStartIndex, to: self.endIndex), - maxSize - ) - let chunkEndIndex = self.index(chunkStartIndex, offsetBy: chunkEndOffset) - return self[chunkStartIndex.. [SubSequence] { - let firstChunkSize = self.count % maxSize - if firstChunkSize == 0 { - return self.chunked(by: maxSize) - } - var chunkStartIndex = self.startIndex - return stride(from: firstChunkSize, through: self.count, by: maxSize).map { current in - let chunkEndIndex = self.index(self.startIndex, offsetBy: current) - defer { chunkStartIndex = chunkEndIndex } - return self[chunkStartIndex..