Skip to content

Commit 3e05648

Browse files
committed
Stylistic changes
1 parent a2659f0 commit 3e05648

File tree

1 file changed

+57
-72
lines changed

1 file changed

+57
-72
lines changed

stdlib/public/Darwin/Compression/Compression.swift

Lines changed: 57 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,24 @@ public enum Algorithm {
4444
/**
4545
Compression errors
4646
*/
47-
public enum FilterError : Error {
48-
47+
public enum FilterError: Error {
4948
/// Filter failed to initialize
50-
case FilterInitError
49+
case filterInitError
5150

5251
/// Invalid data in a call to compression_stream_process
53-
case FilterProcessError
52+
case filterProcessError
5453

5554
/// Non-empty write after an output filter has been finalized
56-
case WriteToFinalizedFilter
55+
case writeToFinalizedFilter
5756

5857
/// Invalid count argument in a read() call
59-
case ReadCountInvalid
60-
58+
case readCountInvalid
6159
}
6260

6361
/**
6462
Compression filter direction of operation, compress/decompress
6563
*/
6664
public enum FilterOperation {
67-
6865
/// Compress raw data to a compressed payload
6966
case compress
7067

@@ -77,7 +74,6 @@ public enum FilterOperation {
7774
case .decompress: return COMPRESSION_STREAM_DECODE
7875
}
7976
}
80-
8177
}
8278

8379
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
@@ -89,29 +85,27 @@ extension compression_stream {
8985
- Parameter operation: direction of operation
9086
- Parameter algorithm: compression algorithm
9187

92-
- Throws: `FilterError.FilterInitError` if `algorithm` is not supported by the Compression stream API
88+
- Throws: `FilterError.filterInitError` if `algorithm` is not supported by the Compression stream API
9389
*/
94-
init(operation: FilterOperation, algorithm: Algorithm) throws
95-
{
90+
init(operation: FilterOperation, algorithm: Algorithm) throws {
9691
self.init(dst_ptr: UnsafeMutablePointer<UInt8>.allocate(capacity:0),
9792
dst_size: 0,
9893
src_ptr: UnsafeMutablePointer<UInt8>.allocate(capacity:0),
9994
src_size: 0,
10095
state: nil)
10196
let status = compression_stream_init(&self, operation.rawValue, algorithm.rawValue)
102-
guard status == COMPRESSION_STATUS_OK else { throw FilterError.FilterInitError }
97+
guard status == COMPRESSION_STATUS_OK else { throw FilterError.filterInitError }
10398
}
10499

105100
}
106101

107102
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
108103
public class OutputFilter {
109-
110-
private var _stream : compression_stream
111-
private var _buf : UnsafeMutablePointer<UInt8>
112-
private var _bufCapacity : Int
113-
private var _writeFunc : (Data?) throws -> ()
114-
private var _finalized : Bool = false
104+
private var _stream: compression_stream
105+
private var _buf: UnsafeMutablePointer<UInt8>
106+
private let _bufCapacity: Int
107+
private let _writeFunc: (Data?) throws -> ()
108+
private var _finalized: Bool = false
115109

116110
/**
117111
Initialize an output filter
@@ -124,11 +118,12 @@ public class OutputFilter {
124118

125119
- Throws: `FilterError.StreamInitError` if stream initialization failed
126120
*/
127-
public init(_ operation: FilterOperation,
128-
using algorithm : Algorithm,
129-
bufferCapacity : Int = 65536,
130-
writingTo writeFunc: @escaping (Data?) throws -> () ) throws
131-
{
121+
public init(
122+
_ operation: FilterOperation,
123+
using algorithm: Algorithm,
124+
bufferCapacity: Int = 65536,
125+
writingTo writeFunc: @escaping (Data?) throws -> ()
126+
) throws {
132127
_stream = try compression_stream(operation: operation, algorithm: algorithm)
133128
_buf = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferCapacity)
134129
_bufCapacity = bufferCapacity
@@ -145,16 +140,15 @@ public class OutputFilter {
145140
- Parameter data: data to process
146141

147142
- Throws:
148-
`FilterError.FilterProcessError` if an error occurs during processing
149-
`FilterError.WriteToFinalizedFilter` if `data` is not empty/nil, and the filter is the finalized state
143+
`FilterError.filterProcessError` if an error occurs during processing
144+
`FilterError.writeToFinalizedFilter` if `data` is not empty/nil, and the filter is the finalized state
150145
*/
151-
public func write(_ data: Data?) throws
152-
{
146+
public func write(_ data: Data?) throws {
153147
// Finalize if data is empty/nil
154148
if data == nil || data!.isEmpty { try finalize() ; return }
155149

156150
// Fail if already finalized
157-
if _finalized { throw FilterError.WriteToFinalizedFilter }
151+
if _finalized { throw FilterError.writeToFinalizedFilter }
158152

159153
// Process all incoming data
160154
try data!.withUnsafeBytes { (src_ptr: UnsafePointer<UInt8>) in
@@ -173,8 +167,7 @@ public class OutputFilter {
173167

174168
- Throws: `FilterError.StreamProcessError` if an error occurs during processing
175169
*/
176-
public func finalize() throws
177-
{
170+
public func finalize() throws {
178171
// Do nothing if already finalized
179172
if _finalized { return }
180173

@@ -191,8 +184,7 @@ public class OutputFilter {
191184
}
192185

193186
// Cleanup resources. The filter is finalized now if it was not finalized yet.
194-
deinit
195-
{
187+
deinit {
196188
// Finalize now if not done earlier
197189
try? finalize()
198190

@@ -203,21 +195,19 @@ public class OutputFilter {
203195

204196
// Call compression_stream_process with current src, and dst set to _buf, then write output to the closure
205197
// Return status
206-
private func process(_ finalize: Bool) throws -> compression_status
207-
{
198+
private func process(_ finalize: Bool) throws -> compression_status {
208199
// Process current input, and write to buf
209200
_stream.dst_ptr = _buf
210201
_stream.dst_size = _bufCapacity
211202

212203
let status = compression_stream_process(&_stream, (finalize ? Int32(COMPRESSION_STREAM_FINALIZE.rawValue) : 0))
213-
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.FilterProcessError }
204+
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.filterProcessError }
214205

215206
// Number of bytes written to buf
216207
let writtenBytes = _bufCapacity - _stream.dst_size
217208

218209
// Write output
219-
if writtenBytes > 0
220-
{
210+
if writtenBytes > 0 {
221211
let outData = Data(bytesNoCopy: _buf, count: writtenBytes, deallocator: .none)
222212
try _writeFunc(outData)
223213
}
@@ -229,13 +219,12 @@ public class OutputFilter {
229219

230220
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
231221
public class InputFilter {
232-
233-
private var _stream : compression_stream
234-
private var _buf : Data? = nil // current input data
235-
private var _bufCapacity : Int // size to read when refilling _buf
236-
private var _readFunc : (Int) throws -> Data?
237-
private var _eofReached : Bool = false // did we read end-of-file from the input?
238-
private var _endReached : Bool = false // did we reach end-of-file from the decoder stream?
222+
private var _stream: compression_stream
223+
private var _buf: Data? = nil // current input data
224+
private let _bufCapacity: Int // size to read when refilling _buf
225+
private let _readFunc: (Int) throws -> Data?
226+
private var _eofReached: Bool = false // did we read end-of-file from the input?
227+
private var _endReached: Bool = false // did we reach end-of-file from the decoder stream?
239228

240229
/**
241230
Initialize an input filter
@@ -246,13 +235,14 @@ public class InputFilter {
246235
- bufferCapacity: capacity of the internal data buffer
247236
- readFunc: called to read the input data
248237

249-
- Throws: `FilterError.FilterInitError` if filter initialization failed
238+
- Throws: `FilterError.filterInitError` if filter initialization failed
250239
*/
251-
public init(_ operation: FilterOperation,
252-
using algorithm : Algorithm,
253-
bufferCapacity : Int = 65536,
254-
readingFrom readFunc: @escaping (Int) throws -> Data?) throws
255-
{
240+
public init(
241+
_ operation: FilterOperation,
242+
using algorithm: Algorithm,
243+
bufferCapacity: Int = 65536,
244+
readingFrom readFunc: @escaping (Int) throws -> Data?
245+
) throws {
256246
_stream = try compression_stream(operation: operation, algorithm: algorithm)
257247
_bufCapacity = bufferCapacity
258248
_readFunc = readFunc
@@ -271,19 +261,18 @@ public class InputFilter {
271261
- Returns: a new Data object containing at most `count` output bytes, or nil if no more data is available
272262

273263
- Throws:
274-
`FilterError.FilterProcessError` if an error occurs during processing
275-
`FilterError.ReadCountInvalid` if `count` <= 0
264+
`FilterError.filterProcessError` if an error occurs during processing
265+
`FilterError.readCountInvalid` if `count` <= 0
276266
*/
277-
public func readData(ofLength count: Int) throws -> Data?
278-
{
267+
public func readData(ofLength count: Int) throws -> Data? {
279268
// Sanity check
280-
guard count > 0 else { throw FilterError.ReadCountInvalid }
269+
guard count > 0 else { throw FilterError.readCountInvalid }
281270

282271
// End reached, return early, nothing to do
283272
if _endReached { return nil }
284273

285274
// Allocate result
286-
var result = Data.init(count: count)
275+
var result = Data(count: count)
287276

288277
try result.withUnsafeMutableBytes { (dst_ptr: UnsafeMutablePointer<UInt8>) in
289278

@@ -295,30 +284,27 @@ public class InputFilter {
295284

296285
// Refill _buf if needed, and EOF was not yet read
297286
if _stream.src_size == 0 && !_eofReached {
298-
try _buf = _readFunc(_bufCapacity) // may be nil
287+
_buf = try _readFunc(_bufCapacity) // may be nil
299288
// Reset src_size to full _buf size
300-
if _buf == nil || _buf!.count == 0 { _eofReached = true ; _stream.src_size = 0 }
301-
else { _stream.src_size = _buf!.count }
289+
if _buf?.count ?? 0 == 0 { _eofReached = true }
290+
_stream.src_size = _buf?.count ?? 0
302291
}
303292

304293
// Process some data
305-
if _buf != nil {
306-
307-
let bufCount = _buf!.count
308-
try _buf!.withUnsafeBytes { (src_ptr: UnsafePointer<UInt8>) in
294+
if let buf = _buf {
295+
try buf.withUnsafeBytes { (src_ptr: UnsafePointer<UInt8>) in
309296

310297
// Next byte to read
311-
_stream.src_ptr = src_ptr.advanced(by: bufCount - _stream.src_size)
298+
_stream.src_ptr = src_ptr.advanced(by: buf.count - _stream.src_size)
312299

313300
let status = compression_stream_process(&_stream, (_eofReached ? Int32(COMPRESSION_STREAM_FINALIZE.rawValue) : 0))
314-
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.FilterProcessError }
301+
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.filterProcessError }
315302
if status == COMPRESSION_STATUS_END { _endReached = true }
316303
}
317-
318-
} else {
319-
304+
}
305+
else {
320306
let status = compression_stream_process(&_stream, (_eofReached ? Int32(COMPRESSION_STREAM_FINALIZE.rawValue) : 0))
321-
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.FilterProcessError }
307+
guard status != COMPRESSION_STATUS_ERROR else { throw FilterError.filterProcessError }
322308
if status == COMPRESSION_STATUS_END { _endReached = true }
323309

324310
}
@@ -332,8 +318,7 @@ public class InputFilter {
332318
}
333319

334320
// Cleanup resources
335-
deinit
336-
{
321+
deinit {
337322
compression_stream_destroy(&_stream)
338323
}
339324

0 commit comments

Comments
 (0)