Skip to content

Commit

Permalink
Wrap maxWindowBits with Gzip (1024jp#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jul 19, 2022
1 parent 241bb44 commit df506dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions Sources/Gzip/Data+Gzip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ import struct Foundation.Data
import zlib
#endif

/// Maximum value for windowBits (`MAX_WBITS`)
public let maxWindowBits = MAX_WBITS
public enum Gzip {

/// Maximum value for windowBits (`MAX_WBITS`)
public static let maxWindowBits = MAX_WBITS
}


/// Compression level whose rawValue is based on the zlib's constants.
public struct CompressionLevel: RawRepresentable {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Tests/GzipTests/GzipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, "{")
Expand Down

0 comments on commit df506dc

Please sign in to comment.