Fix string to hash problem
[TortoiseGit.git] / src / Git / GitHash.h
blobf61e3a874b67437437db4271aec95bdf93767824
1 #pragma once
2 #if defined(_MFC_VER)
3 #include "afx.h"
4 #endif
5 #define GIT_HASH_SIZE 20
7 class CGitHash
9 public:
10 unsigned char m_hash[GIT_HASH_SIZE];
12 CGitHash()
14 memset(m_hash,0, GIT_HASH_SIZE);
16 CGitHash(char *p)
18 memcpy(m_hash,p,GIT_HASH_SIZE);
20 CGitHash & operator = (CString &str)
22 CGitHash hash(str);
23 *this = hash;
24 return *this;
26 CGitHash(CString &str)
28 for(int i=0;i<GIT_HASH_SIZE;i++)
30 unsigned char a;
31 a=0;
32 for(int j=2*i;j<=2*i+1;j++)
34 a =a<<4;
36 TCHAR ch = str[j];
37 if(ch >= _T('0') && ch <= _T('9'))
38 a |= (ch - _T('0'))&0xF;
39 else if(ch >=_T('A') && ch <= _T('F'))
40 a |= ((ch - _T('A'))&0xF) + 10 ;
41 else if(ch >=_T('a') && ch <= _T('f'))
42 a |= ((ch - _T('a'))&0xF) + 10;
45 m_hash[i]=a;
48 void Empty()
50 memset(m_hash,0, GIT_HASH_SIZE);
52 bool IsEmpty()
54 for(int i=0;i<GIT_HASH_SIZE;i++)
56 if(m_hash[i] != 0)
57 return false;
59 return true;
62 CString ToString()
64 CString str;
65 CString a;
66 for(int i=0;i<GIT_HASH_SIZE;i++)
68 a.Format(_T("%02x"),m_hash[i]);
69 str+=a;
71 return str;
73 operator CString ()
75 return ToString();
78 bool operator == (const CGitHash &hash)
80 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;
84 friend bool operator<(const CGitHash& left, const CGitHash& right)
86 return memcmp(left.m_hash,right.m_hash,GIT_HASH_SIZE) < 0;
89 friend bool operator>(const CGitHash& left, const CGitHash& right)
91 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) > 0;
94 #if defined(_MFC_VER)
95 friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)
97 for(int i=0;i<GIT_HASH_SIZE;i++)
98 ar<<hash.m_hash[i];
99 return ar;
101 friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)
103 for(int i=0;i<GIT_HASH_SIZE;i++)
104 ar>>hash.m_hash[i];
105 return ar;
107 #endif