Fix PostgresNumeric.init crash at large number#187
Fix PostgresNumeric.init crash at large number#187sidepelican wants to merge 2 commits intovapor:mainfrom
Conversation
|
Hi, I am coworker with @sidepelican . |
gwynne
left a comment
There was a problem hiding this comment.
I suggest replacing both chunked() and reverseChunked() with these simpler versions:
/// 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: self.startIndex, to: self.endIndex, by: maxSize).map {
self[$0 ..< (self.index($0, offsetBy: maxSize, limitedBy: self.endIndex) ?? self.endIndex)]
}
}
/// Split the collection into chunks of `maxSize` length each. The **first** chunk may contain anywhere between 1 and maxSize elements. `maxSize` must be greater than zero.
func reverseChunked(by maxSize: Int) -> [SubSequence] {
return stride(from: self.endIndex, to: self.startIndex, by: -maxSize).map {
self[(self.index($0, offsetBy: -maxSize, limitedBy: self.startIndex) ?? self.startIndex) ..< $0]
}.reversed()
}|
|
|
I tried next patterns: let aaa: [Substring] = [
""[...],
"1"[...],
"1234"[...],
"12345"[...],
"12345678"[...],
"1234567890abcd"[...],
]
for v in aaa {
print(v.chunked(by: 4), v.reverseChunked(by: 4))
} |
Chunking a And there's also a mistake we both made in /// - distance: The distance to offset `i`. `distance` must not be negative
/// unless the collection conforms to the `BidirectionalCollection`
/// protocol.
func index(_ i: Self.Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Self.Index?There is no check for extension Collection where Index: Strideable, Index.Stride == Int {
/// 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.
///
/// Example:
/// ```swift
/// print(Array(1...10).chunked(by: 3)) // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
/// ```
func chunked(by maxSize: Int) -> [SubSequence] {
assert(maxSize > 0, "Can not chunk less than one element at a time.")
return stride(from: self.startIndex, to: self.endIndex, by: maxSize).map {
self[$0 ..< (self.index($0, offsetBy: maxSize, limitedBy: self.endIndex) ?? self.endIndex)]
}
}
}
extension BidirectionalCollection where Index: Strideable, Index.Stride == Int {
/// Split the collection into chunks of `maxSize` length each. The **first** chunk may contain anywhere between 1 and maxSize elements. `maxSize` must be greater than zero.
///
/// Example:
/// ```swift
/// print(Array(1...10).reverseChunked(by: 3)) // [[1], [2, 3, 4], [5, 6, 7], [8, 9, 10]]
/// print(Array(1...11).reverseChunked(by: 3)) // [[1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
/// ```
func reverseChunked(by maxSize: Int) -> [SubSequence] {
assert(maxSize > 0, "Can not chunk less than one element at a time.")
return stride(from: self.endIndex, to: self.startIndex, by: -maxSize).map {
self[(self.index($0, offsetBy: -maxSize, limitedBy: self.startIndex) ?? self.startIndex) ..< $0]
}.reversed()
}
}Of course, it's possible to write versions that work without the constraints, and a reversed version that doesn't use a negative offset, but I don't think it's worthwhile to do so. |
|
Sorry, I can't understand what you mean in the first sentence.
That makes sense. nice review. |
Fix #186
Fix
reverseChunkedto comply withmaxSize.