8000 oid: add git_oid_fmt_substr · libgit2/libgit2@4ffc6b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ffc6b4

Browse files
committed
oid: add git_oid_fmt_substr
Tidy up `nfmt` / `pathfmt`.
1 parent 4583a98 commit 4ffc6b4

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

src/oid.c

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ const git_oid git_oid__empty_tree_sha1 =
2222
{ 0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
2323
0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04 }};
2424

25-
static char to_hex[] = "0123456789abcdef";
26-
2725
static int oid_error_invalid(const char *msg)
2826
{
2927
git_error_set(GIT_ERROR_INVALID, "unable to parse OID - %s", msg);
@@ -75,16 +73,9 @@ int git_oid_fromstr(git_oid *out, const char *str, git_oid_t type)
7573
return git_oid_fromstrn(out, str, git_oid_hexsize(type), type);
7674
}
7775

78-
GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
79-
{
80-
*str++ = to_hex[val >> 4];
81-
*str++ = to_hex[val & 0xf];
82-
return str;
83-
}
84-
8576
int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
8677
{
87-
size_t hex_size, i, max_i;
78+
size_t hex_size;
8879

8980
if (!oid) {
9081
memset(str, 0, n);
@@ -99,14 +90,7 @@ int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
9990
n = hex_size;
10091
}
10192

102-
max_i = n / 2;
103-
104-
for (i = 0; i < max_i; i++)
105-
str = fmt_one(str, oid->id[i]);
106-
107-
if (n & 1)
108-
*str++ = to_hex[oid->id[i] >> 4];
109-
93+
git_oid_fmt_substr(str, oid, 0, n);
11094
return 0;
11195
}
11296

@@ -117,16 +101,14 @@ int git_oid_fmt(char *str, const git_oid *oid)
117101

118102
int git_oid_pathfmt(char *str, const git_oid *oid)
119103
{
120-
size_t size, i;
104+
size_t hex_size;
121105

122-
if (!(size = git_oid_size(oid->type)))
106+
if (!(hex_size = git_oid_hexsize(oid->type)))
123107
return oid_error_invalid("unknown type");
124108

125-
str = fmt_one(str, oid->id[0]);
126-
*str++ = '/';
127-
for (i = 1; i < size; i++)
128-
str = fmt_one(str, oid->id[i]);
129-
109+
git_oid_fmt_substr(str, oid, 0, 2);
110+
str[2] = '/';
111+
git_oid_fmt_substr(&str[3], oid, 2, (hex_size - 2));
130112
return 0;
131113
}
132114

src/oid.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ GIT_INLINE(git_hash_algorithm_t) git_oid_algorithm(git_oid_t type)
6262
*/
6363
char *git_oid_allocfmt(const git_oid *id);
6464

65+
/**
66+
* Format the requested nibbles of an object id.
67+
*
68+
* @param str the string to write into
69+
* @param oid the oid structure to format
70+
* @param start the starting number of nibbles
71+
* @param count the number of nibbles to format
72+
*/
73+
GIT_INLINE(void) git_oid_fmt_substr(
74+
char *str,
75+
const git_oid *oid,
76+
size_t start,
77+
size_t count)
78+
{
79+
static char hex[] = "0123456789abcdef";
80+
size_t i, end = start + count, min = start / 2, max = end / 2;
81+
82+
if (start & 1)
83+
*str++ = hex[oid->id[min++] & 0x0f];
84+
85+
for (i = min; i < max; i++) {
86+
*str++ = hex[oid->id[i] >> 4];
87+
*str++ = hex[oid->id[i] & 0x0f];
88+
}
89+
90+
if (end & 1)
91+
*str++ = hex[oid->id[i] >> 4];
92+
}
93+
6594
GIT_INLINE(int) git_oid_raw_ncmp(
6695
const unsigned char *sha1,
6796
const unsigned char *sha2,

tests/core/oid.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,28 @@ void test_core_oid__is_hexstr(void)
151151
cl_assert(!git_oid__is_hexstr("zeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
152152
cl_assert(!git_oid__is_hexstr("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef1"));
153153
}
154+
155+
void test_core_oid__fmt_substr_sha1(void)
156+
{
157+
char buf[GIT_OID_MAX_HEXSIZE];
158+
159+
memset(buf, 0, GIT_OID_MAX_HEXSIZE);
160+
git_oid_fmt_substr(buf, &id_sha1, 0, 40);
161+
cl_assert_equal_s(buf, str_oid_sha1);
162+
163+
memset(buf, 0, GIT_OID_MAX_HEXSIZE);
164+
git_oid_fmt_substr(buf, &id_sha1, 0, 18);
165+
cl_assert_equal_s(buf, str_oid_sha1_p);
166+
167+
memset(buf, 0, GIT_OID_MAX_HEXSIZE);
168+
git_oid_fmt_substr(buf, &id_sha1, 0, 5);
169+
cl_assert_equal_s(buf, "ae90f");
170+
171+
memset(buf, 0, GIT_OID_MAX_HEXSIZE);
172+
git_oid_fmt_substr(buf, &id_sha1, 5, 5);
173+
cl_assert_equal_s(buf, "12eea");
174+
175+
memset(buf, 0, GIT_OID_MAX_HEXSIZE);
176+
git_oid_fmt_substr(buf, &id_sha1, 5, 6);
177+
cl_assert_equal_s(buf, "12eea6");
178+
}

0 commit comments

Comments
 (0)
0