From 3b0f6f7cd9b7b358b220f9df7ccad6bcfead3f7e Mon Sep 17 00:00:00 2001 From: 1024jp <1024jp@wolfrosch.com> Date: Sat, 29 Apr 2023 13:24:33 +0900 Subject: [PATCH] Style code --- Sources/Gzip/Data+Gzip.swift | 40 ++++++++++++++++----------------- Tests/GzipTests/GzipTests.swift | 5 +---- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/Sources/Gzip/Data+Gzip.swift b/Sources/Gzip/Data+Gzip.swift index a3afb22..c9f16bb 100644 --- a/Sources/Gzip/Data+Gzip.swift +++ b/Sources/Gzip/Data+Gzip.swift @@ -5,7 +5,7 @@ /* The MIT License (MIT) - © 2014-2022 1024jp + © 2014-2023 1024jp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -64,7 +64,6 @@ public struct CompressionLevel: RawRepresentable { self.rawValue = rawValue } - } @@ -139,7 +138,6 @@ public struct GzipError: Swift.Error { return self.message } - } @@ -242,55 +240,56 @@ extension Data { guard !self.isEmpty else { return Data() } - + var data = Data(capacity: self.count * 2) var totalIn: uLong = 0 var totalOut: uLong = 0 - + repeat { var stream = z_stream() var status: Int32 - + status = inflateInit2_(&stream, wBits, ZLIB_VERSION, Int32(DataSize.stream)) - + guard status == Z_OK else { // inflateInit2 returns: // Z_VERSION_ERROR The zlib library version is incompatible with the version assumed by the caller. // Z_MEM_ERROR There was not enough memory. // Z_STREAM_ERROR A parameters are invalid. - + throw GzipError(code: status, msg: stream.msg) } - + repeat { if Int(totalOut + stream.total_out) >= data.count { data.count += self.count / 2 } - + let inputCount = self.count let outputCount = data.count - + self.withUnsafeBytes { (inputPointer: UnsafeRawBufferPointer) in let inputStartPosition = totalIn + stream.total_in stream.next_in = UnsafeMutablePointer(mutating: inputPointer.bindMemory(to: Bytef.self).baseAddress!).advanced(by: Int(inputStartPosition)) stream.avail_in = uint(inputCount) - uInt(inputStartPosition) - + data.withUnsafeMutableBytes { (outputPointer: UnsafeMutableRawBufferPointer) in let outputStartPosition = totalOut + stream.total_out stream.next_out = outputPointer.bindMemory(to: Bytef.self).baseAddress!.advanced(by: Int(outputStartPosition)) stream.avail_out = uInt(outputCount) - uInt(outputStartPosition) - + status = inflate(&stream, Z_SYNC_FLUSH) - + stream.next_out = nil } - + stream.next_in = nil } } while (status == Z_OK) + totalIn += stream.total_in - + guard inflateEnd(&stream) == Z_OK, status == Z_STREAM_END else { // inflate returns: // Z_DATA_ERROR The input data was corrupted (input stream not conforming to the zlib format or incorrect check value). @@ -299,16 +298,15 @@ extension Data { // Z_BUF_ERROR No progress is possible or there was not enough room in the output buffer when Z_FINISH is used. throw GzipError(code: status, msg: stream.msg) } - - + totalOut += stream.total_out + } while (totalIn < self.count) + data.count = Int(totalOut) - + return data } - - } diff --git a/Tests/GzipTests/GzipTests.swift b/Tests/GzipTests/GzipTests.swift index eac8bdc..45729b4 100644 --- a/Tests/GzipTests/GzipTests.swift +++ b/Tests/GzipTests/GzipTests.swift @@ -7,7 +7,7 @@ /* The MIT License (MIT) - © 2015-2022 1024jp + © 2015-2023 1024jp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -127,7 +127,6 @@ final class GzipTests: XCTestCase { XCTAssertTrue(data.isGzipped) XCTAssertEqual(String(data: uncompressed, encoding: .utf8), "teststring") } - } @@ -145,7 +144,6 @@ private extension XCTestCase { return Bundle(for: type(of: self)).url(forResource: name, withExtension: nil)! #endif } - } @@ -161,5 +159,4 @@ private extension String { string.append(letters.randomElement()!) } } - }