Wrap maxWindowBits with Gzip (#52)
This commit is contained in:
parent
241bb44bed
commit
df506dc7f2
|
@ -34,8 +34,12 @@ import struct Foundation.Data
|
|||
import zlib
|
||||
#endif
|
||||
|
||||
public enum Gzip {
|
||||
|
||||
/// Maximum value for windowBits (`MAX_WBITS`)
|
||||
public let maxWindowBits = MAX_WBITS
|
||||
public static let maxWindowBits = MAX_WBITS
|
||||
}
|
||||
|
||||
|
||||
/// Compression level whose rawValue is based on the zlib's constants.
|
||||
public struct CompressionLevel: RawRepresentable {
|
||||
|
@ -162,7 +166,7 @@ extension Data {
|
|||
/// - Parameter wBits: Manage the size of the history buffer.
|
||||
/// - Returns: Gzip-compressed `Data` instance.
|
||||
/// - Throws: `GzipError`
|
||||
public func gzipped(level: CompressionLevel = .defaultCompression, wBits: Int32 = MAX_WBITS + 16) throws -> Data {
|
||||
public func gzipped(level: CompressionLevel = .defaultCompression, wBits: Int32 = Gzip.maxWindowBits + 16) throws -> Data {
|
||||
|
||||
guard !self.isEmpty else {
|
||||
return Data()
|
||||
|
@ -233,7 +237,7 @@ extension Data {
|
|||
/// - Parameter wBits: Manage the size of the history buffer.
|
||||
/// - Returns: Gzip-decompressed `Data` instance.
|
||||
/// - Throws: `GzipError`
|
||||
public func gunzipped(wBits: Int32 = MAX_WBITS + 32) throws -> Data {
|
||||
public func gunzipped(wBits: Int32 = Gzip.maxWindowBits + 32) throws -> Data {
|
||||
|
||||
guard !self.isEmpty else {
|
||||
return Data()
|
||||
|
|
|
@ -109,7 +109,7 @@ final class GzipTests: XCTestCase {
|
|||
dHIPIYjvjjbRUSTKjmZHs6N/6WhVStS01VnRrGhW9BeKXsML
|
||||
"""
|
||||
let data = try XCTUnwrap(Data(base64Encoded: encoded))
|
||||
let uncompressed = try data.gunzipped(wBits: -maxWindowBits)
|
||||
let uncompressed = try data.gunzipped(wBits: -Gzip.maxWindowBits)
|
||||
let json = String(data: uncompressed, encoding: .utf8)
|
||||
|
||||
XCTAssertEqual(json?.first, "{")
|
||||
|
|
Loading…
Reference in New Issue