From d23309733a5b2a9e1adc304ee50c5a5ed7a087c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 3 Sep 2016 17:59:20 +0200 Subject: introduce hex2chr() for converting two hexadecimal digits to a character Add and use a helper function that decodes the char value of two hexadecimal digits. It returns a negative number on error, avoids running over the end of the given string and doesn't shift negative values. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- ref-filter.c | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) (limited to 'ref-filter.c') diff --git a/ref-filter.c b/ref-filter.c index bc551a752c..9adbb8af3e 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1576,24 +1576,6 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs); } -static int hex1(char ch) -{ - if ('0' <= ch && ch <= '9') - return ch - '0'; - else if ('a' <= ch && ch <= 'f') - return ch - 'a' + 10; - else if ('A' <= ch && ch <= 'F') - return ch - 'A' + 10; - return -1; -} -static int hex2(const char *cp) -{ - if (cp[0] && cp[1]) - return (hex1(cp[0]) << 4) | hex1(cp[1]); - else - return -1; -} - static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state) { struct strbuf *s = &state->stack->output; @@ -1603,7 +1585,7 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting if (cp[1] == '%') cp++; else { - int ch = hex2(cp + 1); + int ch = hex2chr(cp + 1); if (0 <= ch) { strbuf_addch(s, ch); cp += 3; -- cgit v1.2.3