Skip to content

Commit

Permalink
Further simplify formatting code
Browse files Browse the repository at this point in the history
- Remove redundant length checks before `memcpy`
- Coerce `sign` and `prefix` to boolean for `numLen`
  • Loading branch information
Rangi42 committed Apr 17, 2021
1 parent ee5da44 commit 750e93b
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/asm/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ 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);
memcpy(buf + padLen, value, len);
}

buf[totalLen] = '\0';
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -304,8 +297,7 @@ 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);
memcpy(buf + pos, valueBuf, len);
}

buf[totalLen] = '\0';
Expand Down

0 comments on commit 750e93b

Please sign in to comment.