summaryrefslogtreecommitdiff
path: root/src/oid.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-04-29 08:59:46 -0700
committerRussell Belfer <rb@github.com>2013-04-29 08:59:46 -0700
commitaa8f010120577e61715f3ae1286a03055815f9c3 (patch)
treec6e8f4c96797675040e45e4b420705f5f3c21454 /src/oid.c
parent8564a0224abe09beaacb2d2e7a54b16f8fcea7d1 (diff)
downloadlibgit2-aa8f010120577e61715f3ae1286a03055815f9c3.tar.gz
Add git_oid_strcmp and use it for git_oid_streq
Add a new git_oid_strcmp that compares a string OID with a hex oid for sort order, and then reimplement git_oid_streq using it. This actually should speed up git_oid_streq because it only reads as far into the string as it needs to, whereas previously it would convert the whole string into an OID and then use git_oid_cmp.
Diffstat (limited to 'src/oid.c')
-rw-r--r--src/oid.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/oid.c b/src/oid.c
index 4b6699009..c7ce6ee50 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -194,14 +194,31 @@ int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
return 0;
}
-int git_oid_streq(const git_oid *a, const char *str)
+int git_oid_strcmp(const git_oid *oid_a, const char *str)
{
- git_oid id;
+ const unsigned char *a = oid_a->id;
+ unsigned char strval;
+ int hexval;
- if (git_oid_fromstr(&id, str) < 0)
- return -1;
+ for (a = oid_a->id; *str && (a - oid_a->id) < GIT_OID_RAWSZ; ++a) {
+ if ((hexval = git__fromhex(*str++)) < 0)
+ return -1;
+ strval = hexval << 4;
+ if (*str) {
+ if ((hexval = git__fromhex(*str++)) < 0)
+ return -1;
+ strval |= hexval;
+ }
+ if (*a != strval)
+ return (*a - strval);
+ }
- return git_oid_cmp(a, &id) == 0 ? 0 : -1;
+ return 0;
+}
+
+int git_oid_streq(const git_oid *oid_a, const char *str)
+{
+ return git_oid_strcmp(oid_a, str) == 0 ? 0 : -1;
}
int git_oid_iszero(const git_oid *oid_a)