Skip to content

Commit

Permalink
Clean-up code
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jul 17, 2016
1 parent 91cb18d commit 4dbccee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Change Log

### Changes

- Change to return just empty NSData instead nil if given data is empty
- Change to return just an empty NSData instead of nil if given data is empty
- Log error message if compression/decompression is failed.
- [Note] This is a temporaly improvement.
I'll migrate functions to throw NSError when Swift 2.0 becomes stable.
I'll migrate functions to throw NSError when Swift 2.0 becomes stable. -> Done.
21 changes: 11 additions & 10 deletions Project/Tests/NSData_GZIPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ THE SOFTWARE.
import Foundation
import XCTest

class NSData_GZIPTests: XCTestCase
{
func testGZip()
{
class NSData_GZIPTests: XCTestCase {
func testGZip() {
let testSentence = "foo"

let data = testSentence.data(using: .utf8)!
Expand All @@ -51,17 +51,17 @@ class NSData_GZIPTests: XCTestCase
}


func testZeroLength()
{
func testZeroLength() {
let zeroLengthData = Data()

XCTAssertEqual(try! zeroLengthData.gzipped(), zeroLengthData)
XCTAssertEqual(try! zeroLengthData.gunzipped(), zeroLengthData)
}


func testWrongUngzip()
{
func testWrongUngzip() {
// data not compressed
let data = "testString".data(using: .utf8)!

Expand All @@ -77,8 +77,8 @@ class NSData_GZIPTests: XCTestCase
}


func testCompressionLevel()
{
func testCompressionLevel() {
let data = String.lorem(length: 100_000).data(using: .utf8)!

XCTAssertGreaterThan(try! data.gzipped(level: .bestSpeed).count,
Expand All @@ -105,4 +105,5 @@ private extension String {

return string
}

}
34 changes: 17 additions & 17 deletions Sources/NSData+GZIP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@
import Foundation
import zlib

private let CHUNK_SIZE: Int = 2 ^ 14
private let STREAM_SIZE: Int32 = Int32(sizeof(z_stream.self))


public typealias CompressionLevel = Int32

/**
Constants for compression level based on the zlib's constants.
Compression level with constants based on the zlib's constants.
*/
public typealias CompressionLevel = Int32
public extension CompressionLevel {

public static let noCompression = Z_NO_COMPRESSION
public static let bestSpeed = Z_BEST_SPEED
public static let bestCompression = Z_BEST_COMPRESSION
Expand Down Expand Up @@ -103,8 +99,8 @@ public enum GzipError: ErrorProtocol {
case unknown(message: String, code: Int)


private init(code: Int32, msg: UnsafePointer<CChar>)
{
private init(code: Int32, msg: UnsafePointer<CChar>) {
let message = String(cString: msg) ?? "Unknown error"

switch code {
Expand All @@ -130,8 +126,7 @@ public enum GzipError: ErrorProtocol {
}


public extension Data
{
public extension Data {

/**
Check if the reciever is already gzipped.
Expand All @@ -156,8 +151,8 @@ public extension Data
- throws: `GzipError`
- returns: Gzip-compressed `Data` object.
*/
public func gzipped(level: CompressionLevel = .defaultCompression) throws -> Data
{
public func gzipped(level: CompressionLevel = .defaultCompression) throws -> Data {
guard self.count > 0 else {
return Data()
}
Expand Down Expand Up @@ -204,8 +199,8 @@ public extension Data
- throws: `GzipError`
- returns: Gzip-decompressed `Data` object.
*/
public func gunzipped() throws -> Data
{
public func gunzipped() throws -> Data {
guard self.count > 0 else {
return Data()
}
Expand Down Expand Up @@ -256,8 +251,8 @@ public extension Data
}


private func createZStream() -> z_stream
{
private func createZStream() -> z_stream {
return z_stream(
next_in: UnsafeMutablePointer<Bytef>((self as NSData).bytes),
avail_in: uint(self.count),
Expand All @@ -275,4 +270,9 @@ public extension Data
reserved: 0
)
}

}


private let CHUNK_SIZE: Int = 2 ^ 14
private let STREAM_SIZE: Int32 = Int32(sizeof(z_stream.self))

0 comments on commit 4dbccee

Please sign in to comment.