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,7 +166,6 @@ void fmt_PrintString(char *buf, size_t bufLen, struct FormatSpec const *fmt, cha
} else {
for (size_t i = 0; i < padLen; i++)
buf[i] = ' ';
if (totalLen > padLen)
memcpy(buf + padLen, value, len);
}
@@ -256,13 +255,7 @@ void fmt_PrintNumber(char *buf, size_t bufLen, struct FormatSpec const *fmt, uin
}
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,7 +297,6 @@ void fmt_PrintNumber(char *buf, size_t bufLen, struct FormatSpec const *fmt, uin
if (prefix)
buf[pos++] = prefix;
}
if (totalLen > pos)
memcpy(buf + pos, valueBuf, len);
}