Skip to content

Commit a05ecf9

Browse files
committed
fixes from code review
1 parent 3e05648 commit a05ecf9

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

stdlib/public/Darwin/Compression/Compression.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public enum FilterError: Error {
5353

5454
/// Non-empty write after an output filter has been finalized
5555
case writeToFinalizedFilter
56-
57-
/// Invalid count argument in a read() call
58-
case readCountInvalid
5956
}
6057

6158
/**
@@ -154,7 +151,7 @@ public class OutputFilter {
154151
try data!.withUnsafeBytes { (src_ptr: UnsafePointer<UInt8>) in
155152
_stream.src_size = data!.count
156153
_stream.src_ptr = src_ptr
157-
while (_stream.src_size > 0) { _ = try process(false) }
154+
while (_stream.src_size > 0) { _ = try process(finalizing: false) }
158155
}
159156
}
160157

@@ -174,13 +171,13 @@ public class OutputFilter {
174171
// Finalize stream
175172
_stream.src_size = 0
176173
var status = COMPRESSION_STATUS_OK
177-
while (status != COMPRESSION_STATUS_END) { status = try process(true) }
178-
179-
// Notify end of stream
180-
try _writeFunc(nil)
174+
while (status != COMPRESSION_STATUS_END) { status = try process(finalizing: true) }
181175

182176
// Update state
183177
_finalized = true
178+
179+
// Notify end of stream
180+
try _writeFunc(nil)
184181
}
185182

186183
// Cleanup resources. The filter is finalized now if it was not finalized yet.
@@ -195,7 +192,7 @@ public class OutputFilter {
195192

196193
// Call compression_stream_process with current src, and dst set to _buf, then write output to the closure
197194
// Return status
198-
private func process(_ finalize: Bool) throws -> compression_status {
195+
private func process(finalizing finalize: Bool) throws -> compression_status {
199196
// Process current input, and write to buf
200197
_stream.dst_ptr = _buf
201198
_stream.dst_size = _bufCapacity
@@ -262,11 +259,10 @@ public class InputFilter {
262259

263260
- Throws:
264261
`FilterError.filterProcessError` if an error occurs during processing
265-
`FilterError.readCountInvalid` if `count` <= 0
266262
*/
267263
public func readData(ofLength count: Int) throws -> Data? {
268264
// Sanity check
269-
guard count > 0 else { throw FilterError.readCountInvalid }
265+
precondition(count > 0, "number of bytes to read can't be 0")
270266

271267
// End reached, return early, nothing to do
272268
if _endReached { return nil }
@@ -295,7 +291,7 @@ public class InputFilter {
295291
try buf.withUnsafeBytes { (src_ptr: UnsafePointer<UInt8>) in
296292

297293
// Next byte to read
298-
_stream.src_ptr = src_ptr.advanced(by: buf.count - _stream.src_size)
294+
_stream.src_ptr = src_ptr + buf.count - _stream.src_size
299295

300296
let status = compression_stream_process(&_stream, (_eofReached ? Int32(COMPRESSION_STREAM_FINALIZE.rawValue) : 0))
301297
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.filterProcessError }

0 commit comments

Comments
 (0)