mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Further simplify formatting code
- Remove redundant length checks before `memcpy` - Coerce `sign` and `prefix` to boolean for `numLen`
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user