ref: 750e93be3de46f50220ce4920f064380ec9b704f
parent: ee5da4468d061b5fa0f321643cec35b42aecbcdd
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Fri Apr 16 21:01:36 EDT 2021
Further simplify formatting code - Remove redundant length checks before `memcpy` - Coerce `sign` and `prefix` to boolean for `numLen`
--- a/src/asm/format.c
+++ b/src/asm/format.c
@@ -166,8 +166,7 @@
} else {
for (size_t i = 0; i < padLen; i++)
buf[i] = ' ';
- if (totalLen > padLen)
- memcpy(buf + padLen, value, len);
+ memcpy(buf + padLen, value, len);
}
buf[totalLen] = '\0';
@@ -256,13 +255,7 @@
}
size_t len = strlen(valueBuf);
- size_t numLen = len;
-
- if (sign)
- numLen++;
- if (prefix)
- numLen++;
-
+ size_t numLen = !!sign + !!prefix + len;
size_t totalLen = fmt->width > numLen ? fmt->width : numLen;
if (totalLen > bufLen - 1) { /* bufLen includes terminator */
@@ -304,8 +297,7 @@
if (prefix)
buf[pos++] = prefix;
}
- if (totalLen > pos)
- memcpy(buf + pos, valueBuf, len);
+ memcpy(buf + pos, valueBuf, len);
}
buf[totalLen] = '\0';