зеркало из https://github.com/github/libgit2sharp.git
Introduce ObjectId.StartsWith()
This commit is contained in:
Родитель
5264090d3c
Коммит
41ef06ee76
|
@ -136,5 +136,25 @@ namespace LibGit2Sharp.Tests
|
|||
Assert.True(maybeSha.StartsWith(parsedObjectId.ToString(3)));
|
||||
Assert.Equal(maybeSha, parsedObjectId.ToString(42));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new byte[] { 0xde, 0xad, 0xbe }, 6, true)]
|
||||
[InlineData(new byte[] { 0xde, 0xad }, 4, true)]
|
||||
[InlineData(new byte[] { 0xde, 0xad }, 3, true)]
|
||||
[InlineData(new byte[] { 0xde, 0xad }, 2, true)]
|
||||
[InlineData(new byte[] { 0xde, 0xad }, 1, true)]
|
||||
[InlineData(new byte[] { 0xde, 0xaf }, 3, true)]
|
||||
[InlineData(new byte[] { 0xde, 0xff }, 2, true)]
|
||||
[InlineData(new byte[] { 0xdf, 0xff }, 1, true)]
|
||||
[InlineData(new byte[] { 0x98, 0x76 }, 4, false)]
|
||||
[InlineData(new byte[] { 0x98, 0x76 }, 3, false)]
|
||||
[InlineData(new byte[] { 0x98, 0x76 }, 2, false)]
|
||||
[InlineData(new byte[] { 0x98, 0x76 }, 1, false)]
|
||||
public void StartsWith(byte[] rawId, int len, bool expected)
|
||||
{
|
||||
var id = new ObjectId("deadbeef84650f067bd5703b6a59a8b3b3c99a09");
|
||||
|
||||
Assert.Equal(expected, id.StartsWith(rawId, len));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -198,30 +198,7 @@ namespace LibGit2Sharp.Tests
|
|||
|
||||
foreach (ObjectId objectId in m_objectIdToContent.Keys)
|
||||
{
|
||||
bool match = true;
|
||||
|
||||
int length = len >> 1;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (objectId.RawId[i] != shortOid[i])
|
||||
{
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match && ((len & 1) == 1))
|
||||
{
|
||||
var a = objectId.RawId[length] >> 4;
|
||||
var b = shortOid[length] >> 4;
|
||||
|
||||
if (a != b)
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!match)
|
||||
if (!objectId.StartsWith(shortOid, len))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -297,5 +297,52 @@ namespace LibGit2Sharp
|
|||
|
||||
return objectId.All(c => hexDigits.Contains(c.ToString(CultureInfo.InvariantCulture)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine whether the beginning of this instance matches the
|
||||
/// <paramref name="len"/> first nibbles of <paramref name="rawId"/>.
|
||||
/// </summary>
|
||||
/// <param name="rawId">The byte array to compare the <see cref="ObjectId"/> against.</param>
|
||||
/// <param name="len">The number of nibbles from <paramref name="rawId"/> </param>
|
||||
/// <returns></returns>
|
||||
public bool StartsWith(byte[] rawId, int len)
|
||||
{
|
||||
Ensure.ArgumentNotNull(rawId, "rawId");
|
||||
|
||||
if (len < 1 || len > HexSize)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("len");
|
||||
}
|
||||
|
||||
if (len > rawId.Length * 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("len", "len exceeds the size of rawId");
|
||||
}
|
||||
|
||||
bool match = true;
|
||||
|
||||
int length = len >> 1;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (RawId[i] != rawId[i])
|
||||
{
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match && ((len & 1) == 1))
|
||||
{
|
||||
var a = RawId[length] >> 4;
|
||||
var b = rawId[length] >> 4;
|
||||
|
||||
if (a != b)
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче