On Fri, Oct 06, 2023 at 10:18:08AM -0500, Eric Blake wrote:
Use an array of characters instead of strings for less .data
storage.
Merge the loop conditional for fewer lines of code.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
common/include/human-size.h | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/common/include/human-size.h b/common/include/human-size.h
index 47729c3c..8b1e0132 100644
--- a/common/include/human-size.h
+++ b/common/include/human-size.h
@@ -159,7 +159,7 @@ human_size_parse (const char *str,
static inline char *
human_size (char *buf, uint64_t bytes, bool *human)
{
- static const char ext[][2] = { "E", "P", "T",
"G", "M", "K", "" };
+ static const char ext[] = "EPTGMK";
size_t i;
if (buf == NULL) {
@@ -170,18 +170,16 @@ human_size (char *buf, uint64_t bytes, bool *human)
/* Work out which extension to use, if any. */
i = 6;
- if (bytes != 0) {
- while ((bytes & 1023) == 0) {
- bytes >>= 10;
- i--;
- }
+ while (bytes && (bytes & 1023) == 0) {
+ bytes >>= 10;
+ i--;
}
/* Set the flag to true if we're going to add a human-readable extension. */
if (human)
- *human = ext[i][0] != '\0';
+ *human = ext[i] != '\0';
- snprintf (buf, HUMAN_SIZE_LONGEST, "%" PRIu64 "%s", bytes,
ext[i]);
+ snprintf (buf, HUMAN_SIZE_LONGEST, "%" PRIu64 "%.1s", bytes,
&ext[i]);
return buf;
}
Reviewed-by: Richard W.M. Jones <rjones(a)redhat.com>
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW