提交 98157253 编写于 作者: N nulltoken

Added string to byte[] conversion method to ObjectId.

上级 06315111
using System;
using NUnit.Framework;
namespace libgit2sharp.Tests
{
[TestFixture]
public class ObjectIdFixture
{
[TestCase("DDelORu/9Dw38NA3GCOlUJ7tWx0=", "0c37a5391bbff43c37f0d0371823a5509eed5b1d")]
[TestCase("FqASNFZ4mrze9Ld1ITwjqL109eA=", "16a0123456789abcdef4b775213c23a8bd74f5e0")]
public void ToString(string encoded, string expected)
{
byte[] id = Convert.FromBase64String(encoded);
string objectId = ObjectId.ToString(id);
Assert.AreEqual(expected, objectId);
}
[TestCase("0c37a5391bbff43c37f0d0371823a5509eed5b1d", "DDelORu/9Dw38NA3GCOlUJ7tWx0=")]
[TestCase("16a0123456789abcdef4b775213c23a8bd74f5e0", "FqASNFZ4mrze9Ld1ITwjqL109eA=")]
public void ToByteArray(string objectId, string expected)
{
byte[] id = Convert.FromBase64String(expected);
byte[] rawId = ObjectId.ToByteArray(objectId);
CollectionAssert.AreEqual(id, rawId);
}
}
}
\ No newline at end of file
......@@ -44,6 +44,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EpochHelperFixture.cs" />
<Compile Include="ObjectIdFixture.cs" />
<Compile Include="RepositoryFixtures.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
......
using System.Diagnostics;
using System;
using System.Diagnostics;
using libgit2sharp.Wrapper;
namespace libgit2sharp
{
public static class ObjectId
{
private static readonly char[] hexDigits = new []
{
'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'
};
private static readonly char[] HexDigits = "0123456789abcdef".ToCharArray();
private static readonly byte[] ReverseHexDigits = BuildReverseHexDigits();
private static byte[] BuildReverseHexDigits()
{
var bytes = new byte['f' - '0' + 1];
for (int i = 0; i < 10; i++)
{
bytes[i] = (byte)i;
}
for (int i = 10; i < 16; i++)
{
bytes[i + 'a' - '0' - 0x0a] = (byte)(i);
}
return bytes;
}
public static string ToString(byte[] id)
{
......@@ -17,19 +32,38 @@ public static string ToString(byte[] id)
// Inspired from http://stackoverflow.com/questions/623104/c-byte-to-hex-string/3974535#3974535
var c = new char[Constants.GIT_OID_RAWSZ * 2];
var c = new char[Constants.GIT_OID_HEXSZ];
for (int i = 0; i < Constants.GIT_OID_RAWSZ * 2; i++)
for (int i = 0; i < Constants.GIT_OID_HEXSZ; i++)
{
int index0 = i >> 1;
var b = ((byte)(id[index0] >> 4));
c[i++] = hexDigits[b];
c[i++] = HexDigits[b];
b = ((byte)(id[index0] & 0x0F));
c[i] = hexDigits[b];
c[i] = HexDigits[b];
}
return new string(c);
}
public static byte[] ToByteArray(string id)
{
Debug.Assert(id != null && id.Length == Constants.GIT_OID_HEXSZ);
var bytes = new byte[Constants.GIT_OID_RAWSZ];
for (int i = 0; i < Constants.GIT_OID_HEXSZ; i++)
{
int c1 = ByteConverter(id[i++]) << 4;
int c2 = ByteConverter(id[i]);
bytes[i >> 1] = (byte)(c1 + c2);
}
return bytes;
}
private static readonly Func<int, byte> ByteConverter = i => ReverseHexDigits[i - '0'];
}
}
\ No newline at end of file
......@@ -5,5 +5,6 @@ public static class Constants
//public const int GIT_PATH_MAX = 4096;
public const int GIT_ERROR = -1;
public const int GIT_OID_RAWSZ = 20;
public const int GIT_OID_HEXSZ = GIT_OID_RAWSZ * 2;
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册