Commit 4083e46f by Edward Thomson

oid: provide type lookups by enum value or name

parent f02bcf72
...@@ -66,6 +66,47 @@ GIT_INLINE(size_t) git_oid_hexsize(git_oid_t type) ...@@ -66,6 +66,47 @@ GIT_INLINE(size_t) git_oid_hexsize(git_oid_t type)
return 0; return 0;
} }
GIT_INLINE(const char *) git_oid_type_name(git_oid_t type)
{
switch (type) {
case GIT_OID_SHA1:
return "sha1";
#ifdef GIT_EXPERIMENTAL_SHA256
case GIT_OID_SHA256:
return "sha256";
#endif
}
return "unknown";
}
GIT_INLINE(git_oid_t) git_oid_type_fromstr(const char *name)
{
if (strcmp(name, "sha1") == 0)
return GIT_OID_SHA1;
#ifdef GIT_EXPERIMENTAL_SHA256
if (strcmp(name, "sha256") == 0)
return GIT_OID_SHA256;
#endif
return 0;
}
GIT_INLINE(git_oid_t) git_oid_type_fromstrn(const char *name, size_t len)
{
if (len == CONST_STRLEN("sha1") && strncmp(name, "sha1", len) == 0)
return GIT_OID_SHA1;
#ifdef GIT_EXPERIMENTAL_SHA256
if (len == CONST_STRLEN("sha256") && strncmp(name, "sha256", len) == 0)
return GIT_OID_SHA256;
#endif
return 0;
}
GIT_INLINE(git_hash_algorithm_t) git_oid_algorithm(git_oid_t type) GIT_INLINE(git_hash_algorithm_t) git_oid_algorithm(git_oid_t type)
{ {
switch (type) { switch (type) {
......
...@@ -192,3 +192,22 @@ void test_core_oid__fmt_substr_sha1(void) ...@@ -192,3 +192,22 @@ void test_core_oid__fmt_substr_sha1(void)
git_oid_fmt_substr(buf, &id_sha1, 5, 6); git_oid_fmt_substr(buf, &id_sha1, 5, 6);
cl_assert_equal_s(buf, "12eea6"); cl_assert_equal_s(buf, "12eea6");
} }
void test_core_oid__type_lookup(void)
{
cl_assert_equal_i(GIT_OID_SHA1, git_oid_type_fromstr("sha1"));
cl_assert_equal_i(GIT_OID_SHA1, git_oid_type_fromstrn("sha1...", 4));
cl_assert_equal_s("sha1", git_oid_type_name(GIT_OID_SHA1));
#ifdef GIT_EXPERIMENTAL_SHA256
cl_assert_equal_i(GIT_OID_SHA256, git_oid_type_fromstr("sha256"));
cl_assert_equal_i(GIT_OID_SHA256, git_oid_type_fromstrn("sha256...", 6));
cl_assert_equal_s("sha256", git_oid_type_name(GIT_OID_SHA256));
#endif
cl_assert_equal_i(0, git_oid_type_fromstr("sha42"));
cl_assert_equal_i(0, git_oid_type_fromstrn("sha1", 3));
cl_assert_equal_i(0, git_oid_type_fromstrn("sha1...", 5));
cl_assert_equal_s("unknown", git_oid_type_name(0));
cl_assert_equal_s("unknown", git_oid_type_name(42));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment