Skip to content

Commit

Permalink
Make CompressionLevel struct
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jun 10, 2017
1 parent 943837b commit 51f666f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
Change Log
==========================

4.0.0-beta.2
--------------------------

### Changes

- Make `CompressionLevel` struct.



4.0.0-beta
--------------------------

Expand Down
33 changes: 24 additions & 9 deletions Sources/Data+Gzip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,31 @@ import Foundation
import zlib

/**
Compression level with constants based on the zlib's constants.
Compression level whose rawValue is based on the zlib's constants.
*/
public typealias CompressionLevel = Int32
public extension CompressionLevel {
public struct CompressionLevel: RawRepresentable, Equatable {

public static let noCompression = Z_NO_COMPRESSION
public static let bestSpeed = Z_BEST_SPEED
public static let bestCompression = Z_BEST_COMPRESSION
/// Compression level in the range of `0` (no compression) to `9` (maximum compression).
public let rawValue: Int32

public static let noCompression = CompressionLevel(Z_NO_COMPRESSION)
public static let bestSpeed = CompressionLevel(Z_BEST_SPEED)
public static let bestCompression = CompressionLevel(Z_BEST_COMPRESSION)

public static let defaultCompression = CompressionLevel(Z_DEFAULT_COMPRESSION)


public init(rawValue: Int32) {

self.rawValue = rawValue
}


public init(_ rawValue: Int32) {

self.rawValue = rawValue
}

public static let defaultCompression = Z_DEFAULT_COMPRESSION
}


Expand Down Expand Up @@ -172,7 +187,7 @@ public extension Data {
Throws an error if compression failed.

- parameters:
- level: Compression level in the range of `0` (no compression) to `9` (maximum compression).
- level: Compression level.

- throws: `GzipError`
- returns: Gzip-compressed `Data` object.
Expand All @@ -186,7 +201,7 @@ public extension Data {
var stream = self.createZStream()
var status: Int32

status = deflateInit2_(&stream, level, Z_DEFLATED, MAX_WBITS + 16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY, ZLIB_VERSION, Int32(DataSize.stream))
status = deflateInit2_(&stream, level.rawValue, Z_DEFLATED, MAX_WBITS + 16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY, ZLIB_VERSION, Int32(DataSize.stream))

guard status == Z_OK else {
// deflateInit2 returns:
Expand Down

0 comments on commit 51f666f

Please sign in to comment.