Further simplify formatting code

- Remove redundant length checks before `memcpy`
- Coerce `sign` and `prefix` to boolean for `numLen`
This commit is contained in:
Rangi
2021-04-17 01:01:36 -04:00
parent ee5da4468d
commit 750e93be3d

View File

@@ -166,8 +166,7 @@ void fmt_PrintString(char *buf, size_t bufLen, struct FormatSpec const *fmt, cha
} else { } else {
for (size_t i = 0; i < padLen; i++) for (size_t i = 0; i < padLen; i++)
buf[i] = ' '; buf[i] = ' ';
if (totalLen > padLen) memcpy(buf + padLen, value, len);
memcpy(buf + padLen, value, len);
} }
buf[totalLen] = '\0'; buf[totalLen] = '\0';
@@ -256,13 +255,7 @@ void fmt_PrintNumber(char *buf, size_t bufLen, struct FormatSpec const *fmt, uin
} }
size_t len = strlen(valueBuf); size_t len = strlen(valueBuf);
size_t numLen = len; size_t numLen = !!sign + !!prefix + len;
if (sign)
numLen++;
if (prefix)
numLen++;
size_t totalLen = fmt->width > numLen ? fmt->width : numLen; size_t totalLen = fmt->width > numLen ? fmt->width : numLen;
if (totalLen > bufLen - 1) { /* bufLen includes terminator */ if (totalLen > bufLen - 1) { /* bufLen includes terminator */
@@ -304,8 +297,7 @@ void fmt_PrintNumber(char *buf, size_t bufLen, struct FormatSpec const *fmt, uin
if (prefix) if (prefix)
buf[pos++] = prefix; buf[pos++] = prefix;
} }
if (totalLen > pos) memcpy(buf + pos, valueBuf, len);
memcpy(buf + pos, valueBuf, len);
} }
buf[totalLen] = '\0'; buf[totalLen] = '\0';