SwiftGit2/SwiftGit2/OID.swift
Markos Charatzas 5c8a6000c4 updated to Swift 2.0
updated to Xcode 7.2 recommended settings
integrated with Nimble (v3.0.0), Quick (v0.8.0), Result (1.0.1)
integrated with Guanaco (5031bf67297afbe61ac0f2fbf3e3e8400b3f8888) that supports Swift 2.0
2016-01-16 14:57:32 +00:00

82 lines
1.7 KiB
Swift

//
// OID.swift
// SwiftGit2
//
// Created by Matt Diephouse on 11/17/14.
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
//
import Foundation
import Result
/// An identifier for a Git object.
public struct OID {
// MARK: - Initializers
/// Create an instance from a hex formatted string.
///
/// string - A 40-byte hex formatted string.
public init?(string: String) {
// libgit2 doesn't enforce a maximum length
if (string.lengthOfBytesUsingEncoding(NSASCIIStringEncoding) > 40) {
return nil
}
let pointer = UnsafeMutablePointer<git_oid>.alloc(1)
let result = git_oid_fromstr(pointer, string)
if result < GIT_OK.rawValue {
pointer.dealloc(1)
return nil;
}
oid = pointer.memory;
pointer.dealloc(1)
}
/// Create an instance from a libgit2 `git_oid`.
public init(_ oid: git_oid) {
self.oid = oid
}
// MARK: - Properties
public let oid: git_oid
}
extension OID: CustomStringConvertible {
public var description: String {
let length = Int(GIT_OID_RAWSZ) * 2
let string = UnsafeMutablePointer<Int8>.alloc(length)
var oid = self.oid
git_oid_fmt(string, &oid)
return String(bytesNoCopy: string, length: length, encoding: NSASCIIStringEncoding, freeWhenDone: true)!
}
}
extension OID: Hashable {
public var hashValue: Int {
let bytes = [
self.oid.id.0,
self.oid.id.1,
self.oid.id.2,
self.oid.id.3,
self.oid.id.4,
self.oid.id.5,
self.oid.id.6,
self.oid.id.7
]
return bytes.reduce(0, combine:{ (hash, byte) in
return Int(hash << 8) | Int(byte)
})
}
}
public func == (lhs: OID, rhs: OID) -> Bool {
var left = lhs.oid
var right = rhs.oid
return git_oid_cmp(&left, &right) == 0
}