diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-09-08 22:17:19 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-09-08 22:17:19 +0400 |
commit | 41ef06ee76398ece00fe0832d341a675e5b604a8 (patch) | |
tree | 0982cfd95ad413ac7443ebfa39f15d3535fae692 /LibGit2Sharp/ObjectId.cs | |
parent | 5264090d3c3aa427e15ed86d4665105d178d0c65 (diff) |
Introduce ObjectId.StartsWith()
Diffstat (limited to 'LibGit2Sharp/ObjectId.cs')
-rw-r--r-- | LibGit2Sharp/ObjectId.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/LibGit2Sharp/ObjectId.cs b/LibGit2Sharp/ObjectId.cs index f3bb0481..3818347e 100644 --- a/LibGit2Sharp/ObjectId.cs +++ b/LibGit2Sharp/ObjectId.cs @@ -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; + } } } |