Skip to content

Commit

Permalink
Add platforms for SPM;
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyFanFan committed Nov 5, 2021
1 parent 7023b71 commit 9118211
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import PackageDescription

let package = Package(
name: "FileHash",
platforms: [
.iOS(.v9),
.macOS(.v10_10),
.watchOS(.v3),
.tvOS(.v9)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
25 changes: 11 additions & 14 deletions Sources/FileHash/Hasher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public enum Hasher {
}

extension Hasher {
struct Context<HashObject> {
typealias ObjectMaker = () -> HashObject
typealias InitMethod = (UnsafeMutablePointer<HashObject>?) -> Int32
typealias UpdateMethod = (UnsafeMutablePointer<HashObject>?, UnsafeRawPointer?, CC_LONG) -> Int32
typealias FinalMethod = (UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<HashObject>?) -> Int32
struct Context<CTX> {
typealias ObjectMaker = () -> CTX
typealias InitMethod = (UnsafeMutablePointer<CTX>?) -> Int32
typealias UpdateMethod = (UnsafeMutablePointer<CTX>?, UnsafeRawPointer?, CC_LONG) -> Int32
typealias FinalMethod = (UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<CTX>?) -> Int32

var objectMaker: ObjectMaker
var initMethod: InitMethod
Expand All @@ -28,7 +28,7 @@ extension Hasher {
var digestLength: Int
}

static func hashOfFile<T>(atPath filePath: String?, with context: inout Context<T>) -> String? {
static func hashOfFile<CTX>(atPath filePath: String?, with context: inout Context<CTX>) -> String? {
guard let filePath = filePath else { return nil }

let url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePath as CFString, .cfurlposixPathStyle, false)
Expand Down Expand Up @@ -59,16 +59,13 @@ extension Hasher {
var digest = [UInt8](repeating: 0, count: context.digestLength)
_ = context.finalMethod(&digest, &hashObject)

var result: String = ""
didSucceed = !hasMoreData

if didSucceed {
for index in 0..<context.digestLength {
result += String(format: "%02x", digest[index])
}
} else {
return nil
}
guard didSucceed else { return nil }

var result: String = ""

digest.forEach { result += String(format: "%02x",$0) }

return result
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/FileHashTests/FileHashTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest
@testable import FileHash
import FileHash

final class FileHashTests: XCTestCase {
private var cacheFileURL: URL?
Expand Down

0 comments on commit 9118211

Please sign in to comment.