commit d7981759073c4a9d2d0b67051584c8da62d762f6
parent e3dc813f16086d117987b4ef1eff5e0501de3965
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Fri, 25 Apr 2025 18:46:08 +0200
mhd_str: minor optimisation
Diffstat:
1 file changed, 55 insertions(+), 9 deletions(-)
diff --git a/src/mhd2/mhd_str.c b/src/mhd2/mhd_str.c
@@ -533,6 +533,34 @@ toxdigitvalue (char c)
/**
+ * Convert 4 bit value to US-ASCII hexadecimal digit.
+ *
+ * @param v the value to convert, must be less then 16
+ * @return hexadecimal digit
+ */
+MHD_static_inline_ char
+xdigittovalue (unsigned int v)
+{
+#if ! defined(MHD_FAVOR_SMALL_CODE)
+ static const char map_value_to_xdigit[16] =
+ { '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+ mhd_assert (16 > v);
+
+ return map_value_to_xdigit[v];
+#else /* MHD_FAVOR_SMALL_CODE */
+
+ mhd_assert (16 > v);
+
+ if (xdigit <= 9)
+ return '0' + (char) (xdigit);
+ return 'A' + (char) (xdigit - 10);
+#endif /* MHD_FAVOR_SMALL_CODE */
+}
+
+
+/**
* Caseless compare two characters.
*
* @param c1 the first char to compare
@@ -662,6 +690,30 @@ charsequalcaseless (const char c1, const char c2)
(int) (((unsigned char) (c)) - 'a' + 10) : \
(int) (-1) )))
+
+#if ! defined(MHD_FAVOR_SMALL_CODE)
+static const char map_value_to_xdigit[16] =
+{ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+/**
+ * Convert 4 bit value to US-ASCII hexadecimal digit.
+ *
+ * @param v the value to convert, must be less then 16
+ * @return hexadecimal digit
+ */
+# define xdigittovalue(v) map_value_to_xdigit[v]
+#else /* MHD_FAVOR_SMALL_CODE */
+/**
+ * Convert 4 bit value to US-ASCII hexadecimal digit.
+ *
+ * @param v the value to convert, must be less then 16
+ * @return hexadecimal digit
+ */
+ # define xdigittovalue(v) \
+ (char) ((v <= 9) ? ('0' + (char) v) : ('A' + (char) v - 10))
+#endif /* MHD_FAVOR_SMALL_CODE */
+
/**
* Caseless compare two characters.
*
@@ -1409,10 +1461,7 @@ mhd_uint32_to_strx (uint_fast32_t val,
while (o_pos < buf_size)
{
- buf[o_pos++] =
- (char) ((xdigit <= 9) ?
- ('0' + (char) xdigit) :
- ('A' + (char) xdigit - 10));
+ buf[o_pos++] = xdigittovalue (xdigit);
if (0 == digit_pos)
return o_pos;
digit_pos--;
@@ -1563,12 +1612,9 @@ mhd_bin_to_hex (const void *restrict bin,
for (i = 0; i < size; ++i)
{
- uint8_t j;
const uint8_t b = ((const uint8_t *) bin)[i];
- j = b >> 4;
- hex[i * 2] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a'));
- j = b & 0x0f;
- hex[i * 2 + 1] = (char) ((j < 10) ? (j + '0') : (j - 10 + 'a'));
+ hex[i * 2] = xdigittovalue (b >> 4);
+ hex[i * 2 + 1] = xdigittovalue (b & 0x0Fu);
}
return i * 2;
}