From 316208b365752244253a77886fe3ca2de19ac7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Palmer?= Date: Mon, 22 Apr 2024 16:24:38 +0200 Subject: [PATCH] microtez_to_string: clarify the trailing 0s elimination Using while and index --- src/to_string.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/to_string.c b/src/to_string.c index 7d440d0d..4cfc7987 100644 --- a/src/to_string.c +++ b/src/to_string.c @@ -273,15 +273,11 @@ int microtez_to_string(char *const dest, size_t dest_size, uint64_t number) { // Eliminate trailing 0s char *start = tmp + sizeof(tmp) - DECIMAL_DIGITS; - char *end; - for (end = tmp + sizeof(tmp) - 1u; end >= start; end--) { - if (*end != '0') { - end++; - break; - } + size_t length = DECIMAL_DIGITS; + while ((length > 0u) && (start[length - 1u] == '0')) { + length--; } - size_t length = end - start; if ((dest_size - offset) < length) { return -1; }