hex-ll: separate out non-hash-algo functions

In order to further reduce all-in-one headers, separate out functions in hex.h that do not operate on object hashes into its own file, hex-ll.h, and update the include directives in the .c files that need only such functions accordingly. Signed-off-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Calvin Wan committed Sep 29, 2023 at 14:20 UTC d88e8106e892109f4f4b13312dd026d0a7fcfeec
10 files changed +83 -75
Makefile
+1
@@ -1040,6 +1040,7 @@ LIB_OBJS += hash-lookup.o
1040 LIB_OBJS += hashmap.o
1041 LIB_OBJS += help.o
1042 LIB_OBJS += hex.o
1043 +LIB_OBJS += hex-ll.o
1044 LIB_OBJS += hook.o
1045 LIB_OBJS += ident.o
1046 LIB_OBJS += json-writer.o
color.c
+1 -1
@@ -3,7 +3,7 @@
3 #include "color.h"
4 #include "editor.h"
5 #include "gettext.h"
6 -#include "hex.h"
6 +#include "hex-ll.h"
7 #include "pager.h"
8 #include "strbuf.h"
9
hex-ll.c new
+49
@@ -0,0 +1,49 @@
1 +#include "git-compat-util.h"
2 +#include "hex-ll.h"
3 +
4 +const signed char hexval_table[256] = {
5 + -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
6 + -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
7 + -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
8 + -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
9 + -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
10 + -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
11 + 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
12 + 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
13 + -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
14 + -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
15 + -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
16 + -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
17 + -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
18 + -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
19 + -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
20 + -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
21 + -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
22 + -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
23 + -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
24 + -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
25 + -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
26 + -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
27 + -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
28 + -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
29 + -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
30 + -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
31 + -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
32 + -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
33 + -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
34 + -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
35 + -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
36 + -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
37 +};
38 +
39 +int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
40 +{
41 + for (; len; len--, hex += 2) {
42 + unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
43 +
44 + if (val & ~0xff)
45 + return -1;
46 + *binary++ = val;
47 + }
48 + return 0;
49 +}
hex-ll.h new
+27
@@ -0,0 +1,27 @@
1 +#ifndef HEX_LL_H
2 +#define HEX_LL_H
3 +
4 +extern const signed char hexval_table[256];
5 +static inline unsigned int hexval(unsigned char c)
6 +{
7 + return hexval_table[c];
8 +}
9 +
10 +/*
11 + * Convert two consecutive hexadecimal digits into a char. Return a
12 + * negative value on error. Don't run over the end of short strings.
13 + */
14 +static inline int hex2chr(const char *s)
15 +{
16 + unsigned int val = hexval(s[0]);
17 + return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
18 +}
19 +
20 +/*
21 + * Read `len` pairs of hexadecimal digits from `hex` and write the
22 + * values to `binary` as `len` bytes. Return 0 on success, or -1 if
23 + * the input does not consist of hex digits).
24 + */
25 +int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
26 +
27 +#endif
hex.c
-47
@@ -2,53 +2,6 @@
2 #include "hash.h"
3 #include "hex.h"
4
5 -const signed char hexval_table[256] = {
6 - -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
7 - -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
8 - -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
9 - -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
10 - -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
11 - -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
12 - 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
13 - 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
14 - -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
15 - -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
16 - -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
17 - -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
18 - -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
19 - -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
20 - -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
21 - -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
22 - -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
23 - -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
24 - -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
25 - -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
26 - -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
27 - -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
28 - -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
29 - -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
30 - -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
31 - -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
32 - -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
33 - -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
34 - -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
35 - -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
36 - -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
37 - -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
38 -};
39 -
40 -int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
41 -{
42 - for (; len; len--, hex += 2) {
43 - unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
44 -
45 - if (val & ~0xff)
46 - return -1;
47 - *binary++ = val;
48 - }
49 - return 0;
50 -}
51 -
5 static int get_hash_hex_algop(const char *hex, unsigned char *hash,
6 const struct git_hash_algo *algop)
7 {
hex.h
+1 -23
@@ -2,22 +2,7 @@
2 #define HEX_H
3
4 #include "hash-ll.h"
5 -
6 -extern const signed char hexval_table[256];
7 -static inline unsigned int hexval(unsigned char c)
8 -{
9 - return hexval_table[c];
10 -}
11 -
12 -/*
13 - * Convert two consecutive hexadecimal digits into a char. Return a
14 - * negative value on error. Don't run over the end of short strings.
15 - */
16 -static inline int hex2chr(const char *s)
17 -{
18 - unsigned int val = hexval(s[0]);
19 - return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
20 -}
5 +#include "hex-ll.h"
6
7 /*
8 * Try to read a hash (specified by the_hash_algo) in hexadecimal
@@ -34,13 +19,6 @@ int get_oid_hex(const char *hex, struct object_id *oid);
19 /* Like get_oid_hex, but for an arbitrary hash algorithm. */
20 int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop);
21
37 -/*
38 - * Read `len` pairs of hexadecimal digits from `hex` and write the
39 - * values to `binary` as `len` bytes. Return 0 on success, or -1 if
40 - * the input does not consist of hex digits).
41 - */
42 -int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
43 -
22 /*
23 * Convert a binary hash in "unsigned char []" or an object name in
24 * "struct object_id *" to its hex equivalent. The `_r` variant is reentrant,
mailinfo.c
+1 -1
@@ -1,7 +1,7 @@
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "gettext.h"
4 -#include "hex.h"
4 +#include "hex-ll.h"
5 #include "utf8.h"
6 #include "strbuf.h"
7 #include "mailinfo.h"
strbuf.c
+1 -1
@@ -1,6 +1,6 @@
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 -#include "hex.h"
3 +#include "hex-ll.h"
4 #include "strbuf.h"
5 #include "string-list.h"
6 #include "utf8.h"
url.c
+1 -1
@@ -1,5 +1,5 @@
1 #include "git-compat-util.h"
2 -#include "hex.h"
2 +#include "hex-ll.h"
3 #include "strbuf.h"
4 #include "url.h"
5
urlmatch.c
+1 -1
@@ -1,6 +1,6 @@
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 -#include "hex.h"
3 +#include "hex-ll.h"
4 #include "strbuf.h"
5 #include "urlmatch.h"
6