hex: introduce parse_oid_hex

Introduce a function, parse_oid_hex, which parses a hexadecimal object ID and if successful, sets a pointer to just beyond the last character. This allows for simpler, more robust parsing without needing to hard-code integer values throughout the codebase. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Feb 20, 2017 at 00:10 UTC 605f430efb23ce14ca11750368149acd38b8f1e4
2 files changed +17
cache.h
+9
@@ -1319,6 +1319,15 @@ extern char *oid_to_hex_r(char *out, const struct object_id *oid);
1319 extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
1320 extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sha1_to_hex */
1321
1322 +/*
1323 + * Parse a 40-character hexadecimal object ID starting from hex, updating the
1324 + * pointer specified by end when parsing stops. The resulting object ID is
1325 + * stored in oid. Returns 0 on success. Parsing will stop on the first NUL or
1326 + * other invalid character. end is only updated on success; otherwise, it is
1327 + * unmodified.
1328 + */
1329 +extern int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
1330 +
1331 extern int interpret_branch_name(const char *str, int len, struct strbuf *);
1332 extern int get_oid_mb(const char *str, struct object_id *oid);
1333
hex.c
+8
@@ -53,6 +53,14 @@ int get_oid_hex(const char *hex, struct object_id *oid)
53 return get_sha1_hex(hex, oid->hash);
54 }
55
56 +int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
57 +{
58 + int ret = get_oid_hex(hex, oid);
59 + if (!ret)
60 + *end = hex + GIT_SHA1_HEXSZ;
61 + return ret;
62 +}
63 +
64 char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
65 {
66 static const char hex[] = "0123456789abcdef";