commit 61b997d944dc45a698850191ba6679c12898005f parent ec2725b5d882ad63da87965c05b70c6f98f51dbc Author: Evgeny Grin (Karlson2k) <k2k@narod.ru> Date: Wed, 3 Nov 2021 16:43:56 +0300 MHD_uint32_to_strx(): rewritten for readability and minor optimization Diffstat:
| M | src/microhttpd/mhd_str.c | | | 28 | +++++++++++++++------------- |
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c @@ -1203,25 +1203,27 @@ MHD_uint32_to_strx (uint32_t val, char *buf, size_t buf_size) { - char *chr; /**< pointer to the current printed digit */ - int digit_pos = 7; /** zero-based, digit position in @a 'val' */ + size_t o_pos = 0; /**< position of the output character */ + int digit_pos = 8; /** zero-based, digit position in @a 'val' */ int digit; - chr = buf; - digit = (int) (((val) >> (4 * digit_pos)) & 0xf); - /* Skip leading zeros */ - while ((0 == digit) && (0 != digit_pos)) - digit = (int) (((val) >> (4 * --digit_pos)) & 0xf); + do + { + digit_pos--; + digit = (int) (val >> 28); + val <<= 4; + } while ((0 == digit) && (0 != digit_pos)); - while (0 != buf_size) + while (o_pos < buf_size) { - *chr = (digit <= 9) ? ('0' + (char) digit) : ('A' + (char) digit - 10); - chr++; - buf_size--; + buf[o_pos++] = (digit <= 9) ? ('0' + (char) digit) : + ('A' + (char) digit - 10); if (0 == digit_pos) - return (size_t) (chr - buf); - digit = (int) (((val) >> (4 * --digit_pos)) & 0xf); + return o_pos; + digit_pos--; + digit = (int) (val >> 28); + val <<= 4; } return 0; /* The buffer is too small */ }